Skip to content

Commit

Permalink
Merge pull request #639 from OpenSRP/kigamba-tt-8-9-2020
Browse files Browse the repository at this point in the history
Add tests for SettingsSyncIntentService
  • Loading branch information
ekigamba committed Sep 14, 2020
2 parents 8a6bf9d + 19a19df commit f177cd7
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 38 deletions.
Expand Up @@ -222,16 +222,4 @@ private JSONArray pullSettings(String directoryUrl, String accessToken) throws J
protected Response<String> getResponse(String completeUrl, String accessToken) {
return httpAgent.fetchWithCredentials(completeUrl, accessToken);
}

public void setHttpAgent(HTTPAgent httpAgent) {
this.httpAgent = httpAgent;
}

public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}

public void setSharedPreferences(AllSharedPreferences sharedPreferences) {
this.sharedPreferences = sharedPreferences;
}
}
Expand Up @@ -63,10 +63,5 @@ public void onCreate() {
syncSettingsServiceHelper = new SyncSettingsServiceHelper(context.configuration().dristhiBaseURL(), context.getHttpAgent());
}

@Override
public void onDestroy() {
super.onDestroy();
}

}

@@ -1,11 +1,8 @@
package org.smartregister.shadows;
package com.evernote.android.job;

import android.content.Context;
import android.support.annotation.NonNull;

import com.evernote.android.job.JobManager;
import com.evernote.android.job.JobManagerCreateException;

import org.mockito.Mockito;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
Expand All @@ -17,12 +14,24 @@
public class ShadowJobManager {

public static JobManager mockJobManager;
public static JobStorage jobStorage;

@Implementation
public static JobManager create(@NonNull Context context) throws JobManagerCreateException {

return createMockJobManager();
}

@Implementation
public static JobManager instance() {
return mockJobManager;
}

public static JobManager createMockJobManager() {
if (mockJobManager == null) {
mockJobManager = Mockito.mock(JobManager.class);
jobStorage = Mockito.mock(JobStorage.class);
Mockito.doReturn(jobStorage).when(mockJobManager).getJobStorage();
}

return mockJobManager;
Expand Down
Expand Up @@ -11,7 +11,7 @@
import org.smartregister.customshadows.FontTextViewShadow;
import org.smartregister.shadows.ShadowAppDatabase;
import org.smartregister.shadows.ShadowDrawableResourcesImpl;
import org.smartregister.shadows.ShadowJobManager;
import com.evernote.android.job.ShadowJobManager;
import org.smartregister.shadows.ShadowSQLiteDatabase;

/**
Expand Down
28 changes: 28 additions & 0 deletions opensrp-app/src/test/java/org/smartregister/TestApplication.java
@@ -1,10 +1,17 @@
package org.smartregister;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.evernote.android.job.Job;
import com.evernote.android.job.JobCreator;
import com.evernote.android.job.JobManager;

import org.json.JSONObject;
import org.smartregister.job.SyncServiceJob;
import org.smartregister.repository.Repository;
import org.smartregister.sync.P2PClassifier;
import org.smartregister.sync.intent.SyncIntentService;
import org.smartregister.view.activity.DrishtiApplication;

import static org.mockito.Mockito.mock;
Expand All @@ -24,6 +31,9 @@ public void onCreate() {
CoreLibrary.init(context, new TestSyncConfiguration(), 1588062490000l);

setTheme(R.style.Theme_AppCompat_NoActionBar); //or just R.style.Theme_AppCompat

// Init Job Creator
JobManager.create(this).addJobCreator(new TestJobCreator());
}

@Override
Expand Down Expand Up @@ -55,4 +65,22 @@ public P2PClassifier<JSONObject> getP2PClassifier() {
public void setP2PClassifier(P2PClassifier<JSONObject> p2PClassifier) {
this.p2PClassifier = p2PClassifier;
}

static class TestJobCreator implements JobCreator {

@Nullable
@Override
public Job create(@NonNull String tag) {
switch (tag) {
case SyncServiceJob.TAG:
return new SyncServiceJob(SyncIntentService.class);

default:
break;
}

return null;
}

}
}
Expand Up @@ -2,15 +2,21 @@

import android.content.Intent;

import com.evernote.android.job.JobRequest;

import org.json.JSONException;
import org.junit.After;
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.robolectric.util.ReflectionHelpers;
import org.smartregister.AllConstants;
import org.smartregister.BaseRobolectricUnitTest;
import org.smartregister.Context;

import com.evernote.android.job.ShadowJobManager;


/**
Expand All @@ -27,11 +33,18 @@ public void setUp() throws Exception {
.get();
}

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

@Test
public void onHandleIntent() {
// TODO: Implement this in the next round of tests
Assert.assertEquals(2, 1 + 1);
public void onHandleIntentShouldScheduleJobRequestAndInvokeProcessSettingsWhenGivenNullIntent() {
settingsSyncIntentService = Mockito.spy(settingsSyncIntentService);
settingsSyncIntentService.onHandleIntent(null);

Mockito.verify(ShadowJobManager.mockJobManager).schedule(Mockito.any(JobRequest.class));
Mockito.verify(settingsSyncIntentService).processSettings(Mockito.nullable(Intent.class));
}

@Test
Expand All @@ -55,17 +68,15 @@ public void processSettingsShouldReturnTrueAndCallProcessIntentWhenIntentIsNotNu
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);
}
public void onCreateShouldCreateSyncSettingsServiceHelper() {
settingsSyncIntentService = Robolectric.buildIntentService(SettingsSyncIntentService.class)
.get();
Assert.assertNull(settingsSyncIntentService.syncSettingsServiceHelper);

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

Assert.assertNotNull(settingsSyncIntentService.syncSettingsServiceHelper);
}

}
Expand Up @@ -20,7 +20,7 @@
import org.smartregister.repository.Repository;
import org.smartregister.shadows.ShadowAppDatabase;
import org.smartregister.shadows.ShadowDrawableResourcesImpl;
import org.smartregister.shadows.ShadowJobManager;
import com.evernote.android.job.ShadowJobManager;
import org.smartregister.shadows.ShadowSQLiteDatabase;
import org.smartregister.util.CredentialsHelper;

Expand Down

0 comments on commit f177cd7

Please sign in to comment.