|
| 1 | +package uk.gov.hmcts.darts.common.repository; |
| 2 | + |
| 3 | +import org.hamcrest.Matchers; |
| 4 | +import org.junit.jupiter.api.BeforeEach; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.jupiter.api.TestInstance; |
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.Arguments; |
| 9 | +import org.junit.jupiter.params.provider.MethodSource; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.data.domain.Sort; |
| 12 | +import uk.gov.hmcts.darts.common.entity.UserAccountEntity; |
| 13 | +import uk.gov.hmcts.darts.test.common.data.PersistableFactory; |
| 14 | +import uk.gov.hmcts.darts.testutils.PostgresIntegrationBase; |
| 15 | +import uk.gov.hmcts.darts.testutils.stubs.DartsPersistence; |
| 16 | + |
| 17 | +import java.util.List; |
| 18 | +import java.util.stream.Stream; |
| 19 | + |
| 20 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
| 22 | +import static org.hamcrest.Matchers.equalTo; |
| 23 | + |
| 24 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 25 | +class UserAccountRepositoryTest extends PostgresIntegrationBase { |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private UserAccountRepository userAccountRepository; |
| 29 | + |
| 30 | + @Autowired |
| 31 | + private DartsPersistence dartsPersistence; |
| 32 | + |
| 33 | + private UserAccountEntity userAccountEntity1; |
| 34 | + |
| 35 | + private UserAccountEntity userAccountEntity2; |
| 36 | + |
| 37 | + private UserAccountEntity userAccountEntity3; |
| 38 | + |
| 39 | + @BeforeEach |
| 40 | + void setUpData() { |
| 41 | + UserAccountEntity user1 = PersistableFactory.getUserAccountTestData().someMinimalBuilder() |
| 42 | + . emailAddress( "[email protected]") |
| 43 | + .build() |
| 44 | + .getEntity(); |
| 45 | + dartsPersistence.save(user1); |
| 46 | + |
| 47 | + userAccountEntity1 = user1; |
| 48 | + |
| 49 | + UserAccountEntity user2 = PersistableFactory.getUserAccountTestData().someMinimalBuilder() |
| 50 | + . emailAddress( "[email protected]") |
| 51 | + .build() |
| 52 | + .getEntity(); |
| 53 | + dartsPersistence.save(user2); |
| 54 | + |
| 55 | + userAccountEntity2 = user2; |
| 56 | + |
| 57 | + UserAccountEntity user3 = PersistableFactory.getUserAccountTestData().someMinimalBuilder() |
| 58 | + . emailAddress( "[email protected]") |
| 59 | + .isSystemUser(true) |
| 60 | + .build() |
| 61 | + .getEntity(); |
| 62 | + dartsPersistence.save(user3); |
| 63 | + |
| 64 | + userAccountEntity3 = user3; |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void findUsers_shouldReturnUsers_WhenOnlyEmailProvided() { |
| 70 | + List<UserAccountEntity> users = userAccountRepository.findUsers( |
| 71 | + false, userAccountEntity1.getEmailAddress(), null, null |
| 72 | + ); |
| 73 | + |
| 74 | + assertThat(users, Matchers.hasSize(1)); |
| 75 | + assertThat(users.get(0).getId(), equalTo(userAccountEntity1.getId())); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void findUsers_shouldReturnUser_WhenOnlyIdProvided() { |
| 80 | + List<UserAccountEntity> users = userAccountRepository.findUsers( |
| 81 | + false, null, List.of(userAccountEntity1.getId()), null |
| 82 | + ); |
| 83 | + |
| 84 | + assertThat(users, Matchers.hasSize(1)); |
| 85 | + assertThat(users.get(0).getId(), equalTo(userAccountEntity1.getId())); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void findUsers_shouldReturnMultipleUsers_WhenOnlyIDsProvided() { |
| 90 | + List<UserAccountEntity> users = userAccountRepository.findUsers( |
| 91 | + false, null, List.of(userAccountEntity1.getId(), userAccountEntity2.getId()), null |
| 92 | + ); |
| 93 | + assertThat(users, Matchers.hasSize(2)); |
| 94 | + UserAccountEntity firstFoundUser = users.get(0); |
| 95 | + UserAccountEntity secondFoundUser = users.get(1); |
| 96 | + assertThat(firstFoundUser.getId(), equalTo(userAccountEntity1.getId())); |
| 97 | + assertThat(secondFoundUser.getId(), equalTo(userAccountEntity2.getId())); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void findUsers_shouldSortUsersByIdDesc_WhenSortSetToIdDesc() { |
| 102 | + List<UserAccountEntity> users = userAccountRepository.findUsers( |
| 103 | + false, null, List.of(userAccountEntity1.getId(), userAccountEntity2.getId()), Sort.by(Sort.Direction.DESC, "id") |
| 104 | + ); |
| 105 | + assertThat(users, Matchers.hasSize(2)); |
| 106 | + UserAccountEntity firstFoundUser = users.get(0); |
| 107 | + UserAccountEntity secondFoundUser = users.get(1); |
| 108 | + assertThat(firstFoundUser.getId(), equalTo(userAccountEntity2.getId())); |
| 109 | + assertThat(secondFoundUser.getId(), equalTo(userAccountEntity1.getId())); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void findUsers_shouldNotReturnUsers_WhenOnlyNonMatchingEmailProvided() { |
| 114 | + List<UserAccountEntity> users = userAccountRepository.findUsers( |
| 115 | + false, "[email protected]", null, null |
| 116 | + ); |
| 117 | + |
| 118 | + assertThat(users, Matchers.hasSize(0)); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + void findUsers_shouldNotReturnUsers_WhenOnlyNonMatchingIdProvided() { |
| 123 | + List<UserAccountEntity> users = userAccountRepository.findUsers( |
| 124 | + false, null, List.of(123), null |
| 125 | + ); |
| 126 | + |
| 127 | + assertThat(users, Matchers.hasSize(0)); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void findUsers_shouldReturnUsers_WhenOptionalFieldsBlank() { |
| 132 | + List<UserAccountEntity> users = userAccountRepository.findUsers( |
| 133 | + false, null, null, null |
| 134 | + ); |
| 135 | + |
| 136 | + List<Integer> actualIds = users.stream() |
| 137 | + .map(UserAccountEntity::getId) |
| 138 | + .toList(); |
| 139 | + |
| 140 | + assertThat(actualIds, containsInAnyOrder( |
| 141 | + userAccountEntity1.getId(), |
| 142 | + userAccountEntity2.getId(), |
| 143 | + -99 // migration user added by flyway |
| 144 | + )); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void findUsers_shouldReturnUser_WhenAllOptionalFieldsProvided() { |
| 149 | + List<UserAccountEntity> users = userAccountRepository.findUsers( |
| 150 | + false, userAccountEntity1.getEmailAddress(), List.of(userAccountEntity1.getId()), Sort.by(Sort.Direction.DESC, "id") |
| 151 | + ); |
| 152 | + |
| 153 | + assertThat(users, Matchers.hasSize(1)); |
| 154 | + assertThat(users.get(0).getId(), equalTo(userAccountEntity1.getId())); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void findUsers_shouldNotReturnUser_WhenAllOptionalFieldsIncorrect() { |
| 159 | + List<UserAccountEntity> users = userAccountRepository.findUsers( |
| 160 | + false, "[email protected]", List. of( 123), Sort. by( Sort. Direction. DESC, "id") |
| 161 | + ); |
| 162 | + |
| 163 | + assertThat(users, Matchers.hasSize(0)); |
| 164 | + } |
| 165 | + |
| 166 | + @Test |
| 167 | + void findUsers_shouldReturnSystemUser_WhenOnlyIdProvided() { |
| 168 | + List<UserAccountEntity> users = userAccountRepository.findUsers( |
| 169 | + true, null, List.of(userAccountEntity3.getId()), null |
| 170 | + ); |
| 171 | + |
| 172 | + assertThat(users, Matchers.hasSize(1)); |
| 173 | + assertThat(users.get(0).getId(), equalTo(userAccountEntity3.getId())); |
| 174 | + } |
| 175 | + |
| 176 | + @Test |
| 177 | + void findUsers_shouldReturnSystemUser_WhenOnlyEmailProvided() { |
| 178 | + List<UserAccountEntity> users = userAccountRepository.findUsers( |
| 179 | + true, userAccountEntity3.getEmailAddress(), null, null |
| 180 | + ); |
| 181 | + |
| 182 | + assertThat(users, Matchers.hasSize(1)); |
| 183 | + assertThat(users.get(0).getId(), equalTo(userAccountEntity3.getId())); |
| 184 | + } |
| 185 | + |
| 186 | + @ParameterizedTest |
| 187 | + @MethodSource("provideTestCombinations") |
| 188 | + void findUsers_shouldFailWithOneNonMatchingField_WhenOtherFieldsMatch(boolean includeSystemUsers, String emailAddress, List<Integer> userIds) { |
| 189 | + List<UserAccountEntity> users = userAccountRepository.findUsers( |
| 190 | + includeSystemUsers, emailAddress, userIds, null |
| 191 | + ); |
| 192 | + |
| 193 | + assertThat(users, Matchers.hasSize(0)); |
| 194 | + } |
| 195 | + |
| 196 | + private Stream<Arguments> provideTestCombinations() { |
| 197 | + return Stream.of( |
| 198 | + Arguments.of(false, userAccountEntity1.getEmailAddress(), List.of(123)), // Non-matching ID |
| 199 | + Arguments. of( false, "[email protected]", List. of( userAccountEntity1. getId())), // Non-matching Email |
| 200 | + Arguments.of(false, userAccountEntity3.getEmailAddress(), List.of(userAccountEntity3.getId())), // Include system user false |
| 201 | + Arguments.of(true, userAccountEntity3.getEmailAddress(), List.of(123)) // Include system user true, non-matching ID |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | +} |
0 commit comments