Skip to content

Commit

Permalink
Fix tests and add new tests for clone
Browse files Browse the repository at this point in the history
Signed-off-by: BOUHOURS Antoine <[email protected]>
  • Loading branch information
antoinebhs committed Oct 7, 2024
1 parent 7c5c0e9 commit 811cef6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ExtensionAttributes> apc1Attributes = cachedClient.getExtensionAttributes(networkUuid, Resource.INITIAL_VARIANT_NUM, ResourceType.GENERATOR, identifiableId, ActivePowerControl.NAME);
Optional<ExtensionAttributes> apc1Attributes = cachedClient.getExtensionAttributes(networkUuid, targetVariantNum, ResourceType.GENERATOR, identifiableId, ActivePowerControl.NAME);
assertTrue(apc1Attributes.isPresent());
Optional<ExtensionAttributes> os1Attributes = cachedClient.getExtensionAttributes(networkUuid, Resource.INITIAL_VARIANT_NUM, ResourceType.GENERATOR, identifiableId, OperatingStatus.NAME);
Optional<ExtensionAttributes> os1Attributes = cachedClient.getExtensionAttributes(networkUuid, targetVariantNum, ResourceType.GENERATOR, identifiableId, OperatingStatus.NAME);
assertFalse(os1Attributes.isPresent());
server.verify();
server.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Map<String, Map<String, ExtensionAttributes>>>() {
}).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<String, Map<String, ExtensionAttributes>> 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<String, ExtensionAttributes> extensionAttributesByExtensionNameMap = cachedClient.getAllExtensionsAttributesByIdentifiableId(networkUuid, targetVariantNum, ResourceType.GENERATOR, identifiableId1);
assertEquals(2, extensionAttributesByExtensionNameMap.size());
server.verify();
server.reset();
}
}

0 comments on commit 811cef6

Please sign in to comment.