Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FM2-448: Add Unit Test for Servlets in SMART-on-FHIR #24

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO
smartSession.setPatientUuid(patientId);
smartSession.setVisitUuid(visitId);

if (launchContext == null) {
resp.sendError(HttpStatus.SC_BAD_REQUEST, "launchContext must be provided");
return;
}

if (launchContext.equals("patient")) {
url = url + patientId;
smartSessionCache.put(patientId, smartSession);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOExce
String token = getParameter(req, "token");
String patientId = getParameter(req, "patientId");
String visitId = getParameter(req, "visitId");

if (token == null) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Couldn't find token in url");
return;
}

String decodedUrl = URLDecoder.decode(token, StandardCharsets.UTF_8.name());

String jwtKeyToken = null;
Expand Down Expand Up @@ -75,8 +81,8 @@ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOExce
return;
}

if (token == null || (patientId == null && visitId == null)) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST);
if (patientId == null && visitId == null) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "PatientId must be provided");
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.smartonfhir.web.servlet;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.module.smartonfhir.model.SmartSession;
import org.openmrs.module.smartonfhir.util.SmartSecretKeyHolder;
import org.openmrs.module.smartonfhir.util.SmartSessionCache;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

@RunWith(MockitoJUnitRunner.class)
public class SmartAccessConfirmationTest {

private static final String TOKEN = "http://localhost:8180/auth/realms/openmrs/login-actions/action-token?key=abcd&app-token=%7BAPP_TOKEN%7D";

private static final byte[] SMART_SECRET_KEY_HOLDER = "SecretKey".getBytes(StandardCharsets.UTF_8);

private static final String BASE_URL = "http://localhost:8180/auth/realms/openmrs/login-actions/action-token";

private static final String APP_TOKEN_VALUE = "eyJhbGciOiJIUzI1NiJ9.eyJwYXRpZW50IjoiNDU2IiwidmlzaXQiOiI3ODkifQ.XujZXboXbmJ5ZOgmWg6ihX8kN1Vf2XaZO0RQMBlOygA";

private static final String LAUNCH_ID = "12345";

private static final String USER_UUID = "123";

private static final String PATIENT_UUID = "456";

private static final String VISIT_UUID = "789";

private MockHttpServletRequest request;

private MockHttpServletResponse response;

private SmartSession smartSession;

private User user;

private SmartAccessConfirmation smartAccessConfirmation;

private MockedStatic<Context> contextMockedStatic;

private MockedStatic<SmartSecretKeyHolder> smartSecretKeyHolderMockedStatic;

private MockedConstruction<SmartSessionCache> smartSessionCacheMockedConstruction;

@Before
public void setup() throws Exception {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
user = new User();
smartSession = new SmartSession();
smartAccessConfirmation = new SmartAccessConfirmation();

smartSession.setPatientUuid(PATIENT_UUID);
smartSession.setVisitUuid(VISIT_UUID);

request.setParameter("token", TOKEN);
request.setParameter("launch", LAUNCH_ID);

user.setUuid(USER_UUID);

contextMockedStatic = Mockito.mockStatic(Context.class);
smartSecretKeyHolderMockedStatic = Mockito.mockStatic(SmartSecretKeyHolder.class);
smartSessionCacheMockedConstruction = Mockito.mockConstruction(SmartSessionCache.class, (mock, context) -> {
when(mock.get(LAUNCH_ID)).thenReturn(smartSession);
});

contextMockedStatic.when(Context::getAuthenticatedUser).thenReturn(user);
smartSecretKeyHolderMockedStatic.when(SmartSecretKeyHolder::getSecretKey).thenReturn(SMART_SECRET_KEY_HOLDER);

}

@After
public void close() {
contextMockedStatic.close();
smartSecretKeyHolderMockedStatic.close();
smartSessionCacheMockedConstruction.close();
}

@Test
public void shouldReturnCorrectBaseURL() throws IOException {
smartAccessConfirmation.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl(), containsString(BASE_URL));
}

@Test
public void shouldContainsEveryQuery() throws IOException {
smartAccessConfirmation.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl(), containsString("key="));
assertThat(response.getRedirectedUrl(), containsString("app-token="));
}

@Test
public void shouldContainAppToken() throws IOException {
smartAccessConfirmation.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl().contains(APP_TOKEN_VALUE), equalTo(true));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.smartonfhir.web.servlet;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Mockito.when;

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.smartonfhir.util.FhirBaseAddressStrategy;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

