|
16 | 16 | import org.junit.runner.RunWith; |
17 | 17 | import org.mockito.Mock; |
18 | 18 | import org.mockito.MockitoAnnotations; |
| 19 | +import org.openimis.imisclaims.domain.entity.Claim; |
| 20 | +import org.openimis.imisclaims.usecase.FetchClaims; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.Date; |
| 24 | + |
| 25 | + |
19 | 26 | import org.openimis.imisclaims.usecase.PostNewClaims; |
20 | 27 | import org.robolectric.RobolectricTestRunner; |
21 | 28 | import org.robolectric.annotation.Config; |
22 | 29 |
|
23 | 30 | import org.openimis.imisclaims.tools.StorageManager; |
24 | 31 |
|
25 | | -import java.util.Arrays; |
26 | | - |
27 | 32 | @RunWith(RobolectricTestRunner.class) |
28 | 33 | public class SynchronizeServiceTest { |
29 | 34 |
|
@@ -198,4 +203,83 @@ public void handleGetClaimCount_BroadcastsCorrectCounts() throws JSONException { |
198 | 203 | assertEquals(3, capturedIntent.getIntExtra(SynchronizeService.EXTRA_CLAIM_COUNT_ACCEPTED, 0)); |
199 | 204 | assertEquals(2, capturedIntent.getIntExtra(SynchronizeService.EXTRA_CLAIM_COUNT_REJECTED, 0)); |
200 | 205 | } |
| 206 | + |
| 207 | + @Test |
| 208 | + public void testDownloadClaims_WhenNetworkAvailable_ReturnsClaimsList() throws Exception { |
| 209 | + // Given |
| 210 | + when(global.isNetworkAvailable()).thenReturn(true); |
| 211 | + |
| 212 | + // Create a mock claim |
| 213 | + Claim.Service mockService = new Claim.Service( |
| 214 | + "SERVICE1", "Service 1", 100.0, "$", |
| 215 | + "1", "1", null, null, null, null); |
| 216 | + Claim.Medication mockMedication = new Claim.Medication( |
| 217 | + "MED1", "Medication 1", 50.0, "$", |
| 218 | + "2", "2", null, null, null, null); |
| 219 | + |
| 220 | + Claim mockClaim = new Claim( |
| 221 | + "claim-uuid-123", "HF001", "Health Facility 1", |
| 222 | + "INS123", "John Doe", "CLAIM-001", |
| 223 | + new Date(), new Date(), new Date(), |
| 224 | + "O", Claim.Status.PROCESSED, "A01", |
| 225 | + null, null, null, null, |
| 226 | + 150.0, 150.0, null, null, "G123", |
| 227 | + Arrays.asList(mockService), |
| 228 | + Arrays.asList(mockMedication) |
| 229 | + ); |
| 230 | + |
| 231 | + // Mock the FetchClaims class |
| 232 | + FetchClaims fetchClaims = mock(FetchClaims.class); |
| 233 | + when(fetchClaims.execute( |
| 234 | + eq(global.getOfficerCode()), |
| 235 | + any(Claim.Status.class), |
| 236 | + any(Date.class), |
| 237 | + any(Date.class), |
| 238 | + any(Date.class), |
| 239 | + any(Date.class) |
| 240 | + )).thenReturn(Arrays.asList(mockClaim)); |
| 241 | + |
| 242 | + // When |
| 243 | + List<Claim> claims = fetchClaims.execute( |
| 244 | + global.getOfficerCode(), |
| 245 | + Claim.Status.PROCESSED, |
| 246 | + new Date(), |
| 247 | + new Date(), |
| 248 | + new Date(), |
| 249 | + new Date() |
| 250 | + ); |
| 251 | + |
| 252 | + // Then |
| 253 | + assertNotNull(claims); |
| 254 | + assertFalse(claims.isEmpty()); |
| 255 | + assertEquals(1, claims.size()); |
| 256 | + assertEquals("CLAIM-001", claims.get(0).getClaimNumber()); |
| 257 | + assertEquals("John Doe", claims.get(0).getPatientName()); |
| 258 | + assertEquals(Claim.Status.PROCESSED, claims.get(0).getStatus()); |
| 259 | + |
| 260 | + // Verify the service and medication were included |
| 261 | + assertFalse(claims.get(0).getServices().isEmpty()); |
| 262 | + assertFalse(claims.get(0).getMedications().isEmpty()); |
| 263 | + assertEquals("SERVICE1", claims.get(0).getServices().get(0).getCode()); |
| 264 | + assertEquals("MED1", claims.get(0).getMedications().get(0).getCode()); |
| 265 | + } |
| 266 | + |
| 267 | + @Test |
| 268 | + public void testDownloadClaims_WhenNetworkUnavailable_ThrowsException() { |
| 269 | + // Given |
| 270 | + when(global.isNetworkAvailable()).thenReturn(false); |
| 271 | + |
| 272 | + // When/Then |
| 273 | + assertThrows(Exception.class, () -> { |
| 274 | + // This should fail because there's no network |
| 275 | + new FetchClaims().execute( |
| 276 | + global.getOfficerCode(), |
| 277 | + Claim.Status.PROCESSED, |
| 278 | + new Date(), |
| 279 | + new Date(), |
| 280 | + new Date(), |
| 281 | + new Date() |
| 282 | + ); |
| 283 | + }); |
| 284 | + } |
201 | 285 | } |
0 commit comments