Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .java-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
17
17
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
<dependency>
<groupId>com.github.makeOurCity</groupId>
<artifactId>moc-java</artifactId>
<version>0.0.4</version>
<version>73d3ac3</version>
</dependency>

<dependency>
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/city/makeour/fiwarecraft/client/FcMocClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -49,6 +52,7 @@ public boolean auth() {
return true;
}

/*
public void sendPing(String entityId, boolean status) {
Ping pingEntity = new Ping();
pingEntity.setType("Ping");
Expand Down Expand Up @@ -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<String, Object> 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;
Expand Down
187 changes: 20 additions & 167 deletions src/test/java/city/makeour/fiwarecraft/client/FcMocClientTest.java
Original file line number Diff line number Diff line change
@@ -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.*;

Expand All @@ -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());
}
Expand All @@ -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
// }
}