|
| 1 | +package de.symeda.sormas.backend.patch.partial_retrieval; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | + |
| 7 | +import java.util.List; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.mockito.InjectMocks; |
| 12 | + |
| 13 | +import de.symeda.sormas.api.patch.partial_retrieval.FieldInfo; |
| 14 | +import de.symeda.sormas.api.person.PersonContactDetailDto; |
| 15 | +import de.symeda.sormas.api.person.PersonContactDetailType; |
| 16 | +import de.symeda.sormas.api.person.PersonDto; |
| 17 | +import de.symeda.sormas.backend.AbstractUnitTest; |
| 18 | + |
| 19 | +class ContactDetailsFieldValueRetrieverTest extends AbstractUnitTest { |
| 20 | + |
| 21 | + @InjectMocks |
| 22 | + private ContactDetailsFieldValueRetriever victim; |
| 23 | + |
| 24 | + private static final String PHONE_FIELD = PersonContactDetailDto.I18N_PREFIX + "." + PersonContactDetailDto.PHONE_NUMBER_TYPE; |
| 25 | + private static final String EMAIL_FIELD = PersonContactDetailDto.I18N_PREFIX + "." + PersonContactDetailDto.CONTACT_INFORMATION; |
| 26 | + |
| 27 | + @Test |
| 28 | + void getSupportedFields_returnsPhoneAndEmailFields() { |
| 29 | + assertEquals(Set.of(PHONE_FIELD, EMAIL_FIELD), victim.getSupportedFields()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void supports_phoneField_returnsTrue() { |
| 34 | + assertTrue(victim.supports(PHONE_FIELD)); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void supports_emailField_returnsTrue() { |
| 39 | + assertTrue(victim.supports(EMAIL_FIELD)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + void supports_unknownField_returnsFalse() { |
| 44 | + assertFalse(victim.supports("Person.firstName")); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void getFieldInfo_phone_noContacts_returnsEmptyValue() { |
| 49 | + FieldInfo result = victim.getFieldInfo(PHONE_FIELD, new PersonDto()); |
| 50 | + |
| 51 | + assertEquals("", result.getFieldValue()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void getFieldInfo_phone_fieldTypeIsList() { |
| 56 | + FieldInfo result = victim.getFieldInfo(PHONE_FIELD, new PersonDto()); |
| 57 | + |
| 58 | + assertEquals(List.class, result.getFieldType()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void getFieldInfo_phone_translatedFieldName() { |
| 63 | + FieldInfo result = victim.getFieldInfo(PHONE_FIELD, new PersonDto()); |
| 64 | + |
| 65 | + assertEquals("Phone number type", result.getTranslatedFieldName()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void getFieldInfo_phone_singlePhone_returnsNumber() { |
| 70 | + PersonDto person = personWithContacts(phoneContact("0987654321")); |
| 71 | + |
| 72 | + FieldInfo result = victim.getFieldInfo(PHONE_FIELD, person); |
| 73 | + |
| 74 | + assertEquals("0987654321", result.getFieldValue()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void getFieldInfo_phone_multiplePhones_returnsSortedAndJoined() { |
| 79 | + PersonDto person = personWithContacts(phoneContact("9999999"), phoneContact("1111111"), phoneContact("5555555")); |
| 80 | + |
| 81 | + FieldInfo result = victim.getFieldInfo(PHONE_FIELD, person); |
| 82 | + |
| 83 | + assertEquals("1111111; 5555555; 9999999", result.getFieldValue()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void getFieldInfo_phone_blankContactInfoFiltered() { |
| 88 | + PersonDto person = personWithContacts(phoneContact("0987654321"), phoneContact(" "), phoneContact(null)); |
| 89 | + |
| 90 | + FieldInfo result = victim.getFieldInfo(PHONE_FIELD, person); |
| 91 | + |
| 92 | + assertEquals("0987654321", result.getFieldValue()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void getFieldInfo_phone_emailContactsExcluded() { |
| 97 | + PersonDto person = personWithContacts(phoneContact("0987654321"), emailContact("test@example.com")); |
| 98 | + |
| 99 | + FieldInfo result = victim.getFieldInfo(PHONE_FIELD, person); |
| 100 | + |
| 101 | + assertEquals("0987654321", result.getFieldValue()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void getFieldInfo_email_singleEmail_returnsEmail() { |
| 106 | + PersonDto person = personWithContacts(emailContact("user@example.com")); |
| 107 | + |
| 108 | + FieldInfo result = victim.getFieldInfo(EMAIL_FIELD, person); |
| 109 | + |
| 110 | + assertEquals("user@example.com", result.getFieldValue()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void getFieldInfo_email_multipleEmails_returnsSortedAndJoined() { |
| 115 | + PersonDto person = personWithContacts(emailContact("z@example.com"), emailContact("a@example.com")); |
| 116 | + |
| 117 | + FieldInfo result = victim.getFieldInfo(EMAIL_FIELD, person); |
| 118 | + |
| 119 | + assertEquals("a@example.com; z@example.com", result.getFieldValue()); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + void getFieldInfo_email_phoneContactsExcluded() { |
| 124 | + PersonDto person = personWithContacts(emailContact("user@example.com"), phoneContact("0987654321")); |
| 125 | + |
| 126 | + FieldInfo result = victim.getFieldInfo(EMAIL_FIELD, person); |
| 127 | + |
| 128 | + assertEquals("user@example.com", result.getFieldValue()); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + void getFieldInfo_email_translatedFieldName() { |
| 133 | + FieldInfo result = victim.getFieldInfo(EMAIL_FIELD, new PersonDto()); |
| 134 | + |
| 135 | + assertEquals("Contact information", result.getTranslatedFieldName()); |
| 136 | + } |
| 137 | + |
| 138 | + private PersonDto personWithContacts(PersonContactDetailDto... details) { |
| 139 | + PersonDto person = new PersonDto(); |
| 140 | + for (PersonContactDetailDto detail : details) { |
| 141 | + person.getPersonContactDetails().add(detail); |
| 142 | + } |
| 143 | + return person; |
| 144 | + } |
| 145 | + |
| 146 | + private PersonContactDetailDto phoneContact(String number) { |
| 147 | + PersonContactDetailDto dto = new PersonContactDetailDto(); |
| 148 | + dto.setPersonContactDetailType(PersonContactDetailType.PHONE); |
| 149 | + dto.setContactInformation(number); |
| 150 | + return dto; |
| 151 | + } |
| 152 | + |
| 153 | + private PersonContactDetailDto emailContact(String email) { |
| 154 | + PersonContactDetailDto dto = new PersonContactDetailDto(); |
| 155 | + dto.setPersonContactDetailType(PersonContactDetailType.EMAIL); |
| 156 | + dto.setContactInformation(email); |
| 157 | + return dto; |
| 158 | + } |
| 159 | +} |
0 commit comments