Skip to content

Commit

Permalink
Refacto
Browse files Browse the repository at this point in the history
Signed-off-by: David SARTORI <[email protected]>
  • Loading branch information
David SARTORI committed Jan 11, 2024
1 parent e509910 commit c0aee83
Showing 1 changed file with 12 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private static final class ShortCircuitAnalysisResultMock {
static final ShortCircuitAnalysisResult RESULT_BASIC = new ShortCircuitAnalysisResult(List.of(FAULT_RESULT_BASIC_1, FAULT_RESULT_BASIC_2, FAULT_RESULT_BASIC_3));
}

private static class ShortCircuitAnalysisProviderMock implements ShortCircuitAnalysisProvider {
private static final class ShortCircuitAnalysisProviderMock implements ShortCircuitAnalysisProvider {
@Override
public String getName() {
return "ShortCircuitAnalysisMock";
Expand Down Expand Up @@ -180,21 +180,14 @@ public String getVersion() {

private Network nodeBreakerNetwork;

private static void assertResultsEquals(ShortCircuitAnalysisResult result, org.gridsuite.shortcircuit.server.dto.ShortCircuitAnalysisResult resultDto) {
assertEquals(result.getFaultResults().size(), resultDto.getFaults().size());
List<FaultResult> orderedFaultResults = result.getFaultResults().stream().sorted(Comparator.comparing(fr -> fr.getFault().getId())).collect(Collectors.toList());
List<org.gridsuite.shortcircuit.server.dto.FaultResult> orderedFaultResultsDto = resultDto.getFaults().stream().sorted(Comparator.comparing(fr -> fr.getFault().getId())).collect(Collectors.toList());
assertFaultResultsEquals(orderedFaultResults, orderedFaultResultsDto);
}

private static void assertPagedFaultResultsEquals(ShortCircuitAnalysisResult result, List<org.gridsuite.shortcircuit.server.dto.FaultResult> faultResults) {
private static void assertFaultResultsEquals(ShortCircuitAnalysisResult result, List<org.gridsuite.shortcircuit.server.dto.FaultResult> faultResults) {
assertEquals(result.getFaultResults().size(), faultResults.size());
List<FaultResult> orderedFaultResults = result.getFaultResults().stream().sorted(Comparator.comparing(fr -> fr.getFault().getId())).toList();
List<org.gridsuite.shortcircuit.server.dto.FaultResult> orderedFaultResultsDto = faultResults.stream().sorted(Comparator.comparing(fr -> fr.getFault().getId())).toList();
assertFaultResultsEquals(orderedFaultResults, orderedFaultResultsDto);
}

private static void assertPagedFaultResultsEqualsSorted(ShortCircuitAnalysisResult result, List<org.gridsuite.shortcircuit.server.dto.FaultResult> faultResults) {
private static void assertSortedFaultResultsEquals(ShortCircuitAnalysisResult result, List<org.gridsuite.shortcircuit.server.dto.FaultResult> faultResults) {
assertEquals(result.getFaultResults().size(), faultResults.size());
assertFaultResultsEquals(result.getFaultResults(), faultResults);
}
Expand Down Expand Up @@ -322,7 +315,7 @@ public void runTest() throws Exception {
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
org.gridsuite.shortcircuit.server.dto.ShortCircuitAnalysisResult resultDto = mapper.readValue(result.getResponse().getContentAsString(), org.gridsuite.shortcircuit.server.dto.ShortCircuitAnalysisResult.class);
assertResultsEquals(ShortCircuitAnalysisResultMock.RESULT_WITH_LIMIT_VIOLATIONS, resultDto);
assertFaultResultsEquals(ShortCircuitAnalysisResultMock.RESULT_WITH_LIMIT_VIOLATIONS, resultDto.getFaults());

// BASIC mode
result = mockMvc.perform(get(
Expand All @@ -332,7 +325,7 @@ public void runTest() throws Exception {
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
org.gridsuite.shortcircuit.server.dto.ShortCircuitAnalysisResult resultDtoBasic = mapper.readValue(result.getResponse().getContentAsString(), org.gridsuite.shortcircuit.server.dto.ShortCircuitAnalysisResult.class);
assertResultsEquals(ShortCircuitAnalysisResultMock.RESULT_BASIC, resultDtoBasic);
assertFaultResultsEquals(ShortCircuitAnalysisResultMock.RESULT_BASIC, resultDtoBasic.getFaults());

// FULL mode
result = mockMvc.perform(get(
Expand All @@ -342,7 +335,7 @@ public void runTest() throws Exception {
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
org.gridsuite.shortcircuit.server.dto.ShortCircuitAnalysisResult resultDtoFull = mapper.readValue(result.getResponse().getContentAsString(), org.gridsuite.shortcircuit.server.dto.ShortCircuitAnalysisResult.class);
assertResultsEquals(ShortCircuitAnalysisResultMock.RESULT_MAGNITUDE_FULL, resultDtoFull);
assertFaultResultsEquals(ShortCircuitAnalysisResultMock.RESULT_MAGNITUDE_FULL, resultDtoFull.getFaults());

result = mockMvc.perform(get(
"/" + VERSION + "/results/{resultUuid}/fault_results/paged", RESULT_UUID)
Expand All @@ -358,7 +351,7 @@ public void runTest() throws Exception {
JsonNode faultResultsPageNode = mapper.readTree(result.getResponse().getContentAsString());
ObjectReader faultResultsReader = mapper.readerFor(new TypeReference<List<org.gridsuite.shortcircuit.server.dto.FaultResult>>() { });
List<org.gridsuite.shortcircuit.server.dto.FaultResult> faultResultsPageDto0 = faultResultsReader.readValue(faultResultsPageNode.get("content"));
assertPagedFaultResultsEquals(ShortCircuitAnalysisResultMock.RESULT_WITH_LIMIT_VIOLATIONS, faultResultsPageDto0);
assertFaultResultsEquals(ShortCircuitAnalysisResultMock.RESULT_WITH_LIMIT_VIOLATIONS, faultResultsPageDto0);

result = mockMvc.perform(get(
"/" + VERSION + "/results/{resultUuid}/fault_results/paged", RESULT_UUID)
Expand All @@ -371,7 +364,7 @@ public void runTest() throws Exception {
.andReturn();
JsonNode faultResultsPageNode0 = mapper.readTree(result.getResponse().getContentAsString());
List<org.gridsuite.shortcircuit.server.dto.FaultResult> faultResultsPageDto0Full = faultResultsReader.readValue(faultResultsPageNode0.get("content"));
assertPagedFaultResultsEqualsSorted(ShortCircuitAnalysisResultMock.RESULT_SORTED_PAGE_0, faultResultsPageDto0Full);
assertSortedFaultResultsEquals(ShortCircuitAnalysisResultMock.RESULT_SORTED_PAGE_0, faultResultsPageDto0Full);

result = mockMvc.perform(get(
"/" + VERSION + "/results/{resultUuid}/fault_results/paged", RESULT_UUID)
Expand All @@ -384,7 +377,7 @@ public void runTest() throws Exception {
.andReturn();
JsonNode faultResultsPageNode1 = mapper.readTree(result.getResponse().getContentAsString());
List<org.gridsuite.shortcircuit.server.dto.FaultResult> faultResultsPageDto1Full = faultResultsReader.readValue(faultResultsPageNode1.get("content"));
assertPagedFaultResultsEqualsSorted(ShortCircuitAnalysisResultMock.RESULT_SORTED_PAGE_1, faultResultsPageDto1Full);
assertSortedFaultResultsEquals(ShortCircuitAnalysisResultMock.RESULT_SORTED_PAGE_1, faultResultsPageDto1Full);

// should throw not found if result does not exist
mockMvc.perform(get("/" + VERSION + "/results/{resultUuid}", OTHER_RESULT_UUID))
Expand Down Expand Up @@ -439,7 +432,7 @@ public void runWithBusIdTest() throws Exception {
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
org.gridsuite.shortcircuit.server.dto.ShortCircuitAnalysisResult resultDto = mapper.readValue(result.getResponse().getContentAsString(), org.gridsuite.shortcircuit.server.dto.ShortCircuitAnalysisResult.class);
assertResultsEquals(ShortCircuitAnalysisResultMock.RESULT_FORTESCUE_FULL, resultDto);
assertFaultResultsEquals(ShortCircuitAnalysisResultMock.RESULT_FORTESCUE_FULL, resultDto.getFaults());

// paged results (more deeply tested in unit tests)
result = mockMvc.perform(get(
Expand All @@ -455,7 +448,7 @@ public void runWithBusIdTest() throws Exception {
List<org.gridsuite.shortcircuit.server.dto.FeederResult> feederResults = reader.readValue(feederResultsPage.get("content"));
// we update the fault result with the feeders we just get to be able to use the assertion
org.gridsuite.shortcircuit.server.dto.FaultResult formattedFaultResult = new org.gridsuite.shortcircuit.server.dto.FaultResult(faultResult.getFault(), faultResult.getCurrent(), faultResult.getPositiveMagnitude(), faultResult.getShortCircuitPower(), faultResult.getLimitViolations(), feederResults, faultResult.getShortCircuitLimits());
assertPagedFaultResultsEquals(ShortCircuitAnalysisResultMock.RESULT_FORTESCUE_FULL, List.of(formattedFaultResult));
assertFaultResultsEquals(ShortCircuitAnalysisResultMock.RESULT_FORTESCUE_FULL, List.of(formattedFaultResult));
}
}

Expand Down Expand Up @@ -499,7 +492,7 @@ public void runWithBusBarSectionIdTest() throws Exception {
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
org.gridsuite.shortcircuit.server.dto.ShortCircuitAnalysisResult resultDto = mapper.readValue(result.getResponse().getContentAsString(), org.gridsuite.shortcircuit.server.dto.ShortCircuitAnalysisResult.class);
assertResultsEquals(ShortCircuitAnalysisResultMock.RESULT_FORTESCUE_FULL, resultDto);
assertFaultResultsEquals(ShortCircuitAnalysisResultMock.RESULT_FORTESCUE_FULL, resultDto.getFaults());
}
}

Expand Down

0 comments on commit c0aee83

Please sign in to comment.