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); start(myProperties);
} }
public AddressValidatingInterceptor(Properties theProperties) {
super();
start(theProperties);
}
public void start(Properties theProperties) { public void start(Properties theProperties) {
if (!theProperties.containsKey(PROPERTY_VALIDATOR_CLASS)) { if (!theProperties.containsKey(PROPERTY_VALIDATOR_CLASS)) {
ourLog.info("Address validator class is not defined. Validation is disabled"); 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.context.FhirContext;
import ca.uhn.fhir.rest.api.server.RequestDetails; import ca.uhn.fhir.rest.api.server.RequestDetails;
//import org.hl7.fhir.dstu3.model.Address; import org.checkerframework.checker.units.qual.A;
//import org.hl7.fhir.dstu3.model.Person;
//import org.hl7.fhir.dstu3.model.StringType;
import org.hl7.fhir.instance.model.api.IBase; 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.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import javax.annotation.Nonnull;
import java.util.Properties; 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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
@ -24,108 +27,111 @@ import static org.mockito.Mockito.when;
class AddressValidatingInterceptorTest { class AddressValidatingInterceptorTest {
// private static FhirContext ourCtx = FhirContext.forDstu3(); private static FhirContext ourCtx = FhirContext.forR4();
//
// private AddressValidatingInterceptor myInterceptor; private AddressValidatingInterceptor myInterceptor;
//
// private IAddressValidator myValidator; private IAddressValidator myValidator;
//
// private RequestDetails myRequestDetails; private RequestDetails myRequestDetails;
//
// @Test @Test
// void start() throws Exception { void start() throws Exception {
// AddressValidatingInterceptor interceptor = new AddressValidatingInterceptor(); AddressValidatingInterceptor interceptor = new AddressValidatingInterceptor(new Properties());
// interceptor.start(new Properties()); assertNull(interceptor.getAddressValidator());
// assertNull(interceptor.getAddressValidator());
// Properties props = new Properties();
// Properties props = new Properties(); props.setProperty(PROPERTY_VALIDATOR_CLASS, "RandomService");
// props.setProperty(AddressValidatingInterceptor.PROPERTY_VALIDATOR_CLASS, "RandomService"); try {
// interceptor.setProperties(props); new AddressValidatingInterceptor(props);
// try { fail();
// interceptor.start(); } catch (Exception e) {
// fail(); // expected
// } catch (Exception e) { }
// // expected
// } props.setProperty(PROPERTY_VALIDATOR_CLASS, TestAddressValidator.class.getName());
// interceptor = new AddressValidatingInterceptor(props);
// props.setProperty(AddressValidatingInterceptor.PROPERTY_VALIDATOR_CLASS, TestAddressValidator.class.getName()); assertNotNull(interceptor.getAddressValidator());
// interceptor = new AddressValidatingInterceptor(); }
// interceptor.setProperties(props);
// @BeforeEach
// interceptor.start(); void setup() {
// assertNotNull(interceptor.getAddressValidator()); myValidator = mock(IAddressValidator.class);
// } when(myValidator.isValid(any(), any())).thenReturn(mock(AddressValidationResult.class));
//
// @BeforeEach myRequestDetails = mock(RequestDetails.class);
// void setup() { when(myRequestDetails.getFhirContext()).thenReturn(ourCtx);
// myValidator = mock(IAddressValidator.class);
// when(myValidator.isValid(any(), any())).thenReturn(mock(AddressValidationResult.class)); Properties properties = getProperties();
// myInterceptor = new AddressValidatingInterceptor(properties);
// myRequestDetails = mock(RequestDetails.class); myInterceptor.setAddressValidator(myValidator);
// when(myRequestDetails.getFhirContext()).thenReturn(ourCtx); }
//
// myInterceptor = new AddressValidatingInterceptor(); @Nonnull
// myInterceptor.setAddressValidator(myValidator); private Properties getProperties() {
// } Properties properties = new Properties();
// properties.setProperty(PROPERTY_VALIDATOR_CLASS, TestAddressValidator.class.getName());
// @Test return properties;
// void validate() { }
// Address address = new Address();
// address.addLine("Line"); @Test
// address.setCity("City"); void validate() {
// Address address = new Address();
// myInterceptor.validateAddress(address, ourCtx); address.addLine("Line");
// assertValidated(address, "invalid"); address.setCity("City");
// }
// myInterceptor.validateAddress(address, ourCtx);
// private void assertValidated(Address theAddress, String theValidationResult) { assertValidated(address, "invalid");
// assertTrue(theAddress.hasExtension()); }
// assertEquals(1, theAddress.getExtension().size());
// assertEquals(IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL, theAddress.getExtensionFirstRep().getUrl()); private void assertValidated(Address theAddress, String theValidationResult) {
// assertEquals(theValidationResult, theAddress.getExtensionFirstRep().getValueAsPrimitive().toString()); assertTrue(theAddress.hasExtension());
// } assertEquals(1, theAddress.getExtension().size());
// assertEquals(IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL, theAddress.getExtensionFirstRep().getUrl());
// @Test assertEquals(theValidationResult, theAddress.getExtensionFirstRep().getValueAsPrimitive().toString());
// void validateOnCreate() { }
// Address address = new Address();
// address.addLine("Line"); @Test
// address.setCity("City"); void validateOnCreate() {
// Address address = new Address();
// Person person = new Person(); address.addLine("Line");
// person.addAddress(address); address.setCity("City");
//
// myInterceptor.resourcePreCreate(myRequestDetails, person); Person person = new Person();
// person.addAddress(address);
// assertValidated(person.getAddressFirstRep(), "invalid");
// } myInterceptor.resourcePreCreate(myRequestDetails, person);
//
// @Test assertValidated(person.getAddressFirstRep(), "invalid");
// void validateOnUpdate() { }
// Address address = new Address();
// address.addLine("Line"); @Test
// address.setCity("City"); void validateOnUpdate() {
// address.addExtension(IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL, new StringType("...")); Address address = new Address();
// address.addLine("Line");
// Address address2 = new Address(); address.setCity("City");
// address2.addLine("Line 2"); address.addExtension(IAddressValidator.ADDRESS_VALIDATION_EXTENSION_URL, new StringType("..."));
// address2.setCity("City 2");
// Address address2 = new Address();
// Person person = new Person(); address2.addLine("Line 2");
// person.addAddress(address); address2.setCity("City 2");
// person.addAddress(address2);
// Person person = new Person();
// myInterceptor.resourcePreUpdate(myRequestDetails, null, person); person.addAddress(address);
// person.addAddress(address2);
// verify(myValidator, times(1)).isValid(any(), any());
// assertValidated(person.getAddress().get(0), "..."); myInterceptor.resourcePreUpdate(myRequestDetails, null, person);
// assertValidated(person.getAddress().get(1), "invalid");
// } verify(myValidator, times(1)).isValid(any(), any());
// assertValidated(person.getAddress().get(0), "...");
// public static class TestAddressValidator implements IAddressValidator { assertValidated(person.getAddress().get(1), "invalid");
// }
// @Override
// public AddressValidationResult isValid(IBase theAddress, FhirContext theFhirContext) throws AddressValidationException { public static class TestAddressValidator implements IAddressValidator {
// return null;
// } @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 com.fasterxml.jackson.databind.JsonNode;
import ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidationResult; import ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidationResult;
import org.hl7.fhir.instance.model.api.IBase; 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.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -13,51 +13,51 @@ import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
class BaseRestfulValidatorTest { class BaseRestfulValidatorTest {
//
// @Test @Test
// public void testHappyPath() throws Exception { public void testHappyPath() throws Exception {
// ResponseEntity responseEntity = mock(ResponseEntity.class); ResponseEntity responseEntity = mock(ResponseEntity.class);
// when(responseEntity.getStatusCode()).thenReturn(HttpStatus.OK); when(responseEntity.getStatusCode()).thenReturn(HttpStatus.OK);
// when(responseEntity.getBody()).thenReturn("{}"); when(responseEntity.getBody()).thenReturn("{}");
//
// TestRestfulValidator val = spy(new TestRestfulValidator(responseEntity)); TestRestfulValidator val = spy(new TestRestfulValidator(responseEntity));
// assertNotNull(val.isValid(new Address(), FhirContext.forR4())); assertNotNull(val.isValid(new Address(), FhirContext.forR4()));
//
// verify(val, times(1)).getResponseEntity(any(IBase.class), any(FhirContext.class)); verify(val, times(1)).getResponseEntity(any(IBase.class), any(FhirContext.class));
// verify(val, times(1)).getValidationResult(any(), any(), any()); verify(val, times(1)).getValidationResult(any(), any(), any());
// } }
//
// @Test @Test
// public void testIsValid() throws Exception { public void testIsValid() throws Exception {
// ResponseEntity responseEntity = mock(ResponseEntity.class); ResponseEntity responseEntity = mock(ResponseEntity.class);
// when(responseEntity.getStatusCode()).thenReturn(HttpStatus.REQUEST_TIMEOUT); when(responseEntity.getStatusCode()).thenReturn(HttpStatus.REQUEST_TIMEOUT);
//
// TestRestfulValidator val = new TestRestfulValidator(responseEntity); TestRestfulValidator val = new TestRestfulValidator(responseEntity);
// try { try {
// assertNotNull(val.isValid(new Address(), FhirContext.forR4())); assertNotNull(val.isValid(new Address(), FhirContext.forR4()));
// fail(); fail();
// } catch (Exception e) { } catch (Exception e) {
// } }
// } }
//
// private static class TestRestfulValidator extends BaseRestfulValidator { private static class TestRestfulValidator extends BaseRestfulValidator {
// ResponseEntity<String> myResponseEntity; ResponseEntity<String> myResponseEntity;
//
// public TestRestfulValidator(ResponseEntity<String> theResponseEntity) { public TestRestfulValidator(ResponseEntity<String> theResponseEntity) {
// super(null); super(null);
// myResponseEntity = theResponseEntity; myResponseEntity = theResponseEntity;
// } }
//
// @Override @Override
// protected AddressValidationResult getValidationResult(AddressValidationResult theResult, JsonNode response, FhirContext theFhirContext) throws Exception { protected AddressValidationResult getValidationResult(AddressValidationResult theResult, JsonNode response, FhirContext theFhirContext) throws Exception {
// return new AddressValidationResult(); return new AddressValidationResult();
// } }
//
// @Override @Override
// protected ResponseEntity<String> getResponseEntity(IBase theAddress, FhirContext theFhirContext) throws Exception { protected ResponseEntity<String> getResponseEntity(IBase theAddress, FhirContext theFhirContext) throws Exception {
// return myResponseEntity; return myResponseEntity;
// } }
// } }
//
} }

View File

@ -5,7 +5,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; 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.AddressValidationException;
import ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidationResult; 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.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
@ -23,122 +23,121 @@ import static org.mockito.Mockito.verify;
class LoquateAddressValidatorTest { class LoquateAddressValidatorTest {
// private static final String REQUEST = "{\n" + private static final String REQUEST = "{\n" +
// " \"Key\" : \"MY_KEY\",\n" + " \"Key\" : \"MY_KEY\",\n" +
// " \"Geocode\" : false,\n" + " \"Geocode\" : false,\n" +
// " \"Addresses\" : [ {\n" + " \"Addresses\" : [ {\n" +
// " \"Address1\" : \"Line 1\",\n" + " \"Address1\" : \"Line 1\",\n" +
// " \"Address2\" : \"Line 2\",\n" + " \"Address2\" : \"Line 2\",\n" +
// " \"Locality\" : \"City\",\n" + " \"Locality\" : \"City\",\n" +
// " \"PostalCode\" : \"POSTAL\",\n" + " \"PostalCode\" : \"POSTAL\",\n" +
// " \"Country\" : \"Country\"\n" + " \"Country\" : \"Country\"\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" +
// " \"Address\": \"\"\n" + " \"Address\": \"\"\n" +
// " },\n" + " },\n" +
// " \"Matches\": [\n" + " \"Matches\": [\n" +
// " {\n" + " {\n" +
// " \"AQI\": \"C\",\n" + " \"AQI\": \"C\",\n" +
// " \"Address\": \"\"\n" + " \"Address\": \"\"\n" +
// " }\n" + " }\n" +
// " ]\n" + " ]\n" +
// " }\n" + " }\n" +
// "]"; "]";
//
// private static final String RESPONSE_VALID_ADDRESS = "[\n" + private static final String RESPONSE_VALID_ADDRESS = "[\n" +
// " {\n" + " {\n" +
// " \"Input\": {\n" + " \"Input\": {\n" +
// " \"Address\": \"\"\n" + " \"Address\": \"\"\n" +
// " },\n" + " },\n" +
// " \"Matches\": [\n" + " \"Matches\": [\n" +
// " {\n" + " {\n" +
// " \"AQI\": \"A\",\n" + " \"AQI\": \"A\",\n" +
// " \"Address\": \"My Valid Address\"\n" + " \"Address\": \"My Valid Address\"\n" +
// " }\n" + " }\n" +
// " ]\n" + " ]\n" +
// " }\n" + " }\n" +
// "]"; "]";
//
// private static final String RESPONSE_INVALID_KEY = "{\n" + private static final String RESPONSE_INVALID_KEY = "{\n" +
// " \"Number\": 2,\n" + " \"Number\": 2,\n" +
// " \"Description\": \"Unknown key\",\n" + " \"Description\": \"Unknown key\",\n" +
// " \"Cause\": \"The key you are using to access the service was not found.\",\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" + " \"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 static FhirContext ourCtx = FhirContext.forR4();
//
// private LoquateAddressValidator myValidator; private LoquateAddressValidator myValidator;
//
// private Properties myProperties; private Properties myProperties;
//
// @BeforeEach @BeforeEach
// public void initValidator() { public void initValidator() {
// myProperties = new Properties(); myProperties = new Properties();
// myProperties.setProperty(LoquateAddressValidator.PROPERTY_SERVICE_KEY, "MY_KEY"); myProperties.setProperty(LoquateAddressValidator.PROPERTY_SERVICE_KEY, "MY_KEY");
// myValidator = new LoquateAddressValidator(myProperties); myValidator = new LoquateAddressValidator(myProperties);
// } }
//
// @Test @Test
// public void testRequestBody() { public void testRequestBody() {
// try { try {
// assertEquals(REQUEST, myValidator.getRequestBody(ourCtx, getAddress())); assertEquals(REQUEST, myValidator.getRequestBody(ourCtx, getAddress()));
// } catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
// fail(); fail();
// } }
// } }
//
// @Test @Test
// public void testServiceCalled() { public void testServiceCalled() {
// Address address = getAddress(); Address address = getAddress();
//
// final RestTemplate template = mock(RestTemplate.class); final RestTemplate template = mock(RestTemplate.class);
//
// LoquateAddressValidator val = new LoquateAddressValidator(myProperties) { LoquateAddressValidator val = new LoquateAddressValidator(myProperties) {
// @Override @Override
// protected RestTemplate newTemplate() { protected RestTemplate newTemplate() {
// return template; return template;
// } }
// }; };
//
// try { try {
// val.getResponseEntity(address, ourCtx); val.getResponseEntity(address, ourCtx);
// } catch (Exception e) { } catch (Exception e) {
// fail(); fail();
// } }
//
// verify(template, times(1)).postForEntity(any(String.class), any(HttpEntity.class), eq(String.class)); verify(template, times(1)).postForEntity(any(String.class), any(HttpEntity.class), eq(String.class));
// } }
//
// @NotNull private Address getAddress() {
// private Address getAddress() { Address address = new Address();
// Address address = new Address(); address.addLine("Line 1").addLine("Line 2").setCity("City").setPostalCode("POSTAL").setCountry("Country");
// address.addLine("Line 1").addLine("Line 2").setCity("City").setPostalCode("POSTAL").setCountry("Country"); return address;
// return address; }
// }
// @Test
// @Test public void testSuccessfulResponses() throws Exception {
// public void testSuccessfulResponses() throws Exception { AddressValidationResult res = myValidator.getValidationResult(new AddressValidationResult(),
// AddressValidationResult res = myValidator.getValidationResult(new AddressValidationResult(), new ObjectMapper().readTree(RESPONSE_INVALID_ADDRESS), ourCtx);
// new ObjectMapper().readTree(RESPONSE_INVALID_ADDRESS), ourCtx); assertFalse(res.isValid());
// assertFalse(res.isValid());
// res = myValidator.getValidationResult(new AddressValidationResult(),
// res = myValidator.getValidationResult(new AddressValidationResult(), new ObjectMapper().readTree(RESPONSE_VALID_ADDRESS), ourCtx);
// new ObjectMapper().readTree(RESPONSE_VALID_ADDRESS), ourCtx); assertTrue(res.isValid());
// assertTrue(res.isValid()); assertEquals("My Valid Address", res.getValidatedAddressString());
// assertEquals("My Valid Address", res.getValidatedAddressString()); }
// }
// @Test
// @Test public void testErrorResponses() throws Exception {
// public void testErrorResponses() throws Exception { assertThrows(AddressValidationException.class, () -> {
// assertThrows(AddressValidationException.class, () -> { myValidator.getValidationResult(new AddressValidationResult(),
// myValidator.getValidationResult(new AddressValidationResult(), new ObjectMapper().readTree(RESPONSE_INVALID_KEY), ourCtx);
// 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 com.fasterxml.jackson.databind.ObjectMapper;
import ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidationException; import ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidationException;
import ca.uhn.fhir.rest.server.interceptor.validation.address.AddressValidationResult; 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.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@ -24,117 +24,116 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
class MelissaAddressValidatorTest { class MelissaAddressValidatorTest {
//
// private static final String RESPONSE_INVALID_ADDRESS = "{\n" + private static final String RESPONSE_INVALID_ADDRESS = "{\n" +
// " \"Version\": \"3.0.1.160\",\n" + " \"Version\": \"3.0.1.160\",\n" +
// " \"TransmissionReference\": \"1\",\n" + " \"TransmissionReference\": \"1\",\n" +
// " \"TransmissionResults\": \"\",\n" + " \"TransmissionResults\": \"\",\n" +
// " \"TotalRecords\": \"1\",\n" + " \"TotalRecords\": \"1\",\n" +
// " \"Records\": [\n" + " \"Records\": [\n" +
// " {\n" + " {\n" +
// " \"RecordID\": \"1\",\n" + " \"RecordID\": \"1\",\n" +
// " \"Results\": \"AC01,AC12,AE02,AV12,GE02\",\n" + " \"Results\": \"AC01,AC12,AE02,AV12,GE02\",\n" +
// " \"FormattedAddress\": \"100 Main Street\",\n" + " \"FormattedAddress\": \"100 Main Street\",\n" +
// " \"Organization\": \"\",\n" + " \"Organization\": \"\",\n" +
// " \"AddressLine1\": \"100 Main Street\"\n" + " \"AddressLine1\": \"100 Main Street\"\n" +
// " }\n" + " }\n" +
// " ]\n" + " ]\n" +
// "}"; "}";
//
// private static final String RESPONSE_VALID_ADDRESS = "{\n" + private static final String RESPONSE_VALID_ADDRESS = "{\n" +
// " \"Version\": \"3.0.1.160\",\n" + " \"Version\": \"3.0.1.160\",\n" +
// " \"TransmissionReference\": \"1\",\n" + " \"TransmissionReference\": \"1\",\n" +
// " \"TransmissionResults\": \"\",\n" + " \"TransmissionResults\": \"\",\n" +
// " \"TotalRecords\": \"1\",\n" + " \"TotalRecords\": \"1\",\n" +
// " \"Records\": [\n" + " \"Records\": [\n" +
// " {\n" + " {\n" +
// " \"RecordID\": \"1\",\n" + " \"RecordID\": \"1\",\n" +
// " \"Results\": \"AC01,AV24,GS05\",\n" + " \"Results\": \"AC01,AV24,GS05\",\n" +
// " \"FormattedAddress\": \"100 Main St W;Hamilton ON L8P 1H6\"\n" + " \"FormattedAddress\": \"100 Main St W;Hamilton ON L8P 1H6\"\n" +
// " }\n" + " }\n" +
// " ]\n" + " ]\n" +
// "}"; "}";
//
// private static final String RESPONSE_INVALID_KEY = "{\n" + private static final String RESPONSE_INVALID_KEY = "{\n" +
// " \"Version\": \"3.0.1.160\",\n" + " \"Version\": \"3.0.1.160\",\n" +
// " \"TransmissionReference\": \"1\",\n" + " \"TransmissionReference\": \"1\",\n" +
// " \"TransmissionResults\": \"GE05\",\n" + " \"TransmissionResults\": \"GE05\",\n" +
// " \"TotalRecords\": \"0\"\n" + " \"TotalRecords\": \"0\"\n" +
// "}"; "}";
//
// private static FhirContext ourContext = FhirContext.forR4(); private static FhirContext ourContext = FhirContext.forR4();
//
// private MelissaAddressValidator myValidator; private MelissaAddressValidator myValidator;
//
// @BeforeEach @BeforeEach
// public void init() { public void init() {
// Properties props = new Properties(); Properties props = new Properties();
// props.setProperty(MelissaAddressValidator.PROPERTY_SERVICE_KEY, "MY_KEY"); props.setProperty(MelissaAddressValidator.PROPERTY_SERVICE_KEY, "MY_KEY");
// myValidator = new MelissaAddressValidator(props); myValidator = new MelissaAddressValidator(props);
//
// } }
//
// @Test @Test
// public void testRequestBody() { public void testRequestBody() {
// Map<String, String> params = myValidator.getRequestParams(getAddress()); Map<String, String> params = myValidator.getRequestParams(getAddress());
//
// assertEquals("Line 1, Line 2", params.get("a1")); assertEquals("Line 1, Line 2", params.get("a1"));
// assertEquals("City, POSTAL", params.get("a2")); assertEquals("City, POSTAL", params.get("a2"));
// assertEquals("Country", params.get("ctry")); assertEquals("Country", params.get("ctry"));
// assertEquals("MY_KEY", params.get("id")); assertEquals("MY_KEY", params.get("id"));
// assertEquals("json", params.get("format")); assertEquals("json", params.get("format"));
// assertTrue(params.containsKey("t")); assertTrue(params.containsKey("t"));
// } }
//
// @Test @Test
// public void testServiceCalled() { public void testServiceCalled() {
// Address address = getAddress(); Address address = getAddress();
//
// final RestTemplate template = mock(RestTemplate.class); final RestTemplate template = mock(RestTemplate.class);
//
// Properties props = new Properties(); Properties props = new Properties();
// props.setProperty(BaseRestfulValidator.PROPERTY_SERVICE_KEY, "MY_KEY"); props.setProperty(BaseRestfulValidator.PROPERTY_SERVICE_KEY, "MY_KEY");
// MelissaAddressValidator val = new MelissaAddressValidator(props) { MelissaAddressValidator val = new MelissaAddressValidator(props) {
// @Override @Override
// protected RestTemplate newTemplate() { protected RestTemplate newTemplate() {
// return template; return template;
// } }
// }; };
//
// try { try {
// val.getResponseEntity(address, ourContext); val.getResponseEntity(address, ourContext);
// } catch (Exception e) { } catch (Exception e) {
// fail(); fail();
// } }
//
// verify(template, times(1)).getForEntity(any(String.class), eq(String.class), any(Map.class)); verify(template, times(1)).getForEntity(any(String.class), eq(String.class), any(Map.class));
// } }
//
// @NotNull private Address getAddress() {
// private Address getAddress() { Address address = new Address();
// Address address = new Address(); address.addLine("Line 1").addLine("Line 2").setCity("City").setPostalCode("POSTAL").setCountry("Country");
// address.addLine("Line 1").addLine("Line 2").setCity("City").setPostalCode("POSTAL").setCountry("Country"); return address;
// return address; }
// }
// @Test
// @Test public void testSuccessfulResponses() throws Exception {
// public void testSuccessfulResponses() throws Exception { AddressValidationResult res = myValidator.getValidationResult(new AddressValidationResult(),
// AddressValidationResult res = myValidator.getValidationResult(new AddressValidationResult(), new ObjectMapper().readTree(RESPONSE_INVALID_ADDRESS), ourContext);
// new ObjectMapper().readTree(RESPONSE_INVALID_ADDRESS), ourContext); assertFalse(res.isValid());
// assertFalse(res.isValid());
// res = myValidator.getValidationResult(new AddressValidationResult(),
// res = myValidator.getValidationResult(new AddressValidationResult(), new ObjectMapper().readTree(RESPONSE_VALID_ADDRESS), ourContext);
// new ObjectMapper().readTree(RESPONSE_VALID_ADDRESS), ourContext); assertTrue(res.isValid());
// assertTrue(res.isValid()); assertEquals("100 Main St W;Hamilton ON L8P 1H6", res.getValidatedAddressString());
// assertEquals("100 Main St W;Hamilton ON L8P 1H6", res.getValidatedAddressString()); }
// }
// @Test
// @Test public void testErrorResponses() throws Exception {
// public void testErrorResponses() throws Exception { assertThrows(AddressValidationException.class, () -> {
// assertThrows(AddressValidationException.class, () -> { myValidator.getValidationResult(new AddressValidationResult(),
// myValidator.getValidationResult(new AddressValidationResult(), new ObjectMapper().readTree(RESPONSE_INVALID_KEY), ourContext);
// new ObjectMapper().readTree(RESPONSE_INVALID_KEY), ourContext); });
// }); }
// }
} }