Skip to content

Commit 30ace63

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

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

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

Lines changed: 52 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,27 @@ 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 minimal DTO rather than from AS results.
871+
// That way, we can test default/null values.
872+
PreContingencyLimitViolationResultDTO dto = PreContingencyLimitViolationResultDTO.builder()
873+
.subjectId("Ouvrage")
874+
.limitViolation(LimitViolationDTO.builder()
875+
.locationId("BUS")
876+
.limitName("lim_name")
877+
.acceptableDuration(100)
878+
.build())
879+
.build();
880+
byte[] resultAsByteArray = csvRowsToZippedCsv(header, lang, List.of(dto.toCsvRow(ENUM_TRANSLATIONS_FR, lang)));
881+
// and compare with the expected file
882+
checkCsvResultFromBytes(expectedCsvFile, resultAsByteArray);
883+
}
884+
860885
private List<String> getCsvHeaderFromResource(String resourcePath, String lang) {
861886
List<String> lines = readLinesFromFilePath(resourcePath, 1);
862887
String header = lines.isEmpty() ? "" : lines.getFirst();
@@ -908,26 +933,11 @@ private void checkAllZippedCsvResults() throws Exception {
908933
assertRequestsCount(4, 0, 0, 0);
909934
}
910935

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-
936+
private void checkCsvResultFromBytes(String expectedCsvResource, byte[] resultAsByteArray) throws Exception {
927937
// get zip file stream
928938
try (ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(resultAsByteArray));
929-
ByteArrayOutputStream contentOutputStream = new ByteArrayOutputStream();
930-
ByteArrayOutputStream expectedContentOutputStream = new ByteArrayOutputStream()) {
939+
ByteArrayOutputStream contentOutputStream = new ByteArrayOutputStream();
940+
ByteArrayOutputStream expectedContentOutputStream = new ByteArrayOutputStream()) {
931941
// get first entry
932942
ZipEntry zipEntry = zin.getNextEntry();
933943
// check zip entry name
@@ -936,15 +946,38 @@ private void checkZippedCsvResult(String resultType, String resourcePath, String
936946
StreamUtils.copy(zin, contentOutputStream);
937947

938948
// get expected content as outputStream
939-
InputStream csvStream = getClass().getResourceAsStream(resourcePath);
949+
InputStream csvStream = getClass().getResourceAsStream(expectedCsvResource);
940950
StreamUtils.copy(csvStream, expectedContentOutputStream);
941951

952+
// For debug
953+
LOGGER.info("CSV result :\n {}", contentOutputStream);
954+
LOGGER.info("CSV expected:\n {}", expectedContentOutputStream);
955+
942956
// using bytearray comparison to check BOM presence in CSV files
943957
Assertions.assertThat(contentOutputStream.toByteArray()).isEqualTo(expectedContentOutputStream.toByteArray());
944958
zin.closeEntry();
945959
}
946960
}
947961

962+
private void checkZippedCsvResult(String resultType, String expectedCsvResource, String lang) throws Exception {
963+
CsvTranslationDTO csvTranslationDTO = CsvTranslationDTO.builder()
964+
.headers(getCsvHeaderFromResource(expectedCsvResource, lang))
965+
.enumValueTranslations("en".equalsIgnoreCase(lang) ? ENUM_TRANSLATIONS_EN : ENUM_TRANSLATIONS_FR)
966+
.language(lang)
967+
.build();
968+
969+
// get csv file as binary (zip)
970+
byte[] resultAsByteArray = mockMvc.perform(post("/" + VERSION + "/results/" + RESULT_UUID + "/" + resultType + "/csv")
971+
.contentType(MediaType.APPLICATION_JSON)
972+
.content(mapper.writeValueAsString(csvTranslationDTO)))
973+
.andExpectAll(
974+
status().isOk(),
975+
content().contentType(APPLICATION_OCTET_STREAM_VALUE)
976+
).andReturn().getResponse().getContentAsByteArray();
977+
978+
checkCsvResultFromBytes(expectedCsvResource, resultAsByteArray);
979+
}
980+
948981
private void assertResultNotFound(UUID resultUuid) throws Exception {
949982
mockMvc.perform(get("/" + VERSION + "/results/" + resultUuid + "/n-result"))
950983
.andExpect(status().isNotFound());
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
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;;;100;;;0;;0;

0 commit comments

Comments
 (0)