|
| 1 | +package org.openimis.imisclaims; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | +import static org.mockito.ArgumentMatchers.*; |
| 5 | +import static org.mockito.Mockito.*; |
| 6 | + |
| 7 | +import android.content.Context; |
| 8 | +import android.content.Intent; |
| 9 | +import android.content.res.Resources; |
| 10 | + |
| 11 | +import org.json.JSONArray; |
| 12 | +import org.json.JSONException; |
| 13 | +import org.json.JSONObject; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | +import org.junit.runner.RunWith; |
| 17 | +import org.mockito.Mock; |
| 18 | +import org.mockito.MockitoAnnotations; |
| 19 | +import org.openimis.imisclaims.usecase.PostNewClaims; |
| 20 | +import org.robolectric.RobolectricTestRunner; |
| 21 | +import org.robolectric.annotation.Config; |
| 22 | + |
| 23 | +import org.openimis.imisclaims.tools.StorageManager; |
| 24 | + |
| 25 | +import java.util.Arrays; |
| 26 | + |
| 27 | +@RunWith(RobolectricTestRunner.class) |
| 28 | +@Config(sdk = 28, manifest = Config.NONE) |
| 29 | +public class SynchronizeServiceTest { |
| 30 | + |
| 31 | + @Mock private Global global; |
| 32 | + @Mock private SQLHandler sqlHandler; |
| 33 | + @Mock private Resources resources; |
| 34 | + @Mock private Context context; |
| 35 | + @Mock private StorageManager storageManager; |
| 36 | + |
| 37 | + private SynchronizeService synchronizeService; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void setUp() { |
| 41 | + MockitoAnnotations.openMocks(this); |
| 42 | + |
| 43 | + synchronizeService = new SynchronizeService() { |
| 44 | + @Override |
| 45 | + public Context getApplicationContext() { |
| 46 | + return context; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Resources getResources() { |
| 51 | + return resources; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void sendBroadcast(Intent intent) { |
| 56 | + // override to avoid real broadcast and capture intents if needed |
| 57 | + capturedIntent = intent; |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + synchronizeService.global = global; |
| 62 | + synchronizeService.sqlHandler = sqlHandler; |
| 63 | + synchronizeService.storageManager = storageManager; |
| 64 | + |
| 65 | + when(context.getResources()).thenReturn(resources); |
| 66 | + when(resources.getString(anyInt())).thenReturn("Error message"); |
| 67 | + } |
| 68 | + |
| 69 | + private Intent capturedIntent; |
| 70 | + |
| 71 | + // ---------------------------------- |
| 72 | + // Tests handleUploadClaims |
| 73 | + // ---------------------------------- |
| 74 | + |
| 75 | + @Test |
| 76 | + public void handleUploadClaims_WhenNoNetwork_BroadcastsError() { |
| 77 | + when(global.isNetworkAvailable()).thenReturn(false); |
| 78 | + |
| 79 | + synchronizeService.handleUploadClaims(); |
| 80 | + |
| 81 | + verify(global).isNetworkAvailable(); |
| 82 | + assertNotNull(capturedIntent); |
| 83 | + assertEquals(SynchronizeService.ACTION_SYNC_ERROR, capturedIntent.getAction()); |
| 84 | + assertEquals("Error message", capturedIntent.getStringExtra(SynchronizeService.EXTRA_ERROR_MESSAGE)); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void handleUploadClaims_WhenNoPendingClaims_BroadcastsNoClaimError() throws JSONException { |
| 89 | + when(global.isNetworkAvailable()).thenReturn(true); |
| 90 | + when(sqlHandler.getAllPendingClaims()).thenReturn(new JSONArray()); |
| 91 | + |
| 92 | + synchronizeService.handleUploadClaims(); |
| 93 | + |
| 94 | + verify(sqlHandler).getAllPendingClaims(); |
| 95 | + assertNotNull(capturedIntent); |
| 96 | + assertEquals(SynchronizeService.ACTION_SYNC_ERROR, capturedIntent.getAction()); |
| 97 | + assertEquals("Error message", capturedIntent.getStringExtra(SynchronizeService.EXTRA_ERROR_MESSAGE)); |
| 98 | + } |
| 99 | + |
| 100 | + // Note : we can't test PostNewClaims here without refactoring |
| 101 | + // We just test that the method doesn't crash with pending claims |
| 102 | + @Test |
| 103 | + public void handleUploadClaims_WithPendingClaims_DoesNotCrash() throws JSONException { |
| 104 | + when(global.isNetworkAvailable()).thenReturn(true); |
| 105 | + |
| 106 | + JSONArray pendingClaims = new JSONArray(); |
| 107 | + JSONObject claim = new JSONObject(); |
| 108 | + claim.put("id", "claim123"); |
| 109 | + pendingClaims.put(claim); |
| 110 | + |
| 111 | + when(sqlHandler.getAllPendingClaims()).thenReturn(pendingClaims); |
| 112 | + |
| 113 | + synchronizeService.handleUploadClaims(); |
| 114 | + |
| 115 | + verify(sqlHandler).getAllPendingClaims(); |
| 116 | + } |
| 117 | + |
| 118 | + // ---------------------------------- |
| 119 | + // Tests processClaimResponse |
| 120 | + // ---------------------------------- |
| 121 | + |
| 122 | + @Test |
| 123 | + public void processClaimResponse_WithRejectedClaim_UpdatesStatusToRejected() throws JSONException { |
| 124 | + PostNewClaims.Result rejectedResult = |
| 125 | + new PostNewClaims.Result("claim123", PostNewClaims.Result.Status.REJECTED, "Claim rejected"); |
| 126 | + |
| 127 | + when(sqlHandler.getClaimUUIDForCode("claim123")).thenReturn("uuid123"); |
| 128 | + |
| 129 | + JSONArray result = synchronizeService.processClaimResponse(Arrays.asList(rejectedResult)); |
| 130 | + |
| 131 | + verify(sqlHandler).insertClaimUploadStatus( |
| 132 | + eq("uuid123"), |
| 133 | + anyString(), |
| 134 | + eq(SQLHandler.CLAIM_UPLOAD_STATUS_REJECTED), |
| 135 | + isNull() |
| 136 | + ); |
| 137 | + |
| 138 | + assertEquals(1, result.length()); |
| 139 | + assertTrue(result.getString(0).contains("Claim rejected")); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void processClaimResponse_WithError_UpdatesStatusToError() throws JSONException { |
| 144 | + PostNewClaims.Result errorResult = |
| 145 | + new PostNewClaims.Result("claim123", PostNewClaims.Result.Status.ERROR, "Server error"); |
| 146 | + |
| 147 | + when(sqlHandler.getClaimUUIDForCode("claim123")).thenReturn("uuid123"); |
| 148 | + |
| 149 | + JSONArray result = synchronizeService.processClaimResponse(Arrays.asList(errorResult)); |
| 150 | + |
| 151 | + verify(sqlHandler).insertClaimUploadStatus( |
| 152 | + eq("uuid123"), |
| 153 | + anyString(), |
| 154 | + eq(SQLHandler.CLAIM_UPLOAD_STATUS_ERROR), |
| 155 | + eq("Server error") |
| 156 | + ); |
| 157 | + |
| 158 | + assertEquals(1, result.length()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void processClaimResponse_WithSuccess_UpdatesStatusToAccepted() throws JSONException { |
| 163 | + PostNewClaims.Result successResult = |
| 164 | + new PostNewClaims.Result("claim123", PostNewClaims.Result.Status.SUCCESS, null); |
| 165 | + |
| 166 | + when(sqlHandler.getClaimUUIDForCode("claim123")).thenReturn("uuid123"); |
| 167 | + |
| 168 | + JSONArray result = synchronizeService.processClaimResponse(Arrays.asList(successResult)); |
| 169 | + |
| 170 | + verify(sqlHandler).insertClaimUploadStatus( |
| 171 | + eq("uuid123"), |
| 172 | + anyString(), |
| 173 | + eq(SQLHandler.CLAIM_UPLOAD_STATUS_ACCEPTED), |
| 174 | + isNull() |
| 175 | + ); |
| 176 | + |
| 177 | + assertEquals(0, result.length()); // no error message for SUCCESS |
| 178 | + } |
| 179 | + |
| 180 | + // ---------------------------------- |
| 181 | + // Test handleGetClaimCount |
| 182 | + // ---------------------------------- |
| 183 | + |
| 184 | + @Test |
| 185 | + public void handleGetClaimCount_BroadcastsCorrectCounts() throws JSONException { |
| 186 | + JSONObject counts = new JSONObject(); |
| 187 | + counts.put(SQLHandler.CLAIM_UPLOAD_STATUS_ENTERED, 5); |
| 188 | + counts.put(SQLHandler.CLAIM_UPLOAD_STATUS_ACCEPTED, 3); |
| 189 | + counts.put(SQLHandler.CLAIM_UPLOAD_STATUS_REJECTED, 2); |
| 190 | + |
| 191 | + when(sqlHandler.getClaimCounts()).thenReturn(counts); |
| 192 | + |
| 193 | + synchronizeService.handleGetClaimCount(); |
| 194 | + |
| 195 | + assertNotNull(capturedIntent); |
| 196 | + assertEquals(SynchronizeService.ACTION_CLAIM_COUNT_RESULT, capturedIntent.getAction()); |
| 197 | + assertEquals(5, capturedIntent.getIntExtra(SynchronizeService.EXTRA_CLAIM_COUNT_ENTERED, 0)); |
| 198 | + assertEquals(3, capturedIntent.getIntExtra(SynchronizeService.EXTRA_CLAIM_COUNT_ACCEPTED, 0)); |
| 199 | + assertEquals(2, capturedIntent.getIntExtra(SynchronizeService.EXTRA_CLAIM_COUNT_REJECTED, 0)); |
| 200 | + } |
| 201 | +} |
0 commit comments