Added tests
This commit is contained in:
parent
8c80e0838d
commit
04a0ecfc2b
|
@ -75,6 +75,7 @@ public class FieldValidatingInterceptor {
|
||||||
protected void handleRequest(RequestDetails theRequest, IBaseResource theResource) {
|
protected void handleRequest(RequestDetails theRequest, IBaseResource theResource) {
|
||||||
if (!theRequest.getHeaders(VALIDATION_DISABLED_HEADER).isEmpty()) {
|
if (!theRequest.getHeaders(VALIDATION_DISABLED_HEADER).isEmpty()) {
|
||||||
ourLog.debug("Address validation is disabled for this request via header");
|
ourLog.debug("Address validation is disabled for this request via header");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
FhirContext ctx = theRequest.getFhirContext();
|
FhirContext ctx = theRequest.getFhirContext();
|
||||||
|
|
|
@ -10,13 +10,19 @@ import org.hl7.fhir.r4.model.Patient;
|
||||||
import org.hl7.fhir.r4.model.Person;
|
import org.hl7.fhir.r4.model.Person;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
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 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.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
class StandardizingInterceptorTest {
|
class StandardizingInterceptorTest {
|
||||||
|
|
||||||
|
@ -32,6 +38,7 @@ class StandardizingInterceptorTest {
|
||||||
"\t\t\"telecom.where(system='phone').value\" : \"PHONE\"\n" +
|
"\t\t\"telecom.where(system='phone').value\" : \"PHONE\"\n" +
|
||||||
"\t\t}\n" +
|
"\t\t}\n" +
|
||||||
"}";
|
"}";
|
||||||
|
private static final String BAD_CONFIG = "{ \"Person\" : { \"Person.name.family\" : \"org.nonexistent.Standardizer\"}}";
|
||||||
|
|
||||||
private static FhirContext ourCtx = FhirContext.forR4();
|
private static FhirContext ourCtx = FhirContext.forR4();
|
||||||
|
|
||||||
|
@ -59,6 +66,38 @@ class StandardizingInterceptorTest {
|
||||||
assertEquals("Jim O'Brian", p.getName().get(1).getNameAsSingleString());
|
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
|
@Test
|
||||||
public void testTelecomStandardization() throws Exception {
|
public void testTelecomStandardization() throws Exception {
|
||||||
Person p = new Person();
|
Person p = new Person();
|
||||||
|
|
|
@ -9,10 +9,15 @@ import org.hl7.fhir.r4.model.Person;
|
||||||
import org.hl7.fhir.r4.model.StringType;
|
import org.hl7.fhir.r4.model.StringType;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Properties;
|
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 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.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
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.assertTrue;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.times;
|
import static org.mockito.Mockito.times;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
@ -74,6 +80,28 @@ class AddressValidatingInterceptorTest {
|
||||||
return properties;
|
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
|
@Test
|
||||||
void validate() {
|
void validate() {
|
||||||
Address address = new Address();
|
Address address = new Address();
|
||||||
|
@ -128,7 +156,6 @@ class AddressValidatingInterceptorTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class TestAddressValidator implements IAddressValidator {
|
public static class TestAddressValidator implements IAddressValidator {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AddressValidationResult isValid(IBase theAddress, FhirContext theFhirContext) throws AddressValidationException {
|
public AddressValidationResult isValid(IBase theAddress, FhirContext theFhirContext) throws AddressValidationException {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -38,6 +38,14 @@ class LoquateAddressValidatorTest {
|
||||||
" } ]\n" +
|
" } ]\n" +
|
||||||
"}";
|
"}";
|
||||||
|
|
||||||
|
private static final String RESPONSE_INVALID = "[\n" +
|
||||||
|
" {\n" +
|
||||||
|
" \"Input\": {\n" +
|
||||||
|
" \"Address\": \"\"\n" +
|
||||||
|
" }\n" +
|
||||||
|
" }\n" +
|
||||||
|
"]";
|
||||||
|
|
||||||
private static final String RESPONSE_INVALID_ADDRESS = "[\n" +
|
private static final String RESPONSE_INVALID_ADDRESS = "[\n" +
|
||||||
" {\n" +
|
" {\n" +
|
||||||
" \"Input\": {\n" +
|
" \"Input\": {\n" +
|
||||||
|
@ -86,6 +94,25 @@ class LoquateAddressValidatorTest {
|
||||||
myValidator = new LoquateAddressValidator(myProperties);
|
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
|
@Test
|
||||||
public void testRequestBody() {
|
public void testRequestBody() {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -8,7 +8,12 @@ import org.hl7.fhir.r4.model.Person;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
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.junit.jupiter.api.Assertions.*;
|
||||||
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ -28,6 +33,18 @@ class FieldValidatingInterceptorTest {
|
||||||
myInterceptor = new FieldValidatingInterceptor();
|
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
|
@Test
|
||||||
public void testEmailValidation() {
|
public void testEmailValidation() {
|
||||||
Person person = new Person();
|
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
|
@Test
|
||||||
public void testCustomValidation() {
|
public void testCustomValidation() {
|
||||||
myInterceptor.getConfig().put("telecom.where(system='phone').value", EmptyValidator.class.getName());
|
myInterceptor.getConfig().put("telecom.where(system='phone').value", EmptyValidator.class.getName());
|
||||||
|
|
Loading…
Reference in New Issue