From 5ac944eb765cceaa2ff0959d291eeed7fbe84f88 Mon Sep 17 00:00:00 2001 From: Shugo USHIO Date: Sun, 4 May 2025 13:04:04 +0900 Subject: [PATCH 1/3] add create entity test --- README.md | 27 +++++++++++++- .../makeour/moc/FetchCognitoTokenTest.java | 11 ++++-- .../java/city/makeour/moc/MocClientTest.java | 37 ++++++++++++++++++- 3 files changed, 69 insertions(+), 6 deletions(-) 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..2638043 100644 --- a/src/test/java/city/makeour/moc/FetchCognitoTokenTest.java +++ b/src/test/java/city/makeour/moc/FetchCognitoTokenTest.java @@ -4,15 +4,18 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariables; 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 = ".+") + @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..5125cd1 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,33 @@ void testEntitiesApi() { assertNotNull(list); System.out.println("Entities: " + list); } -} \ No newline at end of file + + // TODO: テストのスキップについてきちんと動作確認をする。 + @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"); + + } +} From af5dc78ec529dcad2f05fbba6dd430e3a118c633 Mon Sep 17 00:00:00 2001 From: Shugo USHIO Date: Sun, 4 May 2025 13:26:35 +0900 Subject: [PATCH 2/3] Add env --- .github/workflows/test.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a9e8668..ad805df 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -21,4 +21,9 @@ jobs: cache: maven - name: Run tests with Maven + 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 From 9bf43e6a197df182ebc2b464848089ef3b90629f Mon Sep 17 00:00:00 2001 From: Shugo USHIO Date: Sun, 4 May 2025 14:56:10 +0900 Subject: [PATCH 3/3] Add report --- .github/workflows/test.yml | 9 ++++++++- .../java/city/makeour/moc/FetchCognitoTokenTest.java | 3 ++- src/test/java/city/makeour/moc/MocClientTest.java | 1 - 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ad805df..59ecaeb 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -26,4 +26,11 @@ jobs: 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 + run: > + mvn -B test --file pom.xml + + - name: Generate test report + run: > + mvn -B + site -DgenerateReports=false + surefire-report:report-only diff --git a/src/test/java/city/makeour/moc/FetchCognitoTokenTest.java b/src/test/java/city/makeour/moc/FetchCognitoTokenTest.java index 2638043..9ea376c 100644 --- a/src/test/java/city/makeour/moc/FetchCognitoTokenTest.java +++ b/src/test/java/city/makeour/moc/FetchCognitoTokenTest.java @@ -5,11 +5,12 @@ 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 + @Description("Cognitoのトークンを取得するテスト") @EnabledIfEnvironmentVariables({ @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USER_POOL_ID", matches = ".*"), @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_CLIENT_ID", matches = ".*"), diff --git a/src/test/java/city/makeour/moc/MocClientTest.java b/src/test/java/city/makeour/moc/MocClientTest.java index 5125cd1..0fa3daf 100644 --- a/src/test/java/city/makeour/moc/MocClientTest.java +++ b/src/test/java/city/makeour/moc/MocClientTest.java @@ -59,7 +59,6 @@ void testEntitiesApi() { System.out.println("Entities: " + list); } - // TODO: テストのスキップについてきちんと動作確認をする。 @Test @DisplayName("ログインしてデータ作成できるかのテスト") @EnabledIfEnvironmentVariables({