1111import org .junit .jupiter .api .Nested ;
1212import org .junit .jupiter .api .Test ;
1313import org .junit .jupiter .api .extension .ExtendWith ;
14+ import org .junit .jupiter .params .ParameterizedTest ;
15+ import org .junit .jupiter .params .provider .ValueSource ;
1416import org .mockito .ArgumentCaptor ;
1517import org .mockito .InjectMocks ;
1618import org .mockito .Mock ;
@@ -154,6 +156,7 @@ void bigDecimalIntegerDouble() {
154156 @ Nested
155157 class convertCollectedVariablesTests {
156158 @ Test
159+ @ DisplayName ("Should no nothing if no COLLECTED in payload and no last SU" )
157160 void convertCollectedVariables_shouldDoNothing_whenNoCollectedKeyAndNoLastSurveyUnit () {
158161 Map <String , Object > payload = new HashMap <>();
159162
@@ -176,12 +179,13 @@ void convertCollectedVariables_shouldDoNothing_whenNoCollectedKeyAndNoLastSurvey
176179 }
177180
178181 @ Test
182+ @ DisplayName ("Should no nothing if no COLLECTED in payload and last SU does not match datastate" )
179183 void convertCollectedVariables_shouldDoNothing_whenNoCollectedKeyAndLastSurveyUnitStateDoesNotMatch () {
180- Map <String , Object > payload = new HashMap <>(); // pas de clé "COLLECTED"
184+ Map <String , Object > payload = new HashMap <>();
181185
182186 SurveyUnitModel lastSurveyUnit = SurveyUnitModel .builder ()
183187 .interrogationId ("INT1" )
184- .state (DataState .FORCED ) // différent de DataState.COLLECTED
188+ .state (DataState .FORCED )
185189 .collectedVariables (List .of (
186190 VariableModel .builder ().varId ("VAR1" ).value ("val" ).iteration (1 ).build ()
187191 ))
@@ -206,12 +210,13 @@ void convertCollectedVariables_shouldDoNothing_whenNoCollectedKeyAndLastSurveyUn
206210 }
207211
208212 @ Test
213+ @ DisplayName ("Should add null variable if no COLLECTED in payload and last SU has the variable" )
209214 void convertCollectedVariables_shouldAddNullVariable_whenNoCollectedKeyAndLastSurveyUnitHasSingleVariable () {
210- Map <String , Object > payload = new HashMap <>(); // pas de clé "COLLECTED"
215+ Map <String , Object > payload = new HashMap <>();
211216
212217 SurveyUnitModel lastSurveyUnit = SurveyUnitModel .builder ()
213218 .interrogationId ("INT1" )
214- .state (DataState .COLLECTED ) // même état que dataState attendu
219+ .state (DataState .COLLECTED )
215220 .collectedVariables (List .of (
216221 VariableModel .builder ().varId ("VAR1" ).value ("oldVal" ).iteration (1 ).build ()
217222 ))
@@ -242,9 +247,17 @@ void convertCollectedVariables_shouldAddNullVariable_whenNoCollectedKeyAndLastSu
242247 });
243248 }
244249
245- @ Test
246- void convertCollectedVariables_shouldAddNullVariablesForEachIteration_whenNoCollectedKeyAndLastSurveyUnitHasMultipleIterations () {
247- Map <String , Object > payload = new HashMap <>(); // pas de clé "COLLECTED"
250+ @ ParameterizedTest
251+ @ ValueSource (booleans = {false , true })
252+ @ DisplayName ("Should add null iterations if variable or COLLECED key absent in payload and last SU has the variable with iterations" )
253+ void convertCollectedVariables_shouldAddNullVariablesForEachIteration_whenLastSurveyUnitHasMultipleIterations (
254+ boolean isCollectedKeyPresentInPayload
255+ ) {
256+ Map <String , Object > payload = new HashMap <>();
257+ if (isCollectedKeyPresentInPayload ) {
258+ Map <String , Object > dataMap = new HashMap <>();
259+ payload .put ("COLLECTED" , dataMap );
260+ }
248261
249262 SurveyUnitModel lastSurveyUnit = SurveyUnitModel .builder ()
250263 .interrogationId ("INT1" )
@@ -280,14 +293,78 @@ void convertCollectedVariables_shouldAddNullVariablesForEachIteration_whenNoColl
280293 .extracting (VariableModel ::iteration )
281294 .containsExactlyInAnyOrder (1 , 2 , 3 );
282295 }
296+
297+ @ Test
298+ @ DisplayName ("Should add null iterations that are absent in payload" )
299+ void convertCollectedVariables_shouldAddNullIterations_whenLastSurveyUnitHasMultipleIterations () {
300+ //GIVEN
301+ String variableName = "VAR1" ;
302+
303+ Map <String , Object > dataMap = new HashMap <>();
304+ List <String > newValues = new ArrayList <>();
305+ newValues .add (null ); //Null raw value on first iteration
306+ newValues .addAll (List .of ("new2" , "new3" ));
307+ dataMap .put (variableName , Map .of ("COLLECTED" , newValues ));
308+ Map <String , Object > payload = new HashMap <>();
309+ payload .put ("COLLECTED" , dataMap );
310+
311+ SurveyUnitModel lastSurveyUnit = SurveyUnitModel .builder ()
312+ .interrogationId ("INT1" )
313+ .state (DataState .COLLECTED )
314+ .collectedVariables (List .of (
315+ VariableModel .builder ().varId (variableName ).value ("old1" ).iteration (1 ).build (),
316+ VariableModel .builder ().varId (variableName ).value ("old2" ).iteration (2 ).build (),
317+ VariableModel .builder ().varId (variableName ).value ("old3" ).iteration (3 ).build ()
318+ ))
319+ .build ();
320+
321+ SurveyUnitModel dst = SurveyUnitModel .builder ()
322+ .interrogationId ("INT1" )
323+ .collectedVariables (new ArrayList <>())
324+ .build ();
325+
326+ //WHEN
327+ rawDataConverterTestImpl .convertCollectedVariables (
328+ payload ,
329+ "INT1" ,
330+ lastSurveyUnit ,
331+ dst ,
332+ DataState .COLLECTED ,
333+ RawDataModelType .FILIERE ,
334+ variablesMap
335+ );
336+
337+ assertThat (dst .getCollectedVariables ())
338+ .hasSize (3 )
339+ .extracting (VariableModel ::iteration )
340+ .containsExactlyInAnyOrder (1 , 2 , 3 );
341+
342+ //First iteration present and null
343+ assertThat (dst .getCollectedVariables ())
344+ .filteredOn (variableModel -> variableModel .varId ().equals (variableName )
345+ && variableModel .iteration ().equals (1 ))
346+ .extracting (VariableModel ::value )
347+ .containsOnlyNulls ();
348+
349+ //Other iterations
350+ assertThat (dst .getCollectedVariables ())
351+ .filteredOn (variableModel ->
352+ variableModel .iteration ().equals (2 ) || variableModel .iteration ().equals (3 ))
353+ .hasSize (2 )
354+ .allSatisfy (v -> {
355+ assertThat (v .varId ()).isEqualTo ("VAR1" );
356+ })
357+ .extracting (VariableModel ::value )
358+ .containsExactlyInAnyOrder ("new2" ,"new3" );
359+ }
283360 }
284361
285362 @ DisplayName ("convertExternalVariables tests" )
286363 @ Nested
287364 class convertExternalVariablesTests {
288365 @ Test
289366 void convertExternalVariables_shouldDoNothing_whenNoExternalKeyAndNoLastSurveyUnit () {
290- Map <String , Object > payload = new HashMap <>(); // pas de clé "EXTERNAL"
367+ Map <String , Object > payload = new HashMap <>();
291368
292369 SurveyUnitModel dst = SurveyUnitModel .builder ()
293370 .interrogationId ("INT1" )
0 commit comments