From c743d9a6444dfc4328cf34fc45d463421c8aa421 Mon Sep 17 00:00:00 2001 From: miyabayt <4364793+miyabayt@users.noreply.github.com> Date: Sat, 4 Feb 2023 23:46:57 +0900 Subject: [PATCH] chore: fix tests --- build.gradle | 10 ++++- gradle.properties | 2 +- .../RedisRefreshTokenPersistence.java | 4 +- src/main/resources/application-local.yml | 2 +- .../aws/lambda/HomeControllerSpec.groovy | 36 ------------------ .../lambda/controller/AuthControllerTest.java | 38 +++++++++++++++++++ src/test/resources/application-function.yml | 4 -- src/test/resources/application-test.yml | 36 +++++++++++++++++- 8 files changed, 84 insertions(+), 48 deletions(-) delete mode 100755 src/test/groovy/com/bigtreetc/sample/micronaut/aws/lambda/HomeControllerSpec.groovy create mode 100644 src/test/java/com/bigtreetc/sample/micronaut/aws/lambda/controller/AuthControllerTest.java delete mode 100755 src/test/resources/application-function.yml diff --git a/build.gradle b/build.gradle index 0f4538d..fad7c7c 100755 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,8 @@ plugins { id "groovy" id "idea" id "com.github.johnrengelman.shadow" version "7.1.2" - id "io.micronaut.application" version "3.5.3" + id "io.micronaut.application" version "3.6.5" + id "io.micronaut.test-resources" version "3.6.5" id "com.avast.gradle.docker-compose" version "0.16.9" id "com.diffplug.spotless" version "${spotlessVersion}" } @@ -90,8 +91,13 @@ dependencies { implementation "io.swagger.core.v3:swagger-annotations" implementation "jakarta.annotation:jakarta.annotation-api" + testAnnotationProcessor "io.micronaut:micronaut-inject-java" + testImplementation "org.junit.jupiter:junit-jupiter-api" + testImplementation "io.micronaut.test:micronaut-test-junit5" + testImplementation "io.micronaut.test:micronaut-test-rest-assured" + testImplementation "org.testcontainers:junit-jupiter" + testImplementation "org.testcontainers:r2dbc" testImplementation "org.testcontainers:mysql" - testImplementation "org.testcontainers:spock" testImplementation "org.testcontainers:testcontainers" testImplementation "io.micronaut:micronaut-function" } diff --git a/gradle.properties b/gradle.properties index f213f27..8d502de 100755 --- a/gradle.properties +++ b/gradle.properties @@ -1,7 +1,7 @@ version=0.0.1-SNAPSHOT org.gradle.daemon=false org.gradle.jvmargs=-Duser.language=ja -Duser.country=JP -micronautVersion=3.6.2 +micronautVersion=3.7.4 springSecurityVersion=5.7.3 lombokVersion=1.18.24 spotlessVersion=6.10.0 diff --git a/src/main/java/com/bigtreetc/sample/micronaut/aws/lambda/security/RedisRefreshTokenPersistence.java b/src/main/java/com/bigtreetc/sample/micronaut/aws/lambda/security/RedisRefreshTokenPersistence.java index f2e167c..1aee237 100644 --- a/src/main/java/com/bigtreetc/sample/micronaut/aws/lambda/security/RedisRefreshTokenPersistence.java +++ b/src/main/java/com/bigtreetc/sample/micronaut/aws/lambda/security/RedisRefreshTokenPersistence.java @@ -9,7 +9,7 @@ import com.bigtreetc.sample.micronaut.aws.lambda.domain.repository.StaffRepository; import com.bigtreetc.sample.micronaut.aws.lambda.domain.repository.StaffRoleRepository; import io.lettuce.core.api.StatefulRedisConnection; -import io.micronaut.runtime.event.annotation.EventListener; +import io.micronaut.scheduling.annotation.Async; import io.micronaut.security.authentication.Authentication; import io.micronaut.security.errors.OauthErrorResponseException; import io.micronaut.security.token.event.RefreshTokenGeneratedEvent; @@ -42,8 +42,8 @@ public class RedisRefreshTokenPersistence implements RefreshTokenPersistence { @Setter private int refreshTokenTimeoutHours = 2; + @Async @Override - @EventListener public void persistToken(RefreshTokenGeneratedEvent event) { if (event != null && event.getRefreshToken() != null diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index e84ba87..d861a80 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -12,7 +12,7 @@ flyway: default: enabled: true default-schema: micronaut-aws-lambda-rest-sample - url: jdbc:mysql://127.0.0.1:3306/?useSSL=false&characterEncoding=UTF-8 + url: jdbc:mysql://127.0.0.1:3306/?allowPublicKeyRetrieval=true&useSSL=false&characterEncoding=UTF-8 driverClassName: com.mysql.cj.jdbc.Driver username: root password: passw0rd diff --git a/src/test/groovy/com/bigtreetc/sample/micronaut/aws/lambda/HomeControllerSpec.groovy b/src/test/groovy/com/bigtreetc/sample/micronaut/aws/lambda/HomeControllerSpec.groovy deleted file mode 100755 index 0e15d10..0000000 --- a/src/test/groovy/com/bigtreetc/sample/micronaut/aws/lambda/HomeControllerSpec.groovy +++ /dev/null @@ -1,36 +0,0 @@ -package com.bigtreetc.sample.micronaut.aws.lambda -import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext -import com.amazonaws.serverless.proxy.model.AwsProxyRequest -import com.amazonaws.serverless.proxy.model.AwsProxyResponse -import com.amazonaws.services.lambda.runtime.Context -import com.fasterxml.jackson.databind.ObjectMapper -import io.micronaut.function.aws.proxy.MicronautLambdaHandler -import spock.lang.AutoCleanup -import spock.lang.Shared -import spock.lang.Specification - -class HomeControllerSpec extends Specification { - - @Shared - @AutoCleanup - MicronautLambdaHandler handler = new MicronautLambdaHandler() - - @Shared - Context lambdaContext = new MockLambdaContext() - - @Shared - ObjectMapper objectMapper = handler.applicationContext.getBean(ObjectMapper) - - void "test handler"() { - given: - AwsProxyRequest request = new AwsProxyRequest() - request.setHttpMethod("GET") - request.setPath("/") - when: - AwsProxyResponse response = handler.handleRequest(request, lambdaContext) - - then: - 200 == response.getStatusCode().intValue() - "{\"message\":\"Hello World\"}" == response.getBody() - } -} diff --git a/src/test/java/com/bigtreetc/sample/micronaut/aws/lambda/controller/AuthControllerTest.java b/src/test/java/com/bigtreetc/sample/micronaut/aws/lambda/controller/AuthControllerTest.java new file mode 100644 index 0000000..102b4c4 --- /dev/null +++ b/src/test/java/com/bigtreetc/sample/micronaut/aws/lambda/controller/AuthControllerTest.java @@ -0,0 +1,38 @@ +package com.bigtreetc.sample.micronaut.aws.lambda.controller; + +import static org.hamcrest.Matchers.notNullValue; + +import io.lettuce.core.RedisClient; +import io.micronaut.test.extensions.junit5.annotation.MicronautTest; +import io.restassured.specification.RequestSpecification; +import jakarta.inject.Inject; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +@MicronautTest(transactional = false) +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class AuthControllerTest { + + @Inject RedisClient redisClient; + + @BeforeAll + void beforeAll() { + redisClient.connect(); + } + + @Test + @DisplayName("認証が通った場合はアクセストークンが返ること") + void testAuth(RequestSpecification spec) { + spec.given() + .formParam("username", "test@example.com") + .formParam("password", "passw0rd") + .when() + .post("/api/auth/login") + .then() + .statusCode(200) + .body("username", notNullValue()) + .body("access_token", notNullValue()); + } +} diff --git a/src/test/resources/application-function.yml b/src/test/resources/application-function.yml deleted file mode 100755 index e24c471..0000000 --- a/src/test/resources/application-function.yml +++ /dev/null @@ -1,4 +0,0 @@ -micronaut: - security: - filter: - enabled: false diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml index f2cfa8e..a6382be 100755 --- a/src/test/resources/application-test.yml +++ b/src/test/resources/application-test.yml @@ -1,4 +1,36 @@ datasources: default: - url: jdbc:tc:mysql:8:///db - driverClassName: org.testcontainers.jdbc.ContainerDatabaseDriver + schema-generate: NONE + db-type: mysql +r2dbc: + datasources: + default: + db-type: mysql +flyway: + datasources: + default: + enabled: true +redis: + timeout: 30s +javamail: + authentication: + username: ${SMTP_USERNAME:''} + password: ${SMTP_PASSWORD:''} + properties: + mail: + smtp: + host: 127.0.0.1 + port: 1025 +logger: + levels: + io.micronaut.data.query: DEBUG + +application: + fileUploadLocation: build/sample + +test-resources: + containers: + mysql: + image-name: mysql:8 + redis: + image-name: redis