Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed sorting issue in tests #70

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,18 +180,18 @@ 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());
private static void assertFaultResultsEquals(ShortCircuitAnalysisResult result, List<org.gridsuite.shortcircuit.server.dto.FaultResult> faultResults) {
assertEquals(result.getFaultResults().size(), faultResults.size());

// When we do not provide sort parameter to the request, order is not guaranteed, so we sort results here to not test their order
List<FaultResult> orderedFaultResults = result.getFaultResults().stream().sorted(Comparator.comparing(fr -> fr.getFault().getId())).toList();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertFaultResultsEquals() and assertSortedFaultResultsEquals() should be homogeneous.
Wether you assume that the results given as "ShortCircuitAnalysisResult result" are sorted and you don't need to sort them. Wether you assume they are not sorted and you should sort them in both methods

List<org.gridsuite.shortcircuit.server.dto.FaultResult> orderedFaultResultsDto = faultResults.stream().sorted(Comparator.comparing(fr -> fr.getFault().getId())).toList();
assertFaultResultsEquals(orderedFaultResults, orderedFaultResultsDto);
}

private static void assertPagedFaultResultsEquals(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());
List<FaultResult> orderedFaultResults = result.getFaultResults().stream().sorted(Comparator.comparing(fr -> fr.getFault().getId())).collect(Collectors.toList());
// don't need to sort here it's done in the paged request
assertFaultResultsEquals(orderedFaultResults, faultResults);
assertFaultResultsEquals(result.getFaultResults(), faultResults);
}

private static void assertFaultResultsEquals(List<FaultResult> faultResults, List<org.gridsuite.shortcircuit.server.dto.FaultResult> faultResultsDto) {
Expand Down Expand Up @@ -317,7 +317,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 @@ -327,7 +327,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 @@ -337,7 +337,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 @@ -353,7 +353,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 @@ -366,7 +366,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"));
assertPagedFaultResultsEquals(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 @@ -379,7 +379,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"));
assertPagedFaultResultsEquals(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 @@ -434,7 +434,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 @@ -450,7 +450,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 @@ -494,7 +494,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
Loading