Skip to content

Commit 2f1991f

Browse files
committed
add a test for better covarge
Signed-off-by: David BRAQUART <[email protected]>
1 parent cb087f8 commit 2f1991f

File tree

2 files changed

+69
-19
lines changed

2 files changed

+69
-19
lines changed

src/test/java/org/gridsuite/securityanalysis/server/SecurityAnalysisControllerTest.java

Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import org.junit.jupiter.api.BeforeEach;
4545
import org.junit.jupiter.api.Test;
4646
import org.mockito.MockitoAnnotations;
47+
import org.slf4j.Logger;
48+
import org.slf4j.LoggerFactory;
4749
import org.springframework.beans.factory.annotation.Autowired;
4850
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
4951
import org.springframework.boot.test.context.SpringBootTest;
@@ -73,6 +75,7 @@
7375
import static org.gridsuite.computation.service.NotificationService.*;
7476
import static org.gridsuite.securityanalysis.server.SecurityAnalysisProviderMock.*;
7577
import static org.gridsuite.securityanalysis.server.service.SecurityAnalysisService.COMPUTATION_TYPE;
78+
import static org.gridsuite.securityanalysis.server.util.CsvExportUtils.csvRowsToZippedCsv;
7679
import static org.gridsuite.securityanalysis.server.util.DatabaseQueryUtils.assertRequestsCount;
7780
import static org.gridsuite.securityanalysis.server.util.TestUtils.assertLogMessage;
7881
import static org.gridsuite.securityanalysis.server.util.TestUtils.readLinesFromFilePath;
@@ -94,6 +97,7 @@
9497
@SpringBootTest
9598
@ContextConfigurationWithTestChannel
9699
class SecurityAnalysisControllerTest {
100+
private static final Logger LOGGER = LoggerFactory.getLogger(SecurityAnalysisControllerTest.class);
97101

98102
private static final UUID NETWORK_UUID = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e4");
99103
private static final UUID NETWORK_STOP_UUID = UUID.fromString("7928181c-7977-4592-ba19-88027e4254e6");
@@ -857,6 +861,41 @@ void getZippedCsvResults() throws Exception {
857861
checkAllZippedCsvResults();
858862
}
859863

864+
@Test
865+
void getZippedCsvResultsFromCustomData() throws Exception {
866+
final String expectedCsvFile = "/results/n-result-fr-custom.csv";
867+
final String lang = "fr";
868+
final List<String> header = getCsvHeaderFromResource(expectedCsvFile, lang);
869+
870+
// Build binary/zipped data from custom DTOs rather than from AS results.
871+
PreContingencyLimitViolationResultDTO dto = PreContingencyLimitViolationResultDTO.builder()
872+
.subjectId("Ouvrage")
873+
.limitViolation(LimitViolationDTO.builder()
874+
.locationId("BUS")
875+
.limitName("lim_name")
876+
.acceptableDuration(100)
877+
.patlLoading(111.5)
878+
.patlLimit(80.66)
879+
.upcomingAcceptableDuration(Integer.MAX_VALUE)
880+
.build())
881+
.build();
882+
PreContingencyLimitViolationResultDTO dto2 = PreContingencyLimitViolationResultDTO.builder()
883+
.subjectId("Ouvrage2")
884+
.limitViolation(LimitViolationDTO.builder()
885+
.locationId("BUS2")
886+
.limitName("lim_name2")
887+
.acceptableDuration(100)
888+
.patlLoading(111.5)
889+
.patlLimit(80.66)
890+
.upcomingAcceptableDuration(1000)
891+
.build())
892+
.build();
893+
List<List<String>> csvRows = List.of(dto.toCsvRow(ENUM_TRANSLATIONS_FR, lang), dto2.toCsvRow(ENUM_TRANSLATIONS_FR, lang));
894+
byte[] resultAsByteArray = csvRowsToZippedCsv(header, lang, csvRows);
895+
// and compare with the expected file
896+
checkCsvResultFromBytes(expectedCsvFile, resultAsByteArray);
897+
}
898+
860899
private List<String> getCsvHeaderFromResource(String resourcePath, String lang) {
861900
List<String> lines = readLinesFromFilePath(resourcePath, 1);
862901
String header = lines.isEmpty() ? "" : lines.getFirst();
@@ -908,26 +947,11 @@ private void checkAllZippedCsvResults() throws Exception {
908947
assertRequestsCount(4, 0, 0, 0);
909948
}
910949

911-
private void checkZippedCsvResult(String resultType, String resourcePath, String lang) throws Exception {
912-
CsvTranslationDTO csvTranslationDTO = CsvTranslationDTO.builder()
913-
.headers(getCsvHeaderFromResource(resourcePath, lang))
914-
.enumValueTranslations("en".equalsIgnoreCase(lang) ? ENUM_TRANSLATIONS_EN : ENUM_TRANSLATIONS_FR)
915-
.language(lang)
916-
.build();
917-
918-
// get csv file
919-
byte[] resultAsByteArray = mockMvc.perform(post("/" + VERSION + "/results/" + RESULT_UUID + "/" + resultType + "/csv")
920-
.contentType(MediaType.APPLICATION_JSON)
921-
.content(mapper.writeValueAsString(csvTranslationDTO)))
922-
.andExpectAll(
923-
status().isOk(),
924-
content().contentType(APPLICATION_OCTET_STREAM_VALUE)
925-
).andReturn().getResponse().getContentAsByteArray();
926-
950+
private void checkCsvResultFromBytes(String expectedCsvResource, byte[] resultAsByteArray) throws Exception {
927951
// get zip file stream
928952
try (ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(resultAsByteArray));
929-
ByteArrayOutputStream contentOutputStream = new ByteArrayOutputStream();
930-
ByteArrayOutputStream expectedContentOutputStream = new ByteArrayOutputStream()) {
953+
ByteArrayOutputStream contentOutputStream = new ByteArrayOutputStream();
954+
ByteArrayOutputStream expectedContentOutputStream = new ByteArrayOutputStream()) {
931955
// get first entry
932956
ZipEntry zipEntry = zin.getNextEntry();
933957
// check zip entry name
@@ -936,15 +960,38 @@ private void checkZippedCsvResult(String resultType, String resourcePath, String
936960
StreamUtils.copy(zin, contentOutputStream);
937961

938962
// get expected content as outputStream
939-
InputStream csvStream = getClass().getResourceAsStream(resourcePath);
963+
InputStream csvStream = getClass().getResourceAsStream(expectedCsvResource);
940964
StreamUtils.copy(csvStream, expectedContentOutputStream);
941965

966+
// For debug
967+
LOGGER.info("CSV result :\n {}", contentOutputStream);
968+
LOGGER.info("CSV expected:\n {}", expectedContentOutputStream);
969+
942970
// using bytearray comparison to check BOM presence in CSV files
943971
Assertions.assertThat(contentOutputStream.toByteArray()).isEqualTo(expectedContentOutputStream.toByteArray());
944972
zin.closeEntry();
945973
}
946974
}
947975

976+
private void checkZippedCsvResult(String resultType, String expectedCsvResource, String lang) throws Exception {
977+
CsvTranslationDTO csvTranslationDTO = CsvTranslationDTO.builder()
978+
.headers(getCsvHeaderFromResource(expectedCsvResource, lang))
979+
.enumValueTranslations("en".equalsIgnoreCase(lang) ? ENUM_TRANSLATIONS_EN : ENUM_TRANSLATIONS_FR)
980+
.language(lang)
981+
.build();
982+
983+
// get csv file as binary (zip)
984+
byte[] resultAsByteArray = mockMvc.perform(post("/" + VERSION + "/results/" + RESULT_UUID + "/" + resultType + "/csv")
985+
.contentType(MediaType.APPLICATION_JSON)
986+
.content(mapper.writeValueAsString(csvTranslationDTO)))
987+
.andExpectAll(
988+
status().isOk(),
989+
content().contentType(APPLICATION_OCTET_STREAM_VALUE)
990+
).andReturn().getResponse().getContentAsByteArray();
991+
992+
checkCsvResultFromBytes(expectedCsvResource, resultAsByteArray);
993+
}
994+
948995
private void assertResultNotFound(UUID resultUuid) throws Exception {
949996
mockMvc.perform(get("/" + VERSION + "/results/" + resultUuid + "/n-result"))
950997
.andExpect(status().isNotFound());
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Ouvrage;Type de contrainte;Noeud électrique;Nom de la limite;Charge (% limite);Charge (% IST);Tempo effective;Tempo imminente;Nom de la limite suivante;Limite (A ou kV);IST (A);Valeur calculée (A ou kV);Côté
2+
Ouvrage;;BUS;lim_name;;111,5;100;;;0;80,66;0;
3+
Ouvrage2;;BUS2;lim_name2;;111,5;100;1000;;0;80,66;0;

0 commit comments

Comments
 (0)