Skip to content

Commit 937b628

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

File tree

2 files changed

+55
-19
lines changed

2 files changed

+55
-19
lines changed

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

Lines changed: 53 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,28 @@ 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 DTO 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+
.build())
880+
.build();
881+
byte[] resultAsByteArray = csvRowsToZippedCsv(header, lang, List.of(dto.toCsvRow(ENUM_TRANSLATIONS_FR, lang)));
882+
// and compare with the expected file
883+
checkCsvResultFromBytes(expectedCsvFile, resultAsByteArray);
884+
}
885+
860886
private List<String> getCsvHeaderFromResource(String resourcePath, String lang) {
861887
List<String> lines = readLinesFromFilePath(resourcePath, 1);
862888
String header = lines.isEmpty() ? "" : lines.getFirst();
@@ -908,26 +934,11 @@ private void checkAllZippedCsvResults() throws Exception {
908934
assertRequestsCount(4, 0, 0, 0);
909935
}
910936

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-
937+
private void checkCsvResultFromBytes(String expectedCsvResource, byte[] resultAsByteArray) throws Exception {
927938
// get zip file stream
928939
try (ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(resultAsByteArray));
929-
ByteArrayOutputStream contentOutputStream = new ByteArrayOutputStream();
930-
ByteArrayOutputStream expectedContentOutputStream = new ByteArrayOutputStream()) {
940+
ByteArrayOutputStream contentOutputStream = new ByteArrayOutputStream();
941+
ByteArrayOutputStream expectedContentOutputStream = new ByteArrayOutputStream()) {
931942
// get first entry
932943
ZipEntry zipEntry = zin.getNextEntry();
933944
// check zip entry name
@@ -936,15 +947,38 @@ private void checkZippedCsvResult(String resultType, String resourcePath, String
936947
StreamUtils.copy(zin, contentOutputStream);
937948

938949
// get expected content as outputStream
939-
InputStream csvStream = getClass().getResourceAsStream(resourcePath);
950+
InputStream csvStream = getClass().getResourceAsStream(expectedCsvResource);
940951
StreamUtils.copy(csvStream, expectedContentOutputStream);
941952

953+
// For debug
954+
LOGGER.info("CSV result :\n {}", contentOutputStream);
955+
LOGGER.info("CSV expected:\n {}", expectedContentOutputStream);
956+
942957
// using bytearray comparison to check BOM presence in CSV files
943958
Assertions.assertThat(contentOutputStream.toByteArray()).isEqualTo(expectedContentOutputStream.toByteArray());
944959
zin.closeEntry();
945960
}
946961
}
947962

963+
private void checkZippedCsvResult(String resultType, String expectedCsvResource, String lang) throws Exception {
964+
CsvTranslationDTO csvTranslationDTO = CsvTranslationDTO.builder()
965+
.headers(getCsvHeaderFromResource(expectedCsvResource, lang))
966+
.enumValueTranslations("en".equalsIgnoreCase(lang) ? ENUM_TRANSLATIONS_EN : ENUM_TRANSLATIONS_FR)
967+
.language(lang)
968+
.build();
969+
970+
// get csv file as binary (zip)
971+
byte[] resultAsByteArray = mockMvc.perform(post("/" + VERSION + "/results/" + RESULT_UUID + "/" + resultType + "/csv")
972+
.contentType(MediaType.APPLICATION_JSON)
973+
.content(mapper.writeValueAsString(csvTranslationDTO)))
974+
.andExpectAll(
975+
status().isOk(),
976+
content().contentType(APPLICATION_OCTET_STREAM_VALUE)
977+
).andReturn().getResponse().getContentAsByteArray();
978+
979+
checkCsvResultFromBytes(expectedCsvResource, resultAsByteArray);
980+
}
981+
948982
private void assertResultNotFound(UUID resultUuid) throws Exception {
949983
mockMvc.perform(get("/" + VERSION + "/results/" + resultUuid + "/n-result"))
950984
.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;;111,5;100;;;0;80,66;0;

0 commit comments

Comments
 (0)