package com.example.service
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import javax.inject.Singleton
def globalConfig = [environment: "prod", version: "1.0"]
def processDataClosure = { List<String> data ->
data.each { println it }
}
def scriptMethod(String input) {
return input.toUpperCase()
}
String anotherScriptMethod() {
return "Hello from script"
}
interface Repository<T> {
T findById(String id)
List<T> findAll()
}
trait Auditable {
String auditUser = "system"
def logAction(String action) {
println "Audit [${auditUser}]: ${action}"
}
}
enum Status {
ACTIVE, INACTIVE, DELETED, PENDING
}
abstract class BaseService {
protected String environment
BaseService(String environment) {
this.environment = environment
}
abstract void initialize()
}
@Slf4j
@Singleton
@CompileStatic
class UserService extends BaseService implements Repository<String>, Auditable {
public static final String DEFAULT_ROLE = "USER"
private int maxLoginAttempts = 5
String serviceName = "UserService"
UserService() {
super("production")
}
@Override
void initialize() {
log.info("Initializing ${serviceName} in ${environment}")
}
@Override
String findById(String id) {
logAction("Finding user ${id}") return "user_${id}"
}
@Override
List<String> findAll() {
return ["user_1", "user_2"]
}
def calculateTotal(int a, int b) {
def result = a + b
return result
}
static class DatabaseConfig {
String url
int port
}
}
class CalculatorSpec extends Specification {
@Feature
@Unroll
void "addition of #num1 and #num2 should be #expected"() {
given: "a calculator instance"
def calculator = new Calculator()
when: "adding two numbers"
def result = calculator.add(num1, num2)
then: "the result is correct"
result == expected
where:
num1 | num2 | expected
3 | 5 | 8
2 | 3 | 5
7 | 4 | 11
0 | 0 | 0
}
}