Skip to content

Commit d726ac9

Browse files
authored
Merge pull request #19 from makeOurCity/feature/add-get-entity
added deleteEntity & its tests
2 parents 73d3ac3 + 4c82c5a commit d726ac9

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

src/main/java/city/makeour/moc/MocClient.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,22 @@ public ResponseSpec updateEntity(String id, String type, Map<String, Object> att
181181
}
182182
}
183183

184+
// 指定された ID と Type を持つEntityを削除
185+
public ResponseSpec deleteEntity(String entityId, String type) {
186+
if (entityId == null || entityId.isBlank()) {
187+
throw new IllegalArgumentException("id is required");
188+
}
189+
// EntitiesApi に定義されている removeEntityWithResponseSpec を呼び出す
190+
return this.entities().removeEntityWithResponseSpec(entityId, type);
191+
}
192+
193+
// 指定された ID を持つEntityを削除
194+
public ResponseSpec deleteEntity(String entityId) {
195+
return this.deleteEntity(entityId, null);
196+
}
197+
198+
199+
200+
201+
184202
}

src/test/java/city/makeour/moc/MocClientTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
67

78
import java.security.GeneralSecurityException;
89
import java.security.NoSuchAlgorithmException;
@@ -16,6 +17,8 @@
1617
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
1718
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariables;
1819
import org.springframework.core.ParameterizedTypeReference;
20+
import org.springframework.http.HttpStatus;
21+
import org.springframework.web.client.RestClientResponseException;
1922

2023
import city.makeour.ngsi.v2.api.EntitiesApi;
2124
import city.makeour.ngsi.v2.model.CreateEntityRequest;
@@ -205,4 +208,52 @@ void testUpdateEntity_UpsertLogic() throws GeneralSecurityException, NoSuchAlgor
205208

206209
assertEquals("active", updatedEntity.get("status"));
207210
}
211+
212+
@Test
213+
@DisplayName("deleteEntityのテスト(ID+Type指定、IDのみ指定の2パターン)")
214+
@EnabledIfEnvironmentVariables({
215+
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USER_POOL_ID", matches = ".*"),
216+
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_CLIENT_ID", matches = ".*"),
217+
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USERNAME", matches = ".*"),
218+
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_PASSWORD", matches = ".*")
219+
})
220+
void testDeleteEntity() throws GeneralSecurityException, NoSuchAlgorithmException {
221+
// 1. セットアップ
222+
MocClient client = new MocClient();
223+
client.setMocAuthInfo(System.getenv("TEST_COGNITO_USER_POOL_ID"), System.getenv("TEST_COGNITO_CLIENT_ID"));
224+
client.login(System.getenv("TEST_COGNITO_USERNAME"), System.getenv("TEST_COGNITO_PASSWORD"));
225+
226+
String type = "TestDeleteType";
227+
228+
// ケース 1: deleteEntity(id, type)
229+
String id1 = "urn:ngsi-ld:TestDelete:1:" + UUID.randomUUID().toString();
230+
231+
// データ作成(既存のupdateEntityを利用して作成)
232+
client.updateEntity(id1, type, Map.of("value", 100));
233+
234+
// 削除実行(テスト対象)
235+
client.deleteEntity(id1, type).toBodilessEntity();
236+
237+
// 検証: 取得しようとして 404 Not Found になることを確認
238+
RestClientResponseException ex1 = assertThrows(RestClientResponseException.class, () -> {
239+
client.getEntity(id1, type).toBodilessEntity();
240+
});
241+
assertEquals(HttpStatus.NOT_FOUND.value(), ex1.getStatusCode().value());
242+
243+
// ケース 2: deleteEntity(id) - IDのみでの削除
244+
String id2 = "urn:ngsi-ld:TestDelete:2:" + UUID.randomUUID().toString();
245+
246+
// データ作成
247+
client.updateEntity(id2, type, Map.of("value", 200));
248+
249+
// 削除実行(テスト対象)
250+
client.deleteEntity(id2).toBodilessEntity();
251+
252+
// 検証: 取得しようとして 404 Not Found になることを確認
253+
RestClientResponseException ex2 = assertThrows(RestClientResponseException.class, () -> {
254+
client.getEntity(id2, type).toBodilessEntity();
255+
});
256+
assertEquals(HttpStatus.NOT_FOUND.value(), ex2.getStatusCode().value());
257+
}
258+
208259
}

0 commit comments

Comments
 (0)