Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 9ea67fe

Browse files
Merge pull request SORMAS-Foundation#3358 from hzi-braunschweig/feature-3195-Unit-tests-for-the-ExternalVisitsRessource
Feature 3195 unit tests for the external visits ressource
2 parents cc1e542 + c8e121a commit 9ea67fe

7 files changed

Lines changed: 8635 additions & 16 deletions

File tree

sormas-backend/src/main/java/de/symeda/sormas/backend/externaljournal/ExternalJournalFacadeEjb.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,4 @@ public PatientDiaryPersonValidation validatePatientDiaryPerson(PersonDto person)
4040
return externalJournalService.validatePatientDiaryPerson(person);
4141
}
4242

43-
4443
}

sormas-backend/src/main/java/de/symeda/sormas/backend/externaljournal/ExternalJournalService.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,20 @@ public void notifyExternalJournalFollowUpUntilUpdate(ContactDto contact, Date pr
199199
* the person already available in the external journal
200200
* @param updatedPerson
201201
* the updated person in SORMAS
202+
* @return true if the person data change was considered relevant for external journals, false otherwise.
203+
*
202204
*/
203-
public void notifyExternalJournalPersonUpdate(PersonDto existingPerson, PersonDto updatedPerson) {
204-
if (shouldNotify(existingPerson, updatedPerson)) {
205+
public boolean notifyExternalJournalPersonUpdate(PersonDto existingPerson, PersonDto updatedPerson) {
206+
boolean shouldNotify = shouldNotify(existingPerson, updatedPerson);
207+
if (shouldNotify) {
205208
if (configFacade.getSymptomJournalConfig().getUrl() != null) {
206209
notifySymptomJournal(existingPerson.getUuid());
207210
}
208211
if (configFacade.getPatientDiaryConfig().getUrl() != null) {
209212
notifyPatientDiary(existingPerson.getUuid());
210213
}
211214
}
215+
return shouldNotify;
212216
}
213217

214218
/**
@@ -317,9 +321,7 @@ public PatientDiaryRegisterResult registerPatientDiaryPerson(PersonDto person) {
317321
private Invocation.Builder getExternalDataPersonInvocationBuilder(String personUuid) {
318322
String externalDataUrl = configFacade.getPatientDiaryConfig().getProbandsUrl() + "/external-data/" + personUuid;
319323
Client client = ClientBuilder.newClient();
320-
return client.target(externalDataUrl)
321-
.request(MediaType.APPLICATION_JSON)
322-
.header("x-access-token", getPatientDiaryAuthToken());
324+
return client.target(externalDataUrl).request(MediaType.APPLICATION_JSON).header("x-access-token", getPatientDiaryAuthToken());
323325
}
324326

325327
/**
@@ -367,14 +369,14 @@ public PatientDiaryPersonValidation validatePatientDiaryPerson(PersonDto person)
367369

368370
private boolean isEmailAvailable(String emailAddress) {
369371
return queryPatientDiary(EMAIL_QUERY_PARAM, emailAddress)
370-
.orElseThrow(() -> new RuntimeException("Could not query patient diary for Email address availability"))
371-
.getCount() == 0;
372+
.orElseThrow(() -> new RuntimeException("Could not query patient diary for Email address availability"))
373+
.getCount() == 0;
372374
}
373375

374376
private boolean isPhoneAvailable(String phone) {
375377
return queryPatientDiary(MOBILE_PHONE_QUERY_PARAM, phone)
376-
.orElseThrow(() -> new RuntimeException("Could not query patient diary for phone number availability"))
377-
.getCount() == 0;
378+
.orElseThrow(() -> new RuntimeException("Could not query patient diary for phone number availability"))
379+
.getCount() == 0;
378380
}
379381

380382
/**
@@ -393,10 +395,7 @@ public Optional<PatientDiaryPersonQueryResponse> queryPatientDiary(String key, S
393395
String encodedParams = URLEncoder.encode(queryParam, StandardCharsets.UTF_8.toString());
394396
String fullUrl = probandsUrl + "?q=" + encodedParams;
395397
Client client = ClientBuilder.newClient();
396-
Response response = client.target(fullUrl)
397-
.request(MediaType.APPLICATION_JSON)
398-
.header("x-access-token", getPatientDiaryAuthToken())
399-
.get();
398+
Response response = client.target(fullUrl).request(MediaType.APPLICATION_JSON).header("x-access-token", getPatientDiaryAuthToken()).get();
400399
if (response.getStatus() == NOT_FOUND_STATUS) {
401400
return Optional.empty();
402401
}

sormas-backend/src/test/java/de/symeda/sormas/backend/AbstractBeanTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import de.symeda.sormas.api.region.CountryFacade;
3030
import de.symeda.sormas.backend.region.CountryFacadeEjb;
3131
import de.symeda.sormas.backend.region.CountryService;
32+
import de.symeda.sormas.backend.externaljournal.ExternalJournalService;
3233
import org.junit.Before;
3334

3435
import de.symeda.sormas.api.ConfigFacade;
@@ -490,4 +491,8 @@ public QuarantineOrderFacade getQuarantineOrderFacade() {
490491
public BAGExportFacade getBAGExportFacade() {
491492
return getBean(BAGExportFacadeEjb.BAGExportFacadeEjbLocal.class);
492493
}
494+
495+
public ExternalJournalService getExternalJournalService() {
496+
return getBean(ExternalJournalService.class);
497+
}
493498
}
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
package de.symeda.sormas.backend.externaljournal;
2+
3+
import de.symeda.sormas.api.externaljournal.PatientDiaryPersonQueryResponse;
4+
import de.symeda.sormas.api.person.PersonDto;
5+
6+
import de.symeda.sormas.api.person.Sex;
7+
import de.symeda.sormas.api.person.SymptomJournalStatus;
8+
import de.symeda.sormas.api.utils.DataHelper;
9+
import de.symeda.sormas.backend.AbstractBeanTest;
10+
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.mockito.MockitoAnnotations;
14+
import org.mockito.Spy;
15+
16+
import java.lang.reflect.InvocationTargetException;
17+
import java.lang.reflect.Method;
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
import java.util.Optional;
21+
22+
import static org.junit.Assert.assertFalse;
23+
import static org.junit.Assert.assertTrue;
24+
import static org.mockito.Mockito.doReturn;
25+
26+
public class ExternalJournalServiceTest extends AbstractBeanTest {
27+
28+
@Spy
29+
private ExternalJournalService externalJournalService;
30+
31+
@Before
32+
public void setUp() {
33+
MockitoAnnotations.initMocks(this);
34+
PatientDiaryPersonQueryResponse patientDiaryPersonQueryResponse = new PatientDiaryPersonQueryResponse();
35+
patientDiaryPersonQueryResponse.setCount(0);
36+
doReturn(Optional.ofNullable(patientDiaryPersonQueryResponse)).when(externalJournalService).queryPatientDiary("Email", "[email protected]");
37+
doReturn(Optional.ofNullable(patientDiaryPersonQueryResponse)).when(externalJournalService).queryPatientDiary("Email", "test@test");
38+
doReturn(Optional.ofNullable(patientDiaryPersonQueryResponse)).when(externalJournalService).queryPatientDiary("Email", "[email protected]");
39+
doReturn(Optional.ofNullable(patientDiaryPersonQueryResponse)).when(externalJournalService)
40+
.queryPatientDiary("Mobile phone", "+49 621 1218490");
41+
doReturn(Optional.ofNullable(patientDiaryPersonQueryResponse)).when(externalJournalService)
42+
.queryPatientDiary("Mobile phone", "+49 621 1218491");
43+
doReturn(Optional.ofNullable(patientDiaryPersonQueryResponse)).when(externalJournalService).queryPatientDiary("Mobile phone", "0");;
44+
}
45+
46+
@Test
47+
/*
48+
* If you need to change this test to make it pass, you probably changed the behaviour of the ExternalVisitsResource.
49+
* Please note that other system used alongside with SORMAS are depending on this, so that their developers must be notified of any
50+
* relevant API changes some time before they go into any test and productive system. Please inform the SORMAS core development team at
51+
* https://gitter.im/SORMAS-Project!
52+
*/
53+
public void givenValidEmailIsExportable() {
54+
PatientDiaryPersonQueryResponse response = new PatientDiaryPersonQueryResponse();
55+
response.setCount(0);
56+
PersonDto person = new PersonDto();
57+
person.setEmailAddress("[email protected]");
58+
assertTrue(externalJournalService.validatePatientDiaryPerson(person).isValid());
59+
}
60+
61+
@Test
62+
/*
63+
* If you need to change this test to make it pass, you probably changed the behaviour of the ExternalVisitsResource.
64+
* Please note that other system used alongside with SORMAS are depending on this, so that their developers must be notified of any
65+
* relevant API changes some time before they go into any test and productive system. Please inform the SORMAS core development team at
66+
* https://gitter.im/SORMAS-Project!
67+
*/
68+
public void givenInvalidEmailIsNotExportable() {
69+
PersonDto person = new PersonDto();
70+
person.setEmailAddress("test@test");
71+
assertFalse(externalJournalService.validatePatientDiaryPerson(person).isValid());
72+
person.setPhone("+496211218490");
73+
assertFalse(externalJournalService.validatePatientDiaryPerson(person).isValid());
74+
}
75+
76+
@Test
77+
/*
78+
* If you need to change this test to make it pass, you probably changed the behaviour of the ExternalVisitsResource.
79+
* Please note that other system used alongside with SORMAS are depending on this, so that their developers must be notified of any
80+
* relevant API changes some time before they go into any test and productive system. Please inform the SORMAS core development team at
81+
* https://gitter.im/SORMAS-Project!
82+
*/
83+
public void givenValidPhoneIsExportable() {
84+
85+
PersonDto person = new PersonDto();
86+
person.setPhone("+496211218490");
87+
assertTrue(externalJournalService.validatePatientDiaryPerson(person).isValid());
88+
}
89+
90+
@Test
91+
/*
92+
* If you need to change this test to make it pass, you probably changed the behaviour of the ExternalVisitsResource.
93+
* Please note that other system used alongside with SORMAS are depending on this, so that their developers must be notified of any
94+
* relevant API changes some time before they go into any test and productive system. Please inform the SORMAS core development team at
95+
* https://gitter.im/SORMAS-Project!
96+
*/
97+
public void givenInvalidPhoneIsNotExportable() {
98+
99+
PersonDto person = new PersonDto();
100+
person.setPhone("0");
101+
assertFalse(getExternalJournalService().validatePatientDiaryPerson(person).isValid());
102+
person.setEmailAddress("[email protected]");
103+
assertFalse(externalJournalService.validatePatientDiaryPerson(person).isValid());
104+
}
105+
106+
@Test
107+
/*
108+
* If you need to change this test to make it pass, you probably changed the behaviour of the ExternalVisitsResource.
109+
* Please note that other system used alongside with SORMAS are depending on this, so that their developers must be notified of any
110+
* relevant API changes some time before they go into any test and productive system. Please inform the SORMAS core development team at
111+
* https://gitter.im/SORMAS-Project!
112+
*/
113+
public void givenNeitherEmailNorPhoneIsNotExportable() {
114+
115+
PersonDto person = new PersonDto();
116+
person.setBirthdateYYYY(2000);
117+
person.setBirthdateMM(6);
118+
person.setBirthdateDD(1);
119+
assertFalse(externalJournalService.validatePatientDiaryPerson(person).isValid());
120+
}
121+
122+
@Test
123+
/*
124+
* If you need to change this test to make it pass, you probably changed the behaviour of the ExternalVisitsResource.
125+
* Please note that other system used alongside with SORMAS are depending on this, so that their developers must be notified of any
126+
* relevant API changes some time before they go into any test and productive system. Please inform the SORMAS core development team at
127+
* https://gitter.im/SORMAS-Project!
128+
*/
129+
public void givenIncompleteBirthdateIsNotExportable() {
130+
PersonDto person = new PersonDto();
131+
person.setEmailAddress("[email protected]");
132+
person.setPhone("+496211218490");
133+
person.setBirthdateYYYY(2000);
134+
assertFalse(externalJournalService.validatePatientDiaryPerson(person).isValid());
135+
person.setBirthdateMM(6);
136+
assertFalse(externalJournalService.validatePatientDiaryPerson(person).isValid());
137+
person.setBirthdateYYYY(null);
138+
person.setBirthdateDD(1);
139+
assertFalse(externalJournalService.validatePatientDiaryPerson(person).isValid());
140+
}
141+
142+
@Test
143+
/*
144+
* If you need to change this test to make it pass, you probably changed the behaviour of the ExternalVisitsResource.
145+
* Please note that other system used alongside with SORMAS are depending on this, so that their developers must be notified of any
146+
* relevant API changes some time before they go into any test and productive system. Please inform the SORMAS core development team at
147+
* https://gitter.im/SORMAS-Project!
148+
*/
149+
public void givenRelevantChangeShouldNotify() {
150+
151+
// Define relevant properties
152+
HashMap<String, Object> relevantProperties = new HashMap<String, Object>() {
153+
154+
{
155+
put("FirstName", "Klaus");
156+
put("LastName", "Draufle");
157+
put("Sex", Sex.MALE);
158+
put("EmailAddress", "[email protected]");
159+
put("Phone", "+496211218490");
160+
put("BirthdateYYYY", 2000);
161+
put("BirthdateMM", 6);
162+
put("BirthdateDD", 1);
163+
put("SymptomJournalStatus", SymptomJournalStatus.REGISTERED);
164+
}
165+
};
166+
167+
// Create two person with those properties
168+
PersonDto person = new PersonDto();
169+
PersonDto updatedPerson = new PersonDto();
170+
person.setUuid(DataHelper.createUuid());
171+
for (Map.Entry<String, Object> property : relevantProperties.entrySet()) {
172+
setPersonProperty(person, property.getKey(), property.getValue());
173+
setPersonProperty(updatedPerson, property.getKey(), property.getValue());
174+
}
175+
assertFalse(getExternalJournalService().notifyExternalJournalPersonUpdate(person, updatedPerson));
176+
177+
// Define relevant changes
178+
HashMap<String, Object> relevantChanges = new HashMap<String, Object>() {
179+
180+
{
181+
put("FirstName", "Heinz");
182+
put("LastName", "Müller");
183+
put("Sex", Sex.FEMALE);
184+
put("EmailAddress", "[email protected]");
185+
put("Phone", "+496211218491");
186+
put("BirthdateYYYY", 2001);
187+
put("BirthdateMM", 7);
188+
put("BirthdateDD", 2);
189+
}
190+
};
191+
// Apply each change and make sure it makes notification considered necessary
192+
for (String propertyName : relevantChanges.keySet()) {
193+
setPersonProperty(updatedPerson, propertyName, relevantChanges.get(propertyName));
194+
assertTrue(getExternalJournalService().notifyExternalJournalPersonUpdate(person, updatedPerson));
195+
196+
// Modify the SymptomJournalStatus of the original person
197+
person.setSymptomJournalStatus(SymptomJournalStatus.DELETED);
198+
assertFalse(getExternalJournalService().notifyExternalJournalPersonUpdate(person, updatedPerson));
199+
person.setSymptomJournalStatus(SymptomJournalStatus.REJECTED);
200+
assertFalse(getExternalJournalService().notifyExternalJournalPersonUpdate(person, updatedPerson));
201+
person.setSymptomJournalStatus(SymptomJournalStatus.UNREGISTERED);
202+
assertFalse(getExternalJournalService().notifyExternalJournalPersonUpdate(person, updatedPerson));
203+
person.setSymptomJournalStatus(SymptomJournalStatus.ACCEPTED);
204+
assertTrue(getExternalJournalService().notifyExternalJournalPersonUpdate(person, updatedPerson));
205+
person.setSymptomJournalStatus(SymptomJournalStatus.REGISTERED);
206+
207+
// Apply any other relevant change and make sure notification is still considered necessary
208+
for (String secondPropertyName : relevantChanges.keySet()) {
209+
setPersonProperty(updatedPerson, secondPropertyName, relevantChanges.get(secondPropertyName));
210+
assertTrue(getExternalJournalService().notifyExternalJournalPersonUpdate(person, updatedPerson));
211+
setPersonProperty(updatedPerson, secondPropertyName, relevantProperties.get(secondPropertyName));
212+
}
213+
// reset updatedPerson
214+
setPersonProperty(updatedPerson, propertyName, relevantProperties.get(propertyName));
215+
}
216+
}
217+
218+
/*
219+
* If you need to change this method to make it pass, you probably changed the behaviour of the ExternalVisitsResource.
220+
* Please note that other system used alongside with SORMAS are depending on this, so that their developers must be notified of any
221+
* relevant API changes some time before they go into any test and productive system. Please inform the SORMAS core development team at
222+
* https://gitter.im/SORMAS-Project!
223+
*/
224+
private static void setPersonProperty(PersonDto person, String propertyName, Object propertyValue) {
225+
try {
226+
Method method = person.getClass().getMethod("set" + propertyName, propertyValue.getClass());
227+
method.invoke(person, propertyValue);
228+
229+
} catch (NoSuchMethodException e) {
230+
// This probably means that the set method is gone, which may impose changes to the External Journal Interface
231+
assertTrue(false);
232+
} catch (IllegalAccessException e) {
233+
e.printStackTrace();
234+
} catch (InvocationTargetException e) {
235+
e.printStackTrace();
236+
}
237+
}
238+
}

0 commit comments

Comments
 (0)