Skip to content

Commit 00987b0

Browse files
FM2-466: Add Unit Test for Controllers in SMART-on-FHIR (#23)
1 parent 3bad3d4 commit 00987b0

File tree

5 files changed

+478
-3
lines changed

5 files changed

+478
-3
lines changed

omod/src/main/java/org/openmrs/module/smartonfhir/web/filter/SmartSessionLogoutFilter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ public class SmartSessionLogoutFilter implements Filter {
3636
@Override
3737
public void init(FilterConfig filterConfig) {
3838
final KeycloakUriBuilder keycloakUriBuilder = KeycloakDeploymentBuilder
39-
.build(KeycloakConfigHolder.getKeycloakConfig()).getLogoutUrl();
40-
39+
.build(KeycloakConfigHolder.getKeycloakConfig()).getLogoutUrl();
40+
4141
if (keycloakUriBuilder != null) {
4242
logoutUrl = keycloakUriBuilder.toTemplate();
4343
} else {
4444
log.error(
45-
"Could not find Keycloak configuration file. Please run keycloak server before openmrs to avoid this error");
45+
"Could not find Keycloak configuration file. Please run keycloak server before openmrs to avoid this error");
4646
}
4747
}
4848

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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.equalTo;
14+
import static org.hamcrest.Matchers.notNullValue;
15+
import static org.powermock.api.mockito.PowerMockito.doNothing;
16+
import static org.powermock.api.mockito.PowerMockito.mockStatic;
17+
18+
import java.io.UnsupportedEncodingException;
19+
import java.net.URLEncoder;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
import org.codehaus.jackson.node.JsonNodeFactory;
24+
import org.codehaus.jackson.node.ObjectNode;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.openmrs.module.appframework.domain.AppDescriptor;
29+
import org.openmrs.module.appframework.domain.Extension;
30+
import org.openmrs.module.appui.UiSessionContext;
31+
import org.openmrs.module.coreapps.helper.BreadcrumbHelper;
32+
import org.openmrs.ui.framework.UiUtils;
33+
import org.openmrs.ui.framework.page.PageModel;
34+
import org.powermock.core.classloader.annotations.PrepareForTest;
35+
import org.powermock.modules.junit4.PowerMockRunner;
36+
37+
@RunWith(PowerMockRunner.class)
38+
@PrepareForTest(BreadcrumbHelper.class)
39+
public class FindPatientPageControllerTest {
40+
41+
public static final String AFTER_SELECTED_URL = "/ms/smartLaunchOptionSelected?app=smart_client&patientId=123";
42+
43+
public static final String TOKEN_URL = "http://localhost:8180/auth/realms/openmrs/login-actions/action-token?key=123";
44+
45+
public static final String LABEL = "coreapps.findPatient.app.label";
46+
47+
public static final String SHOW_LAST_VIEWED_PATIENT = "true";
48+
49+
public static final String REGISTRATION_APP_LINK = "SMART-on-FHIR";
50+
51+
public static final String HEADING = "";
52+
53+
private UiSessionContext uiSessionContext;
54+
55+
private PageModel pageModel;
56+
57+
private FindPatientPageController findPatientPageController;
58+
59+
private AppDescriptor appDescriptor;
60+
61+
private List<Extension> list;
62+
63+
private UiUtils ui;
64+
65+
@Before
66+
public void setup() throws Exception {
67+
appDescriptor = new AppDescriptor();
68+
appDescriptor.setConfig(new ObjectNode(JsonNodeFactory.instance));
69+
findPatientPageController = new FindPatientPageController();
70+
pageModel = new PageModel();
71+
uiSessionContext = new UiSessionContext();
72+
list = new ArrayList<>();
73+
74+
appDescriptor.getConfig().put("afterSelectedUrl", AFTER_SELECTED_URL);
75+
appDescriptor.getConfig().put("heading", HEADING);
76+
appDescriptor.getConfig().put("label", LABEL);
77+
appDescriptor.getConfig().put("showLastViewedPatients", SHOW_LAST_VIEWED_PATIENT);
78+
79+
mockStatic(BreadcrumbHelper.class);
80+
doNothing().when(BreadcrumbHelper.class, "addBreadcrumbsIfDefinedInApp", appDescriptor, pageModel, ui);
81+
}
82+
83+
@Test
84+
public void shouldReturnAllCorrectAttributes() throws Exception {
85+
appDescriptor.getConfig().put("registrationAppLink", REGISTRATION_APP_LINK);
86+
findPatientPageController.get(pageModel, appDescriptor, TOKEN_URL, uiSessionContext, ui);
87+
88+
assertThat(pageModel, notNullValue());
89+
assertThat(pageModel.isEmpty(), equalTo(false));
90+
assertThat(pageModel.get("afterSelectedUrl"), notNullValue());
91+
assertThat(pageModel.get("afterSelectedUrl").toString().contains(URLEncoder.encode(TOKEN_URL)), equalTo(true));
92+
assertThat(pageModel.get("heading"), equalTo(HEADING));
93+
assertThat(pageModel.get("label"), equalTo(LABEL));
94+
assertThat(pageModel.get("showLastViewedPatients"), equalTo(false));
95+
assertThat(pageModel.get("registrationAppLink"), equalTo(REGISTRATION_APP_LINK));
96+
}
97+
98+
@Test
99+
public void shouldReturnCorrectResultWhenRegistrationLinkNotNull() throws UnsupportedEncodingException {
100+
findPatientPageController.get(pageModel, appDescriptor, TOKEN_URL, uiSessionContext, ui);
101+
102+
assertThat(pageModel, notNullValue());
103+
assertThat(pageModel.isEmpty(), equalTo(false));
104+
assertThat(pageModel.get("afterSelectedUrl"), notNullValue());
105+
assertThat(pageModel.get("afterSelectedUrl").toString().contains(URLEncoder.encode(TOKEN_URL)), equalTo(true));
106+
assertThat(pageModel.get("heading"), equalTo(HEADING));
107+
assertThat(pageModel.get("label"), equalTo(LABEL));
108+
assertThat(pageModel.get("showLastViewedPatients"), equalTo(false));
109+
assertThat(pageModel.get("registrationAppLink"), equalTo(""));
110+
}
111+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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

Comments
 (0)