From 37d7ac6b155a9cae20571dc9ff65e181c36c5480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Chirose=E2=80=9D?= Date: Fri, 26 Dec 2025 14:56:57 +0900 Subject: [PATCH 1/7] imported updateEntity --- .java-version | 2 +- pom.xml | 2 +- .../fiwarecraft/client/FcMocClient.java | 31 +++ .../fiwarecraft/client/FcMocClientTest.java | 187 ++---------------- 4 files changed, 53 insertions(+), 169 deletions(-) diff --git a/.java-version b/.java-version index 8e2afd3..98d9bcb 100644 --- a/.java-version +++ b/.java-version @@ -1 +1 @@ -17 \ No newline at end of file +17 diff --git a/pom.xml b/pom.xml index 8166acf..e090abd 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,7 @@ com.github.makeOurCity moc-java - 0.0.4 + 73d3ac3 diff --git a/src/main/java/city/makeour/fiwarecraft/client/FcMocClient.java b/src/main/java/city/makeour/fiwarecraft/client/FcMocClient.java index 5191080..1361ac9 100644 --- a/src/main/java/city/makeour/fiwarecraft/client/FcMocClient.java +++ b/src/main/java/city/makeour/fiwarecraft/client/FcMocClient.java @@ -6,6 +6,9 @@ import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; import org.springframework.web.client.RestClient.ResponseSpec; @@ -49,6 +52,7 @@ public boolean auth() { return true; } + /* public void sendPing(String entityId, boolean status) { Ping pingEntity = new Ping(); pingEntity.setType("Ping"); @@ -78,6 +82,33 @@ public void sendPing(Ping pingEntity) { System.out.println("Ping sent: " + resp.body(String.class)); } } + */ + + public void sendPing(String entityId, boolean status) { + // 1. 更新用データの作成 + Map attributes = new HashMap<>(); + + // 現在時刻を文字列形式に変換(FIWAREの標準的な形式) + String now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); + + attributes.put("lastSucceededAt", now); + attributes.put("status", status); + + // 2. Fiware-Service ヘッダーの設定(もしあれば) + if (this.fiwareService != null && !this.fiwareService.isEmpty()) { + mocClient.setFiwareService(this.fiwareService); + } + + // 3. updateEntity を呼び出す(これが内部で Create か Update かを判定してくれる) + // 引数: ID, Type, 属性のMap + var resp = mocClient.updateEntity(entityId, "Ping", attributes); + + // 4. レスポンスの確認 + if (resp != null) { + System.out.println("Ping sent/updated for ID: " + entityId); + } + } + public void setFiwareService(String fiwareService) { this.fiwareService = fiwareService; diff --git a/src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java b/src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java index 3bddb32..48fadd0 100644 --- a/src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java +++ b/src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java @@ -1,21 +1,17 @@ package city.makeour.fiwarecraft.client; import city.makeour.moc.MocClient; -import city.makeour.fiwarecraft.model.Ping; import org.junit.Before; import org.junit.Test; -import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; -import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariables; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; -import java.time.LocalDateTime; +import java.util.Map; import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; @@ -29,24 +25,26 @@ public class FcMocClientTest { @Before public void setUp() { + // テストごとに FcMocClient を初期化 fcMocClient = new FcMocClient(mockMocClient); - fcMocClient.auth(); } @Test public void testConstructorWithDefaultService() { - FcMocClient client = new FcMocClient(mockMocClient); - assertEquals("fiwarecraft", client.getFiwareService()); + // デフォルトの Fiware-Service 名が正しくセットされているか + assertEquals("fiwarecraft", fcMocClient.getFiwareService()); } @Test public void testConstructorWithCustomService() { + // カスタムの Fiware-Service 名が正しくセットされているか FcMocClient client = new FcMocClient(mockMocClient, "custom-service"); assertEquals("custom-service", client.getFiwareService()); } @Test public void testSetAndGetFiwareService() { + // 途中でサービス名を変更できるか fcMocClient.setFiwareService("test-service"); assertEquals("test-service", fcMocClient.getFiwareService()); } @@ -58,186 +56,41 @@ public void testSendPingWithIdAndStatus() throws Exception { fcMocClient.sendPing(entityId, status); + // 1. Fiware-Service ヘッダーがセットされたか検証 verify(mockMocClient, times(1)).setFiwareService("fiwarecraft"); - verify(mockMocClient, times(1)).createEntity(eq("application/json"), any(Ping.class)); - } - - @Test - public void testSendPingWithPingEntity() throws Exception { - LocalDateTime testTime = LocalDateTime.of(2023, 12, 25, 14, 30, 45); - Ping pingEntity = new Ping(); - pingEntity.setType("Ping"); - pingEntity.setId("test-ping-002"); - pingEntity.setLastSucceededAt(testTime); - pingEntity.setStatus(true); - - fcMocClient.sendPing(pingEntity); - - verify(mockMocClient, times(1)).setFiwareService("fiwarecraft"); - verify(mockMocClient, times(1)).createEntity("application/json", pingEntity); - assertEquals("Ping", pingEntity.getType()); - assertEquals("test-ping-002", pingEntity.getId()); - assertEquals(testTime, pingEntity.getLastSucceededAt()); - assertTrue(pingEntity.isStatus()); - } - - @Test - public void testSendPingSetsDefaultType() throws Exception { - Ping pingEntity = new Ping(); - pingEntity.setId("test-ping-003"); - pingEntity.setStatus(false); - - fcMocClient.sendPing(pingEntity); - - verify(mockMocClient, times(1)).setFiwareService("fiwarecraft"); - verify(mockMocClient, times(1)).createEntity("application/json", pingEntity); - assertEquals("Ping", pingEntity.getType()); - } - - @Test - public void testSendPingSetsDefaultTimestamp() throws Exception { - LocalDateTime beforeCall = LocalDateTime.now(); - - Ping pingEntity = new Ping(); - pingEntity.setType("Ping"); - pingEntity.setId("test-ping-004"); - pingEntity.setStatus(true); - - fcMocClient.sendPing(pingEntity); - - LocalDateTime afterCall = LocalDateTime.now(); - - verify(mockMocClient, times(1)).setFiwareService("fiwarecraft"); - verify(mockMocClient, times(1)).createEntity("application/json", pingEntity); - assertNotNull(pingEntity.getLastSucceededAt()); - assertTrue(pingEntity.getLastSucceededAt().isAfter(beforeCall.minusSeconds(1))); - assertTrue(pingEntity.getLastSucceededAt().isBefore(afterCall.plusSeconds(1))); + + // 2. updateEntity が正しい引数 (ID, Type, Map) で呼ばれたか検証 + // 第3引数の Map には時刻とステータスが入っているため any(Map.class) で検証 + verify(mockMocClient, times(1)).updateEntity(eq(entityId), eq("Ping"), any(Map.class)); } @Test public void testSendPingWithCustomService() throws Exception { + // サービス名を変更した状態で送信した場合 fcMocClient.setFiwareService("custom-service"); - - Ping pingEntity = new Ping(); - pingEntity.setType("Ping"); - pingEntity.setId("test-ping-005"); - pingEntity.setStatus(true); - - fcMocClient.sendPing(pingEntity); + fcMocClient.sendPing("test-id", true); verify(mockMocClient, times(1)).setFiwareService("custom-service"); - verify(mockMocClient, times(1)).createEntity("application/json", pingEntity); - } - - @Test - public void testSendPingHandlesException() throws Exception { - Exception testException = new RuntimeException("Network error"); - doThrow(testException).when(mockMocClient).createEntity(anyString(), any(Ping.class)); - - Ping pingEntity = new Ping(); - pingEntity.setType("Ping"); - pingEntity.setId("test-ping-error"); - pingEntity.setStatus(true); - - RuntimeException thrown = assertThrows(RuntimeException.class, () -> { - fcMocClient.sendPing(pingEntity); - }); - - verify(mockMocClient, times(1)).setFiwareService("fiwarecraft"); - assertEquals("Network error", thrown.getMessage()); - } - - @Test - public void testSendPingPreservesExistingValues() throws Exception { - LocalDateTime existingTime = LocalDateTime.of(2023, 1, 1, 12, 0, 0); - - Ping pingEntity = new Ping(); - pingEntity.setType("CustomPing"); - pingEntity.setId("test-ping-preserve"); - pingEntity.setLastSucceededAt(existingTime); - pingEntity.setStatus(false); - - fcMocClient.sendPing(pingEntity); - - verify(mockMocClient, times(1)).setFiwareService("fiwarecraft"); - verify(mockMocClient, times(1)).createEntity("application/json", pingEntity); - assertEquals("CustomPing", pingEntity.getType()); - assertEquals(existingTime, pingEntity.getLastSucceededAt()); - assertFalse(pingEntity.isStatus()); - } - - @Test - public void testSendPingWithIdAndStatusCreatesCorrectEntity() throws Exception { - LocalDateTime beforeCall = LocalDateTime.now(); - - fcMocClient.sendPing("simple-ping", false); - - LocalDateTime afterCall = LocalDateTime.now(); - - verify(mockMocClient, times(1)).setFiwareService("fiwarecraft"); - verify(mockMocClient, times(1)).createEntity(eq("application/json"), argThat(ping -> { - Ping p = (Ping) ping; - return "Ping".equals(p.getType()) && - "simple-ping".equals(p.getId()) && - !p.isStatus() && - p.getLastSucceededAt() != null && - p.getLastSucceededAt().isAfter(beforeCall.minusSeconds(1)) && - p.getLastSucceededAt().isBefore(afterCall.plusSeconds(1)); - })); + verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any(Map.class)); } @Test public void testSendPingWithNullService() throws Exception { + // サービス名が null の場合、ヘッダー設定がスキップされるか fcMocClient.setFiwareService(null); - - Ping pingEntity = new Ping(); - pingEntity.setType("Ping"); - pingEntity.setId("test-ping-null-service"); - pingEntity.setStatus(true); - - fcMocClient.sendPing(pingEntity); + fcMocClient.sendPing("test-id", true); verify(mockMocClient, never()).setFiwareService(anyString()); - verify(mockMocClient, times(1)).createEntity("application/json", pingEntity); + verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any(Map.class)); } @Test public void testSendPingWithEmptyService() throws Exception { + // サービス名が空文字の場合、ヘッダー設定がスキップされるか fcMocClient.setFiwareService(""); - - Ping pingEntity = new Ping(); - pingEntity.setType("Ping"); - pingEntity.setId("test-ping-empty-service"); - pingEntity.setStatus(true); - - fcMocClient.sendPing(pingEntity); + fcMocClient.sendPing("test-id", true); verify(mockMocClient, never()).setFiwareService(anyString()); - verify(mockMocClient, times(1)).createEntity("application/json", pingEntity); + verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any(Map.class)); } - - // @Test - // @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 testSendPingWithRealClient() { - // FcMocClient realClient = new FcMocClient(new MocClient()); - // boolean authenticated = realClient.auth(); - // assertTrue("Authentication should succeed with valid credentials", - // authenticated); - - // String entityId = "real-ping-001"; - // boolean status = true; - - // realClient.sendPing(entityId, status); - // // Here you would typically verify the entity was created in the actual MOC - // // service - // } } \ No newline at end of file From 8963be89ac90a60891f64a31fbbb64a9bc7789c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Chirose=E2=80=9D?= Date: Tue, 17 Feb 2026 21:21:43 +0900 Subject: [PATCH 2/7] Refactor updateEntity to support Upsert with Ping object --- .../fiwarecraft/client/FcMocClient.java | 25 ++++++++----------- .../fiwarecraft/client/FcMocClientTest.java | 8 +++--- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/main/java/city/makeour/fiwarecraft/client/FcMocClient.java b/src/main/java/city/makeour/fiwarecraft/client/FcMocClient.java index 1361ac9..8293b87 100644 --- a/src/main/java/city/makeour/fiwarecraft/client/FcMocClient.java +++ b/src/main/java/city/makeour/fiwarecraft/client/FcMocClient.java @@ -85,27 +85,24 @@ public void sendPing(Ping pingEntity) { */ public void sendPing(String entityId, boolean status) { - // 1. 更新用データの作成 - Map attributes = new HashMap<>(); - - // 現在時刻を文字列形式に変換(FIWAREの標準的な形式) - String now = LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); - - attributes.put("lastSucceededAt", now); - attributes.put("status", status); - - // 2. Fiware-Service ヘッダーの設定(もしあれば) + // 1. Pingオブジェクトの生成(Mapではなくこちらを使う) + Ping pingEntity = new Ping(); + pingEntity.setId(entityId); + pingEntity.setType("Ping"); + pingEntity.setLastSucceededAt(LocalDateTime.now()); + pingEntity.setStatus(status); + + // 2. Fiware-Service ヘッダーの設定 if (this.fiwareService != null && !this.fiwareService.isEmpty()) { mocClient.setFiwareService(this.fiwareService); } - // 3. updateEntity を呼び出す(これが内部で Create か Update かを判定してくれる) - // 引数: ID, Type, 属性のMap - var resp = mocClient.updateEntity(entityId, "Ping", attributes); + // 3. MocClientのupdateEntityを呼び出し(作成 or 更新を自動判別) + var resp = mocClient.updateEntity(entityId, "Ping", pingEntity); // 4. レスポンスの確認 if (resp != null) { - System.out.println("Ping sent/updated for ID: " + entityId); + System.out.println("Ping Upserted for ID: " + entityId); } } diff --git a/src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java b/src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java index 48fadd0..4144972 100644 --- a/src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java +++ b/src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java @@ -61,7 +61,7 @@ public void testSendPingWithIdAndStatus() throws Exception { // 2. updateEntity が正しい引数 (ID, Type, Map) で呼ばれたか検証 // 第3引数の Map には時刻とステータスが入っているため any(Map.class) で検証 - verify(mockMocClient, times(1)).updateEntity(eq(entityId), eq("Ping"), any(Map.class)); + verify(mockMocClient, times(1)).updateEntity(eq(entityId), eq("Ping"), any()); } @Test @@ -71,7 +71,7 @@ public void testSendPingWithCustomService() throws Exception { fcMocClient.sendPing("test-id", true); verify(mockMocClient, times(1)).setFiwareService("custom-service"); - verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any(Map.class)); + verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any()); } @Test @@ -81,7 +81,7 @@ public void testSendPingWithNullService() throws Exception { fcMocClient.sendPing("test-id", true); verify(mockMocClient, never()).setFiwareService(anyString()); - verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any(Map.class)); + verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any()); } @Test @@ -91,6 +91,6 @@ public void testSendPingWithEmptyService() throws Exception { fcMocClient.sendPing("test-id", true); verify(mockMocClient, never()).setFiwareService(anyString()); - verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any(Map.class)); + verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any()); } } \ No newline at end of file From 442ba57343d303f2cd8c2cc397fdc41a39cff861 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Chirose=E2=80=9D?= Date: Tue, 17 Feb 2026 23:09:00 +0900 Subject: [PATCH 3/7] update moc-java to object-based version (2105acb) --- .vscode/settings.json | 1 + pom.xml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 524af95..5046525 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -18,4 +18,5 @@ "spigotmc", "vmargs" ], + "java.configuration.updateBuildConfiguration": "interactive", } \ No newline at end of file diff --git a/pom.xml b/pom.xml index c4a05c0..39c6fa4 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,7 @@ com.github.makeOurCity moc-java - main-SNAPSHOT + 2105acb From 39ff3b8cc5c71bbbd465527140d17e53ecbcf999 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Chirose=E2=80=9D?= Date: Wed, 4 Mar 2026 13:35:40 +0900 Subject: [PATCH 4/7] updated commit hash on pom.xml --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 39c6fa4..44c0c0e 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,7 @@ com.github.makeOurCity moc-java - 2105acb + cef8f9749baaf2efcb33b7c945cf7ec6a453b2f8 From ef1721c6089f1beef6d5062b6b852eecae18028f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Chirose=E2=80=9D?= Date: Wed, 4 Mar 2026 13:41:55 +0900 Subject: [PATCH 5/7] updated commit hash --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 44c0c0e..f9d4ef2 100644 --- a/pom.xml +++ b/pom.xml @@ -62,7 +62,7 @@ com.github.makeOurCity moc-java - cef8f9749baaf2efcb33b7c945cf7ec6a453b2f8 + 6a1a81e15fb74def837b8c788a449d77cdbf0f88 From c2c8c9a9fa3acffaabb7a47324d2dc5d54d8d684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Chirose=E2=80=9D?= Date: Wed, 4 Mar 2026 13:52:51 +0900 Subject: [PATCH 6/7] updated FcMocClientTest --- .../city/makeour/fiwarecraft/client/FcMocClientTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java b/src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java index 4144972..78068f6 100644 --- a/src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java +++ b/src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java @@ -61,7 +61,7 @@ public void testSendPingWithIdAndStatus() throws Exception { // 2. updateEntity が正しい引数 (ID, Type, Map) で呼ばれたか検証 // 第3引数の Map には時刻とステータスが入っているため any(Map.class) で検証 - verify(mockMocClient, times(1)).updateEntity(eq(entityId), eq("Ping"), any()); + verify(mockMocClient, times(1)).updateEntity(eq(entityId), eq("Ping"), any(Object.class)); } @Test @@ -71,7 +71,7 @@ public void testSendPingWithCustomService() throws Exception { fcMocClient.sendPing("test-id", true); verify(mockMocClient, times(1)).setFiwareService("custom-service"); - verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any()); + verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any(Object.class)); } @Test @@ -81,7 +81,7 @@ public void testSendPingWithNullService() throws Exception { fcMocClient.sendPing("test-id", true); verify(mockMocClient, never()).setFiwareService(anyString()); - verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any()); + verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any(Object.class)); } @Test @@ -91,6 +91,6 @@ public void testSendPingWithEmptyService() throws Exception { fcMocClient.sendPing("test-id", true); verify(mockMocClient, never()).setFiwareService(anyString()); - verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any()); + verify(mockMocClient, times(1)).updateEntity(anyString(), eq("Ping"), any(Object.class)); } } \ No newline at end of file From e7d4d877f95a3d544c7683dc44db41750adeacb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Chirose=E2=80=9D?= Date: Tue, 7 Apr 2026 14:50:02 +0900 Subject: [PATCH 7/7] Update server status to offline in onDisable --- src/main/java/city/makeour/fiwarecraft/App.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/city/makeour/fiwarecraft/App.java b/src/main/java/city/makeour/fiwarecraft/App.java index 51093a7..7e9c7c0 100644 --- a/src/main/java/city/makeour/fiwarecraft/App.java +++ b/src/main/java/city/makeour/fiwarecraft/App.java @@ -48,5 +48,10 @@ public void onEnable() { @Override public void onDisable() { getLogger().info("Fiwarecraft plugin has been disabled!"); + if (this.mocClient != null) { + // onEnableと同じ Entity ID を指定して、ステータスを false にして送信 + this.mocClient.sendPing("urn:ngsi-ld:ping:test-serer-001", false); + getLogger().info("Send ping (offline)"); + } } } \ No newline at end of file