4444import org .junit .jupiter .api .BeforeEach ;
4545import org .junit .jupiter .api .Test ;
4646import org .mockito .MockitoAnnotations ;
47+ import org .slf4j .Logger ;
48+ import org .slf4j .LoggerFactory ;
4749import org .springframework .beans .factory .annotation .Autowired ;
4850import org .springframework .boot .test .autoconfigure .web .servlet .AutoConfigureMockMvc ;
4951import org .springframework .boot .test .context .SpringBootTest ;
7375import static org .gridsuite .computation .service .NotificationService .*;
7476import static org .gridsuite .securityanalysis .server .SecurityAnalysisProviderMock .*;
7577import static org .gridsuite .securityanalysis .server .service .SecurityAnalysisService .COMPUTATION_TYPE ;
78+ import static org .gridsuite .securityanalysis .server .util .CsvExportUtils .csvRowsToZippedCsv ;
7679import static org .gridsuite .securityanalysis .server .util .DatabaseQueryUtils .assertRequestsCount ;
7780import static org .gridsuite .securityanalysis .server .util .TestUtils .assertLogMessage ;
7881import static org .gridsuite .securityanalysis .server .util .TestUtils .readLinesFromFilePath ;
9497@ SpringBootTest
9598@ ContextConfigurationWithTestChannel
9699class 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 ());
0 commit comments