@RunWith(MockitoJUnitRunner.class)
public class SmartAppSelectorServletTest {

private static final String BASE_LAUNCH_ADDRESS_R4 = "http://127.0.0.1:9090/launch-standalone.html?iss=http://demo.org/openmrs/ws/fhir2/R4&launch=";

private static final String BASE_LAUNCH_ADDRESS_R3 = "http://127.0.0.1:9090/launch-standalone.html?iss=http://demo.org/openmrs/ws/fhir2/R3&launch=";

private static final String SMART_APP_BASE_URL = "http://127.0.0.1:9090/launch-standalone.html";

private MockHttpServletResponse response;

private MockHttpServletRequest request;

private SmartAppSelectorServlet smartAppSelectorServlet;

private MockedConstruction<FhirBaseAddressStrategy> fhirBaseAddressStrategyMockedConstruction;

@Before
public void setup() throws Exception {
response = new MockHttpServletResponse();
request = new MockHttpServletRequest();
smartAppSelectorServlet = new SmartAppSelectorServlet();
}

@After
public void close() {
fhirBaseAddressStrategyMockedConstruction.close();
}

@Test
public void shouldReturnCorrectSMARTAppBaseURLForR4() throws IOException {
fhirBaseAddressStrategyMockedConstruction = Mockito.mockConstruction(FhirBaseAddressStrategy.class,
(mock, context) -> {
when(mock.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R4);
});

smartAppSelectorServlet.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl(), containsString(SMART_APP_BASE_URL));
assertThat(response.getRedirectedUrl(), equalTo(BASE_LAUNCH_ADDRESS_R4));
}

@Test
public void shouldReturnCorrectSMARTAppBaseURLForR3() throws IOException {
fhirBaseAddressStrategyMockedConstruction = Mockito.mockConstruction(FhirBaseAddressStrategy.class,
(mock, context) -> {
when(mock.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R3);
});

smartAppSelectorServlet.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl(), containsString(SMART_APP_BASE_URL));
assertThat(response.getRedirectedUrl(), equalTo(BASE_LAUNCH_ADDRESS_R3));
}

@Test
public void shouldContainsEveryQuery() throws IOException {
fhirBaseAddressStrategyMockedConstruction = Mockito.mockConstruction(FhirBaseAddressStrategy.class,
(mock, context) -> {
when(mock.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R4);
});

smartAppSelectorServlet.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl(), containsString("iss="));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.smartonfhir.web.servlet;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Mockito.when;

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockedConstruction;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.smartonfhir.util.FhirBaseAddressStrategy;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

@RunWith(MockitoJUnitRunner.class)
public class SmartEhrLaunchServletTest {

private static final String BASE_LAUNCH_ADDRESS = "http://127.0.0.1:9090/launch-standalone.html?iss=http://demo.org/openmrs/ws/fhir2/R4&launch=";

private static final String PATIENT_UUID = "12345";

private static final String VISIT_UUID = "67890";

private static final String PATIENT_LAUNCH_CONTEXT = "patient";

private static final String VISIT_LAUNCH_CONTEXT = "encounter";

private MockHttpServletResponse response;

private MockHttpServletRequest request;

private SmartEhrLaunchServlet servlet;

private MockedConstruction<FhirBaseAddressStrategy> fhirBaseAddressStrategyMockedConstruction;

@Before
public void setup() throws Exception {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
servlet = new SmartEhrLaunchServlet();

request.setParameter("patientId", PATIENT_UUID);
request.setParameter("visitId", VISIT_UUID);
}

@After
public void close() {
fhirBaseAddressStrategyMockedConstruction.close();
}

@Test
public void shouldReturnCorrectURLForPatientContext() throws IOException {
fhirBaseAddressStrategyMockedConstruction = Mockito.mockConstruction(FhirBaseAddressStrategy.class,
(mock, context) -> {
when(mock.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS);
});
request.setParameter("launchContext", PATIENT_LAUNCH_CONTEXT);

servlet.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl().equals(BASE_LAUNCH_ADDRESS + PATIENT_UUID), equalTo(true));
}

@Test
public void shouldReturnCorrectURLForEncounterContext() throws IOException {
fhirBaseAddressStrategyMockedConstruction = Mockito.mockConstruction(FhirBaseAddressStrategy.class,
(mock, context) -> {
when(mock.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS);
});
request.setParameter("launchContext", VISIT_LAUNCH_CONTEXT);

servlet.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl().equals(BASE_LAUNCH_ADDRESS + VISIT_UUID), equalTo(true));
}

@Test
public void shouldReturnErrorWhenLaunchContextNotPresent() throws IOException {
fhirBaseAddressStrategyMockedConstruction = Mockito.mockConstruction(FhirBaseAddressStrategy.class,
(mock, context) -> {
when(mock.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS);
});

servlet.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getErrorMessage(), equalTo("launchContext must be provided"));
}

}
Loading