|
| 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