Skip to content

Commit

Permalink
Rebase off master
Browse files Browse the repository at this point in the history
  • Loading branch information
qiarie committed Dec 7, 2020
1 parent 001ae7e commit dddca6c
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 50 deletions.
1 change: 0 additions & 1 deletion opensrp-app/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,5 @@
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />


</application>
</manifest>
2 changes: 1 addition & 1 deletion opensrp-app/src/main/java/org/smartregister/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ private SharedPreferences createEncryptedSharedPreferences(android.content.Conte
} catch (Exception e) {
Timber.e(e, "Error creating encrypted SharedPreferences");

// fallback to unencrypted SharedPreferences
// fall back to unencrypted SharedPreferences
sharedPreferences = getDefaultSharedPreferences(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public ClientProcessorForJava getClientProcessor() {

public String getUsername() {
if (username == null) {
username = context.allSharedPreferences().fetchRegisteredANM();
username = context.userService().getAllSharedPreferences().fetchRegisteredANM();
}
return username;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.powermock.reflect.Whitebox;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.BaseRobolectricUnitTest;
import org.smartregister.Context;
import org.smartregister.CoreLibrary;
import org.smartregister.domain.UniqueId;
import org.smartregister.view.activity.DrishtiApplication;
Expand Down Expand Up @@ -67,37 +66,23 @@ public class UniqueIdRepositoryTest extends BaseRobolectricUnitTest {

private static final String testUsername = "testUser1";

@Mock
private CoreLibrary coreLibrary;

@Mock
private Context context;

@Mock
private AllSharedPreferences allSharedPreferences;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);

CoreLibrary.init(context);
AllSharedPreferences allSharedPreferences = new AllSharedPreferences(getDefaultSharedPreferences(ApplicationProvider.getApplicationContext()));
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "allSharedPreferences", allSharedPreferences);

ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary);
Whitebox.setInternalState(DrishtiApplication.getInstance(), "repository", repository);

uniqueIdRepository = new UniqueIdRepository();

when(coreLibrary.context()).thenReturn(context);
when(context.allSharedPreferences()).thenReturn(allSharedPreferences);
when(allSharedPreferences.fetchRegisteredANM()).thenReturn(testUsername);
when(repository.getReadableDatabase()).thenReturn(sqLiteDatabase);
when(repository.getWritableDatabase()).thenReturn(sqLiteDatabase);

uniqueIdRepository = new UniqueIdRepository();
}

@After
public void tearDown() {
Whitebox.setInternalState(DrishtiApplication.getInstance(), "repository",(Repository) null);
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null);
Whitebox.setInternalState(DrishtiApplication.getInstance(), "repository", (Repository) null);
}

@Test
Expand All @@ -118,7 +103,7 @@ public void testUpdateOpenMRSIdentifierStatusInvokesDatabaseUpdateMethodCorrectl

