1111import fr .insee .genesis .domain .service .contextualvariable .external .ContextualExternalVariableJsonService ;
1212import fr .insee .genesis .domain .service .contextualvariable .previous .ContextualPreviousVariableJsonService ;
1313import fr .insee .genesis .exceptions .GenesisException ;
14+ import fr .insee .genesis .exceptions .GenesisExceptionHandler ;
15+ import fr .insee .genesis .exceptions .NoDataException ;
16+ import fr .insee .genesis .exceptions .QuestionnaireNotFoundException ;
17+ import fr .insee .genesis .exceptions .ReviewDisabledException ;
18+ import fr .insee .genesis .exceptions .SpecificationNotFoundException ;
1419import fr .insee .genesis .infrastructure .document .contextualexternal .ContextualExternalVariableDocument ;
1520import fr .insee .genesis .infrastructure .document .contextualprevious .ContextualPreviousVariableDocument ;
1621import fr .insee .genesis .stubs .ConfigStub ;
2631import org .junit .jupiter .params .provider .Arguments ;
2732import org .junit .jupiter .params .provider .MethodSource ;
2833import org .junit .jupiter .params .provider .ValueSource ;
34+ import org .springframework .http .HttpStatus ;
2935import org .springframework .http .ResponseEntity ;
3036import org .springframework .util .FileSystemUtils ;
3137
@@ -607,10 +613,14 @@ void readPreviousJson_sourceState_too_long(String sourceState){
607613 );
608614
609615 //WHEN + THEN
610- ResponseEntity <Object > response = contextualVariableController .readContextualPreviousJson (QUESTIONNAIRE_ID_PREVIOUS , Mode .WEB , sourceState ,
611- fileName );
612- Assertions .assertEquals (400 ,response .getStatusCode ().value ());
613- }
616+ GenesisException ex = Assertions .assertThrows (
617+ GenesisException .class ,
618+ () -> contextualVariableController .readContextualPreviousJson (
619+ QUESTIONNAIRE_ID_PREVIOUS , Mode .WEB , sourceState , fileName
620+ )
621+ );
622+
623+ Assertions .assertEquals (400 , ex .getStatus ().value ()); }
614624
615625 @ Test
616626 @ SneakyThrows
@@ -628,9 +638,17 @@ void readPreviousJson_invalid_syntax(){
628638 );
629639
630640 //WHEN + THEN
631- ResponseEntity <Object > response = contextualVariableController .readContextualPreviousJson (QUESTIONNAIRE_ID_PREVIOUS , Mode .WEB , null ,
632- syntaxErrorFileName );
633- Assertions .assertEquals (400 ,response .getStatusCode ().value ());
641+ GenesisException ex = Assertions .assertThrows (
642+ GenesisException .class ,
643+ () -> contextualVariableController .readContextualPreviousJson (
644+ QUESTIONNAIRE_ID_PREVIOUS ,
645+ Mode .WEB ,
646+ null ,
647+ syntaxErrorFileName
648+ )
649+ );
650+
651+ Assertions .assertEquals (400 , ex .getStatus ().value ());
634652 }
635653 @ Test
636654 @ SneakyThrows
@@ -648,9 +666,14 @@ void readPreviousJson_not_a_json(){
648666 );
649667
650668 //WHEN + THEN
651- ResponseEntity <Object > response = contextualVariableController .readContextualPreviousJson (QUESTIONNAIRE_ID_PREVIOUS , Mode .WEB , null ,
652- syntaxErrorFileName );
653- Assertions .assertEquals (400 ,response .getStatusCode ().value ());
669+ GenesisException ex = Assertions .assertThrows (
670+ GenesisException .class ,
671+ () -> contextualVariableController .readContextualPreviousJson (
672+ QUESTIONNAIRE_ID_PREVIOUS , Mode .WEB , null , syntaxErrorFileName
673+ )
674+ );
675+
676+ Assertions .assertEquals (400 , ex .getStatus ().value ());
654677 }
655678
656679
@@ -671,8 +694,14 @@ void readPreviousJson_no_interrogation_id(String fileName){
671694 );
672695
673696 //WHEN + THEN
674- ResponseEntity <Object > response = contextualVariableController .readContextualPreviousJson (QUESTIONNAIRE_ID_PREVIOUS , Mode .WEB , null , fileName );
675- Assertions .assertEquals (400 ,response .getStatusCode ().value ());
697+ GenesisException ex = Assertions .assertThrows (
698+ GenesisException .class ,
699+ () -> contextualVariableController .readContextualPreviousJson (
700+ QUESTIONNAIRE_ID_PREVIOUS , Mode .WEB , null , fileName
701+ )
702+ );
703+
704+ Assertions .assertEquals (400 , ex .getStatus ().value ());
676705 }
677706
678707
@@ -856,6 +885,27 @@ static Stream<Arguments> overrideExternalCases() {
856885 );
857886 }
858887
888+ private ResponseEntity <String > toResponse (Exception e ) {
889+ GenesisExceptionHandler handler = new GenesisExceptionHandler ();
890+
891+ if (e instanceof GenesisException ge ) {
892+ return handler .handleGenesis (ge );
893+ }
894+ if (e instanceof QuestionnaireNotFoundException qnfe ) {
895+ return handler .handleQuestionnaireNotFound (qnfe );
896+ }
897+ if (e instanceof NoDataException nde ) {
898+ return handler .handleNoData (nde );
899+ }
900+ if (e instanceof SpecificationNotFoundException snfe ) {
901+ return handler .handleSpec (snfe );
902+ }
903+ if (e instanceof ReviewDisabledException rde ) {
904+ return handler .handleReviewDisabled (rde );
905+ }
906+ return ResponseEntity .status (HttpStatus .INTERNAL_SERVER_ERROR ).body ("Internal server error" );
907+ }
908+
859909 @ ParameterizedTest
860910 @ ValueSource (strings = {"invalid_syntax.json" ,
861911 "not_a_json.xml" ,
@@ -864,8 +914,8 @@ static Stream<Arguments> overrideExternalCases() {
864914 "double_interrogationId.json" }
865915 )
866916 @ SneakyThrows
867- void readExternalJson_error_400 (String fileName ){
868- //GIVEN
917+ void readExternalJson_error_400 (String fileName ) {
918+ // GIVEN
869919 Path contextualPath = SOURCE_PATH_EXTERNAL .resolve ("contextual" );
870920 Files .createDirectories (contextualPath );
871921
@@ -877,9 +927,19 @@ void readExternalJson_error_400(String fileName){
877927 StandardCopyOption .REPLACE_EXISTING
878928 );
879929
880- //WHEN + THEN
881- ResponseEntity <Object > response = contextualVariableController .readContextualExternalJson (QUESTIONNAIRE_ID_EXTERNAL , Mode .WEB , fileName );
882- Assertions .assertEquals (400 ,response .getStatusCode ().value ());
930+ // WHEN + THEN
931+ ResponseEntity <String > response ;
932+ try {
933+ ResponseEntity <Object > raw = contextualVariableController .readContextualExternalJson (
934+ QUESTIONNAIRE_ID_EXTERNAL , Mode .WEB , fileName
935+ );
936+ response = ResponseEntity .status (raw .getStatusCode ())
937+ .body (raw .getBody () == null ? null : raw .getBody ().toString ());
938+ } catch (Exception e ) {
939+ response = toResponse (e );
940+ }
941+
942+ Assertions .assertEquals (400 , response .getStatusCode ().value ());
883943 }
884944
885945 //UTILS
0 commit comments