diff --git a/pom.xml b/pom.xml index 597dc0d..fd38c36 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ opensrp-server-connector jar - 2.0.0-alpha1-SNAPSHOT + 2.0.0-alpha2-SNAPSHOT opensrp-server-connector OpenSRP Server Connector module @@ -19,7 +19,9 @@ 1.5.1 5.2.3.RELEASE 1.0.1 - 2.0.0-alpha1-SNAPSHOT + 2.0.0-alpha2-SNAPSHOT + 2.0.0-alpha3-SNAPSHOT + 2.0.5 @@ -93,28 +95,10 @@ opensrp-server-core ${opensrp.core.version} - - junit - junit - 4.11 - test - - - org.mockito - mockito-all - 1.8.5 - - - org.powermock - powermock-module-junit4 - 1.4.9 - test - - - org.powermock - powermock-api-mockito - 1.4.9 - test + + org.smartregister + opensrp-server-common + ${opensrp.common.version} org.javassist @@ -147,6 +131,43 @@ okhttp 2.7.5 + + + junit + junit + 4.13 + test + + + org.powermock + powermock-module-junit4 + ${powermock.version} + test + + + org.powermock + powermock-module-junit4-rule + ${powermock.version} + test + + + org.powermock + powermock-api-mockito2 + ${powermock.version} + test + + + org.powermock + powermock-classloading-xstream + ${powermock.version} + test + + + org.mockito + mockito-core + 3.3.0 + test + org.springframework spring-test diff --git a/src/main/java/org/opensrp/connector/MultipartHttpUtil.java b/src/main/java/org/opensrp/connector/MultipartHttpUtil.java index d157835..20ad35f 100644 --- a/src/main/java/org/opensrp/connector/MultipartHttpUtil.java +++ b/src/main/java/org/opensrp/connector/MultipartHttpUtil.java @@ -9,14 +9,13 @@ import java.net.URL; import org.apache.commons.codec.binary.Base64; -import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.opensrp.common.util.HttpResponse; import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; -import com.mysql.jdbc.StringUtils; - /** * The class is a gateway to connect to external system via http for reading and writing data. All * get and post requests use Basic Authorization. @@ -51,7 +50,7 @@ public static HttpResponse post(String url, String payload, String data, String if (writer != null) writer.close(); - return new HttpResponse(con.getResponseCode() == HttpStatus.SC_OK, IOUtils.toString(con.getInputStream())); + return new HttpResponse(con.getResponseCode() == HttpStatus.OK.value(), IOUtils.toString(con.getInputStream())); } catch (Exception e) { @@ -71,7 +70,7 @@ public static HttpResponse post(String url, String payload, String data, String public static HttpResponse get(String url, String payload, String username, String password) { try { HttpURLConnection con = makeConnection(url, payload, HttpMethod.GET, true, username, password); - return new HttpResponse(con.getResponseCode() == HttpStatus.SC_OK, IOUtils.toString(con.getInputStream())); + return new HttpResponse(con.getResponseCode() == HttpStatus.OK.value(), IOUtils.toString(con.getInputStream())); } catch (FileNotFoundException e) { return new HttpResponse(true, ""); @@ -91,7 +90,7 @@ static HttpURLConnection makeConnection(String url, String payload, HttpMethod r if (url.endsWith("/")) { url = url.substring(0, url.lastIndexOf("/")); } - url = (url + (StringUtils.isEmptyOrWhitespaceOnly(payload) ? "" : ("?" + payload))).replaceAll(" ", "%20"); + url = (url + (StringUtils.isBlank(payload) ? "" : ("?" + payload))).replaceAll(" ", "%20"); URL urlo = new URL(url); HttpURLConnection conn = (HttpURLConnection) urlo.openConnection(); conn.setRequestProperty("Accept-Charset", charset); diff --git a/src/main/java/org/opensrp/connector/atomfeed/AllFailedEventsCouchImpl.java b/src/main/java/org/opensrp/connector/atomfeed/AllFailedEventsCouchImpl.java index f6dd730..a92d6ee 100644 --- a/src/main/java/org/opensrp/connector/atomfeed/AllFailedEventsCouchImpl.java +++ b/src/main/java/org/opensrp/connector/atomfeed/AllFailedEventsCouchImpl.java @@ -9,8 +9,8 @@ import org.ict4h.atomfeed.client.domain.FailedEvent; import org.ict4h.atomfeed.client.domain.FailedEventRetryLog; import org.ict4h.atomfeed.client.repository.AllFailedEvents; -import org.motechproject.dao.MotechBaseRepository; import org.opensrp.connector.openmrs.constants.OpenmrsConstants; +import org.opensrp.repository.couch.BaseRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; @@ -91,7 +91,7 @@ public void insert(FailedEventRetryLog failedEventRetryLog) { } - public class AllFailedEventInnerRepository extends MotechBaseRepository { + public class AllFailedEventInnerRepository extends BaseRepository { AllFailedEventInnerRepository(CouchDbConnector db) { super(org.opensrp.connector.atomfeed.domain.FailedEvent.class, db); diff --git a/src/main/java/org/opensrp/connector/atomfeed/AllMarkersCouchImpl.java b/src/main/java/org/opensrp/connector/atomfeed/AllMarkersCouchImpl.java index e409125..2dc1be5 100644 --- a/src/main/java/org/opensrp/connector/atomfeed/AllMarkersCouchImpl.java +++ b/src/main/java/org/opensrp/connector/atomfeed/AllMarkersCouchImpl.java @@ -4,22 +4,21 @@ import java.net.URISyntaxException; import java.util.List; +import org.apache.commons.lang3.StringUtils; import org.ektorp.CouchDbConnector; import org.ektorp.support.GenerateView; import org.ektorp.support.View; import org.ict4h.atomfeed.client.repository.AllMarkers; -import org.motechproject.dao.MotechBaseRepository; import org.opensrp.connector.atomfeed.domain.Marker; import org.opensrp.connector.openmrs.constants.OpenmrsConstants; +import org.opensrp.repository.couch.BaseRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Repository; -import com.mysql.jdbc.StringUtils; - @Repository -public class AllMarkersCouchImpl extends MotechBaseRepository implements AllMarkers { +public class AllMarkersCouchImpl extends BaseRepository implements AllMarkers { private CouchDbConnector db; @@ -33,7 +32,7 @@ public AllMarkersCouchImpl(@Value("#{opensrp['couchdb.atomfeed-db.revision-limit @GenerateView public Marker findByfeedUri(String feedUri) { - if (StringUtils.isEmptyOrWhitespaceOnly(feedUri)) + if (StringUtils.isBlank(feedUri)) return null; List ol = queryView("by_feedUri", feedUri); if (ol == null || ol.isEmpty()) { diff --git a/src/main/java/org/opensrp/connector/atomfeed/domain/FailedEvent.java b/src/main/java/org/opensrp/connector/atomfeed/domain/FailedEvent.java index bd089c1..bb94858 100644 --- a/src/main/java/org/opensrp/connector/atomfeed/domain/FailedEvent.java +++ b/src/main/java/org/opensrp/connector/atomfeed/domain/FailedEvent.java @@ -4,10 +4,10 @@ import org.ektorp.support.TypeDiscriminator; import org.ict4h.atomfeed.client.domain.Event; -import org.motechproject.model.MotechBaseDataObject; +import org.opensrp.domain.BaseDataObject; @TypeDiscriminator("type == 'FailedEvent'") -public class FailedEvent extends MotechBaseDataObject { +public class FailedEvent extends BaseDataObject { private long failedAt; diff --git a/src/main/java/org/opensrp/connector/atomfeed/domain/Marker.java b/src/main/java/org/opensrp/connector/atomfeed/domain/Marker.java index 00b3c24..1d0d7d5 100644 --- a/src/main/java/org/opensrp/connector/atomfeed/domain/Marker.java +++ b/src/main/java/org/opensrp/connector/atomfeed/domain/Marker.java @@ -4,10 +4,10 @@ import java.net.URISyntaxException; import org.ektorp.support.TypeDiscriminator; -import org.motechproject.model.MotechBaseDataObject; +import org.opensrp.domain.BaseDataObject; @TypeDiscriminator("doc.type == 'Marker'") -public class Marker extends MotechBaseDataObject { +public class Marker extends BaseDataObject { private String feedUri; diff --git a/src/main/java/org/opensrp/connector/dhis2/Dhis2HttpUtils.java b/src/main/java/org/opensrp/connector/dhis2/Dhis2HttpUtils.java index 34f85b3..b74d553 100644 --- a/src/main/java/org/opensrp/connector/dhis2/Dhis2HttpUtils.java +++ b/src/main/java/org/opensrp/connector/dhis2/Dhis2HttpUtils.java @@ -9,17 +9,16 @@ import java.net.URL; import org.apache.commons.codec.binary.Base64; -import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.lang3.StringUtils; import org.json.JSONException; import org.json.JSONObject; import org.opensrp.common.util.HttpResponse; import org.opensrp.common.util.HttpUtil; import org.opensrp.common.util.HttpUtil.AuthType; import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; -import com.mysql.jdbc.StringUtils; - @Component public class Dhis2HttpUtils extends DHIS2Service { @@ -37,7 +36,7 @@ public static HttpResponse post(String url, String payload, String data, String if (url.endsWith("/")) { url = url.substring(0, url.lastIndexOf("/")); } - url = (url + (StringUtils.isEmptyOrWhitespaceOnly(payload) ? "" : ("?" + payload))).replaceAll(" ", "%20"); + url = (url + (StringUtils.isBlank(payload) ? "" : ("?" + payload))).replaceAll(" ", "%20"); try { URL urlo = new URL(url); HttpURLConnection con = (HttpURLConnection) urlo.openConnection(); @@ -63,7 +62,7 @@ public static HttpResponse post(String url, String payload, String data, String while ((output = br.readLine()) != null) { sb.append(output); } - return new HttpResponse(con.getResponseCode() == HttpStatus.SC_OK, sb.toString()); + return new HttpResponse(con.getResponseCode() == HttpStatus.OK.value(), sb.toString()); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e); diff --git a/src/main/java/org/opensrp/connector/openmrs/service/EncounterService.java b/src/main/java/org/opensrp/connector/openmrs/service/EncounterService.java index 2ab2591..06a2cdb 100644 --- a/src/main/java/org/opensrp/connector/openmrs/service/EncounterService.java +++ b/src/main/java/org/opensrp/connector/openmrs/service/EncounterService.java @@ -5,6 +5,7 @@ import java.util.List; import java.util.Map; +import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; import org.json.JSONArray; import org.json.JSONException; @@ -23,8 +24,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import com.mysql.jdbc.StringUtils; - @Service public class EncounterService extends OpenmrsService { private static Logger logger = LoggerFactory.getLogger(EncounterService.class.toString()); @@ -141,7 +140,7 @@ public JSONObject createEncounter(Event event) throws JSONException { if (observationLists != null) for (Obs obs : observationLists) { - if (!StringUtils.isEmptyOrWhitespaceOnly(obs.getFieldCode()) + if (!StringUtils.isBlank(obs.getFieldCode()) && (obs.getFieldType() == null || obs.getFieldType().equalsIgnoreCase(ConnectorConstants.CONCEPT))) { // skipping empty obs and fields that don't have concepts if no parent simply make it root obs if (ConnectorConstants.CONCEPT.equals(obs.getFieldType())) { @@ -243,7 +242,7 @@ public JSONObject buildUpdateEncounter(Event event) throws JSONException { if (obsList != null) for (Obs obs : obsList) { - if (!StringUtils.isEmptyOrWhitespaceOnly(obs.getFieldCode()) + if (!StringUtils.isBlank(obs.getFieldCode()) && (obs.getFieldType() == null || obs.getFieldType().equalsIgnoreCase(ConnectorConstants.CONCEPT))) { //skipping empty obs if no parent simply make it root obs if (obs.getFieldType().equals(ConnectorConstants.CONCEPT) && obs.getFormSubmissionField().equals(ConnectorConstants.BIRTH_FACILITY_NAME) @@ -286,7 +285,7 @@ public JSONObject buildUpdateEncounter(Event event) throws JSONException { private void generateObs(Map parent, Map parentChild, Obs obs, List obsList) { try { - if (StringUtils.isEmptyOrWhitespaceOnly(obs.getParentCode())) { + if (StringUtils.isBlank(obs.getParentCode())) { parent.put(obs.getFieldCode(), convertObsToJson(obs)); } else { //find parent obs if not found search and fill or create one @@ -311,7 +310,7 @@ private void generateObs(Map parent, Map p } public JSONObject updateEncounter(Event event) throws JSONException { - if (StringUtils.isEmptyOrWhitespaceOnly(event.getIdentifier(OPENMRS_UUID_IDENTIFIER_TYPE))) { + if (StringUtils.isBlank(event.getIdentifier(OPENMRS_UUID_IDENTIFIER_TYPE))) { throw new IllegalArgumentException("Encounter was never pushed to OpenMRS as " + OPENMRS_UUID_IDENTIFIER_TYPE + " is empty. Consider creating a new one"); } diff --git a/src/main/java/org/opensrp/connector/openmrs/service/HouseholdService.java b/src/main/java/org/opensrp/connector/openmrs/service/HouseholdService.java index 23fe1d0..4114e17 100644 --- a/src/main/java/org/opensrp/connector/openmrs/service/HouseholdService.java +++ b/src/main/java/org/opensrp/connector/openmrs/service/HouseholdService.java @@ -1,5 +1,6 @@ package org.opensrp.connector.openmrs.service; +import org.apache.commons.lang3.StringUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -9,8 +10,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import com.mysql.jdbc.StringUtils; - @Service public class HouseholdService extends OpenmrsService { @@ -93,7 +92,7 @@ public JSONObject saveHH(OpenmrsHouseHold household, boolean ignoreExisting) thr JSONArray relationships = new JSONArray(); encounters.put(hhe); for (HouseholdMember m : household.getMembers()) { - if (StringUtils.isEmptyOrWhitespaceOnly(m.getClient().getFirstName()) + if (StringUtils.isBlank(m.getClient().getFirstName()) && m.getClient().getIdentifiers().size() < 2) {//we need to ignore uuid of entity //skip Data push for now } else { diff --git a/src/main/java/org/opensrp/connector/openmrs/service/OpenmrsLocationService.java b/src/main/java/org/opensrp/connector/openmrs/service/OpenmrsLocationService.java index 2840f78..95ca3f5 100644 --- a/src/main/java/org/opensrp/connector/openmrs/service/OpenmrsLocationService.java +++ b/src/main/java/org/opensrp/connector/openmrs/service/OpenmrsLocationService.java @@ -6,6 +6,7 @@ import java.util.Set; import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.ReflectionToStringBuilder; import org.json.JSONArray; import org.json.JSONException; @@ -18,7 +19,6 @@ import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; -import com.mysql.jdbc.StringUtils; import com.squareup.okhttp.Call; import com.squareup.okhttp.Credentials; import com.squareup.okhttp.OkHttpClient; @@ -48,7 +48,7 @@ private String getURL(String url) { try { response = call.execute(); String responseBody=response.body().string(); - if (!StringUtils.isEmptyOrWhitespaceOnly(responseBody)) { + if (!StringUtils.isBlank(responseBody)) { return responseBody; } } @@ -62,7 +62,7 @@ private String getURL(String url) { public Location getLocation(String locationIdOrName) throws JSONException { String response = getURL(HttpUtil.removeEndingSlash(OPENMRS_BASE_URL) + "/" + LOCATION_URL + "/" + (locationIdOrName.replaceAll(" ", "%20")) + "?v=full"); - if (!StringUtils.isEmptyOrWhitespaceOnly(response) && (new JSONObject(response).has("uuid"))) { + if (!StringUtils.isBlank(response) && (new JSONObject(response).has("uuid"))) { return makeLocation(response); } diff --git a/src/main/java/org/opensrp/connector/openmrs/service/PatientService.java b/src/main/java/org/opensrp/connector/openmrs/service/PatientService.java index af504e5..2af1a91 100644 --- a/src/main/java/org/opensrp/connector/openmrs/service/PatientService.java +++ b/src/main/java/org/opensrp/connector/openmrs/service/PatientService.java @@ -8,6 +8,7 @@ import java.util.Map.Entry; import java.util.UUID; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.joda.time.DateTime; import org.json.JSONArray; @@ -38,8 +39,6 @@ import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; -import com.mysql.jdbc.StringUtils; - @Service public class PatientService extends OpenmrsService { @@ -369,7 +368,7 @@ public void processClients(List cl, JSONArray patientsJsonArray, } else { JSONObject patientJson = createPatient(c); patient = patientJson;//only for test code purpose - if (patientJson != null && !StringUtils.isNullOrEmpty(patientJson.optString("uuid"))) { + if (patientJson != null && !StringUtils.isEmpty(patientJson.optString("uuid"))) { c.addIdentifier(PatientService.OPENMRS_UUID_IDENTIFIER_TYPE, patientJson.getString("uuid")); clientService.addorUpdate(c, false); } @@ -415,7 +414,7 @@ public JSONObject convertBaseEntityToOpenmrsJson(Client be, boolean update) thro per.put("deathDate", OPENMRS_DATE.format(be.getDeathdate().toDate())); } - String fn = be.getFirstName() == null || StringUtils.isEmptyOrWhitespaceOnly(be.getFirstName()) ? "-" + String fn = be.getFirstName() == null || StringUtils.isBlank(be.getFirstName()) ? "-" : be.getFirstName(); if (!fn.equals("-")) { fn = fn.replaceAll("[^A-Za-z0-9\\s]+", ""); @@ -592,7 +591,7 @@ public JSONArray convertAddressesToOpenmrsJson(Client client, String clientAddre public JSONObject createPatient(Client c) throws JSONException { String personUUIDString = createPerson(c).optString("uuid"); - if(!StringUtils.isNullOrEmpty(personUUIDString)) { + if(!StringUtils.isEmpty(personUUIDString)) { JSONObject p = new JSONObject(); p.put("person", personUUIDString); @@ -927,7 +926,7 @@ private Object convertToOpenmrsString(Object o) { public String fetchLocationByUUID(String locationUUID) { try { - if (locationUUID == null || StringUtils.isEmptyOrWhitespaceOnly(locationUUID) + if (locationUUID == null || StringUtils.isBlank(locationUUID) || locationUUID.equalsIgnoreCase("Other")) { return locationUUID; } @@ -954,7 +953,7 @@ private String fetchIdentifierTypeUUID(String identifierType) throws JSONExcepti private String cleanIdentifierWithCheckDigit(String rawId) { - if (StringUtils.isNullOrEmpty(rawId)) { + if (StringUtils.isEmpty(rawId)) { return rawId; } diff --git a/src/test/java/org/opensrp/connector/dhis2/DHIS2DatasetPushTest.java b/src/test/java/org/opensrp/connector/dhis2/DHIS2DatasetPushTest.java index ad6e9c0..15a0034 100644 --- a/src/test/java/org/opensrp/connector/dhis2/DHIS2DatasetPushTest.java +++ b/src/test/java/org/opensrp/connector/dhis2/DHIS2DatasetPushTest.java @@ -1,12 +1,10 @@ package org.opensrp.connector.dhis2; import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.anyString; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.initMocks; import java.io.IOException; import java.util.ArrayList; @@ -17,8 +15,11 @@ import org.json.JSONException; import org.json.JSONObject; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; import org.opensrp.api.domain.Location; import org.opensrp.connector.openmrs.service.OpenmrsLocationService; import org.opensrp.connector.openmrs.service.TestResourceLoader; @@ -29,6 +30,9 @@ public class DHIS2DatasetPushTest extends TestResourceLoader { + @Rule + public MockitoRule rule = MockitoJUnit.rule(); + @Mock DHIS2DatasetPush mockDatasetPush; @@ -48,9 +52,9 @@ public class DHIS2DatasetPushTest extends TestResourceLoader { String hia2ReportId = "XQDrq0oQEyN"; - public DHIS2DatasetPushTest() throws IOException { + + public DHIS2DatasetPushTest() throws IOException { super(); - initMocks(this); } @Before @@ -180,8 +184,8 @@ public void testPushToDHIS2() throws JSONException { // Customized mock api responses setup when(dhis2HttpUtils.post(anyString(), anyString(), anyString())).thenReturn(apiResponse); - when(mockReportService.findByServerVersion(anyInt())).thenReturn(reports); - + when(mockReportService.findByServerVersion(0)).thenReturn(reports); + dhis2DatasetPush.dhis2HttpUtils = dhis2HttpUtils; dhis2DatasetPush.openmrsLocationService = this.mockOpenmrsLocationService; dhis2DatasetPush.config = mockConfig; diff --git a/src/test/java/org/opensrp/connector/openmrs/service/EncounterServiceTest.java b/src/test/java/org/opensrp/connector/openmrs/service/EncounterServiceTest.java index 64e5c28..2e983c1 100644 --- a/src/test/java/org/opensrp/connector/openmrs/service/EncounterServiceTest.java +++ b/src/test/java/org/opensrp/connector/openmrs/service/EncounterServiceTest.java @@ -1,7 +1,16 @@ package org.opensrp.connector.openmrs.service; -import org.hamcrest.Matchers; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.eq; +import static org.opensrp.common.AllConstants.Event.OPENMRS_UUID_IDENTIFIER_TYPE; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + import org.joda.time.DateTime; import org.json.JSONArray; import org.json.JSONException; @@ -13,43 +22,18 @@ import org.opensrp.common.util.HttpResponse; import org.opensrp.common.util.HttpUtil; import org.opensrp.connector.ConnectorTestConstants; -import org.opensrp.connector.openmrs.constants.OpenmrsHouseHold; -import org.opensrp.domain.Client; import org.opensrp.domain.Event; import org.opensrp.domain.Obs; -import org.opensrp.form.domain.FormSubmission; -import org.opensrp.form.service.FormAttributeParser; -import org.opensrp.service.formSubmission.FormEntityConverter; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import java.io.IOException; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.startsWith; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.eq; -import static org.opensrp.common.AllConstants.Event.OPENMRS_UUID_IDENTIFIER_TYPE; - @RunWith (PowerMockRunner.class) @PrepareForTest ({HttpUtil.class}) @PowerMockIgnore ({"org.apache.http.conn.ssl.*", "javax.net.ssl.*"}) public class EncounterServiceTest extends TestResourceLoader { private EncounterService encounterService1; - private FormEntityConverter formEntityConverter; private HouseholdService householdService; public EncounterServiceTest() throws IOException { @@ -67,342 +51,8 @@ public void setUp() throws IOException { householdService = new HouseholdService(openmrsOpenmrsUrl, openmrsUsername, openmrsPassword); householdService.setPatientService(patientService1); householdService.setEncounterService(encounterService1); - FormAttributeParser fam = new FormAttributeParser(formDirPath); - formEntityConverter = new FormEntityConverter(fam); - } - - @Test - public void testEncounter() throws JSONException, IOException { - FormSubmission fs = getFormSubmissionFor("basic_reg"); - - Client client = formEntityConverter.getClientFromFormSubmission(fs); - assertEquals(client.getBaseEntityId(), "b716d938-1aea-40ae-a081-9ddddddcccc9"); - assertEquals(client.getFirstName(), "test woman_name"); - assertEquals(client.getGender(), "FEMALE"); - assertEquals(client.getAddresses().get(0).getAddressType(), "birthplace"); - assertEquals(client.getAddresses().get(1).getAddressType(), "usual_residence"); - assertEquals(client.getAddresses().get(2).getAddressType(), "previous_residence"); - assertEquals(client.getAddresses().get(3).getAddressType(), "deathplace"); - assertTrue(client.getAttributes().isEmpty()); - - Event event = formEntityConverter.getEventFromFormSubmission(fs); - assertEquals(event.getEventType(), "patient_register"); - assertEquals(event.getEventDate(), new DateTime(new DateTime("2015-02-01"))); - assertEquals(event.getLocationId(), "unknown location"); - - if (pushToOpenmrsForTest) { - JSONObject en = encounterService1.createEncounter(event); - System.out.println(en); - } - } - - @Test - public void shouldHandleSubForm() throws IOException { - FormSubmission fs = getFormSubmissionFor("new_household_registration", 1); - - Client c = formEntityConverter.getClientFromFormSubmission(fs); - assertEquals(c.getBaseEntityId(), "a3f2abf4-2699-4761-819a-cea739224164"); - assertEquals(c.getFirstName(), "test"); - assertEquals(c.getGender(), "male"); - assertEquals(c.getBirthdate(), new DateTime("1900-01-01")); - assertEquals(c.getAddresses().get(0).getAddressField("landmark"), "nothing"); - assertEquals(c.getAddresses().get(0).getAddressType(), "usual_residence"); - assertEquals(c.getIdentifiers().get("GOB HHID"), "1234"); - assertEquals(c.getIdentifiers().get("JiVitA HHID"), "1234"); - - Event e = formEntityConverter.getEventFromFormSubmission(fs); - assertEquals(e.getBaseEntityId(), "a3f2abf4-2699-4761-819a-cea739224164"); - assertEquals(e.getEventDate(), new DateTime(new DateTime("2015-05-07"))); - assertEquals(e.getLocationId(), "KUPTALA"); - assertEquals(e.getFormSubmissionId(), "88c0e824-10b4-44c2-9429-754b8d823776"); - - assertEquals(e.getObs().get(0).getFieldCode(), "160753AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); - assertEquals(e.getObs().get(0).getFormSubmissionField(), "FWNHREGDATE"); - assertEquals(e.getObs().get(0).getValue(), "2015-05-07"); - - assertEquals(e.getObs().get(1).getFieldCode(), "5611AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); - assertEquals(e.getObs().get(1).getFormSubmissionField(), "FWNHHMBRNUM"); - assertEquals(e.getObs().get(1).getValue(), "2"); - - Map> dc = formEntityConverter.getDependentClientsFromFormSubmission(fs); - for (String id : dc.keySet()) { - Client cl = (Client) dc.get(id).get("client"); - Event ev = (Event) dc.get(id).get("event"); - assertEquals(cl.getBaseEntityId(), id); - assertEquals(ev.getBaseEntityId(), id); - } - } - - @Test - public void shouldHandleEmptyRepeatGroup() throws IOException { - FormSubmission fs = getFormSubmissionFor("new_household_registration", 5); - - Client c = formEntityConverter.getClientFromFormSubmission(fs); - assertEquals(c.getBaseEntityId(), "a3f2abf4-2699-4761-819a-cea739224164"); - assertEquals(c.getFirstName(), "test"); - assertEquals(c.getGender(), "male"); - assertEquals(c.getBirthdate(), new DateTime("1900-01-01")); - assertEquals(c.getAddresses().get(0).getAddressField("landmark"), "nothing"); - assertEquals(c.getAddresses().get(0).getAddressType(), "usual_residence"); - assertEquals(c.getIdentifiers().get("GOB HHID"), "1234"); - assertEquals(c.getIdentifiers().get("JiVitA HHID"), "1234"); - - Event e = formEntityConverter.getEventFromFormSubmission(fs); - assertEquals(e.getBaseEntityId(), "a3f2abf4-2699-4761-819a-cea739224164"); - assertEquals(e.getEventDate(), new DateTime(new DateTime("2015-05-07"))); - assertEquals(e.getLocationId(), "KUPTALA"); - assertEquals(e.getFormSubmissionId(), "88c0e824-10b4-44c2-9429-754b8d823776"); - - assertEquals(e.getObs().get(0).getFieldCode(), "160753AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); - assertEquals(e.getObs().get(0).getFormSubmissionField(), "FWNHREGDATE"); - assertEquals(e.getObs().get(0).getValue(), "2015-05-07"); - - assertEquals(e.getObs().get(1).getFieldCode(), "5611AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"); - assertEquals(e.getObs().get(1).getFormSubmissionField(), "FWNHHMBRNUM"); - assertEquals(e.getObs().get(1).getValue(), "2"); - - Map> dc = formEntityConverter.getDependentClientsFromFormSubmission(fs); - assertTrue(dc.isEmpty()); - } - - @Test - public void shouldGetBirthdateNotEstimatedForMainAndApproxForRepeatGroup() - throws IOException { - FormSubmission fs = getFormSubmissionFor("new_household_registration", 7); - - Client c = formEntityConverter.getClientFromFormSubmission(fs); - assertEquals(c.getBirthdate(), new DateTime("1900-01-01")); - assertTrue(c.getBirthdateApprox()); - - Map> dc = formEntityConverter.getDependentClientsFromFormSubmission(fs); - for (String id : dc.keySet()) { - Client cl = (Client) dc.get(id).get("client"); - assertEquals(cl.getBirthdate(), new DateTime("2000-05-07")); - assertFalse(cl.getBirthdateApprox()); - } - } - - @Test - public void shouldGetBirthdateNotEstimatedForMainAndRepeatGroupIfNotSpecified() - throws IOException { - FormSubmission fs = getFormSubmissionFor("new_household_registration", 8); - - Client c = formEntityConverter.getClientFromFormSubmission(fs); - assertEquals(c.getBirthdate(), new DateTime("1900-01-01")); - assertFalse(c.getBirthdateApprox()); - - Map> dc = formEntityConverter.getDependentClientsFromFormSubmission(fs); - for (String id : dc.keySet()) { - Client cl = (Client) dc.get(id).get("client"); - assertEquals(cl.getBirthdate(), new DateTime("2000-05-07")); - assertFalse(cl.getBirthdateApprox()); - } - } - - @SuppressWarnings ("unchecked") - @Test - public void shouldGetDataSpecifiedInGroupInsideSubform() throws IOException, ParseException, JSONException { - FormSubmission fs = getFormSubmissionFor("new_household_registration_with_grouped_subform_data", 1); - - Client c = formEntityConverter.getClientFromFormSubmission(fs); - assertEquals(c.getBirthdate(), new DateTime("1900-01-01")); - assertFalse(c.getBirthdateApprox()); - assertThat(c.getAttributes(), Matchers.hasEntry(equalTo("GoB_HHID"), equalTo((Object) "2322"))); - assertThat(c.getAttributes(), Matchers.hasEntry(equalTo("JiVitA_HHID"), equalTo((Object) "9889"))); - - Event e = formEntityConverter.getEventFromFormSubmission(fs); - assertEquals(e.getBaseEntityId(), c.getBaseEntityId()); - assertEquals(e.getEventType(), "New Household Registration"); - assertEquals(e.getEventDate(), new DateTime(new SimpleDateFormat("yyyy-M-dd").parse("2015-10-11"))); - assertEquals(e.getLocationId(), "2fc43738-ace5-g961-8e8f-ab7dg0e5bc63"); - - assertThat(e.getObs(), - Matchers.hasItem(Matchers.allOf( - Matchers.hasProperty("fieldCode", equalTo("5611AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")), - Matchers.hasProperty("value", equalTo("23")), - Matchers.hasProperty("formSubmissionField", equalTo("FWNHHMBRNUM"))))); - - Map> dc = formEntityConverter.getDependentClientsFromFormSubmission(fs); - for (String id : dc.keySet()) { - Client cl = (Client) dc.get(id).get("client"); - assertEquals(cl.getBirthdate(), new DateTime("1988-10-08")); - assertFalse(cl.getBirthdateApprox()); - assertEquals(cl.getFirstName(), "jackfruit"); - assertEquals(cl.getAddresses().get(0).getCountry(), "Bangladesh"); - assertEquals(cl.getAddresses().get(0).getAddressType(), "usual_residence"); - assertEquals(cl.getAddresses().get(0).getStateProvince(), "RANGPUR"); - assertThat(cl.getIdentifiers(), Matchers.hasEntry(equalTo("NID"), equalTo("7675788777775"))); - assertThat(cl.getIdentifiers(), - Matchers.hasEntry(equalTo("Birth Registration ID"), equalTo("98899998888888888"))); - assertThat(cl.getAttributes(), - Matchers.hasEntry(equalTo("GoB_HHID"), equalTo((Object) "2322"))); - assertThat(cl.getAttributes(), - Matchers.hasEntry(equalTo("JiVitA_HHID"), equalTo((Object) "9889"))); - - Event ev = (Event) dc.get(id).get("event"); - assertEquals(ev.getBaseEntityId(), cl.getBaseEntityId()); - assertEquals(ev.getEventType(), "New Woman Registration"); - assertEquals(ev.getEventDate(), new DateTime(new SimpleDateFormat("yyyy-M-dd").parse("2015-10-11"))); - assertEquals(ev.getLocationId(), "2fc43738-ace5-g961-8e8f-ab7dg0e5bc63"); - - assertThat(ev.getObs(), - Matchers.hasItem(Matchers.allOf( - Matchers.hasProperty("fieldCode", equalTo("161135AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")), - Matchers.hasProperty("value", equalTo("zoom")), - Matchers.hasProperty("formSubmissionField", equalTo("FWHUSNAME"))))); - assertThat(ev.getObs(), - Matchers.hasItem(Matchers.allOf( - Matchers.hasProperty("fieldCode", equalTo("163087AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")), - Matchers.hasProperty("values", - hasItems(equalTo("163084AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"), - equalTo("163083AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"))), - Matchers.hasProperty("formSubmissionField", equalTo("FWWOMANYID"))))); - } - - if (pushToOpenmrsForTest) { - OpenmrsHouseHold hh = new OpenmrsHouseHold(c, e); - for (Map cm : dc.values()) { - hh.addHHMember((Client) cm.get("client"), (Event) cm.get("event")); - } - - householdService.saveHH(hh, true); - } - } - - @SuppressWarnings ("unchecked") - @Test - public void shouldGetDataSpecifiedInMultiselect() throws IOException { - FormSubmission formSubmission = getFormSubmissionFor("new_household_registration_with_grouped_subform_data", 1); - Map> dependentClientsFromFormSubmission = formEntityConverter.getDependentClientsFromFormSubmission(formSubmission); - for (String id : dependentClientsFromFormSubmission.keySet()) { - Event event = (Event) dependentClientsFromFormSubmission.get(id).get("event"); - - assertThat(event.getObs(), - Matchers.hasItem(Matchers.allOf( - Matchers.hasProperty("fieldCode", equalTo("163087AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")), - Matchers.hasProperty("values", - hasItems(equalTo("163084AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"), - equalTo("163083AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"))), - Matchers.hasProperty("formSubmissionField", equalTo("FWWOMANYID")), - Matchers.hasProperty("fieldType", equalTo("concept")), - Matchers.hasProperty("fieldDataType", startsWith("select all"))))); - } } - @SuppressWarnings ("unchecked") - @Test - public void shouldHandleTTEnrollmentform() throws IOException { - FormSubmission fs = getFormSubmissionFor("woman_enrollment"); - - Client c = formEntityConverter.getClientFromFormSubmission(fs); - assertEquals(c.getBaseEntityId(), "69995674-bb29-4985-967a-fec8d372a475"); - assertEquals(c.getFirstName(), "barsaat"); - assertEquals(c.getGender(), "female"); - assertEquals(c.getBirthdate(), new DateTime("1979-04-05")); - assertEquals(c.getAddresses().get(0).getAddressField("landmark"), "nishaani"); - assertEquals(c.getAddresses().get(0).getStateProvince(), "sindh"); - assertEquals(c.getAddresses().get(0).getCityVillage(), "karachi"); - assertEquals(c.getAddresses().get(0).getTown(), "liaquatabad"); - assertEquals(c.getAddresses().get(0).getSubTown(), "sharifabad"); - assertEquals(c.getAddresses().get(0).getAddressField("house"), "6h"); - assertEquals(c.getIdentifiers().get("Program Client ID"), "14608844"); - assertEquals(c.getAttributes().get("EPI Card Number"), "20160003"); - - Event e = formEntityConverter.getEventFromFormSubmission(fs); - assertEquals(e.getBaseEntityId(), "69995674-bb29-4985-967a-fec8d372a475"); - assertEquals(e.getEventDate(), new DateTime(new DateTime("2016-04-05"))); - assertEquals(e.getLocationId(), "Homeopathic Center"); - assertEquals(e.getFormSubmissionId(), "de408c93-2ec5-40bc-a957-eaf375583e27"); - assertEquals(e.getEntityType(), "pkwoman"); - assertEquals(e.getEventType(), "Woman TT enrollment"); - assertEquals(e.getProviderId(), "demotest"); - - assertThat(e.getObs(), - Matchers.hasItem(Matchers.allOf( - Matchers.hasProperty("fieldCode", equalTo("154384AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")), - Matchers.hasProperty("values", hasItems(equalTo("37"))), - Matchers.hasProperty("formSubmissionField", equalTo("calc_age_confirm")), - Matchers.hasProperty("fieldType", equalTo("concept")), - Matchers.hasProperty("fieldDataType", startsWith("calculate")), - Matchers.hasProperty("effectiveDatetime", equalTo(e.getEventDate()))))); - - assertThat(e.getObs(), - Matchers.hasItem(Matchers.allOf( - Matchers.hasProperty("fieldCode", equalTo("163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")), - Matchers.hasProperty("values", hasItems(equalTo("2016-04-05 16:21:32"))), - Matchers.hasProperty("formSubmissionField", equalTo("start")), - Matchers.hasProperty("fieldType", equalTo("concept")), - Matchers.hasProperty("fieldDataType", startsWith("start")), - Matchers.hasProperty("effectiveDatetime", equalTo(e.getEventDate()))))); - - assertThat(e.getObs(), - Matchers.hasItem(Matchers.allOf( - Matchers.hasProperty("fieldCode", equalTo("163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")), - Matchers.hasProperty("values", hasItems(equalTo("2016-04-05 16:23:59"))), - Matchers.hasProperty("formSubmissionField", equalTo("end")), - Matchers.hasProperty("fieldType", equalTo("concept")), - Matchers.hasProperty("fieldDataType", startsWith("end")), - Matchers.hasProperty("effectiveDatetime", equalTo(e.getEventDate()))))); - - } - - @SuppressWarnings ("unchecked") - @Test - public void shouldHandleChildVaccinationEnrollmentform() throws IOException { - FormSubmission fs = getFormSubmissionFor("child_enrollment"); - - Client c = formEntityConverter.getClientFromFormSubmission(fs); - assertEquals(c.getBaseEntityId(), "ad653225-6bed-48d3-8e5d-741d3d50d61a"); - assertEquals(c.getFirstName(), "aase"); - assertEquals(c.getLastName(), "zeest"); - assertEquals(c.getGender(), "male"); - assertEquals(c.getBirthdate(), new DateTime("2016-01-03")); - assertEquals(c.getAddresses().get(0).getAddressField("landmark"), "nishaani"); - assertEquals(c.getAddresses().get(0).getStateProvince(), "sindh"); - assertEquals(c.getAddresses().get(0).getCityVillage(), "karachi"); - assertEquals(c.getAddresses().get(0).getTown(), "liaquatabad"); - assertEquals(c.getAddresses().get(0).getSubTown(), "mujahid_colony"); - assertEquals(c.getAddresses().get(0).getAddressField("house"), "hi65"); - assertEquals(c.getIdentifiers().get("Program Client ID"), "98120722"); - assertEquals(c.getAttributes().get("EPI Card Number"), "20160009"); - - Event e = formEntityConverter.getEventFromFormSubmission(fs); - assertEquals(e.getBaseEntityId(), "ad653225-6bed-48d3-8e5d-741d3d50d61a"); - assertEquals(e.getEventDate(), new DateTime(new DateTime("2016-03-05"))); - assertEquals(e.getLocationId(), "Homeopathic Center"); - assertEquals(e.getFormSubmissionId(), "8524f6b8-441a-4769-aa74-03e1dde0901a"); - assertEquals(e.getEntityType(), "pkchild"); - assertEquals(e.getEventType(), "Child Vaccination Enrollment"); - assertEquals(e.getProviderId(), "demotest"); - - assertThat(e.getObs(), - Matchers.hasItem(Matchers.allOf( - Matchers.hasProperty("fieldCode", equalTo("154384AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")), - Matchers.hasProperty("values", hasItems(equalTo("2"))), - Matchers.hasProperty("formSubmissionField", equalTo("calc_age_confirm")), - Matchers.hasProperty("fieldType", equalTo("concept")), - Matchers.hasProperty("fieldDataType", startsWith("calculate")), - Matchers.hasProperty("effectiveDatetime", equalTo(e.getEventDate()))))); - - assertThat(e.getObs(), - Matchers.hasItem(Matchers.allOf( - Matchers.hasProperty("fieldCode", equalTo("163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")), - Matchers.hasProperty("values", hasItems(equalTo("2016-03-05 23:01:13"))), - Matchers.hasProperty("formSubmissionField", equalTo("start")), - Matchers.hasProperty("fieldType", equalTo("concept")), - Matchers.hasProperty("fieldDataType", startsWith("start")), - Matchers.hasProperty("effectiveDatetime", equalTo(e.getEventDate()))))); - - assertThat(e.getObs(), - Matchers.hasItem(Matchers.allOf( - Matchers.hasProperty("fieldCode", equalTo("163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")), - Matchers.hasProperty("values", hasItems(equalTo("2016-03-05 23:03:51"))), - Matchers.hasProperty("formSubmissionField", equalTo("end")), - Matchers.hasProperty("fieldType", equalTo("concept")), - Matchers.hasProperty("fieldDataType", startsWith("end")), - Matchers.hasProperty("effectiveDatetime", equalTo(e.getEventDate()))))); - - } @Test public void testCreateEncounter() throws JSONException { diff --git a/src/test/java/org/opensrp/connector/openmrs/service/HouseHoldTest.java b/src/test/java/org/opensrp/connector/openmrs/service/HouseHoldTest.java deleted file mode 100644 index 1a84ec9..0000000 --- a/src/test/java/org/opensrp/connector/openmrs/service/HouseHoldTest.java +++ /dev/null @@ -1,175 +0,0 @@ - -package org.opensrp.connector.openmrs.service; - -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.equalToIgnoringCase; -import static org.hamcrest.Matchers.hasProperty; -import static org.hamcrest.Matchers.isOneOf; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.text.ParseException; -import java.util.HashMap; -import java.util.Map; - -import org.hamcrest.Matchers; -import org.json.JSONException; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.opensrp.connector.openmrs.constants.OpenmrsHouseHold; -import org.opensrp.domain.Client; -import org.opensrp.domain.Event; -import org.opensrp.form.domain.FormSubmission; -import org.opensrp.form.service.FormAttributeParser; -import org.opensrp.service.formSubmission.FormEntityConverter; - -import com.google.gson.JsonIOException; -import com.google.gson.JsonSyntaxException; - -public class HouseHoldTest extends TestResourceLoader { - - public HouseHoldTest() throws IOException { - super(); - } - - EncounterService es; - - FormEntityConverter oc; - - PatientService ps; - - OpenmrsUserService us; - - HouseholdService hhs; - - Map datamap = new HashMap() { - - private static final long serialVersionUID = 3137471662816047127L; - { - put(1, new String[] { "a3f2abf4-2699-4761-819a-cea739224164", "test" }); - put(2, new String[] { "0aac6d81-b51f-4096-b354-5a5786e406c8", "karim mia" }); - put(3, new String[] { "baf59aa4-64e9-46fc-99e6-8cd8f01618ff", "hasan ferox" }); - put(4, new String[] { "f92ee1b5-c3ce-42fb-bbc8-e01f474acc5a", "jashim mia" }); - } - }; - - Map childdatamap = new HashMap() { - - private static final long serialVersionUID = 3137471662816047127L; - { - put(1, new String[] { "babcd9d2-b3e9-4f6d-8a06-2df8f5fbf01f", "74eebb60-a1b9-4691-81a4-5c04ecce7ae9" }); - put(2, new String[] { "b19db74f-6e96-4652-a765-5078beb12434" }); - put(3, new String[] { "409b44c4-262a-40b8-ad7d-748c480c7c13" }); - put(4, new String[] { "0036b7ca-36ec-4242-9885-a0a03a666cda" }); - } - }; - - private String hhRegistrationformName = "new_household_registration"; - - @Before - public void setup() throws IOException { - ps = new PatientService(openmrsOpenmrsUrl, openmrsUsername, openmrsPassword); - us = new OpenmrsUserService(openmrsOpenmrsUrl, openmrsUsername, openmrsPassword); - es = new EncounterService(openmrsOpenmrsUrl, openmrsUsername, openmrsPassword); - es.setPatientService(ps); - es.setUserService(us); - hhs = new HouseholdService(openmrsOpenmrsUrl, openmrsUsername, openmrsPassword); - hhs.setPatientService(ps); - hhs.setEncounterService(es); - FormAttributeParser fam = new FormAttributeParser(formDirPath); - oc = new FormEntityConverter(fam); - } - - @Ignore - @Test - public void shouldGetHHHeadAsClientAndEventFromHHRegistationForm() - throws JsonSyntaxException, JsonIOException, IOException, ParseException { - for (int i = 1; i <= 4; i++) { - FormSubmission fs = getFormSubmissionFor(hhRegistrationformName, i); - Client hhhead = oc.getClientFromFormSubmission(fs); - testClient(hhhead, datamap.get(i)[0], datamap.get(i)[1]); - - Event e = oc.getEventFromFormSubmission(fs); - - assertNotNull(e.getObs()); - assertFalse(e.getObs().isEmpty()); - assertEquals(2, e.getObs().size()); - - Map> dep = oc.getDependentClientsFromFormSubmission(fs); - - assertNotNull(dep); - assertFalse(dep.isEmpty()); - assertThat(dep, Matchers.> hasKey(isOneOf(childdatamap.get(i)))); - - for (Map cm : dep.values()) { - assertThat(cm, Matchers. hasKey(equalTo("client"))); - assertThat(cm, Matchers. hasKey(equalTo("event"))); - - testClient((Client) cm.get("client"), null, null); - - assertNotNull(cm.get("event")); - assertTrue(cm.get("event") instanceof Event); - - Event depe = (Event) cm.get("event"); - assertFalse(depe.getObs().isEmpty()); - assertEquals(1, depe.getObs().size()); - } - } - } - - @Test - public void shouldTestHHRegistrationIntegration() - throws JsonSyntaxException, JsonIOException, IOException, ParseException, JSONException { - for (int i = 1; i <= 4; i++) { - FormSubmission fs = getFormSubmissionFor(hhRegistrationformName, i); - Client hhhClient = oc.getClientFromFormSubmission(fs); - - Event hhhEvent = oc.getEventFromFormSubmission(fs); - - OpenmrsHouseHold hh = new OpenmrsHouseHold(hhhClient, hhhEvent); - - Map> dep = oc.getDependentClientsFromFormSubmission(fs); - - for (Map cm : dep.values()) { - hh.addHHMember((Client) cm.get("client"), (Event) cm.get("event")); - } - - assertEquals(hh.getHouseholdHead().getClient().getBaseEntityId(), datamap.get(i)[0]); - } - } - - private void testClient(Client client, String baseEntityId, String firstName) { - assertNotNull(client); - assertNotNull(client.getBaseEntityId()); - assertNotNull(client.getId()); - assertThat(client, hasProperty("firstName", notNullValue())); - assertThat(client, hasProperty("gender", notNullValue())); - assertThat(client, hasProperty("birthdate", notNullValue())); - assertNotNull(client.getIdentifiers()); - assertFalse(client.getIdentifiers().isEmpty()); - - if (baseEntityId != null) { - assertNotNull(client.getAddresses()); - assertFalse(client.getAddresses().isEmpty()); - assertTrue(client.getAddresses().get(0).getAddressType().equalsIgnoreCase("usual_residence")); - assertThat(client.getAddresses().get(0).getAddressFields(), Matchers. hasKey("landmark")); - assertThat(client.getAddresses().get(0).getAddressFields(), - Matchers. hasEntry(equalTo("geopoint"), notNullValue(String.class))); - - assertThat(client.getAddresses().get(0), hasProperty("latitude", notNullValue())); - assertThat(client.getAddresses().get(0), hasProperty("longitute", notNullValue())); - - assertTrue(client.getIdentifiers().containsKey("GOB HHID")); - assertTrue(client.getIdentifiers().containsKey("JiVitA HHID")); - assertEquals(client.getBaseEntityId(), baseEntityId); - assertEquals(client.getId(), baseEntityId); - assertThat(client, hasProperty("firstName", equalToIgnoringCase(firstName))); - } - } -} diff --git a/src/test/java/org/opensrp/connector/openmrs/service/TestResourceLoader.java b/src/test/java/org/opensrp/connector/openmrs/service/TestResourceLoader.java index f3bd072..5a9cd4c 100644 --- a/src/test/java/org/opensrp/connector/openmrs/service/TestResourceLoader.java +++ b/src/test/java/org/opensrp/connector/openmrs/service/TestResourceLoader.java @@ -1,22 +1,14 @@ package org.opensrp.connector.openmrs.service; -import java.io.File; -import java.io.FileReader; import java.io.IOException; import java.util.Properties; +import org.apache.commons.lang3.StringUtils; import org.opensrp.connector.dhis2.DHIS2DatasetPush; -import org.opensrp.form.domain.FormSubmission; import org.springframework.core.io.ClassPathResource; -import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.Resource; -import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.support.PropertiesLoaderUtils; -import com.google.gson.Gson; -import com.google.gson.JsonIOException; -import com.mysql.jdbc.StringUtils; - public class TestResourceLoader { protected static String openmrsOpenmrsUrl; protected static String openmrsUsername; @@ -48,7 +40,7 @@ public TestResourceLoader() throws IOException { dhis2Password = props.getProperty("dhis2.password"); String rc = props.getProperty("openmrs.test.make-rest-call"); - pushToOpenmrsForTest = StringUtils.isEmptyOrWhitespaceOnly(rc) ? false : Boolean.parseBoolean(rc); + pushToOpenmrsForTest = StringUtils.isBlank(rc) ? false : Boolean.parseBoolean(rc); this.patientService = new PatientService(openmrsOpenmrsUrl, openmrsUsername, openmrsPassword); this.encounterService = new EncounterService(openmrsOpenmrsUrl, openmrsUsername, openmrsPassword, openmrsVersion); @@ -59,14 +51,5 @@ public TestResourceLoader() throws IOException { couchDBPassword = props.getProperty("couchdb.password"); } - protected FormSubmission getFormSubmissionFor(String formName, Integer number) throws JsonIOException, IOException { - ResourceLoader loader = new DefaultResourceLoader(); - String path = loader.getResource(formDirPath).getURI().getPath(); - File fsfile = new File(path + "/" + formName + "/form_submission" + (number == null ? "" : number) + ".json"); - return new Gson().fromJson(new FileReader(fsfile), FormSubmission.class); - } - protected FormSubmission getFormSubmissionFor(String formName) throws JsonIOException, IOException { - return getFormSubmissionFor(formName, null); - } } diff --git a/src/test/java/org/opensrp/connector/openmrs/service/it/HouseHoldServiceTest.java b/src/test/java/org/opensrp/connector/openmrs/service/it/HouseHoldServiceTest.java index 27179ab..27849c1 100644 --- a/src/test/java/org/opensrp/connector/openmrs/service/it/HouseHoldServiceTest.java +++ b/src/test/java/org/opensrp/connector/openmrs/service/it/HouseHoldServiceTest.java @@ -4,47 +4,21 @@ import static org.junit.Assert.assertNotSame; import java.io.IOException; -import java.text.ParseException; -import java.util.Map; -import java.util.UUID; -import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.opensrp.connector.openmrs.constants.OpenmrsHouseHold; -import org.opensrp.connector.openmrs.service.EncounterService; import org.opensrp.connector.openmrs.service.HouseholdService; -import org.opensrp.connector.openmrs.service.OpenmrsUserService; -import org.opensrp.connector.openmrs.service.PatientService; -import org.opensrp.domain.Client; -import org.opensrp.domain.Event; -import org.opensrp.form.domain.FormSubmission; -import org.opensrp.service.formSubmission.FormEntityConverter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.google.gson.JsonIOException; - @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:test-applicationContext-opensrp-connector.xml") public class HouseHoldServiceTest extends OpenmrsApiService { - @Autowired - private EncounterService es; - - @Autowired - private FormEntityConverter oc; - - @Autowired - private PatientService ps; - - @Autowired - private OpenmrsUserService us; - @Autowired private HouseholdService hhs; @@ -131,88 +105,5 @@ public void testConvertRelationshipTypeToOpenmrsJson() throws JSONException { assertNotSame(notSameRelation, actualBIsToA); } - @Test - public void testHHScheduleData() throws JSONException, ParseException, JsonIOException, IOException { - FormSubmission fs = getFormSubmissionFor("new_household_registration", 6); - - /*** create patient *******/ - - String fn = "shumi"; - String mn = "sumaita"; - String ln = "khan"; - String baseEntity = UUID.randomUUID().toString(); - - String attributeName = "HouseholdAttributeName"; - JSONObject attribute = createPersonAttributeType(description, attributeName); - JSONObject firstPatient = EventClient.getCreatedPatientData(fn, mn, ln, "a3f2abf4-2699-4761-819a-cea739224164", - attributeName, baseEntity); - baseEntity = UUID.randomUUID().toString(); - JSONObject secondPatient = EventClient.getCreatedPatientData(fn, mn, ln, "babcd9d2-b3e9-4f6d-8a06-2df8f5fbf01f", - attributeName, baseEntity); - - Client hhhead = oc.getClientFromFormSubmission(fs); - Event ev = oc.getEventFromFormSubmission(fs); - Map> dep = oc.getDependentClientsFromFormSubmission(fs); - - OpenmrsHouseHold household = new OpenmrsHouseHold(hhhead, ev); - for (String hhmid : dep.keySet()) { - household.addHHMember((Client) dep.get(hhmid).get("client"), (Event) dep.get(hhmid).get("event")); - } - - JSONObject pr = us.getProvider(fs.anmId(), null); - if (pr == null) { - us.createProvider(fs.anmId(), fs.anmId()); - } - - JSONObject enct = es.getEncounterType(ev.getEventType()); - if (enct == null) { - es.createEncounterType(ev.getEventType(), "Encounter type created to fullfill scheduling test pre-reqs"); - } - - for (String hhmid : dep.keySet()) { - Event ein = (Event) dep.get(hhmid).get("event"); - JSONObject hmenct = es.getEncounterType(ein.getEventType()); - if (hmenct == null) { - es.createEncounterType(ein.getEventType(), "Encounter type created to fullfill scheduling test pre-reqs"); - } - - JSONObject response = hhs.saveHH(household, true); - - JSONArray encounters = response.getJSONArray(encountersKey); - JSONArray relationships = response.getJSONArray(relationshipsKey); - JSONObject hhEncounter = encounters.getJSONObject(firstIndex); - String hhEncounterUUID = hhEncounter.getString(uuidKey); - JSONObject hhEncounterType = hhEncounter.getJSONObject(encounterTypeKey); - String actualHHEncounterTypeName = hhEncounterType.getString(displayKey); - - JSONObject memberEncounter = encounters.getJSONObject(secondIndex); - String memberEncounterUUID = memberEncounter.getString(uuidKey); - - JSONObject meEncounterType = memberEncounter.getJSONObject(encounterTypeKey); - String actualMEEncounterTypeName = meEncounterType.getString(displayKey); - - JSONObject relationship = relationships.getJSONObject(firstIndex); - String relationshipUUID = relationship.getString(uuidKey); - JSONObject personB = relationship.getJSONObject("personB"); - JSONObject personA = relationship.getJSONObject("personA"); - String actualPersonBName = personB.getString(displayKey); - String actualPersonAName = personA.getString(displayKey); - - /* cleaning openmrs data */ - deleteEncounter(hhEncounterUUID); - deleteEncounter(memberEncounterUUID); - deleteRelation(relationshipUUID); - - String uuid = firstPatient.getString(uuidKey); - deletePerson(uuid); - String uuids = secondPatient.getString(uuidKey); - deletePerson(uuids); - deletePersonAttributeType(attribute.getString(uuidKey)); - assertEquals("New Household Registration", actualHHEncounterTypeName); - assertEquals("Census and New Woman Registration", actualMEEncounterTypeName); - assertEquals(fn + " " + mn + " " + ln, actualPersonBName); - assertEquals(fn + " " + mn + " " + ln, actualPersonAName); - } - } }