Skip to content
Merged
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
54 changes: 54 additions & 0 deletions src/main/java/city/makeour/moc/MocClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>
* 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.
* </p>
*/
public ResponseSpec getEntity(String entityId, String type, String attrs, String metadata, String options) {
return this.entities().retrieveEntityWithResponseSpec(entityId, type, attrs, metadata, "keyValues");
Copy link

Copilot AI Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The options parameter is ignored and 'keyValues' is hard-coded, causing any caller-supplied options value to be silently dropped. Either pass options through or remove the parameter if only 'keyValues' is supported.

Suggested change
return this.entities().retrieveEntityWithResponseSpec(entityId, type, attrs, metadata, "keyValues");
return this.entities().retrieveEntityWithResponseSpec(entityId, type, attrs, metadata, options);

Copilot uses AI. Check for mistakes.
}

/**
* 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.
*
* <p>
* This overload defaults {@code metadata} to {@code null} (all metadata) and {@code options} to {@code "keyValues"}
* for a simplified response format.
* </p>
*/
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.
*
* <p>
* 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.
* </p>
*/
public ResponseSpec getEntity(String entityId, String type) {
return this.entities().retrieveEntityWithResponseSpec(entityId, type, null, null, "keyValues");
}

}
33 changes: 33 additions & 0 deletions src/test/java/city/makeour/moc/MocClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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");
Expand All @@ -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());
}
}