From 7cef07b0c13ba702b824d305b0e2cb794d1d52c7 Mon Sep 17 00:00:00 2001 From: Nick Goupinets Date: Tue, 27 Apr 2021 11:47:57 -0400 Subject: [PATCH] Improved test coverage --- .../address/impl/BaseRestfulValidator.java | 1 + .../AddressValidatingInterceptorTest.java | 42 +++++++++++++++++++ .../FieldValidatingInterceptorTest.java | 12 ++++++ 3 files changed, 55 insertions(+) diff --git a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/validation/address/impl/BaseRestfulValidator.java b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/validation/address/impl/BaseRestfulValidator.java index 799ef5877a1..de74741adb7 100644 --- a/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/validation/address/impl/BaseRestfulValidator.java +++ b/hapi-fhir-server/src/main/java/ca/uhn/fhir/rest/server/interceptor/validation/address/impl/BaseRestfulValidator.java @@ -26,6 +26,7 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidationException; import ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidationResult; +import org.apache.commons.lang3.Validate; import org.hl7.fhir.instance.model.api.IBase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; 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 b1f5ffe480f..355d9fd53d3 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 @@ -3,6 +3,7 @@ package ca.uhn.fhir.rest.server.interceptor.validation.address; import ca.uhn.fhir.context.FhirContext; import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.server.interceptor.validation.address.impl.LoquateAddressValidator; +import org.checkerframework.checker.units.qual.A; import org.hl7.fhir.instance.model.api.IBase; import org.hl7.fhir.r4.model.Address; import org.hl7.fhir.r4.model.Extension; @@ -15,10 +16,13 @@ import org.mockito.Mockito; import javax.annotation.Nonnull; import java.util.Arrays; +import java.util.HashMap; import java.util.Properties; 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_EXTENSION_URL; import static ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidatingInterceptor.PROPERTY_VALIDATOR_CLASS; +import static ca.uhn.fhir.rest.server.interceptor.validation.address.IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL; import static ca.uhn.fhir.rest.server.interceptor.validation.address.impl.BaseRestfulValidator.PROPERTY_SERVICE_KEY; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -91,6 +95,22 @@ class AddressValidatingInterceptorTest { assertNotNull(interceptor.getAddressValidator()); } + @Test + public void testEmptyRequest() { + try { + myInterceptor.handleRequest(null, null); + } catch (Exception ex) { + fail(); + } + + try { + myInterceptor.setAddressValidator(null); + myInterceptor.handleRequest(null, null); + } catch (Exception ex) { + fail(); + } + } + @BeforeEach void setup() { myValidator = mock(IAddressValidator.class); @@ -135,6 +155,28 @@ class AddressValidatingInterceptorTest { assertEquals("error", ext.getExtensionFirstRep().getUrl()); } + @Test + public void testValidationWithCustomUrl() { + myInterceptor.getProperties().setProperty(PROPERTY_EXTENSION_URL, "MY_URL"); + Address address = new Address(); + address.setCity("City"); + address.addLine("Line"); + AddressValidationResult res = new AddressValidationResult(); + res.setValidatedAddressString("City, Line"); + res.setValidatedAddress(address); + when(myValidator.isValid(any(), any())).thenReturn(res); + + Address addressToValidate = new Address(); + myInterceptor.validateAddress(addressToValidate, ourCtx); + + assertNotNull(res.toString()); + assertTrue(addressToValidate.hasExtension()); + assertNotNull(addressToValidate.getExtensionByUrl("MY_URL")); + assertFalse(address.hasExtension()); + assertEquals(address.getCity(), addressToValidate.getCity()); + assertTrue(address.getLine().get(0).equalsDeep(addressToValidate.getLine().get(0))); + } + @Test void validate() { Address address = new Address(); 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 075bab1d3f8..d3c14a77244 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 @@ -12,6 +12,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Arrays; +import java.util.HashMap; 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; @@ -38,6 +39,17 @@ class FieldValidatingInterceptorTest { myInterceptor = new FieldValidatingInterceptor(); } + @Test + public void testEmptyRequests() { + try { + myInterceptor.setConfig(new HashMap<>()); + myInterceptor.resourcePreCreate(null, null); + myInterceptor.resourcePreUpdate(null, null, null); + } catch (Exception ex) { + fail(); + } + } + @Test public void testDisablingValidationViaHeader() { RequestDetails request = newRequestDetails();