44import static org .mockito .Mockito .*;
55
66import java .lang .reflect .Method ;
7+ import java .util .ArrayList ;
78import java .util .List ;
89import java .util .Optional ;
910import java .util .Set ;
2324import de .symeda .sormas .api .immunization .ImmunizationDto ;
2425import de .symeda .sormas .api .person .PersonDto ;
2526import de .symeda .sormas .api .person .PersonReferenceDto ;
27+ import de .symeda .sormas .api .utils .Tuple ;
2628import de .symeda .sormas .api .vaccination .VaccinationDto ;
2729import de .symeda .sormas .backend .AbstractUnitTest ;
2830import de .symeda .sormas .backend .caze .CaseFacadeEjb ;
@@ -125,50 +127,65 @@ void tryFetchByI18nNameForCreateUpdate_returnsNewImmunization_forImmunizationPre
125127 // — save(List) —
126128
127129 @ Test
128- void save_list_caseData_delegatesToCaseFacade () {
130+ void save_caseData_delegatesToCaseFacade () {
131+ // PREPARE
129132 CaseDataDto caseData = new CaseDataDto ();
130133
131- victim .save (List .of (caseData ));
134+ // EXECUTE
135+ victim .save (List .of (Tuple .<Integer , EntityDto > secondOnly (caseData )));
132136
137+ // CHECK
133138 verify (caseFacade ).save (caseData );
134139 }
135140
136141 @ Test
137- void save_list_personDto_delegatesToPersonFacade () {
142+ void save_personDto_delegatesToPersonFacade () {
143+ // PREPARE
138144 PersonDto personDto = new PersonDto ();
139145
140- victim .save (List .of (personDto ));
146+ // EXECUTE
147+ victim .save (List .of (Tuple .<Integer , EntityDto > secondOnly (personDto )));
141148
149+ // CHECK
142150 verify (personFacade ).save (personDto );
143151 }
144152
145153 @ Test
146- void save_list_immunizationDto_delegatesToImmunizationFacade () {
154+ void save_immunizationDto_delegatesToImmunizationFacade () {
155+ // PREPARE
147156 ImmunizationDto immunization = new ImmunizationDto ();
148157
149- victim .save (List .of (immunization ));
158+ // EXECUTE
159+ victim .save (List .of (Tuple .<Integer , EntityDto > secondOnly (immunization )));
150160
161+ // CHECK
151162 verify (immunizationFacade ).save (immunization );
152163 }
153164
154165 @ Test
155- void save_list_vaccinationWithExistingImmunization_attachesVaccinationThenSavesImmunization () {
166+ void save_vaccinationWithExistingImmunization_attachesVaccinationThenSavesImmunization () {
167+ // PREPARE
156168 ImmunizationDto immunization = new ImmunizationDto ();
157169 VaccinationDto vaccination = new VaccinationDto ();
158170
159- victim .save (List .of (immunization , vaccination ));
171+ // EXECUTE
172+ victim .save (List .of (Tuple .<Integer , EntityDto > secondOnly (immunization ), Tuple .<Integer , EntityDto > secondOnly (vaccination )));
160173
174+ // CHECK
161175 verify (immunizationFacade ).save (immunization );
162176 assertAll (() -> assertEquals (1 , immunization .getVaccinations ().size ()), () -> assertSame (vaccination , immunization .getVaccinations ().get (0 )));
163177 }
164178
165179 @ Test
166- void save_list_vaccinationWithoutImmunization_autoCreatesImmunizationAttachesAndSaves () {
180+ void save_vaccinationWithoutImmunization_autoCreatesImmunizationAttachesAndSaves () {
181+ // PREPARE
167182 CaseDataDto caseData = buildCaseDataWithPerson ("person-uuid" );
168183 VaccinationDto vaccination = new VaccinationDto ();
169184
170- victim .save (List .of (caseData , vaccination ));
185+ // EXECUTE
186+ victim .save (List .of (Tuple .<Integer , EntityDto > secondOnly (caseData ), Tuple .<Integer , EntityDto > secondOnly (vaccination )));
171187
188+ // CHECK
172189 ArgumentCaptor <ImmunizationDto > captor = ArgumentCaptor .forClass (ImmunizationDto .class );
173190 verify (immunizationFacade ).save (captor .capture ());
174191 ImmunizationDto savedImmunization = captor .getValue ();
@@ -178,22 +195,60 @@ void save_list_vaccinationWithoutImmunization_autoCreatesImmunizationAttachesAnd
178195 }
179196
180197 @ Test
181- void save_list_vaccinationWithoutImmunizationOrCaseData_throwsIllegalState () {
198+ void save_vaccinationWithoutImmunizationOrCaseData_throwsIllegalState () {
199+ // PREPARE
182200 VaccinationDto vaccination = new VaccinationDto ();
183201
184- assertThrows (IllegalStateException .class , () -> victim .save (List .of (vaccination )));
202+ // EXECUTE & CHECK
203+ assertThrows (IllegalStateException .class , () -> victim .save (List .of (Tuple .<Integer , EntityDto > secondOnly (vaccination ))));
185204 }
186205
187206 @ Test
188- void save_list_vaccinationWithImmunization_doesNotCallCaseFacadeSave () {
207+ void save_vaccinationWithImmunization_doesNotCallCaseFacadeSave () {
208+ // PREPARE
189209 ImmunizationDto immunization = new ImmunizationDto ();
190210 VaccinationDto vaccination = new VaccinationDto ();
191211
192- victim .save (List .of (immunization , vaccination ));
212+ // EXECUTE
213+ victim .save (List .of (Tuple .<Integer , EntityDto > secondOnly (immunization ), Tuple .<Integer , EntityDto > secondOnly (vaccination )));
193214
215+ // CHECK
194216 verify (caseFacade , never ()).save (ArgumentMatchers .<@ Valid @ NotNull CaseDataDto > any ());
195217 }
196218
219+ @ Test
220+ void save_groupedVaccinations_eachGroupIndexAttachesToItsOwnImmunization () {
221+ // PREPARE
222+ CaseDataDto caseData = buildCaseDataWithPerson ("person-uuid" );
223+ ImmunizationDto immunization0 = new ImmunizationDto ();
224+ VaccinationDto vaccination0 = new VaccinationDto ();
225+ VaccinationDto vaccination1 = new VaccinationDto ();
226+
227+ List <Tuple <Integer , EntityDto >> entityDtosByKey = new ArrayList <>();
228+ entityDtosByKey .add (Tuple .secondOnly ((EntityDto ) caseData ));
229+ entityDtosByKey .add (Tuple .of (0 , (EntityDto ) immunization0 ));
230+ entityDtosByKey .add (Tuple .of (0 , (EntityDto ) vaccination0 ));
231+ entityDtosByKey .add (Tuple .of (1 , (EntityDto ) vaccination1 ));
232+
233+ // EXECUTE
234+ victim .save (entityDtosByKey );
235+
236+ // CHECK
237+ ArgumentCaptor <ImmunizationDto > captor = ArgumentCaptor .forClass (ImmunizationDto .class );
238+ verify (immunizationFacade , times (2 )).save (captor .capture ());
239+ ImmunizationDto savedImmunization0 = captor .getAllValues ().get (0 );
240+ ImmunizationDto savedImmunization1 = captor .getAllValues ().get (1 );
241+
242+ assertAll (
243+ () -> assertSame (immunization0 , savedImmunization0 ),
244+ () -> assertEquals (1 , savedImmunization0 .getVaccinations ().size ()),
245+ () -> assertSame (vaccination0 , savedImmunization0 .getVaccinations ().get (0 )),
246+
247+ () -> assertNotSame (immunization0 , savedImmunization1 ),
248+ () -> assertEquals (1 , savedImmunization1 .getVaccinations ().size ()),
249+ () -> assertSame (vaccination1 , savedImmunization1 .getVaccinations ().get (0 )));
250+ }
251+
197252 private static CaseDataDto buildCaseDataWithPerson (String personUuid ) {
198253 CaseDataDto caseData = new CaseDataDto ();
199254 caseData .setPerson (new PersonReferenceDto (personUuid ));
0 commit comments