@Test
public void testUpdateOpenMRSIdentifierStatusInvokesDatabaseUpdateMethodOnceIfRowUpdated() {
allSharedPreferences.updateANMUserName(testUsername);
CoreLibrary.getInstance().context().allSharedPreferences().updateANMUserName(testUsername);
String openMrsId = "3298938-2";

doReturn(1).when(sqLiteDatabase).update(stringArgumentCaptor.capture(), contentValuesArgumentCaptor.capture(), stringArgumentCaptor.capture(), argsCaptor.capture());
Expand All @@ -136,7 +121,7 @@ public void testUpdateOpenMRSIdentifierStatusInvokesDatabaseUpdateMethodOnceIfRo

@Test
public void testBulkInsertOpenMrsIds() {
allSharedPreferences.updateANMUserName(testUsername);
CoreLibrary.getInstance().context().allSharedPreferences().updateANMUserName(testUsername);
String openMrsId = "3298938-2";
List<String> openMrsIds = Collections.singletonList(openMrsId);

Expand All @@ -157,15 +142,15 @@ public void testBulkInsertOpenMrsIds() {

@Test
public void testBulkInsertOpenMrsIdsWithEmptyParamList() {
allSharedPreferences.updateANMUserName(testUsername);
CoreLibrary.getInstance().context().allSharedPreferences().updateANMUserName(testUsername);
uniqueIdRepository.bulkInsertOpenmrsIds(null);

verifyNoMoreInteractions(sqLiteDatabase);
}

@Test
public void testBulkInsertOpenMrsIdsWithExceptionThrown() {
allSharedPreferences.updateANMUserName(testUsername);
CoreLibrary.getInstance().context().allSharedPreferences().updateANMUserName(testUsername);
String openMrsId = "3298938-2";
List<String> openMrsIds = Collections.singletonList(openMrsId);
uniqueIdRepository = spy(uniqueIdRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.powermock.reflect.Whitebox;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.BaseRobolectricUnitTest;
import org.smartregister.Context;
import org.smartregister.CoreLibrary;
import org.smartregister.repository.ClientRelationshipRepository;
import org.smartregister.repository.Repository;
Expand Down Expand Up @@ -43,12 +42,6 @@ public class ClientDaoImplTest extends BaseRobolectricUnitTest {
@Mock
private SQLiteDatabase sqLiteDatabase;

@Mock
private CoreLibrary coreLibrary;

@Mock
private Context context;

@Before
public void setUp() {
clientDao = new ClientDaoImpl();
Expand Down Expand Up @@ -134,28 +127,20 @@ public void testFindFamilyMemberByResidence() throws Exception {

@Test
public void testFindClientByRelationship() throws Exception {

ClientRelationshipRepository clientRelationshipRepository = new ClientRelationshipRepository();
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "clientRelationshipRepository", clientRelationshipRepository);

String query = "SELECT json FROM client_relationship JOIN client ON base_entity_id=baseEntityId WHERE relationship=? AND relational_id =?";
String[] params = new String[]{"41587456-b7c8-4c4e-b433-23a786f742fc", "Family"};
when(sqLiteDatabase.rawQuery(anyString(), any())).thenReturn(getCursor(2));

ClientRelationshipRepository clientRelationshipRepository = new ClientRelationshipRepository();

ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", coreLibrary);
when(coreLibrary.context()).thenReturn(context);
when(context.getClientRelationshipRepository()).thenReturn(clientRelationshipRepository);

List<Patient> patients = clientDao.findClientByRelationship(params[0], params[1]);
verify(sqLiteDatabase).rawQuery(query, params);

assertEquals(2, patients.size());
Patient patient = patients.iterator().next();
assertNotNull(patient);
assertEquals("03b1321a-d1fb-4fd0-b1cd-a3f3509fc6a6", patient.getId());

ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null);
}

public static MatrixCursor getCursor() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.powermock.reflect.Whitebox;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
Expand Down Expand Up @@ -42,9 +41,6 @@ public class DrishtiApplicationTest {
@Before
public void setUp() throws Exception {
drishtiApplication = Mockito.spy(new Application());

CoreLibrary.init(drishtiApplication.getContext(), new TestSyncConfiguration());

Mockito.doReturn(RuntimeEnvironment.application.getFilesDir()).when(drishtiApplication).getFilesDir();
Mockito.doReturn(RuntimeEnvironment.application).when(drishtiApplication).getApplicationContext();
Mockito.doReturn(RuntimeEnvironment.application.getResources()).when(drishtiApplication).getResources();
Expand All @@ -70,9 +66,6 @@ public void onCreateShouldRegisterSingletonAndInitialiseCrashReporting() {
public void getRepository() {
drishtiApplication.onCreate();

Context context = Mockito.mock(Context.class);
Whitebox.setInternalState(CoreLibrary.getInstance(), "context", context);

Assert.assertNull(ReflectionHelpers.getField(drishtiApplication, "repository"));
Assert.assertNotNull(drishtiApplication.getRepository());
}
Expand Down

0 comments on commit dddca6c

Please sign in to comment.