Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed code and added unit tests. #145

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,17 @@ private List<NameSearchResult> buildNameSearch(FindCandidatesResponse findCandid
nameSearchResult.setIdentifierTypeCode(IDENTIFIER_TYPE_CODE);
nameSearchResult.setAssigningAuthority(ASSIGNING_AUTHORITY);

Name nameObj = ns.getPerson().getDeclaredName();
if (nameObj == null) {
nameObj = ns.getPerson().getDocumentedName();
// "Documented" should always be shown over a "Declared" name.
Name name = ns.getPerson().getDocumentedName();
if (name == null) {
name = ns.getPerson().getDeclaredName();
}

if (nameObj != null) {
nameSearchResult.setGivenName(Optional.ofNullable(nameObj.getFirstGivenName()).orElse(""));
nameSearchResult.setSecondName(Optional.ofNullable(nameObj.getSecondGivenName()).orElse(""));
nameSearchResult.setSurname(Optional.ofNullable(nameObj.getSurname()).orElse(""));
nameSearchResult.setNameTypeCode(Optional.ofNullable(nameObj.getType()).orElse(""));
if (name != null) {
nameSearchResult.setGivenName(Optional.ofNullable(name.getFirstGivenName()).orElse(""));
nameSearchResult.setSecondName(Optional.ofNullable(name.getSecondGivenName()).orElse(""));
nameSearchResult.setSurname(Optional.ofNullable(name.getSurname()).orElse(""));
nameSearchResult.setNameTypeCode(Optional.ofNullable(name.getType()).orElse(""));

String birthDate = V3MessageUtil.convertDateToString(ns.getPerson().getBirthDate());
nameSearchResult.setDateOfBirth(birthDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,17 @@ private GetPersonDetailsResponse buildGetPersonDetailsResponse(GetDemographicsRe

private void buildPersonDetails(GetDemographicsResponse demographicsResponse,
GetPersonDetailsResponse personDetailsResponse) {
Name nameObj = demographicsResponse.getPerson().getDeclaredName();
if (nameObj == null) {
nameObj = demographicsResponse.getPerson().getDocumentedName();
// "Documented" should always be shown over a "Declared" name.
Name name = demographicsResponse.getPerson().getDocumentedName();
if (name == null) {
name = demographicsResponse.getPerson().getDeclaredName();
}

if (nameObj != null) {
if (name != null) {
personDetailsResponse.setPhn(demographicsResponse.getPerson().getPhn());
personDetailsResponse.setGivenName(nameObj.getFirstGivenName());
personDetailsResponse.setSecondName(nameObj.getSecondGivenName());
personDetailsResponse.setSurname(nameObj.getSurname());
personDetailsResponse.setGivenName(name.getFirstGivenName());
personDetailsResponse.setSecondName(name.getSecondGivenName());
personDetailsResponse.setSurname(name.getSurname());

String birthDate = V3MessageUtil.convertDateToString(demographicsResponse.getPerson().getBirthDate());
String deathDate = demographicsResponse.getPerson().getDeathDate() != null ? V3MessageUtil.convertDateToString(demographicsResponse.getPerson().getDeathDate()) :"N/A";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;

Expand All @@ -24,6 +25,7 @@
import ca.bc.gov.hlth.hnweb.model.rest.enrollment.GetPersonDetailsResponse;
import ca.bc.gov.hlth.hnweb.model.rest.enrollment.NameSearchRequest;
import ca.bc.gov.hlth.hnweb.model.rest.enrollment.NameSearchResponse;
import ca.bc.gov.hlth.hnweb.model.rest.enrollment.NameSearchResult;
import ca.bc.gov.hlth.hnweb.persistence.entity.AffectedPartyDirection;
import ca.bc.gov.hlth.hnweb.security.TransactionType;
import ca.bc.gov.hlth.hnweb.utils.TestUtil;
Expand Down Expand Up @@ -60,6 +62,8 @@ public class EnrollmentControllerTest extends BaseControllerTest {

private static final String FC_ERROR_MESSAGE = "BCHCIM.FC.2.0006 The HL7 message is invalid. Please correct the HL7 message, and resubmit it.Results from Schematron validation";

private static final String FC_SUCCESS_MESSAGE = "BCHCIM.FC.0.0012 The search completed successfully.";

private static final String GD_ERROR_MESSAGE = "BCHCIM.GD.2.0006 The HL7 message is invalid. Please correct the HL7 message, and resubmit it.Results from Schematron validation";

private static final String GD_WARNING_MESSAGE = "BCHCIM.GD.0.0015 The identifier you used in the query has been merged. The surviving identifier was returned.";
Expand Down Expand Up @@ -209,6 +213,35 @@ void testGetDemographicsDetails_Success() throws Exception {
assertAffectedPartyCount(AffectedPartyDirection.OUTBOUND, 1);
}

@Test
void testGetDemographicsDetails_DocumentedName() throws Exception {

mockBackEnd.enqueue(new MockResponse()
.setBody(TestUtil.convertXMLFileToString("src/test/resources/GetDemographicsResponse_DocumentedName.xml"))
.addHeader(CONTENT_TYPE, MediaType.TEXT_XML_VALUE.toString()));

GetPersonDetailsRequest getPersonQuery = new GetPersonDetailsRequest();
getPersonQuery.setPhn("9862716574");

ResponseEntity<GetPersonDetailsResponse> response = enrollmentController.getPersonDetails(getPersonQuery, createHttpServletRequest());
GetPersonDetailsResponse getPersonDetailsResponse = response.getBody();
assertEquals("9862716574", getPersonDetailsResponse.getPhn());
// This is the documented name
assertEquals("Robert", getPersonDetailsResponse.getGivenName());
assertEquals("Smith", getPersonDetailsResponse.getSurname());
assertEquals("M", getPersonDetailsResponse.getGender());

//Check the client request is sent as expected
RecordedRequest recordedRequest = mockBackEnd.takeRequest();
assertEquals(HttpMethod.POST.name(), recordedRequest.getMethod());
assertEquals(MediaType.TEXT_XML.toString(), recordedRequest.getHeader(CONTENT_TYPE));
assertEquals("/", recordedRequest.getPath());

assertTransactionCreated(TransactionType.GET_PERSON_DETAILS);
assertAffectedPartyCount(AffectedPartyDirection.INBOUND, 1);
assertAffectedPartyCount(AffectedPartyDirection.OUTBOUND, 1);
}

@Test
void testGetDemographicsDetails_Warning() throws Exception {
mockBackEnd.enqueue(new MockResponse()
Expand Down Expand Up @@ -381,6 +414,42 @@ void testGetNameSearch_Error() throws Exception {
assertAffectedPartyCount(AffectedPartyDirection.OUTBOUND, 0);
}

/**
* Tests that the documentedName gets used instead of the declaredName when available.
* @throws IOException
*/
@Test
void testGetNameSearch_documentedName() throws Exception {
mockBackEnd.enqueue(new MockResponse()
.setBody(TestUtil.convertXMLFileToString("src/test/resources/FindCandidatesResponse_DocumentedName.xml"))
.addHeader(CONTENT_TYPE, MediaType.TEXT_XML_VALUE.toString()));

NameSearchRequest nameSearchRequest = new NameSearchRequest();
nameSearchRequest.setGender("M");
nameSearchRequest.setDateOfBirth(LocalDate.of(1973, 8, 11));

ResponseEntity<NameSearchResponse> response = enrollmentController.getNameSearch(nameSearchRequest, createHttpServletRequest());
NameSearchResponse nameSearchResponse = response.getBody();
assertEquals(StatusEnum.SUCCESS, nameSearchResponse.getStatus());
assertEquals(FC_SUCCESS_MESSAGE, nameSearchResponse.getMessage());
assertEquals(1, nameSearchResponse.getCandidates().size());

NameSearchResult nameSearchResult = nameSearchResponse.getCandidates().get(0);
// This is the documented name
assertEquals("PURPLE DINO", nameSearchResult.getGivenName());
assertEquals("BARNEY", nameSearchResult.getSurname());

// Check the client request is sent as expected
RecordedRequest recordedRequest = mockBackEnd.takeRequest();
assertEquals(HttpMethod.POST.name(), recordedRequest.getMethod());
assertEquals(MediaType.TEXT_XML.toString(), recordedRequest.getHeader(CONTENT_TYPE));
assertEquals("/", recordedRequest.getPath());

assertTransactionCreated(TransactionType.NAME_SEARCH);
assertAffectedPartyCount(AffectedPartyDirection.INBOUND, 0);
assertAffectedPartyCount(AffectedPartyDirection.OUTBOUND, 1);
}

/**
* The URL property used by the mocked endpoint needs to be set after the MockWebServer starts as the port it uses is
* created dynamically on start up to ensure it uses an available port so it is not known before then.
Expand Down
112 changes: 112 additions & 0 deletions backend/src/test/resources/FindCandidatesResponse_DocumentedName.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<HCIM_IN_FindCandidatesResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:hl7-org:v3">
<!-- ========================
BC Client Registry Find Candidates response sample message.
September 20, 2011

Comments:

1. contains known datatype and markup validation errors.

Release: BC.CR.R1.7
======================= -->
<id root="2.16.840.1.113883.3.51.1.1.1" extension="e770dd20-7fe9-406f-a57c-52ed466f7a3b"/>
<creationTime value="20110913161143"/>
<versionCode code="V3PR1"/>
<interactionId root="2.16.840.1.113883.3.51.1.1.2" extension="HCIM_IN_FindCandidatesResponse"/>
<processingCode code="P"/>
<processingModeCode code="T"/>
<acceptAckCode code="NE"/>
<receiver typeCode="RCV">
<device classCode="DEV" determinerCode="INSTANCE">
<id root="2.16.840.1.113883.3.51.1.1.5" extension="VCHA_COMM"/>
<asAgent classCode="AGNT">
<representedOrganization classCode="ORG" determinerCode="INSTANCE">
<id root="2.16.840.1.113883.3.51.1.1.3" extension="VCHA"/>
</representedOrganization>
</asAgent>
</device>
</receiver>
<sender typeCode="SND">
<device classCode="DEV" determinerCode="INSTANCE">
<id root="2.16.840.1.113883.3.51.1.1.4" extension="192.168.0.1"/>
<asAgent classCode="AGNT">
<representedOrganization classCode="ORG" determinerCode="INSTANCE">
<id root="2.16.840.1.113883.3.51.1.1.3" extension="HCIM"/>
</representedOrganization>
</asAgent>
</device>
</sender>
<controlActProcess classCode="ACCM" moodCode="EVN">
<subject typeCode="SUBJ">
<target classCode="IDENT">
<addr use="PHYS">
<postalCode>V8W 3P9</postalCode>
<streetAddressLine>666 YELLOW BRICK RD</streetAddressLine>
<city>PRINCE GEORGE</city>
<state>BC</state>
<country>CA</country>
</addr>
<telecom value="tel:2505554848" use="H"/>
<identifiedPerson classCode="PERSON" determinerCode="INSTANCE">
<id root="2.16.840.1.113883.3.51.1.1.6.1" extension="9999999999" assigningAuthorityName="MOH_CRS"/>
<name use="C">
<given>PURPLE DINO</given>
<family>BARNEY</family>
</name>
<name use="L">
<given>PURPLE</given>
<family>BARNEY</family>
</name>
<administrativeGenderCode code="M"/>
<birthTime value="19400606"/>
</identifiedPerson>
<subjectOf>
<observationEvent>
<code code="SCORE"/>
<value value="31"/>
</observationEvent>
</subjectOf>
</target>
</subject>
<queryAck>
<!-- BC CR 20-sep-2011. Compound value in code field fails datatype validation. -->
<queryResponseCode code="BCHCIM.FC.0.0012 | The search completed successfully."/>
<resultTotalQuantity value="1"/>
</queryAck>
<queryByParameterPayload>
<person.addr>
<value use="PHYS PST">
<postalCode>V1V1V1</postalCode>
<streetAddressLine>123 Any St</streetAddressLine>
<streetAddressLine>line 2</streetAddressLine>
<streetAddressLine>line 3</streetAddressLine>
<streetAddressLine>line 4</streetAddressLine>
<city>Victoria</city>
<state>BC</state>
<country>CA</country>
</value>
</person.addr>
<person.administrativeGender>
<value code="M"/>
</person.administrativeGender>
<person.birthTime>
<value value="19591230"/>
</person.birthTime>
<person.deceasedTime>
<value value="20050505"/>
</person.deceasedTime>
<person.id>
<value root="2.16.840.1.113883.3.51.1.1.6.2" extension="12345892" assigningAuthorityName="ABPHN"/>
</person.id>
<person.name>
<value use="L">
<given>Barney</given>
<family>Purple</family>
</value>
</person.name>
<person.telecom>
<value value="tel:1234567890" use="H WP MC"/>
</person.telecom>
</queryByParameterPayload>
</controlActProcess>
</HCIM_IN_FindCandidatesResponse>
Loading