|
1 | 1 | package city.makeour.moc; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
4 | 5 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
5 | 6 |
|
6 | 7 | import java.security.GeneralSecurityException; |
7 | 8 | import java.security.NoSuchAlgorithmException; |
| 9 | +import java.util.HashMap; |
8 | 10 | import java.util.List; |
| 11 | +import java.util.Map; |
9 | 12 | import java.util.UUID; |
10 | 13 |
|
11 | 14 | import org.junit.jupiter.api.DisplayName; |
12 | 15 | import org.junit.jupiter.api.Test; |
13 | 16 | import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; |
14 | 17 | import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariables; |
| 18 | +import org.springframework.core.ParameterizedTypeReference; |
15 | 19 |
|
16 | 20 | import city.makeour.ngsi.v2.api.EntitiesApi; |
17 | 21 | import city.makeour.ngsi.v2.model.CreateEntityRequest; |
@@ -145,5 +149,60 @@ void testCreateAndGetEntity_Minimal() throws GeneralSecurityException, NoSuchAlg |
145 | 149 |
|
146 | 150 | assertNotNull(retrievedEntity); |
147 | 151 | assertEquals(entityId, retrievedEntity.getId()); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + @DisplayName("updateEntityのUpsert(作成・更新)ロジックをテストする") |
| 156 | + @EnabledIfEnvironmentVariables({ |
| 157 | + @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USER_POOL_ID", matches = ".*"), |
| 158 | + @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_CLIENT_ID", matches = ".*"), |
| 159 | + @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_USERNAME", matches = ".*"), |
| 160 | + @EnabledIfEnvironmentVariable(named = "TEST_COGNITO_PASSWORD", matches = ".*") |
| 161 | + }) |
| 162 | + void testUpdateEntity_UpsertLogic() throws GeneralSecurityException, NoSuchAlgorithmException { |
| 163 | + MocClient client = new MocClient(); |
| 164 | + client.setMocAuthInfo(System.getenv("TEST_COGNITO_USER_POOL_ID"), System.getenv("TEST_COGNITO_CLIENT_ID")); |
| 165 | + client.login(System.getenv("TEST_COGNITO_USERNAME"), System.getenv("TEST_COGNITO_PASSWORD")); |
| 166 | + |
| 167 | + String entityId = "urn:ngsi-ld:TestUpsert:" + UUID.randomUUID().toString(); |
| 168 | + String entityType = "TestUpsertType"; |
| 169 | + |
| 170 | + // 1. "作成" (Insert) pathのテスト |
| 171 | + // エンティティが存在しない --> catchブロックの this.createEntity |
| 172 | + Map<String, Object> initialAttrs = new HashMap<>(); |
| 173 | + initialAttrs.put("temperature", 25); |
| 174 | + initialAttrs.put("humidity", 50); // "temperature" 以外の属性も指定 |
| 175 | + |
| 176 | + client.updateEntity(entityId, entityType, initialAttrs); |
| 177 | + |
| 178 | + // 検証 (作成) |
| 179 | + ParameterizedTypeReference<Map<String, Object>> mapType = new ParameterizedTypeReference<>() {}; |
| 180 | + Map<String, Object> createdEntity = client.getEntity(entityId, entityType).body(mapType); |
| 181 | + |
| 182 | + assertNotNull(createdEntity); |
| 183 | + assertEquals(entityId, createdEntity.get("id")); |
| 184 | + assertEquals(25, createdEntity.get("temperature")); |
| 185 | + assertEquals(50, createdEntity.get("humidity")); |
| 186 | + |
| 187 | + // 2. "更新" (Update/PATCH) pathのテスト |
| 188 | + // エンティティが既に存在する --> tryブロックの updateExistingEntityAttributesWithResponseSpec |
| 189 | + Map<String, Object> updateAttrs = new HashMap<>(); |
| 190 | + updateAttrs.put("temperature", 30); // 更新 |
| 191 | + updateAttrs.put("seatNumber", 10); // 追加 |
| 192 | + updateAttrs.put("status", "active"); |
| 193 | + |
| 194 | + client.updateEntity(entityId, entityType, updateAttrs); |
| 195 | + |
| 196 | + // 検証 (更新) |
| 197 | + Map<String, Object> updatedEntity = client.getEntity(entityId, entityType).body(mapType); |
| 198 | + |
| 199 | + assertNotNull(updatedEntity); |
| 200 | + // 更新・追加されている |
| 201 | + assertEquals(30, updatedEntity.get("temperature")); |
| 202 | + assertEquals(10, updatedEntity.get("seatNumber")); |
| 203 | + // 最初の作成時から変更されず残っている |
| 204 | + assertEquals(50, updatedEntity.get("humidity")); |
| 205 | + |
| 206 | + assertEquals("active", updatedEntity.get("status")); |
148 | 207 | } |
149 | 208 | } |
0 commit comments