Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for SettingsSyncIntentService #639

Merged
merged 6 commits into from Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}
Comment on lines -225 to -236
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason you removed this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had added the setters on another testing PR. Their role was to enable setting the values instead of using the constructor. I no longer need them

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool

}
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