Skip to content

Commit

Permalink
Refactor JsonFormUtils 💚
Browse files Browse the repository at this point in the history
- Add unit tests
- Overload Init method to add appVersionName
  • Loading branch information
ndegwamartin committed Jul 19, 2022
1 parent e1bdaa5 commit a575176
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=3.0.0-ALPHA-SNAPSHOT
VERSION_NAME=3.0.0-BETA-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Growth Monitoring Application
Expand Down
2 changes: 1 addition & 1 deletion opensrp-growth-monitoring/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ allprojects {

dependencies {

implementation('org.smartregister:opensrp-client-core:6.0.0-ALPHA-SNAPSHOT@aar') {
implementation('org.smartregister:opensrp-client-core:6.0.0-BETA2-SNAPSHOT@aar') {
transitive = true
exclude group: 'com.github.bmelnychuk', module: 'atv'
exclude group: 'com.ibm.fhir', module: 'fhir-model'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,51 @@ public class GrowthMonitoringLibrary {
private EventClientRepository eventClientRepository;
private int applicationVersion;
private int databaseVersion;
private String applicationVersionName;
private AppProperties appProperties;

private long growthMonitoringSyncTime = -1;

private GrowthMonitoringLibrary(Context context, Repository repository, int applicationVersion, int databaseVersion) {
private GrowthMonitoringLibrary(Context context, Repository repository, int applicationVersion, String applicationVersionName, int databaseVersion) {
this.repository = repository;
this.context = context;
this.applicationVersion = applicationVersion;
this.databaseVersion = databaseVersion;
this.applicationVersionName = applicationVersionName;
this.appProperties = GrowthMonitoringUtils.getProperties(this.context.applicationContext());
}

public static void init(Context context, Repository repository, int applicationVersion, int databaseVersion,
GrowthMonitoringConfig growthMonitoringConfig) {
/**
* This init is deprecated, use {@link #init(Context context, Repository repository, int applicationVersion, int databaseVersion, GrowthMonitoringConfig growthMonitoringConfig)} instead which adds application version name.
*/
@Deprecated
public static void init(Context context, Repository repository, int applicationVersion, int databaseVersion, GrowthMonitoringConfig growthMonitoringConfig) {

init(context, repository, applicationVersion, databaseVersion);

config = growthMonitoringConfig != null ? growthMonitoringConfig : config;

}

public static void init(Context context, Repository repository, int applicationVersion, String applicationVersionName, int databaseVersion, GrowthMonitoringConfig growthMonitoringConfig) {

init(context, repository, applicationVersion, applicationVersionName, databaseVersion);

config = growthMonitoringConfig != null ? growthMonitoringConfig : config;

}

/**
* This init is deprecated, use {@link #init(Context, Repository, int, String, int)} instead which adds application version name.
*/
@Deprecated
public static void init(Context context, Repository repository, int applicationVersion, int databaseVersion) {
init(context, repository, applicationVersion, null, databaseVersion);
}

public static void init(Context context, Repository repository, int applicationVersion, String applicationVersionName, int databaseVersion) {
if (instance == null) {
instance = new GrowthMonitoringLibrary(context, repository, applicationVersion, databaseVersion);
instance = new GrowthMonitoringLibrary(context, repository, applicationVersion, applicationVersionName, databaseVersion);
}
}

Expand Down Expand Up @@ -129,6 +150,10 @@ public int getDatabaseVersion() {
return databaseVersion;
}

public String getApplicationVersionName() {
return applicationVersionName;
}

public GrowthMonitoringConfig getConfig() {
return config;
}
Expand Down Expand Up @@ -156,4 +181,12 @@ public void setGrowthMonitoringSyncTime(int growthMonitoringSyncTimeInHours) {
public void setGrowthMonitoringSyncTime(int growthMonitoringSyncTime, @NonNull TimeUnit timeUnit) {
this.growthMonitoringSyncTime = timeUnit.toMinutes(growthMonitoringSyncTime);
}

/**
* Public method to clear the instance/destroy useful for testing
*/
public static void destroy() {
instance = null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.smartregister.AllConstants;
import org.smartregister.Context;
import org.smartregister.clientandeventmodel.Event;
import org.smartregister.domain.Observation;
import org.smartregister.growthmonitoring.GrowthMonitoringLibrary;
import org.smartregister.growthmonitoring.domain.Height;
import org.smartregister.growthmonitoring.domain.Weight;
import org.smartregister.repository.EventClientRepository;

import java.util.Date;

Expand All @@ -17,9 +20,15 @@
* Created by keyman on 26/07/2017.
*/
public class JsonFormUtils extends org.smartregister.util.JsonFormUtils {

public static void createWeightEvent(Weight weight, String eventType, String entityType, JSONArray fields) {

Context context = GrowthMonitoringLibrary.getInstance().context();
createWeightEvent(weight, eventType, entityType, fields, context);
}

public static void createWeightEvent(Weight weight, String eventType, String entityType, JSONArray fields, Context context) {
try {
EventClientRepository db = GrowthMonitoringLibrary.getInstance().eventClientRepository();

Event event =
(Event) new Event().withBaseEntityId(weight.getBaseEntityId()).withIdentifiers(weight.getIdentifiers())
Expand All @@ -31,8 +40,16 @@ public static void createWeightEvent(Weight weight, String eventType, String ent
event.setTeam(weight.getTeam());
event.setTeamId(weight.getTeamId());
event.setChildLocationId(weight.getChildLocationId());
event.addDetails(AllConstants.DATA_STRATEGY, context.allSharedPreferences().fetchCurrentDataStrategy());

try {
addFormSubmissionFieldObservation(AllConstants.DATA_STRATEGY, context.allSharedPreferences().fetchCurrentDataStrategy(), Observation.TYPE.TEXT, event);
} catch (JSONException jsonException) {
Timber.e(jsonException);
}

event.setClientApplicationVersion(GrowthMonitoringLibrary.getInstance().getApplicationVersion());
event.setClientApplicationVersionName(GrowthMonitoringLibrary.getInstance().getApplicationVersionName());
event.setClientDatabaseVersion(GrowthMonitoringLibrary.getInstance().getDatabaseVersion());

if (fields != null && fields.length() != 0) for (int i = 0; i < fields.length(); i++) {
Expand All @@ -43,18 +60,17 @@ public static void createWeightEvent(Weight weight, String eventType, String ent
}
}


if (event != null) {
JSONObject eventJson = new JSONObject(org.smartregister.util.JsonFormUtils.gson.toJson(event));

//check if an event already exists and update instead
if (weight.getEventId() != null) {
JSONObject existingEvent = db.getEventsByEventId(weight.getEventId());
JSONObject existingEvent = context.getEventClientRepository().getEventsByEventId(weight.getEventId());
eventJson = merge(existingEvent, eventJson);
}

//merge if event exists
db.addEvent(event.getBaseEntityId(), eventJson);
context.getEventClientRepository().addEvent(event.getBaseEntityId(), eventJson);

}
} catch (Exception e) {
Expand All @@ -63,8 +79,13 @@ public static void createWeightEvent(Weight weight, String eventType, String ent
}

public static void createHeightEvent(Height height, String eventType, String entityType, JSONArray fields) {

Context context = GrowthMonitoringLibrary.getInstance().context();
createHeightEvent(height, eventType, entityType, fields, context);
}

public static void createHeightEvent(Height height, String eventType, String entityType, JSONArray fields, Context context) {
try {
EventClientRepository db = GrowthMonitoringLibrary.getInstance().eventClientRepository();

Event event =
(Event) new Event().withBaseEntityId(height.getBaseEntityId()).withIdentifiers(height.getIdentifiers())
Expand All @@ -76,8 +97,16 @@ public static void createHeightEvent(Height height, String eventType, String ent
event.setTeam(height.getTeam());
event.setTeamId(height.getTeamId());
event.setChildLocationId(height.getChildLocationId());
event.addDetails(AllConstants.DATA_STRATEGY, context.allSharedPreferences().fetchCurrentDataStrategy());

try {
addFormSubmissionFieldObservation(AllConstants.DATA_STRATEGY, context.allSharedPreferences().fetchCurrentDataStrategy(), Observation.TYPE.TEXT, event);
} catch (JSONException jsonException) {
Timber.e(jsonException);
}

event.setClientApplicationVersion(GrowthMonitoringLibrary.getInstance().getApplicationVersion());
event.setClientApplicationVersionName(GrowthMonitoringLibrary.getInstance().getApplicationVersionName());
event.setClientDatabaseVersion(GrowthMonitoringLibrary.getInstance().getDatabaseVersion());

if (fields != null && fields.length() != 0) for (int i = 0; i < fields.length(); i++) {
Expand All @@ -88,17 +117,16 @@ public static void createHeightEvent(Height height, String eventType, String ent
}
}


if (event != null) {
JSONObject eventJson = new JSONObject(org.smartregister.util.JsonFormUtils.gson.toJson(event));
//check if an event already exists and update instead
if (height.getEventId() != null) {
JSONObject existingEvent = db.getEventsByEventId(height.getEventId());
JSONObject existingEvent = context.getEventClientRepository().getEventsByEventId(height.getEventId());
eventJson = merge(existingEvent, eventJson);
}

//merge if event exists
db.addEvent(event.getBaseEntityId(), eventJson);
context.getEventClientRepository().addEvent(event.getBaseEntityId(), eventJson);

}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ protected List<Height> getHeights() {
height.setAnmId("demo");
height.setBaseEntityId(TEST_BASE_ENTITY_ID);
height.setChildLocationId(TEST_STRING);
height.setLocationId(TEST_STRING);
height.setCm(30.4f);
heightArrayList.add(height);

Expand Down Expand Up @@ -118,6 +119,7 @@ protected List<Weight> getWeights() {
weight.setAnmId("demo");
weight.setBaseEntityId(TEST_BASE_ENTITY_ID);
weight.setChildLocationId(TEST_STRING);
weight.setLocationId(TEST_STRING);
weight.setKg(3.4f);

weightArrayList.add(weight);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.smartregister.growthmonitoring;

import static org.junit.Assert.assertEquals;

import android.os.Build;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -16,8 +19,6 @@
import org.smartregister.Context;
import org.smartregister.repository.Repository;

import static org.junit.Assert.assertEquals;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = {Build.VERSION_CODES.P})
public class GrowthMonitoringLibraryTest {
Expand All @@ -31,12 +32,21 @@ public void setUp() throws Exception {
}

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

@Test
public void getGrowthMonitoringSyncTimeShouldReturnDefault15hoursInMinutes() {
assertEquals(60 * 15, GrowthMonitoringLibrary.getInstance().getGrowthMonitoringSyncTime());
}

@Test
public void testGrowthMonitoringLibraryClassInitsCorrectly() {
GrowthMonitoringLibrary.destroy();
GrowthMonitoringLibrary.init(Mockito.mock(Context.class), Mockito.mock(Repository.class), 1, "1.0.0", 1);
Assert.assertEquals(1, GrowthMonitoringLibrary.getInstance().getApplicationVersion());
Assert.assertEquals("1.0.0", GrowthMonitoringLibrary.getInstance().getApplicationVersionName());
Assert.assertEquals(1, GrowthMonitoringLibrary.getInstance().getDatabaseVersion());
}
}
Loading

0 comments on commit a575176

Please sign in to comment.