diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a9e8668..59ecaeb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,4 +21,16 @@ jobs: cache: maven - name: Run tests with Maven - run: mvn -B test --file pom.xml + env: + TEST_COGNITO_USER_POOL_ID: ${{ secrets.TEST_COGNITO_USER_POOL_ID }} + TEST_COGNITO_CLIENT_ID: ${{ secrets.TEST_COGNITO_CLIENT_ID }} + TEST_COGNITO_USERNAME: ${{ secrets.TEST_COGNITO_USERNAME }} + TEST_COGNITO_PASSWORD: ${{ secrets.TEST_COGNITO_PASSWORD }} + run: > + mvn -B test --file pom.xml + + - name: Generate test report + run: > + mvn -B + site -DgenerateReports=false + surefire-report:report-only diff --git a/README.md b/README.md index 741e3f4..86a3e0b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,29 @@ # MoC Java MoC client for Java -[![Java CI with Maven](https://github.com/makeOurCity/moc-java/actions/workflows/test.yml/badge.svg)](https://github.com/makeOurCity/moc-java/actions/workflows/test.yml) \ No newline at end of file +[![Java CI with Maven](https://github.com/makeOurCity/moc-java/actions/workflows/test.yml/badge.svg)](https://github.com/makeOurCity/moc-java/actions/workflows/test.yml) + + +# Development + +## Testing + +Set `.vscode/settings` env + +```json +{ + "java.test.config": { + "env": { + "TEST_COGNITO_USER_POOL_ID": "", + "TEST_COGNITO_CLIENT_ID": "", + "TEST_COGNITO_USERNAME": "", + "TEST_COGNITO_PASSWORD": "" + } + } +} +``` + +```console +$ mvn clean install +$ mvn teset +``` \ No newline at end of file diff --git a/src/test/java/city/makeour/moc/FetchCognitoTokenTest.java b/src/test/java/city/makeour/moc/FetchCognitoTokenTest.java index b24fc0f..9ea376c 100644 --- a/src/test/java/city/makeour/moc/FetchCognitoTokenTest.java +++ b/src/test/java/city/makeour/moc/FetchCognitoTokenTest.java @@ -4,15 +4,19 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariables; +import org.springframework.context.annotation.Description; class FetchCognitoTokenTest { - // TODO: テストのスキップについてきちんと動作確認をする。 @Test - @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USER_POOL_ID", matches = ".+") - @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_CLIENT_ID", matches = ".+") - @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USERNAME", matches = ".+") - @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_PASSWORD", matches = ".+") + @Description("Cognitoのトークンを取得するテスト") + @EnabledIfEnvironmentVariables({ + @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USER_POOL_ID", matches = ".*"), + @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_CLIENT_ID", matches = ".*"), + @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USERNAME", matches = ".*"), + @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_PASSWORD", matches = ".*") + }) public void testFetchTokenWithSrpAuth() throws Exception { String cognitoUserPoolId = System.getenv("TEST_COGNITO_USER_POOL_ID"); String cognitoClientId = System.getenv("TEST_COGNITO_CLIENT_ID"); diff --git a/src/test/java/city/makeour/moc/MocClientTest.java b/src/test/java/city/makeour/moc/MocClientTest.java index 7dc17e8..0fa3daf 100644 --- a/src/test/java/city/makeour/moc/MocClientTest.java +++ b/src/test/java/city/makeour/moc/MocClientTest.java @@ -3,12 +3,18 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.security.GeneralSecurityException; +import java.security.NoSuchAlgorithmException; import java.util.List; +import java.util.UUID; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariables; import city.makeour.ngsi.v2.api.EntitiesApi; +import city.makeour.ngsi.v2.model.CreateEntityRequest; import city.makeour.ngsi.v2.model.ListEntitiesResponse; class MocClientTest { @@ -52,4 +58,32 @@ void testEntitiesApi() { assertNotNull(list); System.out.println("Entities: " + list); } -} \ No newline at end of file + + @Test + @DisplayName("ログインしてデータ作成できるかのテスト") + @EnabledIfEnvironmentVariables({ + @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USER_POOL_ID", matches = ".*"), + @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_CLIENT_ID", matches = ".*"), + @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USERNAME", matches = ".*"), + @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_PASSWORD", matches = ".*") + }) + void testSetMocAuthInfo() throws GeneralSecurityException, NoSuchAlgorithmException { + String cognitoUserPoolId = System.getenv("TEST_COGNITO_USER_POOL_ID"); + String cognitoClientId = System.getenv("TEST_COGNITO_CLIENT_ID"); + String username = System.getenv("TEST_COGNITO_USERNAME"); + String password = System.getenv("TEST_COGNITO_PASSWORD"); + + MocClient client = new MocClient(); + client.setMocAuthInfo(cognitoUserPoolId, cognitoClientId); + client.login(username, password); + + String uuid = UUID.randomUUID().toString(); + + CreateEntityRequest entity = new CreateEntityRequest(); + entity.setType("TestEntity"); + entity.setId("urn:ngsi-ld:TestEntity:" + uuid); + + client.entities().createEntity("application/json", entity, "keyValues"); + + } +}