Skip to content

Commit

Permalink
add test and consider code review remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
ghazwarhili committed Dec 12, 2023
1 parent 785084d commit 7c402c0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ public ResponseEntity<UUID> runAndSave(@Parameter(description = "Network UUID")
@Operation(summary = "Get computation count of the total of identifiables for a list of filters")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The sensitivity analysis identifiables count"),
@ApiResponse(responseCode = "404", description = "Sensitivity analysis identifiables count result has not been found")})
public ResponseEntity<Integer> getComputationCount(@Parameter(description = "Result UUID") @PathVariable("networkUuid") UUID networkUuid,
public ResponseEntity<Integer> getComputationCount(@Parameter(description = "Network UUID") @PathVariable("networkUuid") UUID networkUuid,
@Parameter(description = "Variant Id") @RequestParam(name = "variantId", required = false) String variantId,
@Parameter(description = "Is Injections Set") @RequestParam(name = "isInjectionsSet", required = false) Boolean isInjectionsSet,
@RequestBody Map<String, List<UUID>> ids) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(workerService.getComputationCount(ids, networkUuid, isInjectionsSet));
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(workerService.getComputationCount(ids, networkUuid, variantId, isInjectionsSet));
}

@GetMapping(value = "/results/{resultUuid}", produces = APPLICATION_JSON_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class SensitivityAnalysisInputBuilderService {
private static final String EXPECTED_TYPE = "expectedType";
private static final Logger LOGGER = LoggerFactory.getLogger(SensitivityAnalysisInputBuilderService.class);
public static final String INJECTIONS = "injections";
public static final String CONTINGENCIES = "contingencies";
private final ActionsService actionsService;
private final FilterService filterService;

Expand Down Expand Up @@ -114,15 +115,27 @@ private List<IdentifiableAttributes> goGetIdentifiables(EquipmentsContainer filt
}
}

private Integer getComputationCount(Map<String, List<UUID>> ids, UUID networkUuid, Integer containersAttributesCount) {
private Integer getContingenciesCount(List<UUID> ids, UUID networkUuid, String variantId) {
return ids.stream()
.mapToInt(uuid -> actionsService.getContingencyList(uuid, networkUuid, variantId).size())
.sum();
}

private Integer getComputationCount(Map<String, List<UUID>> ids, UUID networkUuid, String variantId, Integer containersAttributesCount) {
int contAttributesCountTemp = containersAttributesCount;
if (ids.containsKey(CONTINGENCIES)) {
int sumContingencyListSizes = Optional.of(getContingenciesCount(ids.get(CONTINGENCIES), networkUuid, variantId)).orElse(1);
contAttributesCountTemp *= sumContingencyListSizes;
ids.remove(CONTINGENCIES);
}
Map<String, List<Integer>> map = filterService.getIdentifiablesCount(ids, networkUuid, null);
Integer contAttributesCountTemp = containersAttributesCount;
for (String key : map.keySet()) {
int sensiFactorCount = map.get(key).stream().mapToInt(Integer::intValue).sum();
for (List<Integer> valueList : map.values()) {
int sensiFactorCount = valueList.stream().mapToInt(Integer::intValue).sum();
if (sensiFactorCount != 0) {
contAttributesCountTemp *= sensiFactorCount;
}
}

return contAttributesCountTemp;
}

Expand Down Expand Up @@ -443,13 +456,13 @@ private void buildSensitivityNodes(SensitivityAnalysisRunContext context, Networ
});
}

public Integer getComputationCount(Map<String, List<UUID>> ids, UUID networkUuid, Boolean isInjectionsSet) {
public Integer getComputationCount(Map<String, List<UUID>> ids, UUID networkUuid, String variantId, Boolean isInjectionsSet) {
Integer containersAttributesCount = 1;
if (Boolean.TRUE.equals(isInjectionsSet)) {
containersAttributesCount *= ids.get(INJECTIONS).size();
ids.remove(INJECTIONS);
}
containersAttributesCount = getComputationCount(ids, networkUuid, containersAttributesCount);
containersAttributesCount = getComputationCount(ids, networkUuid, variantId, containersAttributesCount);
return containersAttributesCount;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public Consumer<Message<String>> consumeCancel() {
return message -> cancelSensitivityAnalysisAsync(SensitivityAnalysisCancelContext.fromMessage(message));
}

public Integer getComputationCount(Map<String, List<UUID>> ids, UUID networkUuid, Boolean isInjectionsSet) {
return sensitivityAnalysisInputBuilderService.getComputationCount(ids, networkUuid, isInjectionsSet);
public Integer getComputationCount(Map<String, List<UUID>> ids, UUID networkUuid, String variantId, Boolean isInjectionsSet) {
return sensitivityAnalysisInputBuilderService.getComputationCount(ids, networkUuid, variantId, isInjectionsSet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ public class SensitivityAnalysisControllerTest {
private static final List<UUID> MONITORED_BRANCHES_FILTERS_UUID = List.of(UUID.randomUUID());
private static final List<UUID> INJECTIONS_FILTERS_UUID = List.of(UUID.randomUUID());
private static final List<UUID> CONTINGENCIES_FILTERS_UUID = List.of(UUID.randomUUID());
private static final Map<String, List<UUID>> IDS = Map.of("0", MONITORED_BRANCHES_FILTERS_UUID, "1", INJECTIONS_FILTERS_UUID, "2", CONTINGENCIES_FILTERS_UUID);
public static final String MONITORED_BRANCHS_KEY = "monitoredBranchs";
public static final String INJECTIONS_KEY = "injections";
public static final String CONTINGENCIES_KEY = "contingencies";
private static final Map<String, List<UUID>> IDS = Map.of(MONITORED_BRANCHS_KEY, MONITORED_BRANCHES_FILTERS_UUID, INJECTIONS_KEY, INJECTIONS_FILTERS_UUID, CONTINGENCIES_KEY, CONTINGENCIES_FILTERS_UUID);
private static final Map<String, List<UUID>> IDS_1 = Map.of(MONITORED_BRANCHS_KEY, MONITORED_BRANCHES_FILTERS_UUID, INJECTIONS_KEY, INJECTIONS_FILTERS_UUID);

private static final List<Contingency> CONTINGENCIES = List.of(
new Contingency("l1", new BranchContingency("l1")),
Expand Down Expand Up @@ -406,6 +410,7 @@ public void setUp() {
given(actionsService.getContingencyList(CONTINGENCIES_NODES_UUID, NETWORK_UUID, VARIANT_1_ID)).willReturn(CONTINGENCIES);
given(actionsService.getContingencyList(CONTINGENCIES_NODES_UUID, NETWORK_UUID, VARIANT_3_ID)).willReturn(CONTINGENCIES_VARIANT);
given(actionsService.getContingencyList(CONTINGENCIES_NODES_UUID, NETWORK_UUID, VARIANT_2_ID)).willReturn(CONTINGENCIES);
given(actionsService.getContingencyList(CONTINGENCIES_FILTERS_UUID.get(0), NETWORK_UUID, VARIANT_1_ID)).willReturn(CONTINGENCIES_VARIANT);

// filter service mocking
given(filterService.getIdentifiablesFromFilter(MONITORED_BRANCHES_FILTERS_INJECTIONS_SET_UUID, NETWORK_UUID, VARIANT_1_ID)).willReturn(BRANCHES);
Expand Down Expand Up @@ -469,7 +474,7 @@ public void setUp() {
given(filterService.getIdentifiablesFromFilter(EQUIPMENTS_IN_VOLTAGE_REGULATION_FILTERS_UUID, NETWORK_UUID, VARIANT_2_ID)).willReturn(EQUIPMENTS_IN_VOLTAGE_REGULATION);
given(filterService.getIdentifiablesFromFilter(EQUIPMENTS_IN_VOLTAGE_REGULATION_FILTERS_UUID, NETWORK_UUID, null)).willReturn(EQUIPMENTS_IN_VOLTAGE_REGULATION);
given(filterService.getIdentifiablesFromFilter(EQUIPMENTS_IN_VOLTAGE_REGULATION_FILTERS_UUID, NETWORK_STOP_UUID, VARIANT_2_ID)).willReturn(EQUIPMENTS_IN_VOLTAGE_REGULATION);
given(filterService.getIdentifiablesCount(IDS, NETWORK_UUID, null)).willReturn(Map.of("0", List.of(6), "1", List.of(6), "2", List.of(6)));
given(filterService.getIdentifiablesCount(IDS_1, NETWORK_UUID, null)).willReturn(Map.of(MONITORED_BRANCHS_KEY, List.of(6), INJECTIONS_KEY, List.of(6)));
// report service mocking
doAnswer(i -> null).when(reportService).sendReport(any(), any());

Expand Down Expand Up @@ -749,11 +754,12 @@ public void testStatus() {

@Test
public void testGetComputationCount() throws Exception {
MvcResult result = mockMvc.perform(post("/" + VERSION + "/networks/{networkUuid}/computation-count", NETWORK_UUID)
MvcResult result = mockMvc.perform(post("/" + VERSION + "/networks/{networkUuid}/computation-count?variantId={variantId}", NETWORK_UUID, VARIANT_1_ID)
.contentType(MediaType.APPLICATION_JSON).content(mapper.writeValueAsString(IDS)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
assertEquals("216", result.getResponse().getContentAsString());
assertEquals("72", result.getResponse().getContentAsString());
}

@Test
Expand Down

0 comments on commit 7c402c0

Please sign in to comment.