Skip to content

Commit

Permalink
test: api client unit test
Browse files Browse the repository at this point in the history
extras:
- move date patternt to constant

Signed-off-by: Kathurima Kimathi <[email protected]>
  • Loading branch information
KathurimaKimathi committed Sep 1, 2023
1 parent aef54e6 commit a3014be
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,10 +24,12 @@ public class ApiClient {

private static final long timeoutDuration = 2;

public static <S> 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

Check warning on line 32 in api/src/main/java/org/openmrs/module/mycarehub/api/rest/ApiClient.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/openmrs/module/mycarehub/api/rest/ApiClient.java#L31-L32

Added lines #L31 - L32 were not covered by tests
String apiUrl = MyCareHubUtil.getApiUrl();

if (new UrlValidator().isValid(apiUrl)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Check warning on line 175 in api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/MyCareHubPatientServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

api/src/main/java/org/openmrs/module/mycarehub/api/service/impl/MyCareHubPatientServiceImpl.java#L175

Added line #L175 was not covered by tests
registrationRequest.setDateOfBirth(sf.format(patient.getBirthdate()));
registrationRequest.setBirthdateEstimated(patient.getBirthdateEstimated());
Context.getProgramWorkflowService().getPatientProgram(patient.getPatientId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");

Expand Down

0 comments on commit a3014be

Please sign in to comment.