|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 3 | + * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 4 | + * obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under |
| 5 | + * the terms of the Healthcare Disclaimer located at http://openmrs.org/license. |
| 6 | + * |
| 7 | + * Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS |
| 8 | + * graphic logo is a trademark of OpenMRS Inc. |
| 9 | + */ |
| 10 | +package org.openmrs.module.smartonfhir.page.controller; |
| 11 | + |
| 12 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 13 | +import static org.hamcrest.Matchers.*; |
| 14 | +import static org.powermock.api.mockito.PowerMockito.*; |
| 15 | + |
| 16 | +import java.io.UnsupportedEncodingException; |
| 17 | +import java.net.URLEncoder; |
| 18 | +import java.nio.charset.StandardCharsets; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import org.codehaus.jackson.node.JsonNodeFactory; |
| 25 | +import org.codehaus.jackson.node.ObjectNode; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | +import org.junit.runner.RunWith; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.Spy; |
| 31 | +import org.openmrs.Patient; |
| 32 | +import org.openmrs.Visit; |
| 33 | +import org.openmrs.VisitType; |
| 34 | +import org.openmrs.api.PatientService; |
| 35 | +import org.openmrs.api.VisitService; |
| 36 | +import org.openmrs.api.context.Context; |
| 37 | +import org.openmrs.module.appframework.domain.AppDescriptor; |
| 38 | +import org.openmrs.module.appui.UiSessionContext; |
| 39 | +import org.openmrs.module.coreapps.CoreAppsConstants; |
| 40 | +import org.openmrs.module.coreapps.utils.VisitTypeHelper; |
| 41 | +import org.openmrs.ui.framework.UiUtils; |
| 42 | +import org.openmrs.ui.framework.page.PageModel; |
| 43 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 44 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 45 | + |
| 46 | +@RunWith(PowerMockRunner.class) |
| 47 | +@PrepareForTest({ PatientService.class, VisitService.class, Context.class }) |
| 48 | +public class FindVisitPageControllerTest { |
| 49 | + |
| 50 | + private static final String AFTER_SELECTED_URL = "/ms/smartLaunchOptionSelected?app=smart_client&patientId={{patient.uuid}}&token={{token}}"; |
| 51 | + |
| 52 | + private static final String UNAFFECTED_AFTER_SELECTED_URL = "/ms/smartLaunchOptionSelected?app=smart_client"; |
| 53 | + |
| 54 | + private static final String TOKEN_URL = "http://localhost:8180/auth/realms/openmrs/login-actions/action-token?key=123"; |
| 55 | + |
| 56 | + private static final String PATIENT_UUID = "1234"; |
| 57 | + |
| 58 | + private static final String PATIENT_ID = "12"; |
| 59 | + |
| 60 | + private static final String VISIT_UUID = "56789"; |
| 61 | + |
| 62 | + private static final String VISIT_TYPE_UUID = "9876"; |
| 63 | + |
| 64 | + @Spy |
| 65 | + private VisitTypeHelper visitTypeHelper = new VisitTypeHelper(); |
| 66 | + |
| 67 | + @Mock |
| 68 | + private PatientService patientService; |
| 69 | + |
| 70 | + @Mock |
| 71 | + private VisitService visitService; |
| 72 | + |
| 73 | + private UiSessionContext uiSessionContext; |
| 74 | + |
| 75 | + private PageModel pageModel; |
| 76 | + |
| 77 | + private FindVisitPageController findVisitPageController; |
| 78 | + |
| 79 | + private AppDescriptor appDescriptor; |
| 80 | + |
| 81 | + private List<Visit> list; |
| 82 | + |
| 83 | + private UiUtils ui; |
| 84 | + |
| 85 | + private Patient patient; |
| 86 | + |
| 87 | + private Visit visit; |
| 88 | + |
| 89 | + @Before |
| 90 | + public void setup() throws Exception { |
| 91 | + appDescriptor = new AppDescriptor(); |
| 92 | + appDescriptor.setConfig(new ObjectNode(JsonNodeFactory.instance)); |
| 93 | + pageModel = new PageModel(); |
| 94 | + findVisitPageController = new FindVisitPageController(); |
| 95 | + uiSessionContext = new UiSessionContext(); |
| 96 | + patient = new Patient(); |
| 97 | + list = new ArrayList<>(); |
| 98 | + visit = new Visit(); |
| 99 | + |
| 100 | + visit.setUuid(VISIT_UUID); |
| 101 | + list.add(visit); |
| 102 | + patient.setUuid(PATIENT_UUID); |
| 103 | + patient.setId(Integer.valueOf(PATIENT_ID)); |
| 104 | + |
| 105 | + appDescriptor.getConfig().put("afterSelectedUrl", AFTER_SELECTED_URL); |
| 106 | + |
| 107 | + mockStatic(Context.class); |
| 108 | + mockStatic(PatientService.class); |
| 109 | + mockStatic(VisitService.class); |
| 110 | + |
| 111 | + doReturn(patientService).when(Context.class, "getPatientService"); |
| 112 | + doReturn(visitService).when(Context.class, "getVisitService"); |
| 113 | + when(patientService.getPatientByUuid(PATIENT_ID)).thenReturn(patient); |
| 114 | + when(visitService.getVisitsByPatient(patient)).thenReturn(list); |
| 115 | + doReturn(true).when(Context.class, "hasPrivilege", CoreAppsConstants.PRIVILEGE_PATIENT_VISITS); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void shouldReturnAllCorrectAttributes() throws Exception { |
| 120 | + findVisitPageController.get(uiSessionContext, pageModel, appDescriptor, null, visitService, PATIENT_ID, TOKEN_URL, |
| 121 | + uiSessionContext, visitTypeHelper); |
| 122 | + |
| 123 | + assertThat(pageModel, notNullValue()); |
| 124 | + assertThat(pageModel.get("visitSummaries"), notNullValue()); |
| 125 | + assertThat(pageModel.get("canViewVisits"), notNullValue()); |
| 126 | + assertThat(pageModel.get("visitTypesWithAttr"), notNullValue()); |
| 127 | + assertThat(pageModel.get("afterSelectedUrl"), notNullValue()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void shouldReturnCorrectVisitSummaries() throws UnsupportedEncodingException { |
| 132 | + findVisitPageController.get(uiSessionContext, pageModel, appDescriptor, null, visitService, PATIENT_ID, TOKEN_URL, |
| 133 | + uiSessionContext, visitTypeHelper); |
| 134 | + |
| 135 | + assertThat(pageModel, notNullValue()); |
| 136 | + assertThat(pageModel.get("visitSummaries"), notNullValue()); |
| 137 | + |
| 138 | + List<Visit> result = (List) pageModel.get("visitSummaries"); |
| 139 | + |
| 140 | + assertThat(result, notNullValue()); |
| 141 | + assertThat(result.size(), equalTo(1)); |
| 142 | + assertThat(result.get(0).getUuid(), equalTo(VISIT_UUID)); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void shouldReturnCorrectCanViewVisits() throws UnsupportedEncodingException { |
| 147 | + findVisitPageController.get(uiSessionContext, pageModel, appDescriptor, null, visitService, PATIENT_ID, TOKEN_URL, |
| 148 | + uiSessionContext, visitTypeHelper); |
| 149 | + |
| 150 | + assertThat(pageModel, notNullValue()); |
| 151 | + assertThat(pageModel.get("canViewVisits"), equalTo(true)); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void shouldReturnCorrectVisitTypeWithAttr() throws UnsupportedEncodingException { |
| 156 | + VisitType visitType = new VisitType(); |
| 157 | + visitType.setUuid(VISIT_TYPE_UUID); |
| 158 | + visitService.saveVisitType(visitType); |
| 159 | + |
| 160 | + Map<String, Object> typeAttr = new HashMap<>(); |
| 161 | + doReturn(typeAttr).when(visitTypeHelper).getVisitTypeColorAndShortName(visitType); |
| 162 | + |
| 163 | + findVisitPageController.get(uiSessionContext, pageModel, appDescriptor, null, visitService, PATIENT_ID, TOKEN_URL, |
| 164 | + uiSessionContext, visitTypeHelper); |
| 165 | + |
| 166 | + assertThat(pageModel, notNullValue()); |
| 167 | + assertThat(pageModel.get("visitTypesWithAttr"), notNullValue()); |
| 168 | + |
| 169 | + Map<Integer, Object> result = (Map<Integer, Object>) pageModel.get("visitTypesWithAttr"); |
| 170 | + |
| 171 | + assertThat(result, notNullValue()); |
| 172 | + assertThat(result.equals(typeAttr), equalTo(true)); |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + public void shouldReturnCorrectAfterSelectedURL() throws UnsupportedEncodingException { |
| 177 | + findVisitPageController.get(uiSessionContext, pageModel, appDescriptor, null, visitService, PATIENT_ID, TOKEN_URL, |
| 178 | + uiSessionContext, visitTypeHelper); |
| 179 | + |
| 180 | + assertThat(pageModel, notNullValue()); |
| 181 | + assertThat(pageModel.get("afterSelectedUrl"), notNullValue()); |
| 182 | + |
| 183 | + String result = (String) pageModel.get("afterSelectedUrl"); |
| 184 | + |
| 185 | + assertThat(result, notNullValue()); |
| 186 | + assertThat(result.contains(UNAFFECTED_AFTER_SELECTED_URL), equalTo(true)); |
| 187 | + assertThat(result.contains(URLEncoder.encode(TOKEN_URL, StandardCharsets.UTF_8.name())), equalTo(true)); |
| 188 | + } |
| 189 | +} |
0 commit comments