|
1 | 1 | package uk.gov.hmcts.reform.migration; |
2 | 2 |
|
| 3 | +import static org.codehaus.groovy.runtime.InvokerHelper.asList; |
3 | 4 | import static org.hamcrest.Matchers.contains; |
4 | | -import static org.hamcrest.Matchers.containsInAnyOrder; |
5 | 5 | import static org.hamcrest.Matchers.hasSize; |
6 | | -import static org.junit.Assert.assertEquals; |
| 6 | +import static org.hamcrest.Matchers.is; |
7 | 7 | import static org.junit.Assert.assertThat; |
| 8 | +import static org.mockito.ArgumentMatchers.any; |
| 9 | +import static org.mockito.ArgumentMatchers.anyString; |
8 | 10 | import static org.mockito.Mockito.times; |
9 | 11 | import static org.mockito.Mockito.verify; |
10 | 12 | import static org.mockito.Mockito.when; |
11 | 13 |
|
12 | | -import java.time.LocalDate; |
| 14 | +import java.util.ArrayList; |
13 | 15 | import java.util.HashMap; |
14 | 16 | import java.util.List; |
15 | 17 | import java.util.Map; |
16 | | -import org.junit.Ignore; |
| 18 | + |
| 19 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
17 | 20 | import org.junit.Test; |
18 | 21 | import org.junit.runner.RunWith; |
19 | 22 | import org.mockito.InjectMocks; |
20 | 23 | import org.mockito.Mock; |
21 | 24 | import org.mockito.junit.MockitoJUnitRunner; |
22 | 25 | import uk.gov.hmcts.reform.ccd.client.model.CaseDetails; |
| 26 | +import uk.gov.hmcts.reform.ccd.client.model.SearchResult; |
23 | 27 | import uk.gov.hmcts.reform.migration.ccd.CoreCaseDataService; |
24 | 28 | import uk.gov.hmcts.reform.migration.service.DataMigrationService; |
25 | 29 |
|
@@ -120,51 +124,104 @@ public void shouldProcessNoCaseWhenNoCasesAvailable() { |
120 | 124 | } |
121 | 125 |
|
122 | 126 | @Test |
123 | | - public void shouldContainSingleDate() { |
124 | | - String firstDate = "2021-01-01"; |
125 | | - String lastDate = "2021-01-02"; |
| 127 | + public void shouldDoNothingIfNoCasesToProcess() throws InterruptedException { |
| 128 | + SearchResult result = SearchResult.builder() |
| 129 | + .cases(new ArrayList<>()) |
| 130 | + .total(0).build(); |
| 131 | + |
| 132 | + MigrationPageParams pageParams = new MigrationPageParams(10, 10); |
| 133 | + |
| 134 | + when(coreCaseDataService.searchCases(anyString(), |
| 135 | + any(SearchSourceBuilder.class))).thenReturn(result); |
| 136 | + |
| 137 | + caseMigrationProcessor.fetchAndProcessCases(USER_TOKEN, false, 1, pageParams); |
| 138 | + |
| 139 | + assertThat(caseMigrationProcessor.getFailedCases().size(), is(0)); |
| 140 | + assertThat(caseMigrationProcessor.getMigratedCases().size(), is(0)); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void shouldUseOverrideIfProvided() throws InterruptedException { |
| 145 | + SearchResult result = SearchResult.builder() |
| 146 | + .cases(List.of(caseDetails1, caseDetails2, caseDetails3)) |
| 147 | + .total(3).build(); |
| 148 | + |
| 149 | + MigrationPageParams pageParams = new MigrationPageParams(10, 2); |
| 150 | + |
| 151 | + when(coreCaseDataService.searchCases(anyString(), |
| 152 | + any(SearchSourceBuilder.class))).thenReturn(result); |
126 | 153 |
|
127 | | - List<LocalDate> listOfDates = caseMigrationProcessor.getListOfDates(LocalDate.parse(firstDate), |
128 | | - LocalDate.parse(lastDate)); |
| 154 | + when(coreCaseDataService.fetchNCases(USER_TOKEN, 1, |
| 155 | + caseDetails1.getId())).thenReturn(List.of(caseDetails2)); |
129 | 156 |
|
130 | | - assertEquals(1, listOfDates.size()); |
| 157 | + caseMigrationProcessor.fetchAndProcessCases(USER_TOKEN, false, 1, pageParams); |
| 158 | + |
| 159 | + assertThat(caseMigrationProcessor.getFailedCases().size(), is(0)); |
| 160 | + assertThat(caseMigrationProcessor.getMigratedCases().size(), is(2)); |
131 | 161 | } |
132 | 162 |
|
133 | 163 | @Test |
134 | | - public void shouldContainTwoDates() { |
135 | | - String firstDate = "2021-01-01"; |
136 | | - String lastDate = "2021-01-03"; |
| 164 | + public void shouldProcessAllCasesIfNoOverride() throws InterruptedException { |
| 165 | + SearchResult result = SearchResult.builder() |
| 166 | + .cases(List.of(caseDetails1, caseDetails2, caseDetails3)) |
| 167 | + .total(3).build(); |
| 168 | + |
| 169 | + MigrationPageParams pageParams = new MigrationPageParams(10, 0); |
| 170 | + |
| 171 | + when(coreCaseDataService.searchCases(anyString(), |
| 172 | + any(SearchSourceBuilder.class))).thenReturn(result); |
137 | 173 |
|
138 | | - List<LocalDate> listOfDates = caseMigrationProcessor.getListOfDates(LocalDate.parse(firstDate), |
139 | | - LocalDate.parse(lastDate)); |
| 174 | + when(coreCaseDataService.fetchNCases(USER_TOKEN, 2, |
| 175 | + caseDetails1.getId())).thenReturn(List.of(caseDetails2, caseDetails3)); |
140 | 176 |
|
141 | | - assertEquals(2, listOfDates.size()); |
| 177 | + caseMigrationProcessor.fetchAndProcessCases(USER_TOKEN, false, 1, pageParams); |
| 178 | + |
| 179 | + assertThat(caseMigrationProcessor.getFailedCases().size(), is(0)); |
| 180 | + assertThat(caseMigrationProcessor.getMigratedCases().size(), is(3)); |
142 | 181 | } |
143 | 182 |
|
144 | 183 | @Test |
145 | | - public void shouldContainNormalYearOfDates() { |
146 | | - String firstDate = "2021-01-01"; |
147 | | - String lastDate = "2022-01-01"; |
| 184 | + public void shouldBreakWhenNoMoreCasesReturned() throws InterruptedException { |
| 185 | + SearchResult result = SearchResult.builder() |
| 186 | + .cases(List.of(caseDetails1, caseDetails2, caseDetails3)) |
| 187 | + .total(3).build(); |
| 188 | + |
| 189 | + MigrationPageParams pageParams = new MigrationPageParams(10, 2); |
148 | 190 |
|
149 | | - List<LocalDate> listOfDates = caseMigrationProcessor.getListOfDates(LocalDate.parse(firstDate), |
150 | | - LocalDate.parse(lastDate)); |
| 191 | + when(coreCaseDataService.searchCases(any(String.class), |
| 192 | + any(SearchSourceBuilder.class))).thenReturn(result); |
151 | 193 |
|
152 | | - assertEquals(365, listOfDates.size()); |
| 194 | + caseMigrationProcessor.fetchAndProcessCases(USER_TOKEN, false, 1, pageParams); |
| 195 | + |
| 196 | + assertThat(caseMigrationProcessor.getFailedCases().size(), is(0)); |
| 197 | + assertThat(caseMigrationProcessor.getMigratedCases().size(), is(1)); |
153 | 198 | } |
154 | 199 |
|
155 | 200 | @Test |
156 | | - public void shouldContainLeapYearOfDates() { |
157 | | - String firstDate = "2020-01-01"; |
158 | | - String lastDate = "2021-01-01"; |
| 201 | + public void shouldSearchFromLastCaseInPreviousResult() throws InterruptedException { |
| 202 | + SearchResult result = SearchResult.builder() |
| 203 | + .cases(List.of(caseDetails1, caseDetails2, caseDetails3)) |
| 204 | + .total(3).build(); |
| 205 | + |
| 206 | + MigrationPageParams pageParams = new MigrationPageParams(1, 0); |
| 207 | + |
| 208 | + when(coreCaseDataService.searchCases(any(String.class), |
| 209 | + any(SearchSourceBuilder.class))).thenReturn(result); |
| 210 | + |
| 211 | + when(coreCaseDataService.fetchNCases(USER_TOKEN, 1, |
| 212 | + caseDetails1.getId())).thenReturn(List.of(caseDetails2)); |
| 213 | + |
| 214 | + when(coreCaseDataService.fetchNCases(USER_TOKEN, 1, |
| 215 | + caseDetails2.getId())).thenReturn(List.of(caseDetails3)); |
159 | 216 |
|
160 | | - List<LocalDate> listOfDates = caseMigrationProcessor.getListOfDates(LocalDate.parse(firstDate), |
161 | | - LocalDate.parse(lastDate)); |
| 217 | + caseMigrationProcessor.fetchAndProcessCases(USER_TOKEN, false, 1, pageParams); |
162 | 218 |
|
163 | | - assertEquals(366, listOfDates.size()); |
| 219 | + assertThat(caseMigrationProcessor.getFailedCases().size(), is(0)); |
| 220 | + assertThat(caseMigrationProcessor.getMigratedCases().size(), is(3)); |
164 | 221 | } |
165 | 222 |
|
166 | 223 | private void mockDataFetch(CaseDetails... caseDetails) { |
167 | | -// when(coreCaseDataService.fetchAllForDay(eq(USER_TOKEN), anyString(), eq(false))).thenReturn(Optional.of(Stream.of(caseDetails))); |
| 224 | + when(coreCaseDataService.fetchAll(USER_TOKEN, USER_ID)).thenReturn(asList(caseDetails)); |
168 | 225 | } |
169 | 226 |
|
170 | 227 | private void mockDataUpdate(CaseDetails caseDetails) { |
|
0 commit comments