Skip to content

Commit

Permalink
Merge 562084d into 82dde40
Browse files Browse the repository at this point in the history
  • Loading branch information
ekigamba committed Aug 25, 2020
2 parents 82dde40 + 562084d commit 4ce9852
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.smartregister.sync.intent;

import android.content.Intent;
import android.support.annotation.VisibleForTesting;

import org.jetbrains.annotations.NotNull;
import org.smartregister.CoreLibrary;
import org.smartregister.DristhiConfiguration;
import org.smartregister.repository.ClientFormRepository;
Expand Down Expand Up @@ -44,10 +46,16 @@ protected void onHandleIntent(Intent intent) {
super.onHandleIntent(intent);

try {
DocumentConfigurationService documentConfigurationService = new DocumentConfigurationService(httpAgent, manifestRepository, clientFormRepository, configuration);
DocumentConfigurationService documentConfigurationService = getDocumentConfigurationService();
documentConfigurationService.fetchManifest();
} catch (Exception e) {
Timber.e(e);
}
}

@VisibleForTesting
@NotNull
protected DocumentConfigurationService getDocumentConfigurationService() {
return new DocumentConfigurationService(httpAgent, manifestRepository, clientFormRepository, configuration);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.smartregister.sync.intent;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;

import org.smartregister.CoreLibrary;
import org.smartregister.domain.FetchStatus;
Expand Down Expand Up @@ -82,7 +82,8 @@ protected void onHandleIntent(@Nullable Intent intent) {
}
}

