-
Notifications
You must be signed in to change notification settings - Fork 6
FM2-448: Add Unit Test for Servlets in SMART-on-FHIR #24
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
Open
theanandankit
wants to merge
14
commits into
openmrs:master
Choose a base branch
from
theanandankit:FM2-448
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
037ce09
CORS filter fix
theanandankit 9421ccb
options methode added
theanandankit bc964e9
Small change in CORSFilter
theanandankit 882bdc6
Update CORSFilter.java
theanandankit caba4bf
Update CORSFilter
theanandankit 94a107c
Merge remote-tracking branch 'origin/master'
theanandankit e24dde6
Merge pull request #1 from openmrs/master
theanandankit 679b709
Merge pull request #2 from openmrs/master
theanandankit 9b06be4
Merge pull request #3 from openmrs/master
theanandankit 521ce2e
Merge branch 'master' of https://github.com/openmrs/openmrs-module-sm…
theanandankit 029c4ec
FM2-448: Add Unit Test for Servlets in SMART-on-FHIR
theanandankit 1547912
Refactoring
theanandankit c1c767d
Merge branch 'master' of https://github.com/openmrs/openmrs-module-sm…
theanandankit b22de3c
Change the dependency from PowerMock to Mockito
theanandankit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
...src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartAccessConfirmationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* | ||
* 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.powermock.api.mockito.PowerMockito.doReturn; | ||
import static org.powermock.api.mockito.PowerMockito.mockStatic; | ||
import static org.powermock.api.mockito.PowerMockito.when; | ||
import static org.powermock.api.mockito.PowerMockito.whenNew; | ||
|
||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
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.powermock.core.classloader.annotations.PowerMockIgnore; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PowerMockIgnore("javax.crypto.*") | ||
@PrepareForTest({ Context.class, SmartSessionCache.class, SmartAccessConfirmation.class, SecretKeySpec.class, | ||
SmartSecretKeyHolder.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; | ||
|
||
@Mock | ||
private SmartSessionCache smartSessionCache; | ||
|
||
private SmartSession smartSession; | ||
|
||
private User user; | ||
|
||
private SmartAccessConfirmation smartAccessConfirmation; | ||
|
||
@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); | ||
|
||
mockStatic(Context.class); | ||
mockStatic(SmartSecretKeyHolder.class); | ||
|
||
doReturn(user).when(Context.class, "getAuthenticatedUser"); | ||
doReturn(SMART_SECRET_KEY_HOLDER).when(SmartSecretKeyHolder.class, "getSecretKey"); | ||
|
||
whenNew(SmartSessionCache.class).withNoArguments().thenReturn(smartSessionCache); | ||
when(smartSessionCache.get(LAUNCH_ID)).thenReturn(smartSession); | ||
} | ||
|
||
@Test | ||
public void shouldReturnCorrectBaseURL() throws IOException { | ||
smartAccessConfirmation.doGet(request, response); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.getRedirectedUrl(), notNullValue()); | ||
assertThat(response.getRedirectedUrl().contains(BASE_URL), equalTo(true)); | ||
} | ||
|
||
@Test | ||
public void shouldContainsEveryQuery() throws IOException { | ||
smartAccessConfirmation.doGet(request, response); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.getRedirectedUrl(), notNullValue()); | ||
assertThat(response.getRedirectedUrl().contains("key="), equalTo(true)); | ||
assertThat(response.getRedirectedUrl().contains("app-token="), equalTo(true)); | ||
} | ||
|
||
@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)); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartAppSelectorServletTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* 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.powermock.api.mockito.PowerMockito.when; | ||
import static org.powermock.api.mockito.PowerMockito.whenNew; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.openmrs.module.smartonfhir.util.FhirBaseAddressStrategy; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({ SmartAppSelectorServlet.class, FhirBaseAddressStrategy.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; | ||
|
||
@Mock | ||
private FhirBaseAddressStrategy fhirBaseAddressStrategy; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
response = new MockHttpServletResponse(); | ||
request = new MockHttpServletRequest(); | ||
smartAppSelectorServlet = new SmartAppSelectorServlet(); | ||
|
||
whenNew(FhirBaseAddressStrategy.class).withNoArguments().thenReturn(fhirBaseAddressStrategy); | ||
} | ||
|
||
@Test | ||
public void shouldReturnCorrectSMARTAppBaseURLForR4() throws IOException { | ||
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R4); | ||
|
||
smartAppSelectorServlet.doGet(request, response); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.getRedirectedUrl(), notNullValue()); | ||
assertThat(response.getRedirectedUrl().contains(SMART_APP_BASE_URL), equalTo(true)); | ||
assertThat(response.getRedirectedUrl(), equalTo(BASE_LAUNCH_ADDRESS_R4)); | ||
} | ||
|
||
@Test | ||
public void shouldReturnCorrectSMARTAppBaseURLForR3() throws IOException { | ||
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R3); | ||
|
||
smartAppSelectorServlet.doGet(request, response); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.getRedirectedUrl(), notNullValue()); | ||
assertThat(response.getRedirectedUrl().contains(SMART_APP_BASE_URL), equalTo(true)); | ||
assertThat(response.getRedirectedUrl(), equalTo(BASE_LAUNCH_ADDRESS_R3)); | ||
} | ||
|
||
@Test | ||
public void shouldContainsEveryQuery() throws IOException { | ||
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R4); | ||
|
||
smartAppSelectorServlet.doGet(request, response); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.getRedirectedUrl(), notNullValue()); | ||
assertThat(response.getRedirectedUrl().contains("iss="), equalTo(true)); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartEhrLaunchServletTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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.powermock.api.mockito.PowerMockito.when; | ||
import static org.powermock.api.mockito.PowerMockito.whenNew; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.openmrs.module.smartonfhir.util.FhirBaseAddressStrategy; | ||
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
import org.springframework.mock.web.MockHttpServletRequest; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest({ SmartEhrLaunchServlet.class, FhirBaseAddressStrategy.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"; | ||
|
||
@Mock | ||
private FhirBaseAddressStrategy fhirBaseAddressStrategy; | ||
|
||
private MockHttpServletResponse response; | ||
|
||
private MockHttpServletRequest request; | ||
|
||
private SmartEhrLaunchServlet servlet; | ||
|
||
@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); | ||
|
||
whenNew(FhirBaseAddressStrategy.class).withNoArguments().thenReturn(fhirBaseAddressStrategy); | ||
} | ||
|
||
@Test | ||
public void shouldReturnCorrectURLForPatientContext() throws IOException { | ||
when(fhirBaseAddressStrategy.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)); | ||
} | ||
|
||
public void shouldReturnCorrectURLForEncounterContext() throws IOException { | ||
when(fhirBaseAddressStrategy.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)); | ||
} | ||
|
||
public void shouldReturnErrorWhenURLNotPresent() throws IOException { | ||
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(""); | ||
|
||
servlet.doGet(request, response); | ||
|
||
assertThat(response, notNullValue()); | ||
assertThat(response.getErrorMessage(), equalTo("A url must be provided")); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.