From 811cef6efcae9093e842562a3e5c54ef75261c81 Mon Sep 17 00:00:00 2001 From: BOUHOURS Antoine Date: Mon, 7 Oct 2024 17:36:10 +0200 Subject: [PATCH] Fix tests and add new tests for clone Signed-off-by: BOUHOURS Antoine --- .../client/CachedNetworkStoreClientTest.java | 5 +- .../PreloadingNetworkStoreClientTest.java | 56 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/network-store-client/src/test/java/com/powsybl/network/store/client/CachedNetworkStoreClientTest.java b/network-store-client/src/test/java/com/powsybl/network/store/client/CachedNetworkStoreClientTest.java index 0de899341..b2351b3b1 100644 --- a/network-store-client/src/test/java/com/powsybl/network/store/client/CachedNetworkStoreClientTest.java +++ b/network-store-client/src/test/java/com/powsybl/network/store/client/CachedNetworkStoreClientTest.java @@ -645,10 +645,11 @@ public void testGetExtensionCacheWithClonedNetwork() throws IOException { server.expect(ExpectedCount.once(), requestTo("/networks/" + networkUuid + "/" + Resource.INITIAL_VARIANT_NUM + "/to/" + targetVariantNum + "?targetVariantId=" + targetVariantId)) .andExpect(method(PUT)) .andRespond(withSuccess()); + // Clone network and verify that there is the expected extension in the cloned cache cachedClient.cloneNetwork(networkUuid, Resource.INITIAL_VARIANT_NUM, targetVariantNum, targetVariantId); - Optional apc1Attributes = cachedClient.getExtensionAttributes(networkUuid, Resource.INITIAL_VARIANT_NUM, ResourceType.GENERATOR, identifiableId, ActivePowerControl.NAME); + Optional apc1Attributes = cachedClient.getExtensionAttributes(networkUuid, targetVariantNum, ResourceType.GENERATOR, identifiableId, ActivePowerControl.NAME); assertTrue(apc1Attributes.isPresent()); - Optional os1Attributes = cachedClient.getExtensionAttributes(networkUuid, Resource.INITIAL_VARIANT_NUM, ResourceType.GENERATOR, identifiableId, OperatingStatus.NAME); + Optional os1Attributes = cachedClient.getExtensionAttributes(networkUuid, targetVariantNum, ResourceType.GENERATOR, identifiableId, OperatingStatus.NAME); assertFalse(os1Attributes.isPresent()); server.verify(); server.reset(); diff --git a/network-store-client/src/test/java/com/powsybl/network/store/client/PreloadingNetworkStoreClientTest.java b/network-store-client/src/test/java/com/powsybl/network/store/client/PreloadingNetworkStoreClientTest.java index c80c5a01d..472c28814 100644 --- a/network-store-client/src/test/java/com/powsybl/network/store/client/PreloadingNetworkStoreClientTest.java +++ b/network-store-client/src/test/java/com/powsybl/network/store/client/PreloadingNetworkStoreClientTest.java @@ -34,6 +34,7 @@ import static org.junit.Assert.*; import static org.springframework.http.HttpMethod.GET; +import static org.springframework.http.HttpMethod.PUT; import static org.springframework.test.web.client.match.MockRestRequestMatchers.method; import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; @@ -960,4 +961,59 @@ private void loadTwoIdentifiablesToCache(String identifiableId1, String identifi server.verify(); server.reset(); } + + @Test + public void testGetExtensionsCacheWithClonedNetwork() throws IOException { + int targetVariantNum = 1; + String targetVariantId = "new_variant"; + String identifiableId1 = "GEN"; + String identifiableId2 = "GEN1"; + + // Load the identifiables in the cache + loadTwoIdentifiablesToCache(identifiableId1, identifiableId2); + + // Two successive ExtensionAttributes retrieval, only the first should send a REST request, the second uses the cache + ActivePowerControlAttributes apc1 = ActivePowerControlAttributes.builder() + .droop(5.2) + .participate(true) + .participationFactor(0.5) + .build(); + GeneratorStartupAttributes gs1 = GeneratorStartupAttributes.builder() + .marginalCost(6.8) + .forcedOutageRate(35) + .plannedOutageRate(30) + .startupCost(28) + .plannedActivePowerSetpoint(5) + .build(); + ActivePowerControlAttributes apc2 = ActivePowerControlAttributes.builder() + .droop(5.2) + .participate(true) + .participationFactor(1) + .build(); + + // Load extensions to cache + String multipleExtensionAttributes = objectMapper.writerFor(new TypeReference>>() { + }).writeValueAsString(Map.of(identifiableId1, Map.of(ActivePowerControl.NAME, apc1, GeneratorStartup.NAME, gs1), identifiableId2, Map.of(ActivePowerControl.NAME, apc2))); + server.expect(ExpectedCount.once(), requestTo("/networks/" + networkUuid + "/" + Resource.INITIAL_VARIANT_NUM + "/identifiables/types/" + ResourceType.GENERATOR + "/extensions")) + .andExpect(method(GET)) + .andRespond(withSuccess(multipleExtensionAttributes, MediaType.APPLICATION_JSON)); + Map> extensionAttributesByIdentifiableIdMap = cachedClient.getAllExtensionsAttributesByResourceType(networkUuid, Resource.INITIAL_VARIANT_NUM, ResourceType.GENERATOR); + assertEquals(2, extensionAttributesByIdentifiableIdMap.size()); + server.verify(); + server.reset(); + + // Clone network + server.expect(ExpectedCount.once(), requestTo("/networks/" + networkUuid + "/" + Resource.INITIAL_VARIANT_NUM + "/to/" + targetVariantNum + "?targetVariantId=" + targetVariantId)) + .andExpect(method(PUT)) + .andRespond(withSuccess()); + cachedClient.cloneNetwork(networkUuid, Resource.INITIAL_VARIANT_NUM, targetVariantNum, targetVariantId); + + // Verify that the cache is copied and there is no new fetch + extensionAttributesByIdentifiableIdMap = cachedClient.getAllExtensionsAttributesByResourceType(networkUuid, targetVariantNum, ResourceType.GENERATOR); + assertEquals(2, extensionAttributesByIdentifiableIdMap.size()); + Map extensionAttributesByExtensionNameMap = cachedClient.getAllExtensionsAttributesByIdentifiableId(networkUuid, targetVariantNum, ResourceType.GENERATOR, identifiableId1); + assertEquals(2, extensionAttributesByExtensionNameMap.size()); + server.verify(); + server.reset(); + } }