private void sendSyncStatusBroadcastMessage(FetchStatus fetchStatus) {
@VisibleForTesting
protected void sendSyncStatusBroadcastMessage(FetchStatus fetchStatus) {
CoreLibrary.getInstance().context().applicationContext().sendBroadcast(Utils.completeSync(fetchStatus));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.smartregister.job.SyncServiceJob;
import org.smartregister.sync.helper.SyncSettingsServiceHelper;

import timber.log.Timber;

import static org.smartregister.util.Log.logError;

/**
Expand Down Expand Up @@ -37,7 +39,7 @@ protected void onHandleIntent(Intent intent) {
}

protected boolean processSettings(Intent intent) {
Log.d("ssssssss", "In Settings Sync Intent Service...");
Timber.d("In Settings Sync Intent Service...");
boolean isSuccessfulSync = true;
if (intent != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void onCreate() {
AllSharedPreferences allSharedPreferences = new AllSharedPreferences(getDefaultSharedPreferences(context.applicationContext()));
allSharedPreferences.updateANMUserName("demo");
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null);
CoreLibrary.init(context, null, 1588062490000l, new P2POptions(true));
CoreLibrary.init(context, new TestSyncConfiguration(), 1588062490000l, new P2POptions(true));


setTheme(R.style.Theme_AppCompat_NoActionBar); //or just R.style.Theme_AppCompat
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.smartregister.sync.intent;

import org.json.JSONException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.robolectric.Robolectric;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.BaseRobolectricUnitTest;
import org.smartregister.Context;
import org.smartregister.CoreLibrary;
import org.smartregister.exception.NoHttpResponseException;
import org.smartregister.service.DocumentConfigurationService;

/**
* Created by Ephraim Kigamba - nek.eam@gmail.com on 25-08-2020.
*/
public class DocumentConfigurationIntentServiceTest extends BaseRobolectricUnitTest {

private DocumentConfigurationIntentService documentConfigurationIntentService;

@Before
public void setUp() throws Exception {
documentConfigurationIntentService = Mockito.spy(Robolectric.buildIntentService(DocumentConfigurationIntentService.class)
.create()
.get());
}

@After
public void tearDown() throws Exception {
ReflectionHelpers.setStaticField(Context.class, "context", null);
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null);
}

@Test
public void onStartCommandShouldInstantiateVariables() {
Assert.assertNull(ReflectionHelpers.getField(documentConfigurationIntentService, "httpAgent"));
Assert.assertNull(ReflectionHelpers.getField(documentConfigurationIntentService, "manifestRepository"));
Assert.assertNull(ReflectionHelpers.getField(documentConfigurationIntentService, "clientFormRepository"));
Assert.assertNull(ReflectionHelpers.getField(documentConfigurationIntentService, "configuration"));

documentConfigurationIntentService.onStartCommand(null, 0, 900);


Assert.assertNotNull(ReflectionHelpers.getField(documentConfigurationIntentService, "httpAgent"));
Assert.assertNotNull(ReflectionHelpers.getField(documentConfigurationIntentService, "manifestRepository"));
Assert.assertNotNull(ReflectionHelpers.getField(documentConfigurationIntentService, "clientFormRepository"));
Assert.assertNotNull(ReflectionHelpers.getField(documentConfigurationIntentService, "configuration"));
}

@Test
public void onHandleIntentShouldCallDocumentConfigurationServiceFetchManifest() throws JSONException, NoHttpResponseException {
DocumentConfigurationService documentConfigurationService = Mockito.mock(DocumentConfigurationService.class);
Mockito.doReturn(documentConfigurationService).when(documentConfigurationIntentService).getDocumentConfigurationService();

documentConfigurationIntentService.onHandleIntent(null);

Mockito.verify(documentConfigurationService).fetchManifest();
Mockito.verify(documentConfigurationIntentService).getDocumentConfigurationService();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package org.smartregister.sync.intent;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import org.robolectric.Robolectric;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.BaseRobolectricUnitTest;
import org.smartregister.Context;
import org.smartregister.CoreLibrary;
import org.smartregister.TestP2pApplication;
import org.smartregister.domain.FetchStatus;
import org.smartregister.domain.db.EventClient;
import org.smartregister.repository.AllSharedPreferences;
import org.smartregister.repository.EventClientRepository;
import org.smartregister.sync.ClientProcessorForJava;
import org.smartregister.view.activity.DrishtiApplication;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Ephraim Kigamba - nek.eam@gmail.com on 25-08-2020.
*/
@Config(application = TestP2pApplication.class)
public class P2pProcessRecordsServiceTest extends BaseRobolectricUnitTest {

private P2pProcessRecordsService p2pProcessRecordsService;


private EventClientRepository eventClientRepository;
private ClientProcessorForJava clientProcessorForJava;
private AllSharedPreferences allSharedPreferences;

@Before
public void setUp() throws Exception {
p2pProcessRecordsService = Mockito.spy(Robolectric.buildService(P2pProcessRecordsService.class)
.create()
.get());

eventClientRepository = Mockito.spy(CoreLibrary.getInstance().context().getEventClientRepository());
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "eventClientRepository", eventClientRepository);

clientProcessorForJava = Mockito.spy(DrishtiApplication.getInstance().getClientProcessor());
ReflectionHelpers.setStaticField(ClientProcessorForJava.class, "instance", clientProcessorForJava);


allSharedPreferences = Mockito.spy(CoreLibrary.getInstance().context().allSharedPreferences());
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "allSharedPreferences", allSharedPreferences);
}

@After
public void tearDown() throws Exception {
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "allSharedPreferences", null);
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "eventClientRepository", null);
ReflectionHelpers.setStaticField(ClientProcessorForJava.class, "instance", null);
ReflectionHelpers.setStaticField(Context.class, "context", null);
ReflectionHelpers.setStaticField(CoreLibrary.class, "instance", null);
}

