forked from hapifhir/hapi-fhir-jpaserver-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Added the junit test cases for CustomSearchNarrowingInterceptor #70
Open
vsinghht
wants to merge
7
commits into
master
Choose a base branch
from
junit-tests-for-CustomSearchNarrowingInterceptor
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e8dcb4b
Added the junit test cases for CustomSearchNarrowingInterceptor
vsinghht 1f022dc
Merge branch 'master' of https://github.com/elimuinformatics/hapi-fhi…
vsinghht 8672745
Updated the tests to test resource search
vsinghht 4c4b994
added mockito-inline dependency to support static mock
vsinghht 2e015d6
Merge branch 'master' of https://github.com/elimuinformatics/hapi-fhi…
vsinghht 44fc0e4
removed OAuth2Helper mocks and updated the tests to use test tokens
vsinghht d5a7cd3
managed spacing
vsinghht 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 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 |
---|---|---|
|
@@ -707,4 +707,4 @@ | |
</build> | ||
</profile> | ||
</profiles> | ||
</project> | ||
</project> |
187 changes: 187 additions & 0 deletions
187
src/test/java/ca/uhn/fhir/jpa/starter/interceptor/CustomSearchNarrowingInterceptorTest.java
This file contains 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,187 @@ | ||
package ca.uhn.fhir.jpa.starter.interceptor; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.hl7.fhir.instance.model.api.IBaseResource; | ||
import org.hl7.fhir.r4.model.Observation; | ||
import org.hl7.fhir.r4.model.Patient; | ||
import org.hl7.fhir.r4.model.Resource; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.Spy; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import ca.uhn.fhir.context.FhirContext; | ||
import ca.uhn.fhir.jpa.starter.AppProperties; | ||
import ca.uhn.fhir.jpa.starter.AppProperties.Oauth; | ||
import ca.uhn.fhir.model.api.IQueryParameterOr; | ||
import ca.uhn.fhir.model.api.IQueryParameterType; | ||
import ca.uhn.fhir.rest.annotation.OptionalParam; | ||
import ca.uhn.fhir.rest.annotation.Search; | ||
import ca.uhn.fhir.rest.api.server.RequestDetails; | ||
import ca.uhn.fhir.rest.client.api.IGenericClient; | ||
import ca.uhn.fhir.rest.client.api.IHttpRequest; | ||
import ca.uhn.fhir.rest.client.interceptor.BearerTokenAuthInterceptor; | ||
import ca.uhn.fhir.rest.param.BaseAndListParam; | ||
import ca.uhn.fhir.rest.param.ReferenceAndListParam; | ||
import ca.uhn.fhir.rest.param.TokenAndListParam; | ||
import ca.uhn.fhir.rest.server.IResourceProvider; | ||
import ca.uhn.fhir.rest.server.interceptor.auth.AuthorizedList; | ||
import ca.uhn.fhir.test.utilities.server.RestfulServerExtension; | ||
|
||
class CustomSearchNarrowingInterceptorTest { | ||
|
||
@Spy | ||
AppProperties mockConfig; | ||
|
||
@Spy | ||
Oauth mockOAuth; | ||
|
||
@InjectMocks | ||
CustomSearchNarrowingInterceptor ourSearchNarrowingInterceptor; | ||
|
||
private static Logger logger = LoggerFactory.getLogger(CustomSearchNarrowingInterceptorTest.class); | ||
private static List<Resource> ourReturn; | ||
private static String ourLastHitMethod; | ||
private static TokenAndListParam ourLastIdParam; | ||
private static ReferenceAndListParam ourLastPatientParam; | ||
private static final FhirContext ourCtx = FhirContext.forR4(); | ||
private static final String PATIENT_ID = "12345"; | ||
private static final String TOKEN_WITH_PATIENT_CLAIM = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJwYXRpZW50IjoiMTIzNDUifQ.U6r_6XMH5Bw6Lc4CBwcwrj5HvdAZeL8ZlIQXNnyJGow"; | ||
private static final String TOKEN_WITHOUT_PATIENT_CLAIM = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"; | ||
public IGenericClient myClient; | ||
|
||
@RegisterExtension | ||
public RestfulServerExtension myRestfulServerExtension = new RestfulServerExtension(ourCtx) | ||
.registerProvider(new MockPatientResourceProvider()) | ||
.registerProvider(new MockObservationResourceProvider()); | ||
|
||
@BeforeEach | ||
public void before() { | ||
MockitoAnnotations.openMocks(this); | ||
ourReturn = Collections.emptyList(); | ||
ourLastHitMethod = null; | ||
ourLastIdParam = null; | ||
ourLastPatientParam = null; | ||
mockOAuth.setEnabled(true); | ||
mockConfig.setOauth(mockOAuth); | ||
ourSearchNarrowingInterceptor = new CustomSearchNarrowingInterceptor(mockConfig) { | ||
@Override | ||
protected AuthorizedList buildAuthorizedList(RequestDetails theRequestDetails) { | ||
return super.buildAuthorizedList(theRequestDetails); | ||
} | ||
}; | ||
myRestfulServerExtension.registerInterceptor(ourSearchNarrowingInterceptor); | ||
myClient = myRestfulServerExtension.getFhirClient(); | ||
} | ||
|
||
@Test | ||
void testSearchNarrowObservationWithPatientClaim() throws Exception { | ||
myClient.registerInterceptor(new BearerTokenAuthInterceptor() { | ||
@Override | ||
public void interceptRequest(IHttpRequest theRequest) { | ||
theRequest.addHeader("Authorization", "Bearer " + TOKEN_WITH_PATIENT_CLAIM); | ||
} | ||
}); | ||
myClient.search().forResource("Observation").execute(); | ||
logger.info("Patient Reference Param : {}", ourLastPatientParam); | ||
|
||
assertEquals("Observation.search", ourLastHitMethod); | ||
assertEquals("Patient/" + PATIENT_ID, toStrings(ourLastPatientParam).get(0)); | ||
} | ||
|
||
@Test | ||
void testSearchNarrowObservationWithoutPatientClaim() throws Exception { | ||
myClient.registerInterceptor(new BearerTokenAuthInterceptor() { | ||
@Override | ||
public void interceptRequest(IHttpRequest theRequest) { | ||
theRequest.addHeader("Authorization", "Bearer " + TOKEN_WITHOUT_PATIENT_CLAIM); | ||
} | ||
}); | ||
myClient.search().forResource("Observation").execute(); | ||
logger.info("Patient Reference Param : {}", ourLastPatientParam); | ||
|
||
assertEquals("Observation.search", ourLastHitMethod); | ||
assertNull(ourLastPatientParam); | ||
} | ||
|
||
@Test | ||
void testSearchNarrowPatientWithPatientClaim() throws Exception { | ||
myClient.registerInterceptor(new BearerTokenAuthInterceptor() { | ||
@Override | ||
public void interceptRequest(IHttpRequest theRequest) { | ||
theRequest.addHeader("Authorization", "Bearer " + TOKEN_WITH_PATIENT_CLAIM); | ||
} | ||
}); | ||
myClient.search().forResource("Patient").execute(); | ||
logger.info("Patient Id Param : {}", ourLastIdParam); | ||
|
||
assertEquals("Patient.search", ourLastHitMethod); | ||
assertEquals("Patient/" + PATIENT_ID, toStrings(ourLastIdParam).get(0)); | ||
} | ||
|
||
@Test | ||
void testSearchNarrowPatientWithoutPatientClaim() throws Exception { | ||
myClient.registerInterceptor(new BearerTokenAuthInterceptor() { | ||
@Override | ||
public void interceptRequest(IHttpRequest theRequest) { | ||
theRequest.addHeader("Authorization", "Bearer " + TOKEN_WITHOUT_PATIENT_CLAIM); | ||
} | ||
}); | ||
myClient.search().forResource("Patient").execute(); | ||
logger.info("Patient Id Param : {}", ourLastIdParam); | ||
|
||
assertEquals("Patient.search", ourLastHitMethod); | ||
assertNull(ourLastIdParam); | ||
} | ||
|
||
private List<String> toStrings(BaseAndListParam<? extends IQueryParameterOr<?>> theParams) { | ||
List<? extends IQueryParameterOr<? extends IQueryParameterType>> valuesAsQueryTokens = theParams | ||
.getValuesAsQueryTokens(); | ||
|
||
return valuesAsQueryTokens.stream().map(IQueryParameterOr::getValuesAsQueryTokens) | ||
.map(t -> t.stream().map(j -> j.getValueAsQueryToken(ourCtx)).collect(Collectors.joining(","))) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public static class MockObservationResourceProvider implements IResourceProvider { | ||
|
||
@Override | ||
public Class<? extends IBaseResource> getResourceType() { | ||
return Observation.class; | ||
} | ||
|
||
@Search() | ||
public List<Resource> search(@OptionalParam(name = "_id") TokenAndListParam theIdParam, | ||
@OptionalParam(name = Observation.SP_PATIENT) ReferenceAndListParam thePatientParam) { | ||
ourLastHitMethod = "Observation.search"; | ||
ourLastIdParam = theIdParam; | ||
ourLastPatientParam = thePatientParam; | ||
return ourReturn; | ||
} | ||
} | ||
|
||
public static class MockPatientResourceProvider implements IResourceProvider { | ||
|
||
@Override | ||
public Class<? extends IBaseResource> getResourceType() { | ||
return Patient.class; | ||
} | ||
|
||
@Search() | ||
public List<Resource> search(@OptionalParam(name = "_id") TokenAndListParam theIdParam) { | ||
ourLastHitMethod = "Patient.search"; | ||
ourLastIdParam = theIdParam; | ||
return ourReturn; | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would you want to test the "last hit method"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have followed the upstream repo tests, as when we search for fhirResource( observation or patient ), the variable
ourLastHitMethod
will return the method name we have hit so it helps to verify right method has been called during execution.