Finished tests

This commit is contained in:
Nick Goupinets 2021-03-09 16:17:13 -05:00
parent 02eae95c85
commit 052919eb33
8 changed files with 394 additions and 732 deletions

View File

@ -42,6 +42,11 @@ public class AddressValidatingInterceptor extends ServerOperationInterceptorAdap
start(myProperties);
}
public AddressValidatingInterceptor(Properties theProperties) {
super();
start(theProperties);
}
public void start(Properties theProperties) {
if (!theProperties.containsKey(PROPERTY_VALIDATOR_CLASS)) {
ourLog.info("Address validator class is not defined. Validation is disabled");

View File

@ -1,63 +0,0 @@
package ca.uhn.fhir.rest.server.interceptor.validation.address.impl;
import ca.uhn.fhir.context.FhirContext;
import com.fasterxml.jackson.databind.JsonNode;
import ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidationResult;
import org.hl7.fhir.instance.model.api.IBase;
//import org.hl7.fhir.r4.model.Address;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class BaseRestfulValidatorTest {
//
// @Test
// public void testHappyPath() throws Exception {
// ResponseEntity responseEntity = mock(ResponseEntity.class);
// when(responseEntity.getStatusCode()).thenReturn(HttpStatus.OK);
// when(responseEntity.getBody()).thenReturn("{}");
//
// TestRestfulValidator val = spy(new TestRestfulValidator(responseEntity));
// assertNotNull(val.isValid(new Address(), FhirContext.forR4()));
//
// verify(val, times(1)).getResponseEntity(any(IBase.class), any(FhirContext.class));
// verify(val, times(1)).getValidationResult(any(), any(), any());
// }
//
// @Test
// public void testIsValid() throws Exception {
// ResponseEntity responseEntity = mock(ResponseEntity.class);
// when(responseEntity.getStatusCode()).thenReturn(HttpStatus.REQUEST_TIMEOUT);
//
// TestRestfulValidator val = new TestRestfulValidator(responseEntity);
// try {
// assertNotNull(val.isValid(new Address(), FhirContext.forR4()));
// fail();
// } catch (Exception e) {
// }
// }
//
// private static class TestRestfulValidator extends BaseRestfulValidator {
// ResponseEntity<String> myResponseEntity;
//
// public TestRestfulValidator(ResponseEntity<String> theResponseEntity) {
// super(null);
// myResponseEntity = theResponseEntity;
// }
//
// @Override
// protected AddressValidationResult getValidationResult(AddressValidationResult theResult, JsonNode response, FhirContext theFhirContext) throws Exception {
// return new AddressValidationResult();
// }
//
// @Override
// protected ResponseEntity<String> getResponseEntity(IBase theAddress, FhirContext theFhirContext) throws Exception {
// return myResponseEntity;
// }
// }
//
}

View File

@ -1,144 +0,0 @@
package ca.uhn.fhir.rest.server.interceptor.validation.address.impl;
import ca.uhn.fhir.context.FhirContext;
import com.fasterxml.jackson.core.JsonProcessingException;
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.hl7.fhir.r4.model.Address;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpEntity;
import org.springframework.web.client.RestTemplate;
//import javax.validation.constraints.NotNull;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.*;
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;
class LoquateAddressValidatorTest {
// private static final String REQUEST = "{\n" +
// " \"Key\" : \"MY_KEY\",\n" +
// " \"Geocode\" : false,\n" +
// " \"Addresses\" : [ {\n" +
// " \"Address1\" : \"Line 1\",\n" +
// " \"Address2\" : \"Line 2\",\n" +
// " \"Locality\" : \"City\",\n" +
// " \"PostalCode\" : \"POSTAL\",\n" +
// " \"Country\" : \"Country\"\n" +
// " } ]\n" +
// "}";
//
// private static final String RESPONSE_INVALID_ADDRESS = "[\n" +
// " {\n" +
// " \"Input\": {\n" +
// " \"Address\": \"\"\n" +
// " },\n" +
// " \"Matches\": [\n" +
// " {\n" +
// " \"AQI\": \"C\",\n" +
// " \"Address\": \"\"\n" +
// " }\n" +
// " ]\n" +
// " }\n" +
// "]";
//
// private static final String RESPONSE_VALID_ADDRESS = "[\n" +
// " {\n" +
// " \"Input\": {\n" +
// " \"Address\": \"\"\n" +
// " },\n" +
// " \"Matches\": [\n" +
// " {\n" +
// " \"AQI\": \"A\",\n" +
// " \"Address\": \"My Valid Address\"\n" +
// " }\n" +
// " ]\n" +
// " }\n" +
// "]";
//
// private static final String RESPONSE_INVALID_KEY = "{\n" +
// " \"Number\": 2,\n" +
// " \"Description\": \"Unknown key\",\n" +
// " \"Cause\": \"The key you are using to access the service was not found.\",\n" +
// " \"Resolution\": \"Please check that the key is correct. It should be in the form AA11-AA11-AA11-AA11.\"\n" +
// "}";
//
// private static FhirContext ourCtx = FhirContext.forR4();
//
// private LoquateAddressValidator myValidator;
//
// private Properties myProperties;
//
// @BeforeEach
// public void initValidator() {
// myProperties = new Properties();
// myProperties.setProperty(LoquateAddressValidator.PROPERTY_SERVICE_KEY, "MY_KEY");
// myValidator = new LoquateAddressValidator(myProperties);
// }
//
// @Test
// public void testRequestBody() {
// try {
// assertEquals(REQUEST, myValidator.getRequestBody(ourCtx, getAddress()));
// } catch (JsonProcessingException e) {
// fail();
// }
// }
//
// @Test
// public void testServiceCalled() {
// Address address = getAddress();
//
// final RestTemplate template = mock(RestTemplate.class);
//
// LoquateAddressValidator val = new LoquateAddressValidator(myProperties) {
// @Override
// protected RestTemplate newTemplate() {
// return template;
// }
// };
//
// try {
// val.getResponseEntity(address, ourCtx);
// } catch (Exception e) {
// fail();
// }
//
// verify(template, times(1)).postForEntity(any(String.class), any(HttpEntity.class), eq(String.class));
// }
//
// @NotNull
// private Address getAddress() {
// Address address = new Address();
// address.addLine("Line 1").addLine("Line 2").setCity("City").setPostalCode("POSTAL").setCountry("Country");
// return address;
// }
//
// @Test
// public void testSuccessfulResponses() throws Exception {
// AddressValidationResult res = myValidator.getValidationResult(new AddressValidationResult(),
// new ObjectMapper().readTree(RESPONSE_INVALID_ADDRESS), ourCtx);
// assertFalse(res.isValid());
//
// res = myValidator.getValidationResult(new AddressValidationResult(),
// new ObjectMapper().readTree(RESPONSE_VALID_ADDRESS), ourCtx);
// assertTrue(res.isValid());
// assertEquals("My Valid Address", res.getValidatedAddressString());
// }
//
// @Test
// public void testErrorResponses() throws Exception {
// assertThrows(AddressValidationException.class, () -> {
// myValidator.getValidationResult(new AddressValidationResult(),
// new ObjectMapper().readTree(RESPONSE_INVALID_KEY), ourCtx);
// });
// }
}

View File

@ -1,140 +0,0 @@
package ca.uhn.fhir.rest.server.interceptor.validation.address.impl;
import ca.uhn.fhir.context.FhirContext;
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.hl7.fhir.r4.model.Address;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
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;
class MelissaAddressValidatorTest {
//
// private static final String RESPONSE_INVALID_ADDRESS = "{\n" +
// " \"Version\": \"3.0.1.160\",\n" +
// " \"TransmissionReference\": \"1\",\n" +
// " \"TransmissionResults\": \"\",\n" +
// " \"TotalRecords\": \"1\",\n" +
// " \"Records\": [\n" +
// " {\n" +
// " \"RecordID\": \"1\",\n" +
// " \"Results\": \"AC01,AC12,AE02,AV12,GE02\",\n" +
// " \"FormattedAddress\": \"100 Main Street\",\n" +
// " \"Organization\": \"\",\n" +
// " \"AddressLine1\": \"100 Main Street\"\n" +
// " }\n" +
// " ]\n" +
// "}";
//
// private static final String RESPONSE_VALID_ADDRESS = "{\n" +
// " \"Version\": \"3.0.1.160\",\n" +
// " \"TransmissionReference\": \"1\",\n" +
// " \"TransmissionResults\": \"\",\n" +
// " \"TotalRecords\": \"1\",\n" +
// " \"Records\": [\n" +
// " {\n" +
// " \"RecordID\": \"1\",\n" +
// " \"Results\": \"AC01,AV24,GS05\",\n" +
// " \"FormattedAddress\": \"100 Main St W;Hamilton ON L8P 1H6\"\n" +
// " }\n" +
// " ]\n" +
// "}";
//
// private static final String RESPONSE_INVALID_KEY = "{\n" +
// " \"Version\": \"3.0.1.160\",\n" +
// " \"TransmissionReference\": \"1\",\n" +
// " \"TransmissionResults\": \"GE05\",\n" +
// " \"TotalRecords\": \"0\"\n" +
// "}";
//
// private static FhirContext ourContext = FhirContext.forR4();
//
// private MelissaAddressValidator myValidator;
//
// @BeforeEach
// public void init() {
// Properties props = new Properties();
// props.setProperty(MelissaAddressValidator.PROPERTY_SERVICE_KEY, "MY_KEY");
// myValidator = new MelissaAddressValidator(props);
//
// }
//
// @Test
// public void testRequestBody() {
// Map<String, String> params = myValidator.getRequestParams(getAddress());
//
// assertEquals("Line 1, Line 2", params.get("a1"));
// assertEquals("City, POSTAL", params.get("a2"));
// assertEquals("Country", params.get("ctry"));
// assertEquals("MY_KEY", params.get("id"));
// assertEquals("json", params.get("format"));
// assertTrue(params.containsKey("t"));
// }
//
// @Test
// public void testServiceCalled() {
// Address address = getAddress();
//
// final RestTemplate template = mock(RestTemplate.class);
//
// Properties props = new Properties();
// props.setProperty(BaseRestfulValidator.PROPERTY_SERVICE_KEY, "MY_KEY");
// MelissaAddressValidator val = new MelissaAddressValidator(props) {
// @Override
// protected RestTemplate newTemplate() {
// return template;
// }
// };
//
// try {
// val.getResponseEntity(address, ourContext);
// } catch (Exception e) {
// fail();
// }
//
// verify(template, times(1)).getForEntity(any(String.class), eq(String.class), any(Map.class));
// }
//
// @NotNull
// private Address getAddress() {
// Address address = new Address();
// address.addLine("Line 1").addLine("Line 2").setCity("City").setPostalCode("POSTAL").setCountry("Country");
// return address;
// }
//
// @Test
// public void testSuccessfulResponses() throws Exception {
// AddressValidationResult res = myValidator.getValidationResult(new AddressValidationResult(),
// new ObjectMapper().readTree(RESPONSE_INVALID_ADDRESS), ourContext);
// assertFalse(res.isValid());
//
// res = myValidator.getValidationResult(new AddressValidationResult(),
// new ObjectMapper().readTree(RESPONSE_VALID_ADDRESS), ourContext);
// assertTrue(res.isValid());
// assertEquals("100 Main St W;Hamilton ON L8P 1H6", res.getValidatedAddressString());
// }
//
// @Test
// public void testErrorResponses() throws Exception {
// assertThrows(AddressValidationException.class, () -> {
// myValidator.getValidationResult(new AddressValidationResult(),
// new ObjectMapper().readTree(RESPONSE_INVALID_KEY), ourContext);
// });
// }
}

View File

@ -2,15 +2,18 @@ package ca.uhn.fhir.rest.server.interceptor.validation.address;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.api.server.RequestDetails;
//import org.hl7.fhir.dstu3.model.Address;
//import org.hl7.fhir.dstu3.model.Person;
//import org.hl7.fhir.dstu3.model.StringType;
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.Person;
import org.hl7.fhir.r4.model.StringType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import javax.annotation.Nonnull;
import java.util.Properties;
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;
import static org.junit.jupiter.api.Assertions.assertNull;
@ -24,108 +27,111 @@ import static org.mockito.Mockito.when;
class AddressValidatingInterceptorTest {
// private static FhirContext ourCtx = FhirContext.forDstu3();
//
// private AddressValidatingInterceptor myInterceptor;
//
// private IAddressValidator myValidator;
//
// private RequestDetails myRequestDetails;
//
// @Test
// void start() throws Exception {
// AddressValidatingInterceptor interceptor = new AddressValidatingInterceptor();
// interceptor.start(new Properties());
// assertNull(interceptor.getAddressValidator());
//
// Properties props = new Properties();
// props.setProperty(AddressValidatingInterceptor.PROPERTY_VALIDATOR_CLASS, "RandomService");
// interceptor.setProperties(props);
// try {
// interceptor.start();
// fail();
// } catch (Exception e) {
// // expected
// }
//
// props.setProperty(AddressValidatingInterceptor.PROPERTY_VALIDATOR_CLASS, TestAddressValidator.class.getName());
// interceptor = new AddressValidatingInterceptor();
// interceptor.setProperties(props);
//
// interceptor.start();
// assertNotNull(interceptor.getAddressValidator());
// }
//
// @BeforeEach
// void setup() {
// myValidator = mock(IAddressValidator.class);
// when(myValidator.isValid(any(), any())).thenReturn(mock(AddressValidationResult.class));
//
// myRequestDetails = mock(RequestDetails.class);
// when(myRequestDetails.getFhirContext()).thenReturn(ourCtx);
//
// myInterceptor = new AddressValidatingInterceptor();
// myInterceptor.setAddressValidator(myValidator);
// }
//
// @Test
// void validate() {
// Address address = new Address();
// address.addLine("Line");
// address.setCity("City");
//
// myInterceptor.validateAddress(address, ourCtx);
// assertValidated(address, "invalid");
// }
//
// private void assertValidated(Address theAddress, String theValidationResult) {
// assertTrue(theAddress.hasExtension());
// assertEquals(1, theAddress.getExtension().size());
// assertEquals(IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL, theAddress.getExtensionFirstRep().getUrl());
// assertEquals(theValidationResult, theAddress.getExtensionFirstRep().getValueAsPrimitive().toString());
// }
//
// @Test
// void validateOnCreate() {
// Address address = new Address();
// address.addLine("Line");
// address.setCity("City");
//
// Person person = new Person();
// person.addAddress(address);
//
// myInterceptor.resourcePreCreate(myRequestDetails, person);
//
// assertValidated(person.getAddressFirstRep(), "invalid");
// }
//
// @Test
// void validateOnUpdate() {
// Address address = new Address();
// address.addLine("Line");
// address.setCity("City");
// address.addExtension(IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL, new StringType("..."));
//
// Address address2 = new Address();
// address2.addLine("Line 2");
// address2.setCity("City 2");
//
// Person person = new Person();
// person.addAddress(address);
// person.addAddress(address2);
//
// myInterceptor.resourcePreUpdate(myRequestDetails, null, person);
//
// verify(myValidator, times(1)).isValid(any(), any());
// assertValidated(person.getAddress().get(0), "...");
// assertValidated(person.getAddress().get(1), "invalid");
// }
//
// public static class TestAddressValidator implements IAddressValidator {
//
// @Override
// public AddressValidationResult isValid(IBase theAddress, FhirContext theFhirContext) throws AddressValidationException {
// return null;
// }
// }
private static FhirContext ourCtx = FhirContext.forR4();
private AddressValidatingInterceptor myInterceptor;
private IAddressValidator myValidator;
private RequestDetails myRequestDetails;
@Test
void start() throws Exception {
AddressValidatingInterceptor interceptor = new AddressValidatingInterceptor(new Properties());
assertNull(interceptor.getAddressValidator());
Properties props = new Properties();
props.setProperty(PROPERTY_VALIDATOR_CLASS, "RandomService");
try {
new AddressValidatingInterceptor(props);
fail();
} catch (Exception e) {
// expected
}
props.setProperty(PROPERTY_VALIDATOR_CLASS, TestAddressValidator.class.getName());
interceptor = new AddressValidatingInterceptor(props);
assertNotNull(interceptor.getAddressValidator());
}
@BeforeEach
void setup() {
myValidator = mock(IAddressValidator.class);
when(myValidator.isValid(any(), any())).thenReturn(mock(AddressValidationResult.class));
myRequestDetails = mock(RequestDetails.class);
when(myRequestDetails.getFhirContext()).thenReturn(ourCtx);
Properties properties = getProperties();
myInterceptor = new AddressValidatingInterceptor(properties);
myInterceptor.setAddressValidator(myValidator);
}
@Nonnull
private Properties getProperties() {
Properties properties = new Properties();
properties.setProperty(PROPERTY_VALIDATOR_CLASS, TestAddressValidator.class.getName());
return properties;
}
@Test
void validate() {
Address address = new Address();
address.addLine("Line");
address.setCity("City");
myInterceptor.validateAddress(address, ourCtx);
assertValidated(address, "invalid");
}
private void assertValidated(Address theAddress, String theValidationResult) {
assertTrue(theAddress.hasExtension());
assertEquals(1, theAddress.getExtension().size());
assertEquals(IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL, theAddress.getExtensionFirstRep().getUrl());
assertEquals(theValidationResult, theAddress.getExtensionFirstRep().getValueAsPrimitive().toString());
}
@Test
void validateOnCreate() {
Address address = new Address();
address.addLine("Line");
address.setCity("City");
Person person = new Person();
person.addAddress(address);
myInterceptor.resourcePreCreate(myRequestDetails, person);
assertValidated(person.getAddressFirstRep(), "invalid");
}
@Test
void validateOnUpdate() {
Address address = new Address();
address.addLine("Line");
address.setCity("City");
address.addExtension(IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL, new StringType("..."));
Address address2 = new Address();
address2.addLine("Line 2");
address2.setCity("City 2");
Person person = new Person();
person.addAddress(address);
person.addAddress(address2);
myInterceptor.resourcePreUpdate(myRequestDetails, null, person);
verify(myValidator, times(1)).isValid(any(), any());
assertValidated(person.getAddress().get(0), "...");
assertValidated(person.getAddress().get(1), "invalid");
}
public static class TestAddressValidator implements IAddressValidator {
@Override
public AddressValidationResult isValid(IBase theAddress, FhirContext theFhirContext) throws AddressValidationException {
return null;
}
}
}

View File

@ -4,7 +4,7 @@ import ca.uhn.fhir.context.FhirContext;
import com.fasterxml.jackson.databind.JsonNode;
import ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidationResult;
import org.hl7.fhir.instance.model.api.IBase;
//import org.hl7.fhir.r4.model.Address;
import org.hl7.fhir.r4.model.Address;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@ -13,51 +13,51 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class BaseRestfulValidatorTest {
//
// @Test
// public void testHappyPath() throws Exception {
// ResponseEntity responseEntity = mock(ResponseEntity.class);
// when(responseEntity.getStatusCode()).thenReturn(HttpStatus.OK);
// when(responseEntity.getBody()).thenReturn("{}");
//
// TestRestfulValidator val = spy(new TestRestfulValidator(responseEntity));
// assertNotNull(val.isValid(new Address(), FhirContext.forR4()));
//
// verify(val, times(1)).getResponseEntity(any(IBase.class), any(FhirContext.class));
// verify(val, times(1)).getValidationResult(any(), any(), any());
// }
//
// @Test
// public void testIsValid() throws Exception {
// ResponseEntity responseEntity = mock(ResponseEntity.class);
// when(responseEntity.getStatusCode()).thenReturn(HttpStatus.REQUEST_TIMEOUT);
//
// TestRestfulValidator val = new TestRestfulValidator(responseEntity);
// try {
// assertNotNull(val.isValid(new Address(), FhirContext.forR4()));
// fail();
// } catch (Exception e) {
// }
// }
//
// private static class TestRestfulValidator extends BaseRestfulValidator {
// ResponseEntity<String> myResponseEntity;
//
// public TestRestfulValidator(ResponseEntity<String> theResponseEntity) {
// super(null);
// myResponseEntity = theResponseEntity;
// }
//
// @Override
// protected AddressValidationResult getValidationResult(AddressValidationResult theResult, JsonNode response, FhirContext theFhirContext) throws Exception {
// return new AddressValidationResult();
// }
//
// @Override
// protected ResponseEntity<String> getResponseEntity(IBase theAddress, FhirContext theFhirContext) throws Exception {
// return myResponseEntity;
// }
// }
//
@Test
public void testHappyPath() throws Exception {
ResponseEntity responseEntity = mock(ResponseEntity.class);
when(responseEntity.getStatusCode()).thenReturn(HttpStatus.OK);
when(responseEntity.getBody()).thenReturn("{}");
TestRestfulValidator val = spy(new TestRestfulValidator(responseEntity));
assertNotNull(val.isValid(new Address(), FhirContext.forR4()));
verify(val, times(1)).getResponseEntity(any(IBase.class), any(FhirContext.class));
verify(val, times(1)).getValidationResult(any(), any(), any());
}
@Test
public void testIsValid() throws Exception {
ResponseEntity responseEntity = mock(ResponseEntity.class);
when(responseEntity.getStatusCode()).thenReturn(HttpStatus.REQUEST_TIMEOUT);
TestRestfulValidator val = new TestRestfulValidator(responseEntity);
try {
assertNotNull(val.isValid(new Address(), FhirContext.forR4()));
fail();
} catch (Exception e) {
}
}
private static class TestRestfulValidator extends BaseRestfulValidator {
ResponseEntity<String> myResponseEntity;
public TestRestfulValidator(ResponseEntity<String> theResponseEntity) {
super(null);
myResponseEntity = theResponseEntity;
}
@Override
protected AddressValidationResult getValidationResult(AddressValidationResult theResult, JsonNode response, FhirContext theFhirContext) throws Exception {
return new AddressValidationResult();
}
@Override
protected ResponseEntity<String> getResponseEntity(IBase theAddress, FhirContext theFhirContext) throws Exception {
return myResponseEntity;
}
}
}

View File

@ -5,7 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
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.hl7.fhir.r4.model.Address;
import org.hl7.fhir.r4.model.Address;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpEntity;
@ -23,122 +23,121 @@ import static org.mockito.Mockito.verify;
class LoquateAddressValidatorTest {
// private static final String REQUEST = "{\n" +
// " \"Key\" : \"MY_KEY\",\n" +
// " \"Geocode\" : false,\n" +
// " \"Addresses\" : [ {\n" +
// " \"Address1\" : \"Line 1\",\n" +
// " \"Address2\" : \"Line 2\",\n" +
// " \"Locality\" : \"City\",\n" +
// " \"PostalCode\" : \"POSTAL\",\n" +
// " \"Country\" : \"Country\"\n" +
// " } ]\n" +
// "}";
//
// private static final String RESPONSE_INVALID_ADDRESS = "[\n" +
// " {\n" +
// " \"Input\": {\n" +
// " \"Address\": \"\"\n" +
// " },\n" +
// " \"Matches\": [\n" +
// " {\n" +
// " \"AQI\": \"C\",\n" +
// " \"Address\": \"\"\n" +
// " }\n" +
// " ]\n" +
// " }\n" +
// "]";
//
// private static final String RESPONSE_VALID_ADDRESS = "[\n" +
// " {\n" +
// " \"Input\": {\n" +
// " \"Address\": \"\"\n" +
// " },\n" +
// " \"Matches\": [\n" +
// " {\n" +
// " \"AQI\": \"A\",\n" +
// " \"Address\": \"My Valid Address\"\n" +
// " }\n" +
// " ]\n" +
// " }\n" +
// "]";
//
// private static final String RESPONSE_INVALID_KEY = "{\n" +
// " \"Number\": 2,\n" +
// " \"Description\": \"Unknown key\",\n" +
// " \"Cause\": \"The key you are using to access the service was not found.\",\n" +
// " \"Resolution\": \"Please check that the key is correct. It should be in the form AA11-AA11-AA11-AA11.\"\n" +
// "}";
//
// private static FhirContext ourCtx = FhirContext.forR4();
//
// private LoquateAddressValidator myValidator;
//
// private Properties myProperties;
//
// @BeforeEach
// public void initValidator() {
// myProperties = new Properties();
// myProperties.setProperty(LoquateAddressValidator.PROPERTY_SERVICE_KEY, "MY_KEY");
// myValidator = new LoquateAddressValidator(myProperties);
// }
//
// @Test
// public void testRequestBody() {
// try {
// assertEquals(REQUEST, myValidator.getRequestBody(ourCtx, getAddress()));
// } catch (JsonProcessingException e) {
// fail();
// }
// }
//
// @Test
// public void testServiceCalled() {
// Address address = getAddress();
//
// final RestTemplate template = mock(RestTemplate.class);
//
// LoquateAddressValidator val = new LoquateAddressValidator(myProperties) {
// @Override
// protected RestTemplate newTemplate() {
// return template;
// }
// };
//
// try {
// val.getResponseEntity(address, ourCtx);
// } catch (Exception e) {
// fail();
// }
//
// verify(template, times(1)).postForEntity(any(String.class), any(HttpEntity.class), eq(String.class));
// }
//
// @NotNull
// private Address getAddress() {
// Address address = new Address();
// address.addLine("Line 1").addLine("Line 2").setCity("City").setPostalCode("POSTAL").setCountry("Country");
// return address;
// }
//
// @Test
// public void testSuccessfulResponses() throws Exception {
// AddressValidationResult res = myValidator.getValidationResult(new AddressValidationResult(),
// new ObjectMapper().readTree(RESPONSE_INVALID_ADDRESS), ourCtx);
// assertFalse(res.isValid());
//
// res = myValidator.getValidationResult(new AddressValidationResult(),
// new ObjectMapper().readTree(RESPONSE_VALID_ADDRESS), ourCtx);
// assertTrue(res.isValid());
// assertEquals("My Valid Address", res.getValidatedAddressString());
// }
//
// @Test
// public void testErrorResponses() throws Exception {
// assertThrows(AddressValidationException.class, () -> {
// myValidator.getValidationResult(new AddressValidationResult(),
// new ObjectMapper().readTree(RESPONSE_INVALID_KEY), ourCtx);
// });
// }
private static final String REQUEST = "{\n" +
" \"Key\" : \"MY_KEY\",\n" +
" \"Geocode\" : false,\n" +
" \"Addresses\" : [ {\n" +
" \"Address1\" : \"Line 1\",\n" +
" \"Address2\" : \"Line 2\",\n" +
" \"Locality\" : \"City\",\n" +
" \"PostalCode\" : \"POSTAL\",\n" +
" \"Country\" : \"Country\"\n" +
" } ]\n" +
"}";
private static final String RESPONSE_INVALID_ADDRESS = "[\n" +
" {\n" +
" \"Input\": {\n" +
" \"Address\": \"\"\n" +
" },\n" +
" \"Matches\": [\n" +
" {\n" +
" \"AQI\": \"C\",\n" +
" \"Address\": \"\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"]";
private static final String RESPONSE_VALID_ADDRESS = "[\n" +
" {\n" +
" \"Input\": {\n" +
" \"Address\": \"\"\n" +
" },\n" +
" \"Matches\": [\n" +
" {\n" +
" \"AQI\": \"A\",\n" +
" \"Address\": \"My Valid Address\"\n" +
" }\n" +
" ]\n" +
" }\n" +
"]";
private static final String RESPONSE_INVALID_KEY = "{\n" +
" \"Number\": 2,\n" +
" \"Description\": \"Unknown key\",\n" +
" \"Cause\": \"The key you are using to access the service was not found.\",\n" +
" \"Resolution\": \"Please check that the key is correct. It should be in the form AA11-AA11-AA11-AA11.\"\n" +
"}";
private static FhirContext ourCtx = FhirContext.forR4();
private LoquateAddressValidator myValidator;
private Properties myProperties;
@BeforeEach
public void initValidator() {
myProperties = new Properties();
myProperties.setProperty(LoquateAddressValidator.PROPERTY_SERVICE_KEY, "MY_KEY");
myValidator = new LoquateAddressValidator(myProperties);
}
@Test
public void testRequestBody() {
try {
assertEquals(REQUEST, myValidator.getRequestBody(ourCtx, getAddress()));
} catch (JsonProcessingException e) {
fail();
}
}
@Test
public void testServiceCalled() {
Address address = getAddress();
final RestTemplate template = mock(RestTemplate.class);
LoquateAddressValidator val = new LoquateAddressValidator(myProperties) {
@Override
protected RestTemplate newTemplate() {
return template;
}
};
try {
val.getResponseEntity(address, ourCtx);
} catch (Exception e) {
fail();
}
verify(template, times(1)).postForEntity(any(String.class), any(HttpEntity.class), eq(String.class));
}
private Address getAddress() {
Address address = new Address();
address.addLine("Line 1").addLine("Line 2").setCity("City").setPostalCode("POSTAL").setCountry("Country");
return address;
}
@Test
public void testSuccessfulResponses() throws Exception {
AddressValidationResult res = myValidator.getValidationResult(new AddressValidationResult(),
new ObjectMapper().readTree(RESPONSE_INVALID_ADDRESS), ourCtx);
assertFalse(res.isValid());
res = myValidator.getValidationResult(new AddressValidationResult(),
new ObjectMapper().readTree(RESPONSE_VALID_ADDRESS), ourCtx);
assertTrue(res.isValid());
assertEquals("My Valid Address", res.getValidatedAddressString());
}
@Test
public void testErrorResponses() throws Exception {
assertThrows(AddressValidationException.class, () -> {
myValidator.getValidationResult(new AddressValidationResult(),
new ObjectMapper().readTree(RESPONSE_INVALID_KEY), ourCtx);
});
}
}

