diff --git a/api/src/main/java/org/openmrs/module/mycarehub/api/rest/ApiClient.java b/api/src/main/java/org/openmrs/module/mycarehub/api/rest/ApiClient.java index e764014..0250687 100644 --- a/api/src/main/java/org/openmrs/module/mycarehub/api/rest/ApiClient.java +++ b/api/src/main/java/org/openmrs/module/mycarehub/api/rest/ApiClient.java @@ -1,5 +1,6 @@ package org.openmrs.module.mycarehub.api.rest; +import static org.openmrs.module.mycarehub.utils.Constants.YEAR_MONTH_DAY_PATTERN; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -23,10 +24,12 @@ public class ApiClient { private static final long timeoutDuration = 2; - public static RestApiService getRestService() { + public static RestApiService getRestService() { if (retrofit == null) { Gson gson = - new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); // TODO: lastSyncTime have @Mokaya + new GsonBuilder() + .setDateFormat(YEAR_MONTH_DAY_PATTERN) + .create(); // TODO: lastSyncTime have @Mokaya String apiUrl = MyCareHubUtil.getApiUrl(); if (new UrlValidator().isValid(apiUrl)) { diff --git a/api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/AppointmentServiceImpl.java b/api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/AppointmentServiceImpl.java index 01a2908..d1286bb 100644 --- a/api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/AppointmentServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/AppointmentServiceImpl.java @@ -1,7 +1,6 @@ package org.openmrs.module.mycarehub.api.service.impl; -import static org.openmrs.module.mycarehub.utils.Constants.APPOINTMENT_DATE_CONCEPT_ID; -import static org.openmrs.module.mycarehub.utils.Constants.APPOINTMENT_REASON_CONCEPT_ID; +import static org.openmrs.module.mycarehub.utils.Constants.*; import static org.openmrs.module.mycarehub.utils.Constants.MyCareHubSettingType.PATIENT_APPOINTMENTS; import static org.openmrs.module.mycarehub.utils.Constants.MyCareHubSettingType.PATIENT_APPOINTMENTS_REQUESTS_GET; import static org.openmrs.module.mycarehub.utils.Constants.MyCareHubSettingType.PATIENT_APPOINTMENTS_REQUESTS_POST; @@ -18,7 +17,6 @@ import static org.openmrs.module.mycarehub.utils.Constants.RestKeys.GeneralKeys.CCC_NUMBER; import static org.openmrs.module.mycarehub.utils.Constants.RestKeys.GeneralKeys.FACILITY_MFL_CODE; import static org.openmrs.module.mycarehub.utils.Constants.RestKeys.GeneralKeys.MYCAREHUB_ID_KEY; -import static org.openmrs.module.mycarehub.utils.Constants.mycarehubDateTimePattern; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -52,7 +50,7 @@ public class AppointmentServiceImpl extends BaseOpenmrsService implements Appoin private final SimpleDateFormat mycarehubDateTimeFormatter = new SimpleDateFormat(mycarehubDateTimePattern); - private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + private final SimpleDateFormat dateFormat = new SimpleDateFormat(YEAR_MONTH_DAY_PATTERN); public AppointmentServiceImpl(AppointmentDao dao) { this.dao = dao; diff --git a/api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/MyCareHubPatientServiceImpl.java b/api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/MyCareHubPatientServiceImpl.java index 73321ea..67fc725 100644 --- a/api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/MyCareHubPatientServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/MyCareHubPatientServiceImpl.java @@ -1,7 +1,6 @@ package org.openmrs.module.mycarehub.api.service.impl; -import static org.openmrs.module.mycarehub.utils.Constants.APPOINTMENT_DATE_CONCEPT_ID; -import static org.openmrs.module.mycarehub.utils.Constants.CCC_NUMBER_IDENTIFIER_TYPE_UUID; +import static org.openmrs.module.mycarehub.utils.Constants.*; import static org.openmrs.module.mycarehub.utils.Constants.MedicalRecordConcepts.VitalSigns.BMI; import static org.openmrs.module.mycarehub.utils.Constants.MedicalRecordConcepts.VitalSigns.CD4_COUNT; import static org.openmrs.module.mycarehub.utils.Constants.MedicalRecordConcepts.VitalSigns.HEIGHT; @@ -31,7 +30,6 @@ import static org.openmrs.module.mycarehub.utils.Constants._PersonAttributeType.NEXT_OF_KIN_NAME; import static org.openmrs.module.mycarehub.utils.Constants._PersonAttributeType.NEXT_OF_KIN_RELATIONSHIP; import static org.openmrs.module.mycarehub.utils.Constants._PersonAttributeType.TELEPHONE_CONTACT; -import static org.openmrs.module.mycarehub.utils.Constants.mycarehubDateTimePattern; import com.google.gson.JsonObject; import java.text.SimpleDateFormat; @@ -174,9 +172,7 @@ private PatientRegistration createPatientRegistration(Patient patient) { .getPatientIdentifier(cccIdentifierType.getPatientIdentifierTypeId()) .getIdentifier()); - // TODO: 24/10/2022 Extract these strings into a constants file - String pattern = "yyyy-MM-dd"; - SimpleDateFormat sf = new SimpleDateFormat(pattern); + SimpleDateFormat sf = new SimpleDateFormat(YEAR_MONTH_DAY_PATTERN); registrationRequest.setDateOfBirth(sf.format(patient.getBirthdate())); registrationRequest.setBirthdateEstimated(patient.getBirthdateEstimated()); Context.getProgramWorkflowService().getPatientProgram(patient.getPatientId()); diff --git a/api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/RedFlagServiceImpl.java b/api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/RedFlagServiceImpl.java index 387c9ac..699d515 100644 --- a/api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/RedFlagServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/RedFlagServiceImpl.java @@ -10,6 +10,7 @@ import static org.openmrs.module.mycarehub.utils.Constants.RestKeys.REdFlagsObjectKeys.RED_FLAG_RESOLVED_BY_KEY; import static org.openmrs.module.mycarehub.utils.Constants.RestKeys.REdFlagsObjectKeys.RED_FLAG_RESOLVED_DATE_KEY; import static org.openmrs.module.mycarehub.utils.Constants.RestKeys.REdFlagsObjectKeys.RED_FLAG_STATUS_KEY; +import static org.openmrs.module.mycarehub.utils.Constants.YEAR_MONTH_DAY_PATTERN; import static org.openmrs.module.mycarehub.utils.Constants.mycarehubDateTimePattern; import static org.openmrs.module.mycarehub.utils.MyCareHubUtil.getPatientRedFlagRequests; import static org.openmrs.module.mycarehub.utils.MyCareHubUtil.postPatientRedFlags; @@ -42,7 +43,7 @@ public class RedFlagServiceImpl extends BaseOpenmrsService implements RedFlagSer private final SimpleDateFormat mycarehubDateTimeFormatter = new SimpleDateFormat(mycarehubDateTimePattern); - private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + private final SimpleDateFormat dateFormat = new SimpleDateFormat(YEAR_MONTH_DAY_PATTERN); public RedFlagServiceImpl(MyCareHubRedFlagDao dao) { this.dao = dao; diff --git a/api/src/main/java/org/openmrs/module/mycarehub/utils/Constants.java b/api/src/main/java/org/openmrs/module/mycarehub/utils/Constants.java index 612e2ca..411bc89 100755 --- a/api/src/main/java/org/openmrs/module/mycarehub/utils/Constants.java +++ b/api/src/main/java/org/openmrs/module/mycarehub/utils/Constants.java @@ -11,6 +11,8 @@ public class Constants { public static final String mycarehubDateTimePattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; + public static final String YEAR_MONTH_DAY_PATTERN = "yyyy-MM-dd"; + /** A URL to reach myCareHub API. * */ public static final String GP_MYCAREHUB_API_URL = "mycarehub.api.url"; diff --git a/api/src/test/java/org/openmrs/module/mycarehub/api/rest/ApiClientTest.java b/api/src/test/java/org/openmrs/module/mycarehub/api/rest/ApiClientTest.java new file mode 100644 index 0000000..1eca7b8 --- /dev/null +++ b/api/src/test/java/org/openmrs/module/mycarehub/api/rest/ApiClientTest.java @@ -0,0 +1,54 @@ +package org.openmrs.module.mycarehub.api.rest; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; +import static org.powermock.api.mockito.PowerMockito.mockStatic; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.openmrs.api.AdministrationService; +import org.openmrs.api.context.Context; +import org.openmrs.module.mycarehub.utils.MyCareHubUtil; +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 retrofit2.Retrofit; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({RestApiService.class, Retrofit.class, Context.class, MyCareHubUtil.class}) +@PowerMockIgnore("javax.net.ssl.*") +public class ApiClientTest { + + @Mock private MyCareHubUtil myCareHubUtil; + + @Before + public void setUp() { + mockStatic(ApiClient.class); + mockStatic(RestApiService.class); + AdministrationService administrationService = mock(AdministrationService.class); + + mockStatic(Context.class); + PowerMockito.when(Context.getAdministrationService()).thenReturn(administrationService); + + myCareHubUtil = mock(MyCareHubUtil.class); + } + + @Test + public void testGetRestServiceWithValidApiUrl() { + when(MyCareHubUtil.getApiUrl()).thenReturn("https://example.com"); + + RestApiService restApiService = ApiClient.getRestService(); + } + + @Test + public void testGetRestServiceWithInvalidApiUrl() { + when(MyCareHubUtil.getApiUrl()).thenReturn("invalid_url"); + + RestApiService restApiService = ApiClient.getRestService(); + + assertNull(restApiService); + } +} diff --git a/api/src/test/java/org/openmrs/module/mycarehub/utils/MyCareHubUtilTest.java b/api/src/test/java/org/openmrs/module/mycarehub/utils/MyCareHubUtilTest.java index 3c946d9..4b76090 100644 --- a/api/src/test/java/org/openmrs/module/mycarehub/utils/MyCareHubUtilTest.java +++ b/api/src/test/java/org/openmrs/module/mycarehub/utils/MyCareHubUtilTest.java @@ -2,16 +2,7 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; -import static org.openmrs.module.mycarehub.utils.Constants.APPOINTMENT_DATE_CONCEPT_ID; -import static org.openmrs.module.mycarehub.utils.Constants.EMPTY; -import static org.openmrs.module.mycarehub.utils.Constants.GP_DEFAULT_LOCATION_MFL_CODE; -import static org.openmrs.module.mycarehub.utils.Constants.GP_MYCAREHUB_API_DEFAULT_PASSWORD; -import static org.openmrs.module.mycarehub.utils.Constants.GP_MYCAREHUB_API_DEFAULT_USERNAME; -import static org.openmrs.module.mycarehub.utils.Constants.GP_MYCAREHUB_API_PASSWORD; -import static org.openmrs.module.mycarehub.utils.Constants.GP_MYCAREHUB_API_TOKEN; -import static org.openmrs.module.mycarehub.utils.Constants.GP_MYCAREHUB_API_TOKEN_EXPIRY_TIME; -import static org.openmrs.module.mycarehub.utils.Constants.GP_MYCAREHUB_API_URL; -import static org.openmrs.module.mycarehub.utils.Constants.GP_MYCAREHUB_API_USERNAME; +import static org.openmrs.module.mycarehub.utils.Constants.*; import static org.openmrs.module.mycarehub.utils.Constants.MedicalRecordConcepts.VitalSigns.BMI; import static org.openmrs.module.mycarehub.utils.Constants.MedicalRecordConcepts.VitalSigns.CD4_COUNT; import static org.openmrs.module.mycarehub.utils.Constants.MedicalRecordConcepts.VitalSigns.HEIGHT; @@ -60,7 +51,6 @@ import static org.openmrs.module.mycarehub.utils.Constants.RestKeys.REdFlagsObjectKeys.RED_FLAG_RESOLVED_BY_KEY; import static org.openmrs.module.mycarehub.utils.Constants.RestKeys.REdFlagsObjectKeys.RED_FLAG_RESOLVED_DATE_KEY; import static org.openmrs.module.mycarehub.utils.Constants.RestKeys.REdFlagsObjectKeys.RED_FLAG_STATUS_KEY; -import static org.openmrs.module.mycarehub.utils.Constants.mycarehubDateTimePattern; import static org.powermock.api.mockito.PowerMockito.mock; import static org.powermock.api.mockito.PowerMockito.mockStatic; import static org.powermock.api.mockito.PowerMockito.when; @@ -113,23 +103,17 @@ public class MyCareHubUtilTest { private static final Log log = LogFactory.getLog(MyCareHubUtilTest.class); - private RestApiService restApiService; - - private AdministrationService administrationService; - private MyCareHubSettingsService myCareHubSettingsService; - private final String pattern = "yyyy-MM-dd"; - - private final SimpleDateFormat sf = new SimpleDateFormat(pattern); + private final SimpleDateFormat sf = new SimpleDateFormat(YEAR_MONTH_DAY_PATTERN); private final SimpleDateFormat myCareHubDateTimeFormatter = new SimpleDateFormat(mycarehubDateTimePattern); @Before public void setup() { - restApiService = mock(RestApiService.class); - administrationService = mock(AdministrationService.class); + RestApiService restApiService = mock(RestApiService.class); + AdministrationService administrationService = mock(AdministrationService.class); myCareHubSettingsService = mock(MyCareHubSettingsService.class); mockStatic(Context.class); @@ -269,7 +253,7 @@ private PatientRegistration createDummyPatientRegistration() { public void uploadPatientAppointments_shouldCreateCorrectSyncTimeSetting() { JsonObject appointmentObject = new JsonObject(); - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + SimpleDateFormat dateFormat = new SimpleDateFormat(YEAR_MONTH_DAY_PATTERN); appointmentObject.addProperty(APPOINTMENT_ID_KEY, "58655");