From 04a0ecfc2bf386f2786b7ad007acd31d54e2b312 Mon Sep 17 00:00:00 2001 From: Nick Goupinets Date: Mon, 15 Mar 2021 10:31:38 -0400 Subject: [PATCH] Added tests --- .../fields/FieldValidatingInterceptor.java | 1 + .../StandardizingInterceptorTest.java | 43 ++++++++++++++++++- .../AddressValidatingInterceptorTest.java | 29 ++++++++++++- .../impl/LoquateAddressValidatorTest.java | 27 ++++++++++++ .../FieldValidatingInterceptorTest.java | 27 ++++++++++++ 5 files changed, 124 insertions(+), 3 deletions(-) diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/validation/fields/FieldValidatingInterceptor.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/validation/fields/FieldValidatingInterceptor.java index 76f5382da47..36394169521 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/validation/fields/FieldValidatingInterceptor.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/validation/fields/FieldValidatingInterceptor.java @@ -75,6 +75,7 @@ public class FieldValidatingInterceptor { protected void handleRequest(RequestDetails theRequest, IBaseResource theResource) { if (!theRequest.getHeaders(VALIDATION_DISABLED_HEADER).isEmpty()) { ourLog.debug("Address validation is disabled for this request via header"); + return; } FhirContext ctx = theRequest.getFhirContext(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/s13n/interceptors/StandardizingInterceptorTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/s13n/interceptors/StandardizingInterceptorTest.java index a4cda65c723..d517954cdd9 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/s13n/interceptors/StandardizingInterceptorTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/s13n/interceptors/StandardizingInterceptorTest.java @@ -10,13 +10,19 @@ import org.hl7.fhir.r4.model.Patient; import org.hl7.fhir.r4.model.Person; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.Arrays; import java.util.Map; +import static ca.uhn.fhir.rest.server.interceptor.s13n.StandardizingInterceptor.STANDARDIZATION_DISABLED_HEADER; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; class StandardizingInterceptorTest { @@ -32,6 +38,7 @@ class StandardizingInterceptorTest { "\t\t\"telecom.where(system='phone').value\" : \"PHONE\"\n" + "\t\t}\n" + "}"; + private static final String BAD_CONFIG = "{ \"Person\" : { \"Person.name.family\" : \"org.nonexistent.Standardizer\"}}"; private static FhirContext ourCtx = FhirContext.forR4(); @@ -59,6 +66,38 @@ class StandardizingInterceptorTest { assertEquals("Jim O'Brian", p.getName().get(1).getNameAsSingleString()); } + @Test + public void testNullsWork() { + try { + myInterceptor.resourcePreCreate(myRequestDetails, null); + } catch (Exception e) { + fail(); + } + } + + @Test + public void testBadConfig() throws Exception { + myInterceptor = new StandardizingInterceptor(new ObjectMapper().readValue(BAD_CONFIG, Map.class)); + + try { + myInterceptor.resourcePreCreate(myRequestDetails, new Person()); + fail(); + } catch (Exception e) { + } + } + + @Test + public void testDisablingValidationViaHeader() { + when(myRequestDetails.getHeaders(eq(STANDARDIZATION_DISABLED_HEADER))).thenReturn(Arrays.asList(new String[]{"True"})); + + Person p = new Person(); + p.addName().setFamily("non'normalized").addGiven("name"); + + myInterceptor.resourcePreUpdate(myRequestDetails, null, p); + + assertEquals("name non'normalized", p.getName().get(0).getNameAsSingleString()); + } + @Test public void testTelecomStandardization() throws Exception { Person p = new Person(); diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/validation/address/AddressValidatingInterceptorTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/validation/address/AddressValidatingInterceptorTest.java index 2208d441866..61c1affa123 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/validation/address/AddressValidatingInterceptorTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/validation/address/AddressValidatingInterceptorTest.java @@ -9,10 +9,15 @@ import org.hl7.fhir.r4.model.Person; import org.hl7.fhir.r4.model.StringType; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.Mockito; import javax.annotation.Nonnull; +import java.util.Arrays; import java.util.Properties; +import static ca.uhn.fhir.rest.server.interceptor.s13n.StandardizingInterceptor.STANDARDIZATION_DISABLED_HEADER; +import static ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidatingInterceptor.ADDRESS_VALIDATION_DISABLED_HEADER; import static ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidatingInterceptor.PROPERTY_VALIDATOR_CLASS; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -20,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; @@ -74,6 +80,28 @@ class AddressValidatingInterceptorTest { return properties; } + @Test + public void testDisablingValidationViaHeader() { + when(myRequestDetails.getHeaders(eq(ADDRESS_VALIDATION_DISABLED_HEADER))).thenReturn(Arrays.asList(new String[]{"True"})); + + Person p = new Person(); + AddressValidatingInterceptor spy = Mockito.spy(myInterceptor); + spy.resourcePreCreate(myRequestDetails, p); + + Mockito.verify(spy, times(0)).validateAddress(any(), any()); + } + + @Test + public void testValidationServiceError() { + myValidator = mock(IAddressValidator.class); + when(myValidator.isValid(any(), any())).thenThrow(new RuntimeException()); + myInterceptor.setAddressValidator(myValidator); + + Address address = new Address(); + myInterceptor.validateAddress(address, ourCtx); + assertValidated(address, "not-validated"); + } + @Test void validate() { Address address = new Address(); @@ -128,7 +156,6 @@ class AddressValidatingInterceptorTest { } public static class TestAddressValidator implements IAddressValidator { - @Override public AddressValidationResult isValid(IBase theAddress, FhirContext theFhirContext) throws AddressValidationException { return null; diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/validation/address/impl/LoquateAddressValidatorTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/validation/address/impl/LoquateAddressValidatorTest.java index 8d3353f01a4..be047783458 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/validation/address/impl/LoquateAddressValidatorTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/validation/address/impl/LoquateAddressValidatorTest.java @@ -38,6 +38,14 @@ class LoquateAddressValidatorTest { " } ]\n" + "}"; + private static final String RESPONSE_INVALID = "[\n" + + " {\n" + + " \"Input\": {\n" + + " \"Address\": \"\"\n" + + " }\n" + + " }\n" + + "]"; + private static final String RESPONSE_INVALID_ADDRESS = "[\n" + " {\n" + " \"Input\": {\n" + @@ -86,6 +94,25 @@ class LoquateAddressValidatorTest { myValidator = new LoquateAddressValidator(myProperties); } + @Test + public void testInvalidInit() { + try { + new LoquateAddressValidator(new Properties()); + fail(); + } catch (Exception e) { + } + } + + @Test + public void testInvalidAddressValidationResponse() throws Exception { + try { + AddressValidationResult res = myValidator.getValidationResult(new AddressValidationResult(), + new ObjectMapper().readTree(RESPONSE_INVALID), ourCtx); + fail(); + } catch (AddressValidationException e) { + } + } + @Test public void testRequestBody() { try { diff --git a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/validation/fields/FieldValidatingInterceptorTest.java b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/validation/fields/FieldValidatingInterceptorTest.java index 1d8bae47853..533fc814777 100644 --- a/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/validation/fields/FieldValidatingInterceptorTest.java +++ b/hapi-fhir-structures-r4/src/test/java/ca/uhn/fhir/rest/server/interceptor/validation/fields/FieldValidatingInterceptorTest.java @@ -8,7 +8,12 @@ import org.hl7.fhir.r4.model.Person; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.util.Arrays; + +import static ca.uhn.fhir.rest.server.interceptor.s13n.StandardizingInterceptor.STANDARDIZATION_DISABLED_HEADER; +import static ca.uhn.fhir.rest.server.interceptor.validation.fields.FieldValidatingInterceptor.VALIDATION_DISABLED_HEADER; import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -28,6 +33,18 @@ class FieldValidatingInterceptorTest { myInterceptor = new FieldValidatingInterceptor(); } + @Test + public void testDisablingValidationViaHeader() { + RequestDetails request = newRequestDetails(); + when(request.getHeaders(eq(VALIDATION_DISABLED_HEADER))).thenReturn(Arrays.asList(new String[]{"True"})); + + Person person = new Person(); + person.addTelecom().setSystem(ContactPoint.ContactPointSystem.EMAIL).setValue("EMAIL"); + + myInterceptor.handleRequest(request, person); + assertEquals("EMAIL", person.getTelecom().get(0).getValue()); + } + @Test public void testEmailValidation() { Person person = new Person(); @@ -52,6 +69,16 @@ class FieldValidatingInterceptorTest { } } + @Test + public void testCustomInvalidValidation() { + myInterceptor.getConfig().put("telecom.where(system='phone').value", "ClassThatDoesntExist"); + try { + myInterceptor.handleRequest(newRequestDetails(), new Person()); + fail(); + } catch (Exception e) { + } + } + @Test public void testCustomValidation() { myInterceptor.getConfig().put("telecom.where(system='phone').value", EmptyValidator.class.getName());