-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
We don't make it very easy to add component-level tests for our rest layer. This pull request works with what we have to test the public JAX-RS methods for building the CapabilityStatement resource and the smart-configuration JSON from various configs. Signed-off-by: Lee Surprenant <[email protected]>
- Loading branch information
Showing
11 changed files
with
1,304 additions
and
56 deletions.
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
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
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
123 changes: 123 additions & 0 deletions
123
fhir-server/src/test/java/com/ibm/fhir/server/resources/CapabilitiesTest.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,123 @@ | ||
/* | ||
* (C) Copyright IBM Corp. 2020 | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package com.ibm.fhir.server.resources; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import javax.ws.rs.core.Response; | ||
|
||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
import com.ibm.fhir.config.FHIRConfiguration; | ||
import com.ibm.fhir.config.FHIRRequestContext; | ||
import com.ibm.fhir.exception.FHIRException; | ||
import com.ibm.fhir.model.resource.CapabilityStatement; | ||
import com.ibm.fhir.model.resource.CapabilityStatement.Rest.Resource.Interaction; | ||
import com.ibm.fhir.model.type.code.ResourceType; | ||
import com.ibm.fhir.server.test.MockHttpServletRequest; | ||
import com.ibm.fhir.server.test.MockServletContext; | ||
|
||
public class CapabilitiesTest { | ||
|
||
@BeforeClass | ||
void setup() { | ||
FHIRConfiguration.setConfigHome("target/test-classes"); | ||
} | ||
|
||
@AfterClass | ||
void tearDown() throws FHIRException { | ||
FHIRConfiguration.setConfigHome(""); | ||
FHIRRequestContext.get().setTenantId("default"); | ||
} | ||
|
||
@Test | ||
void testBuildCapabilityStatement_resources_omitted() throws Exception { | ||
FHIRRequestContext.get().setTenantId("omitted"); | ||
FHIRRequestContext.get().setOriginalRequestUri("http://example.com/metadata"); | ||
CapabilitiesChild c = new CapabilitiesChild(); | ||
|
||
Response capabilities = c.capabilities(); | ||
CapabilityStatement capabilityStatement = capabilities.readEntity(CapabilityStatement.class); | ||
|
||
assertEquals(capabilityStatement.getRest().size(), 1, "Number of REST Elements"); | ||
CapabilityStatement.Rest restDefinition = capabilityStatement.getRest().get(0); | ||
|
||
assertRestDefinition(restDefinition, 146, 8, 8); | ||
} | ||
|
||
@Test | ||
void testBuildCapabilityStatement_resources_empty() throws Exception { | ||
FHIRRequestContext.get().setTenantId("empty"); | ||
FHIRRequestContext.get().setOriginalRequestUri("http://example.com/metadata"); | ||
CapabilitiesChild c = new CapabilitiesChild(); | ||
|
||
Response capabilities = c.capabilities(); | ||
CapabilityStatement capabilityStatement = capabilities.readEntity(CapabilityStatement.class); | ||
|
||
assertEquals(capabilityStatement.getRest().size(), 1, "Number of REST Elements"); | ||
CapabilityStatement.Rest restDefinition = capabilityStatement.getRest().get(0); | ||
|
||
assertRestDefinition(restDefinition, 146, 0, 0); | ||
} | ||
|
||
@Test | ||
void testBuildCapabilityStatement_resources_filtered() throws Exception { | ||
FHIRRequestContext.get().setTenantId("smart-enabled"); | ||
FHIRRequestContext.get().setOriginalRequestUri("http://example.com/metadata"); | ||
CapabilitiesChild c = new CapabilitiesChild(); | ||
|
||
Response capabilities = c.capabilities(); | ||
CapabilityStatement capabilityStatement = capabilities.readEntity(CapabilityStatement.class); | ||
|
||
assertEquals(capabilityStatement.getRest().size(), 1, "Number of REST Elements"); | ||
CapabilityStatement.Rest restDefinition = capabilityStatement.getRest().get(0); | ||
|
||
assertRestDefinition(restDefinition, 2, 2, 4); | ||
} | ||
|
||
private void assertRestDefinition(CapabilityStatement.Rest restDefinition, int numOfResources, int patientInteractions, int practitionerInteractions) { | ||
assertEquals(restDefinition.getResource().size(), numOfResources, "Number of supported resources"); | ||
assertFalse(restDefinition.getResource().stream().anyMatch(r -> r.getType().getValueAsEnumConstant() == ResourceType.ValueSet.RESOURCE)); | ||
assertFalse(restDefinition.getResource().stream().anyMatch(r -> r.getType().getValueAsEnumConstant() == ResourceType.ValueSet.DOMAIN_RESOURCE)); | ||
|
||
assertInteractions(restDefinition, ResourceType.ValueSet.PATIENT, patientInteractions); | ||
assertInteractions(restDefinition, ResourceType.ValueSet.PRACTITIONER, practitionerInteractions); | ||
} | ||
|
||
private void assertInteractions(CapabilityStatement.Rest restDefinition, ResourceType.ValueSet resourceType, int numOfInteractions) { | ||
Optional<CapabilityStatement.Rest.Resource> resource = restDefinition.getResource().stream() | ||
.filter(r -> r.getType().getValueAsEnumConstant() == resourceType) | ||
.findFirst(); | ||
assertTrue(resource.isPresent()); | ||
|
||
List<Interaction> interactions = resource.get().getInteraction(); | ||
assertEquals(interactions.size(), numOfInteractions, "Number of supported interactions for the Patient resource type"); | ||
} | ||
|
||
/** | ||
* This class is required because Capabilities uses a few protected fields | ||
* that are normally injected by JAX-RS and so this is the only way to set them. | ||
*/ | ||
private static class CapabilitiesChild extends Capabilities { | ||
public CapabilitiesChild() throws Exception { | ||
super(); | ||
this.context = new MockServletContext(); | ||
} | ||
|
||
@Override | ||
public Response capabilities() { | ||
httpServletRequest = new MockHttpServletRequest(); | ||
return super.capabilities(); | ||
} | ||
} | ||
} |
Oops, something went wrong.