diff --git a/src/main/java/city/makeour/moc/MocClient.java b/src/main/java/city/makeour/moc/MocClient.java index fedfcdd..e0d34af 100644 --- a/src/main/java/city/makeour/moc/MocClient.java +++ b/src/main/java/city/makeour/moc/MocClient.java @@ -92,4 +92,58 @@ public void setFiwareService(String fiwareService) { public ResponseSpec createEntity(String contentType, Object body) { return this.client.createEntity(contentType, body); } + + /** + * Retrieves an entity with the specified parameters. + * + * @param entityId The ID of the entity to retrieve. + * @param type The type of the entity. + * @param attrs Comma-separated list of attribute names to include in the response. If null, all attributes are returned. + * @param metadata Comma-separated list of metadata names to include. If null, all metadata is returned. + * @param options Options to modify the response format (e.g., "keyValues" for simplified representation). + * @return The response specification for the entity retrieval request. + * + *
+ * Use this overload to fully customize the entity retrieval, including which attributes, + * metadata, and options are used. The {@code options} parameter allows you to control + * the response format; for example, "keyValues" returns a simplified JSON object. + *
+ */ + public ResponseSpec getEntity(String entityId, String type, String attrs, String metadata, String options) { + return this.entities().retrieveEntityWithResponseSpec(entityId, type, attrs, metadata, "keyValues"); + } + + /** + * Retrieves an entity with the specified ID, type, and attributes. + * + * @param entityId The ID of the entity to retrieve. + * @param type The type of the entity. + * @param attrs Comma-separated list of attribute names to include in the response. If null, all attributes are returned. + * @return The response specification for the entity retrieval request. + * + *+ * This overload defaults {@code metadata} to {@code null} (all metadata) and {@code options} to {@code "keyValues"} + * for a simplified response format. + *
+ */ + public ResponseSpec getEntity(String entityId, String type, String attrs) { + return this.entities().retrieveEntityWithResponseSpec(entityId, type, attrs, null, "keyValues"); + } + + /** + * Retrieves an entity with the specified ID and type. + * + * @param entityId The ID of the entity to retrieve. + * @param type The type of the entity. + * @return The response specification for the entity retrieval request. + * + *+ * This overload defaults {@code attrs} and {@code metadata} to {@code null} (all attributes and metadata), + * and {@code options} to {@code "keyValues"} for a simplified response format. + *
+ */ + public ResponseSpec getEntity(String entityId, String type) { + return this.entities().retrieveEntityWithResponseSpec(entityId, type, null, null, "keyValues"); + } + } diff --git a/src/test/java/city/makeour/moc/MocClientTest.java b/src/test/java/city/makeour/moc/MocClientTest.java index 403a04c..acc9b68 100644 --- a/src/test/java/city/makeour/moc/MocClientTest.java +++ b/src/test/java/city/makeour/moc/MocClientTest.java @@ -16,6 +16,7 @@ import city.makeour.ngsi.v2.api.EntitiesApi; import city.makeour.ngsi.v2.model.CreateEntityRequest; import city.makeour.ngsi.v2.model.ListEntitiesResponse; +import city.makeour.ngsi.v2.model.RetrieveEntityResponse; class MocClientTest { @@ -94,6 +95,7 @@ void testSetMocAuthInfo() throws GeneralSecurityException, NoSuchAlgorithmExcept @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USERNAME", matches = ".*"), @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_PASSWORD", matches = ".*") }) + void testAuth() throws GeneralSecurityException, NoSuchAlgorithmException { String cognitoUserPoolId = System.getenv("TEST_COGNITO_USER_POOL_ID"); String cognitoClientId = System.getenv("TEST_COGNITO_CLIENT_ID"); @@ -113,4 +115,35 @@ void testAuth() throws GeneralSecurityException, NoSuchAlgorithmException { client.entities().createEntity("application/json", entity, "keyValues"); } + + @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 testCreateAndGetEntity_Minimal() throws GeneralSecurityException, NoSuchAlgorithmException { + MocClient client = new MocClient(); + client.setMocAuthInfo(System.getenv("TEST_COGNITO_USER_POOL_ID"), System.getenv("TEST_COGNITO_CLIENT_ID")); + client.login(System.getenv("TEST_COGNITO_USERNAME"), System.getenv("TEST_COGNITO_PASSWORD")); + + // 作成&取得 + String entityId = "urn:ngsi-ld:TestEntity:" + UUID.randomUUID().toString(); + CreateEntityRequest entity = new CreateEntityRequest(); + entity.setType("TestEntity"); + entity.setId(entityId); + + // 作成を実行 + client.entities().createEntity("application/json", entity, "keyValues"); + + // getEntityの呼び出し、レスポンスの変換 + RetrieveEntityResponse retrievedEntity = client + .getEntity(entityId, "TestEntity", null, null, null) + .body(RetrieveEntityResponse.class); + + assertNotNull(retrievedEntity); + assertEquals(entityId, retrievedEntity.getId()); + } }