|
| 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.graphics.Bitmap; |
| 8 | +import android.view.View; |
| 9 | +import android.widget.ListAdapter; |
| 10 | + |
| 11 | +import org.junit.Before; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | +import org.mockito.MockitoAnnotations; |
| 15 | +import org.openimis.imisclaims.domain.entity.Insuree; |
| 16 | +import org.openimis.imisclaims.domain.entity.Policy; |
| 17 | +import org.openimis.imisclaims.usecase.FetchInsureeInquire; |
| 18 | +import org.robolectric.Robolectric; |
| 19 | +import org.robolectric.RobolectricTestRunner; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.Date; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +@RunWith(RobolectricTestRunner.class) |
| 27 | +public class EnquireActivityTest { |
| 28 | + |
| 29 | + EnquireActivity activity; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setup() { |
| 33 | + MockitoAnnotations.openMocks(this); |
| 34 | + |
| 35 | + activity = Robolectric.buildActivity(EnquireActivity.class) |
| 36 | + .create() |
| 37 | + .start() |
| 38 | + .resume() |
| 39 | + .get(); |
| 40 | + |
| 41 | + // Inject mocked global |
| 42 | + activity.global = mock(Global.class); |
| 43 | + when(activity.global.isNetworkAvailable()).thenReturn(true); |
| 44 | + |
| 45 | + // Default CHFID |
| 46 | + activity.etCHFID.setText("CHF123"); |
| 47 | + } |
| 48 | + |
| 49 | + /* ===================================================== |
| 50 | + buildEnquireValue() |
| 51 | + ===================================================== */ |
| 52 | + |
| 53 | + @Test |
| 54 | + public void buildEnquireValue_WhenNull_ReturnsEmptyString() { |
| 55 | + String result = activity.buildEnquireValue(null, R.string.totalVisitsLeft); |
| 56 | + assertEquals("", result); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void buildEnquireValue_WhenValueProvided_ReturnsFormattedString() { |
| 61 | + String result = activity.buildEnquireValue(5, R.string.totalVisitsLeft); |
| 62 | + assertEquals("TotalVisitsLeft: 5", result); |
| 63 | + } |
| 64 | + |
| 65 | + /* ===================================================== |
| 66 | + renderResult() |
| 67 | + ===================================================== */ |
| 68 | + |
| 69 | + @Test |
| 70 | + public void renderResult_WhenInsureeIsNull_ShowsNotFoundDialog() { |
| 71 | + EnquireActivity spy = spy(activity); |
| 72 | + |
| 73 | + spy.renderResult(null); |
| 74 | + |
| 75 | + verify(spy, atLeastOnce()).showDialog( |
| 76 | + activity.getResources().getString(R.string.RecordNotFound) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void renderResult_WhenCHFIDMismatch_DoesNothing() { |
| 82 | + Insuree insuree = mock(Insuree.class); |
| 83 | + when(insuree.getChfId()).thenReturn("DIFFERENT"); |
| 84 | + |
| 85 | + activity.renderResult(insuree); |
| 86 | + |
| 87 | + assertEquals( |
| 88 | + activity.getResources().getString(R.string.CHFID), |
| 89 | + activity.tvCHFID.getText().toString() |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void renderResult_WhenValidInsuree_PopulatesUI() { |
| 95 | + Insuree insuree = mock(Insuree.class); |
| 96 | + |
| 97 | + when(insuree.getChfId()).thenReturn("CHF123"); |
| 98 | + when(insuree.getName()).thenReturn("John Doe"); |
| 99 | + when(insuree.getGender()).thenReturn("M"); |
| 100 | + when(insuree.getDateOfBirth()).thenReturn(new Date()); |
| 101 | + when(insuree.getPhoto()).thenReturn(null); |
| 102 | + when(insuree.getPhotoPath()).thenReturn(null); |
| 103 | + when(insuree.getPolicies()).thenReturn(Collections.emptyList()); |
| 104 | + |
| 105 | + activity.renderResult(insuree); |
| 106 | + |
| 107 | + assertEquals("CHF123", activity.tvCHFID.getText().toString()); |
| 108 | + assertEquals("John Doe", activity.tvName.getText().toString()); |
| 109 | + assertEquals("M", activity.tvGender.getText().toString()); |
| 110 | + assertEquals(View.VISIBLE, activity.ll.getVisibility()); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void renderResult_WhenPhotoBytesPresent_DisplaysBitmap() { |
| 115 | + Insuree insuree = mock(Insuree.class); |
| 116 | + |
| 117 | + byte[] fakePhoto = new byte[10]; |
| 118 | + |
| 119 | + when(insuree.getChfId()).thenReturn("CHF123"); |
| 120 | + when(insuree.getName()).thenReturn("John Doe"); |
| 121 | + when(insuree.getGender()).thenReturn("M"); |
| 122 | + when(insuree.getDateOfBirth()).thenReturn(new Date()); |
| 123 | + when(insuree.getPhoto()).thenReturn(fakePhoto); |
| 124 | + when(insuree.getPolicies()).thenReturn(Collections.emptyList()); |
| 125 | + |
| 126 | + activity.renderResult(insuree); |
| 127 | + |
| 128 | + assertNotNull(activity.iv.getDrawable()); |
| 129 | + } |
| 130 | + |
| 131 | + /* ===================================================== |
| 132 | + renderResult() with policies |
| 133 | + ===================================================== */ |
| 134 | + |
| 135 | + @Test |
| 136 | + public void renderResult_WithPolicies_PopulatesListView() { |
| 137 | + Policy policy = mock(Policy.class); |
| 138 | + |
| 139 | + when(policy.getCode()).thenReturn("PROD1"); |
| 140 | + when(policy.getName()).thenReturn("Basic Cover"); |
| 141 | + when(policy.getStatus()).thenReturn(Policy.Status.ACTIVE); |
| 142 | + when(policy.getExpiryDate()).thenReturn(new Date()); |
| 143 | + when(policy.getDeductibleType()).thenReturn(1.0); |
| 144 | + when(policy.getDeductibleIp()).thenReturn(10.0); |
| 145 | + when(policy.getCeilingIp()).thenReturn(100.0); |
| 146 | + |
| 147 | + List<Policy> policies = new ArrayList<>(); |
| 148 | + policies.add(policy); |
| 149 | + |
| 150 | + Insuree insuree = mock(Insuree.class); |
| 151 | + when(insuree.getChfId()).thenReturn("CHF123"); |
| 152 | + when(insuree.getName()).thenReturn("John Doe"); |
| 153 | + when(insuree.getGender()).thenReturn("M"); |
| 154 | + when(insuree.getDateOfBirth()).thenReturn(new Date()); |
| 155 | + when(insuree.getPhoto()).thenReturn(null); |
| 156 | + when(insuree.getPolicies()).thenReturn(policies); |
| 157 | + |
| 158 | + activity.renderResult(insuree); |
| 159 | + |
| 160 | + ListAdapter adapter = activity.lv.getAdapter(); |
| 161 | + assertNotNull(adapter); |
| 162 | + assertEquals(1, adapter.getCount()); |
| 163 | + } |
| 164 | + |
| 165 | + /* ===================================================== |
| 166 | + getInsureeInfo() |
| 167 | + ===================================================== */ |
| 168 | + |
| 169 | + @Test |
| 170 | + public void getInsureeInfo_WhenOffline_UsesLocalDb() { |
| 171 | + EnquireActivity spy = spy(activity); |
| 172 | + |
| 173 | + when(spy.global.isNetworkAvailable()).thenReturn(false); |
| 174 | + doReturn(null).when(spy).getDataFromDb(anyString()); |
| 175 | + |
| 176 | + spy.getInsureeInfo(); |
| 177 | + |
| 178 | + verify(spy).getDataFromDb("CHF123"); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void getInsureeInfo_WhenOnline_RecordNotFound_ShowsDialog() throws Exception { |
| 183 | + EnquireActivity spy = spy(activity); |
| 184 | + |
| 185 | + when(spy.global.isNetworkAvailable()).thenReturn(true); |
| 186 | + |
| 187 | + when(spy.createFetchInsureeInquire(anyString())) |
| 188 | + .thenThrow(new org.openimis.imisclaims.network.exception.HttpException( |
| 189 | + 404, "Not Found", null, null |
| 190 | + )); |
| 191 | + |
| 192 | + spy.getInsureeInfo(); |
| 193 | + |
| 194 | + verify(spy, atLeastOnce()).showDialog( |
| 195 | + activity.getResources().getString(R.string.RecordNotFound) |
| 196 | + ); |
| 197 | + } |
| 198 | +} |
0 commit comments