Skip to content

Commit 538b3d5

Browse files
authored
Merge pull request #16 from makeOurCity/feature/add-get-entity
getEntity added
2 parents 03f6476 + b3a279e commit 538b3d5

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,58 @@ public void setFiwareService(String fiwareService) {
9292
public ResponseSpec createEntity(String contentType, Object body) {
9393
return this.client.createEntity(contentType, body);
9494
}
95+
96+
/**
97+
* Retrieves an entity with the specified parameters.
98+
*
99+
* @param entityId The ID of the entity to retrieve.
100+
* @param type The type of the entity.
101+
* @param attrs Comma-separated list of attribute names to include in the response. If null, all attributes are returned.
102+
* @param metadata Comma-separated list of metadata names to include. If null, all metadata is returned.
103+
* @param options Options to modify the response format (e.g., "keyValues" for simplified representation).
104+
* @return The response specification for the entity retrieval request.
105+
*
106+
* <p>
107+
* Use this overload to fully customize the entity retrieval, including which attributes,
108+
* metadata, and options are used. The {@code options} parameter allows you to control
109+
* the response format; for example, "keyValues" returns a simplified JSON object.
110+
* </p>
111+
*/
112+
public ResponseSpec getEntity(String entityId, String type, String attrs, String metadata, String options) {
113+
return this.entities().retrieveEntityWithResponseSpec(entityId, type, attrs, metadata, "keyValues");
114+
}
115+
116+
/**
117+
* Retrieves an entity with the specified ID, type, and attributes.
118+
*
119+
* @param entityId The ID of the entity to retrieve.
120+
* @param type The type of the entity.
121+
* @param attrs Comma-separated list of attribute names to include in the response. If null, all attributes are returned.
122+
* @return The response specification for the entity retrieval request.
123+
*
124+
* <p>
125+
* This overload defaults {@code metadata} to {@code null} (all metadata) and {@code options} to {@code "keyValues"}
126+
* for a simplified response format.
127+
* </p>
128+
*/
129+
public ResponseSpec getEntity(String entityId, String type, String attrs) {
130+
return this.entities().retrieveEntityWithResponseSpec(entityId, type, attrs, null, "keyValues");
131+
}
132+
133+
/**
134+
* Retrieves an entity with the specified ID and type.
135+
*
136+
* @param entityId The ID of the entity to retrieve.
137+
* @param type The type of the entity.
138+
* @return The response specification for the entity retrieval request.
139+
*
140+
* <p>
141+
* This overload defaults {@code attrs} and {@code metadata} to {@code null} (all attributes and metadata),
142+
* and {@code options} to {@code "keyValues"} for a simplified response format.
143+
* </p>
144+
*/
145+
public ResponseSpec getEntity(String entityId, String type) {
146+
return this.entities().retrieveEntityWithResponseSpec(entityId, type, null, null, "keyValues");
147+
}
148+
95149
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import city.makeour.ngsi.v2.api.EntitiesApi;
1717
import city.makeour.ngsi.v2.model.CreateEntityRequest;
1818
import city.makeour.ngsi.v2.model.ListEntitiesResponse;
19+
import city.makeour.ngsi.v2.model.RetrieveEntityResponse;
1920

2021
class MocClientTest {
2122

@@ -94,6 +95,7 @@ void testSetMocAuthInfo() throws GeneralSecurityException, NoSuchAlgorithmExcept
9495
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USERNAME", matches = ".*"),
9596
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_PASSWORD", matches = ".*")
9697
})
98+
9799
void testAuth() throws GeneralSecurityException, NoSuchAlgorithmException {
98100
String cognitoUserPoolId = System.getenv("TEST_COGNITO_USER_POOL_ID");
99101
String cognitoClientId = System.getenv("TEST_COGNITO_CLIENT_ID");
@@ -113,4 +115,35 @@ void testAuth() throws GeneralSecurityException, NoSuchAlgorithmException {
113115

114116
client.entities().createEntity("application/json", entity, "keyValues");
115117
}
118+
119+
@Test
120+
@DisplayName("エンティティを作成・取得できるかのテスト(最小版)")
121+
@EnabledIfEnvironmentVariables({
122+
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USER_POOL_ID", matches = ".*"),
123+
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_CLIENT_ID", matches = ".*"),
124+
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USERNAME", matches = ".*"),
125+
@EnabledIfEnvironmentVariable(named = "TEST_COGNITO_PASSWORD", matches = ".*")
126+
})
127+
void testCreateAndGetEntity_Minimal() throws GeneralSecurityException, NoSuchAlgorithmException {
128+
MocClient client = new MocClient();
129+
client.setMocAuthInfo(System.getenv("TEST_COGNITO_USER_POOL_ID"), System.getenv("TEST_COGNITO_CLIENT_ID"));
130+
client.login(System.getenv("TEST_COGNITO_USERNAME"), System.getenv("TEST_COGNITO_PASSWORD"));
131+
132+
// 作成&取得
133+
String entityId = "urn:ngsi-ld:TestEntity:" + UUID.randomUUID().toString();
134+
CreateEntityRequest entity = new CreateEntityRequest();
135+
entity.setType("TestEntity");
136+
entity.setId(entityId);
137+
138+
// 作成を実行
139+
client.entities().createEntity("application/json", entity, "keyValues");
140+
141+
// getEntityの呼び出し、レスポンスの変換
142+
RetrieveEntityResponse retrievedEntity = client
143+
.getEntity(entityId, "TestEntity", null, null, null)
144+
.body(RetrieveEntityResponse.class);
145+
146+
assertNotNull(retrievedEntity);
147+
assertEquals(entityId, retrievedEntity.getId());
148+
}
116149
}

0 commit comments

Comments
 (0)