View File

@ -4,7 +4,7 @@ import ca.uhn.fhir.context.FhirContext;
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.hl7.fhir.r4.model.Address;
import org.hl7.fhir.r4.model.Address;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.web.client.RestTemplate;
@ -24,117 +24,116 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
class MelissaAddressValidatorTest {
//
// private static final String RESPONSE_INVALID_ADDRESS = "{\n" +
// " \"Version\": \"3.0.1.160\",\n" +
// " \"TransmissionReference\": \"1\",\n" +
// " \"TransmissionResults\": \"\",\n" +
// " \"TotalRecords\": \"1\",\n" +
// " \"Records\": [\n" +
// " {\n" +
// " \"RecordID\": \"1\",\n" +
// " \"Results\": \"AC01,AC12,AE02,AV12,GE02\",\n" +
// " \"FormattedAddress\": \"100 Main Street\",\n" +
// " \"Organization\": \"\",\n" +
// " \"AddressLine1\": \"100 Main Street\"\n" +
// " }\n" +
// " ]\n" +
// "}";
//
// private static final String RESPONSE_VALID_ADDRESS = "{\n" +
// " \"Version\": \"3.0.1.160\",\n" +
// " \"TransmissionReference\": \"1\",\n" +
// " \"TransmissionResults\": \"\",\n" +
// " \"TotalRecords\": \"1\",\n" +
// " \"Records\": [\n" +
// " {\n" +
// " \"RecordID\": \"1\",\n" +
// " \"Results\": \"AC01,AV24,GS05\",\n" +
// " \"FormattedAddress\": \"100 Main St W;Hamilton ON L8P 1H6\"\n" +
// " }\n" +
// " ]\n" +
// "}";
//
// private static final String RESPONSE_INVALID_KEY = "{\n" +
// " \"Version\": \"3.0.1.160\",\n" +
// " \"TransmissionReference\": \"1\",\n" +
// " \"TransmissionResults\": \"GE05\",\n" +
// " \"TotalRecords\": \"0\"\n" +
// "}";
//
// private static FhirContext ourContext = FhirContext.forR4();
//
// private MelissaAddressValidator myValidator;
//
// @BeforeEach
// public void init() {
// Properties props = new Properties();
// props.setProperty(MelissaAddressValidator.PROPERTY_SERVICE_KEY, "MY_KEY");
// myValidator = new MelissaAddressValidator(props);
//
// }
//
// @Test
// public void testRequestBody() {
// Map<String, String> params = myValidator.getRequestParams(getAddress());
//
// assertEquals("Line 1, Line 2", params.get("a1"));
// assertEquals("City, POSTAL", params.get("a2"));
// assertEquals("Country", params.get("ctry"));
// assertEquals("MY_KEY", params.get("id"));
// assertEquals("json", params.get("format"));
// assertTrue(params.containsKey("t"));
// }
//
// @Test
// public void testServiceCalled() {
// Address address = getAddress();
//
// final RestTemplate template = mock(RestTemplate.class);
//
// Properties props = new Properties();
// props.setProperty(BaseRestfulValidator.PROPERTY_SERVICE_KEY, "MY_KEY");
// MelissaAddressValidator val = new MelissaAddressValidator(props) {
// @Override
// protected RestTemplate newTemplate() {
// return template;
// }
// };
//
// try {
// val.getResponseEntity(address, ourContext);
// } catch (Exception e) {
// fail();
// }
//
// verify(template, times(1)).getForEntity(any(String.class), eq(String.class), any(Map.class));
// }
//
// @NotNull
// private Address getAddress() {
// Address address = new Address();
// address.addLine("Line 1").addLine("Line 2").setCity("City").setPostalCode("POSTAL").setCountry("Country");
// return address;
// }
//
// @Test
// public void testSuccessfulResponses() throws Exception {
// AddressValidationResult res = myValidator.getValidationResult(new AddressValidationResult(),
// new ObjectMapper().readTree(RESPONSE_INVALID_ADDRESS), ourContext);
// assertFalse(res.isValid());
//
// res = myValidator.getValidationResult(new AddressValidationResult(),
// new ObjectMapper().readTree(RESPONSE_VALID_ADDRESS), ourContext);
// assertTrue(res.isValid());
// assertEquals("100 Main St W;Hamilton ON L8P 1H6", res.getValidatedAddressString());
// }
//
// @Test
// public void testErrorResponses() throws Exception {
// assertThrows(AddressValidationException.class, () -> {
// myValidator.getValidationResult(new AddressValidationResult(),
// new ObjectMapper().readTree(RESPONSE_INVALID_KEY), ourContext);
// });
// }
private static final String RESPONSE_INVALID_ADDRESS = "{\n" +
" \"Version\": \"3.0.1.160\",\n" +
" \"TransmissionReference\": \"1\",\n" +
" \"TransmissionResults\": \"\",\n" +
" \"TotalRecords\": \"1\",\n" +
" \"Records\": [\n" +
" {\n" +
" \"RecordID\": \"1\",\n" +
" \"Results\": \"AC01,AC12,AE02,AV12,GE02\",\n" +
" \"FormattedAddress\": \"100 Main Street\",\n" +
" \"Organization\": \"\",\n" +
" \"AddressLine1\": \"100 Main Street\"\n" +
" }\n" +
" ]\n" +
"}";
private static final String RESPONSE_VALID_ADDRESS = "{\n" +
" \"Version\": \"3.0.1.160\",\n" +
" \"TransmissionReference\": \"1\",\n" +
" \"TransmissionResults\": \"\",\n" +
" \"TotalRecords\": \"1\",\n" +
" \"Records\": [\n" +
" {\n" +
" \"RecordID\": \"1\",\n" +
" \"Results\": \"AC01,AV24,GS05\",\n" +
" \"FormattedAddress\": \"100 Main St W;Hamilton ON L8P 1H6\"\n" +
" }\n" +
" ]\n" +
"}";
private static final String RESPONSE_INVALID_KEY = "{\n" +
" \"Version\": \"3.0.1.160\",\n" +
" \"TransmissionReference\": \"1\",\n" +
" \"TransmissionResults\": \"GE05\",\n" +
" \"TotalRecords\": \"0\"\n" +
"}";
private static FhirContext ourContext = FhirContext.forR4();
private MelissaAddressValidator myValidator;
@BeforeEach
public void init() {
Properties props = new Properties();
props.setProperty(MelissaAddressValidator.PROPERTY_SERVICE_KEY, "MY_KEY");
myValidator = new MelissaAddressValidator(props);
}
@Test
public void testRequestBody() {
Map<String, String> params = myValidator.getRequestParams(getAddress());
assertEquals("Line 1, Line 2", params.get("a1"));
assertEquals("City, POSTAL", params.get("a2"));
assertEquals("Country", params.get("ctry"));
assertEquals("MY_KEY", params.get("id"));
assertEquals("json", params.get("format"));
assertTrue(params.containsKey("t"));
}
@Test
public void testServiceCalled() {
Address address = getAddress();
final RestTemplate template = mock(RestTemplate.class);
Properties props = new Properties();
props.setProperty(BaseRestfulValidator.PROPERTY_SERVICE_KEY, "MY_KEY");
MelissaAddressValidator val = new MelissaAddressValidator(props) {
@Override
protected RestTemplate newTemplate() {
return template;
}
};
try {
val.getResponseEntity(address, ourContext);
} catch (Exception e) {
fail();
}
verify(template, times(1)).getForEntity(any(String.class), eq(String.class), any(Map.class));
}
private Address getAddress() {
Address address = new Address();
address.addLine("Line 1").addLine("Line 2").setCity("City").setPostalCode("POSTAL").setCountry("Country");
return address;
}
@Test
public void testSuccessfulResponses() throws Exception {
AddressValidationResult res = myValidator.getValidationResult(new AddressValidationResult(),
new ObjectMapper().readTree(RESPONSE_INVALID_ADDRESS), ourContext);
assertFalse(res.isValid());
res = myValidator.getValidationResult(new AddressValidationResult(),
new ObjectMapper().readTree(RESPONSE_VALID_ADDRESS), ourContext);
assertTrue(res.isValid());
assertEquals("100 Main St W;Hamilton ON L8P 1H6", res.getValidatedAddressString());
}
@Test
public void testErrorResponses() throws Exception {
assertThrows(AddressValidationException.class, () -> {
myValidator.getValidationResult(new AddressValidationResult(),
new ObjectMapper().readTree(RESPONSE_INVALID_KEY), ourContext);
});
}
}