@Test
public void onHandleIntentShouldNotProcessEventsIfPeerToPeerUnprocessedEventsReturnsFalse() throws Exception {
// Mock dependencies
int maxEventClientRowId = 20;
CoreLibrary.getInstance().context().allSharedPreferences().setLastPeerToPeerSyncProcessedEvent(maxEventClientRowId);
List<EventClient> eventClientList = new ArrayList<>();
eventClientList.add(new EventClient(null, null));
Mockito.doReturn(new P2pProcessRecordsService.EventClientQueryResult(maxEventClientRowId, eventClientList)).when(eventClientRepository).fetchEventClientsByRowId(maxEventClientRowId);
Mockito.doReturn(maxEventClientRowId).when(eventClientRepository).getMaxRowId(EventClientRepository.Table.event);
Mockito.doNothing().when(clientProcessorForJava).processClient(eventClientList);

// Call method under test
p2pProcessRecordsService.onHandleIntent(null);

// Verifications and assertions
Mockito.verify(eventClientRepository, Mockito.times(1)).getMaxRowId(EventClientRepository.Table.event);
Mockito.verify(eventClientRepository, Mockito.times(1)).fetchEventClientsByRowId(maxEventClientRowId);
Mockito.verify(clientProcessorForJava, Mockito.times(1)).processClient(eventClientList);
Mockito.verify(allSharedPreferences, Mockito.times(1)).resetLastPeerToPeerSyncProcessedEvent();
Assert.assertTrue(CoreLibrary.getInstance().isPeerToPeerProcessing());
Mockito.verify(p2pProcessRecordsService, Mockito.times(1)).sendSyncStatusBroadcastMessage(FetchStatus.fetched);
}


@Test
public void onHandleIntentShouldProcessEventsIfPeerToPeerUnprocessedEventsReturnsTrue() throws Exception {

p2pProcessRecordsService.onHandleIntent(null);

Mockito.verify(eventClientRepository, Mockito.never()).getMaxRowId(Mockito.any(EventClientRepository.Table.class));
Mockito.verify(clientProcessorForJava, Mockito.never()).processClient(ArgumentMatchers.<List< EventClient>>any());
}

@Test
public void onDestroyShouldSetPeerProcessingToFalse() {
CoreLibrary.getInstance().setPeerToPeerProcessing(true);
Assert.assertTrue(CoreLibrary.getInstance().isPeerToPeerProcessing());

// Call the method
p2pProcessRecordsService.onDestroy();

Assert.assertFalse(CoreLibrary.getInstance().isPeerToPeerProcessing());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.smartregister.sync.intent;

import android.content.Intent;

import org.json.JSONException;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.mockito.Mockito;
import org.robolectric.Robolectric;
import org.smartregister.AllConstants;
import org.smartregister.BaseRobolectricUnitTest;


/**
* Created by Ephraim Kigamba - nek.eam@gmail.com on 25-08-2020.
*/
public class SettingsSyncIntentServiceTest extends BaseRobolectricUnitTest {

private SettingsSyncIntentService settingsSyncIntentService;

@Before
public void setUp() throws Exception {
settingsSyncIntentService = Robolectric.buildIntentService(SettingsSyncIntentService.class)
.create()
.get();
}

@Ignore
@Test
public void onHandleIntent() {
// TODO: Implement this in the next round of tests
Assert.assertEquals(2, 1 + 1);
}

@Test
public void processSettingsShouldReturnTrueWhenIntentIsNull() throws JSONException {
settingsSyncIntentService.syncSettingsServiceHelper = Mockito.spy(settingsSyncIntentService.syncSettingsServiceHelper);
Assert.assertTrue(settingsSyncIntentService.processSettings(null));

Mockito.verify(settingsSyncIntentService.syncSettingsServiceHelper, Mockito.never()).processIntent();
}

@Test
public void processSettingsShouldReturnTrueAndCallProcessIntentWhenIntentIsNotNull() throws JSONException {
settingsSyncIntentService.syncSettingsServiceHelper = Mockito.spy(settingsSyncIntentService.syncSettingsServiceHelper);
Mockito.doReturn(30).when(settingsSyncIntentService.syncSettingsServiceHelper).processIntent();

Intent intent = new Intent();

Assert.assertTrue(settingsSyncIntentService.processSettings(intent));
Mockito.verify(settingsSyncIntentService.syncSettingsServiceHelper, Mockito.times(1)).processIntent();

Assert.assertEquals(30, intent.getIntExtra(AllConstants.INTENT_KEY.SYNC_TOTAL_RECORDS, 0));
}

@Ignore
@Test
public void onCreate() {
// TODO: Implement this in the next round of tests
Assert.assertEquals(2, 1 + 1);
}

@Ignore
@Test
public void onDestroy() {
// TODO: Implement this in the next round of tests
Assert.assertEquals(2, 1 + 1);
}
}

0 comments on commit 4ce9852

Please sign in to comment.