mirror of
https://github.com/hapifhir/hapi-fhir.git
synced 2025-03-25 01:18:37 +00:00
Add utils for testing Optional, bulk-export polling, and http requests. (#4452)
* Various utils for testing Optional, bulk-export polling, group test data building, and http requests.
This commit is contained in:
parent
663834ddfe
commit
fb185dc120
@ -28,8 +28,10 @@ import org.hl7.fhir.instance.model.api.IIdType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MethodOutcome {
|
||||
|
||||
@ -190,6 +192,9 @@ public class MethodOutcome {
|
||||
* Gets the headers for the HTTP response
|
||||
*/
|
||||
public Map<String, List<String>> getResponseHeaders() {
|
||||
if (myResponseHeaders == null) {
|
||||
myResponseHeaders = new HashMap<>();
|
||||
}
|
||||
return myResponseHeaders;
|
||||
}
|
||||
|
||||
@ -200,6 +205,17 @@ public class MethodOutcome {
|
||||
myResponseHeaders = theResponseHeaders;
|
||||
}
|
||||
|
||||
public Optional<String> getFirstResponseHeader(String theHeader) {
|
||||
List<String> values = getResponseHeaders().get(theHeader);
|
||||
|
||||
if (values == null || values.isEmpty()) {
|
||||
return Optional.empty();
|
||||
} else {
|
||||
return Optional.of(values.get(0));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Registers a callback to be invoked before the resource in this object gets
|
||||
* returned to the client. Note that this is an experimental API and may change.
|
||||
|
@ -0,0 +1,40 @@
|
||||
package ca.uhn.fhir.rest.api;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class MethodOutcomeTest {
|
||||
|
||||
private MethodOutcome myMethodOutcome;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
myMethodOutcome = new MethodOutcome();
|
||||
myMethodOutcome.setResponseHeaders(new HashMap<>());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFirstHeader_withNoHeaders_empty() {
|
||||
|
||||
Optional<String> firstHeader = myMethodOutcome.getFirstResponseHeader("some-header");
|
||||
|
||||
assertTrue(firstHeader.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getFirstHeader_withTwoHeaders_returnsFirst() {
|
||||
myMethodOutcome.getResponseHeaders().put("some-header", Arrays.asList("value1", "value2"));
|
||||
|
||||
Optional<String> firstHeader = myMethodOutcome.getFirstResponseHeader("some-header");
|
||||
|
||||
assertTrue(firstHeader.isPresent());
|
||||
assertEquals("value1", firstHeader.get());
|
||||
}
|
||||
|
||||
}
|
@ -5,6 +5,7 @@ import org.hl7.fhir.instance.model.api.IBaseResource;
|
||||
import org.hl7.fhir.instance.model.api.IIdType;
|
||||
import org.hl7.fhir.r4.model.CodeableConcept;
|
||||
import org.hl7.fhir.r4.model.Coding;
|
||||
import org.hl7.fhir.r4.model.Group;
|
||||
import org.hl7.fhir.r4.model.Observation;
|
||||
import org.hl7.fhir.r4.model.Quantity;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
@ -13,10 +14,11 @@ import org.junit.jupiter.api.Test;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ITestDataBuilderTest {
|
||||
|
||||
FhirContext myFhirContext = FhirContext.forR4Cached();
|
||||
|
||||
List<IBaseResource> myCreatedList = new ArrayList<>();
|
||||
@ -120,4 +122,18 @@ public class ITestDataBuilderTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void createGroup_withPatients_createsElementAndReference() {
|
||||
|
||||
myTDB.createGroup(
|
||||
myTDB.withGroupMember("Patient/123")
|
||||
);
|
||||
|
||||
assertEquals(1, myCreatedList.size());
|
||||
Group g = (Group) myCreatedList.get(0);
|
||||
assertEquals(1, g.getMember().size());
|
||||
assertTrue(g.getMember().get(0).hasEntity());
|
||||
assertEquals("Patient/123", g.getMember().get(0).getEntity().getReference());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -31,9 +31,11 @@ import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class BulkExportJobParametersValidator implements IJobParametersValidator<BulkExportJobParameters> {
|
||||
|
||||
public static final String UNSUPPORTED_BINARY_TYPE = "Binary";
|
||||
@Autowired
|
||||
private DaoRegistry myDaoRegistry;
|
||||
|
||||
@ -46,7 +48,7 @@ public class BulkExportJobParametersValidator implements IJobParametersValidator
|
||||
List<String> resourceTypes = theParameters.getResourceTypes();
|
||||
if (resourceTypes != null && !resourceTypes.isEmpty()) {
|
||||
for (String resourceType : theParameters.getResourceTypes()) {
|
||||
if (resourceType.equalsIgnoreCase("Binary")) {
|
||||
if (resourceType.equalsIgnoreCase(UNSUPPORTED_BINARY_TYPE)) {
|
||||
errorMsgs.add("Bulk export of Binary resources is forbidden");
|
||||
} else if (!myDaoRegistry.isResourceTypeSupported(resourceType)) {
|
||||
errorMsgs.add("Resource type " + resourceType + " is not a supported resource type!");
|
||||
|
@ -185,6 +185,10 @@ public interface ITestDataBuilder {
|
||||
return createResource("Encounter", theModifiers);
|
||||
}
|
||||
|
||||
default IIdType createGroup(Consumer<IBaseResource>... theModifiers) {
|
||||
return createResource("Group", theModifiers);
|
||||
}
|
||||
|
||||
default IIdType createObservation(Consumer<IBaseResource>... theModifiers) {
|
||||
return createResource("Observation", theModifiers);
|
||||
}
|
||||
@ -248,6 +252,22 @@ public interface ITestDataBuilder {
|
||||
return withSubject(new IdType(theSubject));
|
||||
}
|
||||
|
||||
default Consumer<IBaseResource> withPatient(@Nullable IIdType theSubject) {
|
||||
return withReference("patient", theSubject);
|
||||
}
|
||||
|
||||
default Consumer<IBaseResource> withPatient(@Nullable String theSubject) {
|
||||
return withSubject(new IdType(theSubject));
|
||||
}
|
||||
|
||||
default Consumer<IBaseResource> withGroupMember(@Nullable IIdType theMember) {
|
||||
return withPrimitiveAttribute("member.entity.reference", theMember);
|
||||
}
|
||||
|
||||
default Consumer<IBaseResource> withGroupMember(@Nullable String theMember) {
|
||||
return withGroupMember(new IdType(theMember));
|
||||
}
|
||||
|
||||
default Consumer<IBaseResource> withEncounter(@Nullable String theEncounter) {
|
||||
return withReference("encounter", new IdType(theEncounter));
|
||||
}
|
||||
@ -259,7 +279,7 @@ public interface ITestDataBuilder {
|
||||
IBaseReference reference = (IBaseReference) getFhirContext().getElementDefinition("Reference").newInstance();
|
||||
reference.setReference(theReferenceValue.getValue());
|
||||
|
||||
RuntimeResourceDefinition resourceDef = getFhirContext().getResourceDefinition(t.getClass());
|
||||
RuntimeResourceDefinition resourceDef = getFhirContext().getResourceDefinition(t);
|
||||
resourceDef.getChildByName(theReferenceName).getMutator().addValue(t, reference);
|
||||
}
|
||||
};
|
||||
|
11
pom.xml
11
pom.xml
@ -140,6 +140,12 @@
|
||||
<artifactId>hamcrest</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.npathai</groupId>
|
||||
<artifactId>hamcrest-optional</artifactId>
|
||||
<version>2.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
@ -1409,6 +1415,11 @@
|
||||
<artifactId>httpclient-cache</artifactId>
|
||||
<version>${httpclient_version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>fluent-hc</artifactId>
|
||||
<version>${httpclient_version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient-android</artifactId>
|
||||
|
Loading…
x
Reference in New Issue
Block a user