Skip to content

Commit

Permalink
test: fetch patient health diaries unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Kathurima Kimathi <[email protected]>
  • Loading branch information
KathurimaKimathi committed Sep 20, 2023
1 parent 24086b8 commit 335368a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void fetchPatientHealthDiaries() {
healthDiaries.add(healthDiary);
}
}
if (healthDiaries.size() > 0) {
if (!healthDiaries.isEmpty()) {
saveHealthDiaries(healthDiaries);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,8 @@ public static JsonArray getPatientHealthDiaries(Date lastSyncTime, Date newSyncT
+ "&lastSyncTime="
+ URLEncoder.encode(syncTimeFormat.format(lastSyncTime), "UTF-8");

System.out.println("THE URL IS: " + url);

Call<ResponseBody> call =
restApiService.fetchPatientHealthDiaries(getBearer(getApiToken()), url);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,57 @@
package org.openmrs.module.mycarehub.api.service.impl;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.openmrs.module.mycarehub.utils.Constants.MyCareHubSettingType.PATIENT_HEALTH_DIARY_GET;
import static org.powermock.api.mockito.PowerMockito.*;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.Mockito;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.context.Context;
import org.openmrs.module.mycarehub.api.db.HealthDiaryDao;
import org.openmrs.module.mycarehub.api.rest.RestApiService;
import org.openmrs.module.mycarehub.api.service.MyCareHubSettingsService;
import org.openmrs.module.mycarehub.model.HealthDiary;
import org.openmrs.module.mycarehub.model.MyCareHubSetting;
import org.openmrs.module.mycarehub.utils.MyCareHubUtil;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(MockitoJUnitRunner.class)
@RunWith(PowerMockRunner.class)
@PrepareForTest({Context.class, MyCareHubUtil.class, RestApiService.class})
public class HealthDiaryServiceImplTest {

@Mock private HealthDiaryDao fakeHealthDiaryDao;

@InjectMocks private HealthDiaryServiceImpl healthDiaryImpl;

private Date currentDate;
private static Date currentDate;

private static final String searchString = "RED_FLAG";

@Mock public static MyCareHubSettingsService myCareHubSettingsService;

@Before
public void setUp() {
myCareHubSettingsService = mock(MyCareHubSettingsService.class);
MyCareHubSetting setting = mock(MyCareHubSetting.class);
currentDate = new Date();

mockStatic(Context.class);
mockStatic(MyCareHubUtil.class);
when(Context.getService(MyCareHubSettingsService.class)).thenReturn(myCareHubSettingsService);

AdministrationService administrationService = mock(AdministrationService.class);
}

@Test
Expand All @@ -45,7 +68,7 @@ public void testSaveHealthDiaries() {

@Test
public void testSaveHealthDiaries_EmptyList() {
List<HealthDiary> healthDiaries = Arrays.asList();
List<HealthDiary> healthDiaries = Collections.emptyList();
when(fakeHealthDiaryDao.saveHealthDiaries(healthDiaries)).thenReturn(healthDiaries);

List<HealthDiary> savedHealthDiaryEntries = healthDiaryImpl.saveHealthDiaries(healthDiaries);
Expand All @@ -55,7 +78,7 @@ public void testSaveHealthDiaries_EmptyList() {
@Test
public void testSaveHealthDiaries_SingleEntry() {
HealthDiary entryOne = createHealthDiary(currentDate, 1);
List<HealthDiary> healthDiaries = Arrays.asList(entryOne);
List<HealthDiary> healthDiaries = Collections.singletonList(entryOne);

when(fakeHealthDiaryDao.saveHealthDiaries(healthDiaries)).thenReturn(healthDiaries);

Expand Down Expand Up @@ -90,6 +113,48 @@ public void testGetPagedHealthDiaries() {
assertEquals(2, fetchedHealthDiaries.size());
}

@Test
public void fetchPatientHealthDiariesNullSetting() {
when(myCareHubSettingsService.getLatestMyCareHubSettingByType(PATIENT_HEALTH_DIARY_GET))
.thenReturn(null);

healthDiaryImpl.fetchPatientHealthDiaries();
}

@Test
public void fetchPatientHealthDiaries() {
MyCareHubSetting myCareHubSetting = new MyCareHubSetting();
myCareHubSetting.setSettingType(PATIENT_HEALTH_DIARY_GET);
myCareHubSetting.setLastSyncTime(currentDate);

when(myCareHubSettingsService.getLatestMyCareHubSettingByType(PATIENT_HEALTH_DIARY_GET))
.thenReturn(myCareHubSetting);

JsonArray jsonArray = getJsonElements();

when(MyCareHubUtil.getPatientHealthDiaries(Mockito.any(Date.class), Mockito.any(Date.class)))
.thenReturn(jsonArray);

healthDiaryImpl.fetchPatientHealthDiaries();
}

private static JsonArray getJsonElements() {
JsonArray jsonArray = new JsonArray();

JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("cccNumber", "1234567890");
jsonObject.addProperty("clientName", "testClient");
jsonObject.addProperty("phoneNumber", "0711223344");
jsonObject.addProperty("mood", "test mood");
jsonObject.addProperty("note", "test ");
jsonObject.addProperty("entryType", "HEALTH_DIARY");
jsonObject.addProperty("createdAt", String.valueOf(currentDate));
jsonObject.addProperty("sharedAt", String.valueOf(currentDate));

jsonArray.add(jsonObject);
return jsonArray;
}

// Helper method to create a HealthDiary object
private static HealthDiary createHealthDiary(Date date, int id) {
HealthDiary healthDiary = new HealthDiary();
Expand Down

0 comments on commit 335368a

Please sign in to comment.