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

Niad 2858: storing negative ack error code in DB #359

Merged
merged 13 commits into from
Nov 6, 2023
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
@@ -0,0 +1,20 @@
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
http://www.liquibase.org/xml/ns/pro
http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.1.xsd">

<changeSet id="14" author="ole4ryb">
<addColumn schemaName="public" tableName="migration_status_log">
<column name="gp2gp_error_code" type="varchar(255)">
<constraints nullable="true"/>
</column>
</addColumn>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public interface MigrationStatusLogDao {
@SqlUpdate("insert_migration_status_log")
@UseClasspathSqlLocator
void addMigrationStatusLog(@Bind("status") MigrationStatus status,
@Bind("date") OffsetDateTime date, @Bind("migrationRequestId") int migrationRequestId, @Bind("messageId") String messageId
@Bind("date") OffsetDateTime date, @Bind("migrationRequestId") int migrationRequestId,
@Bind("messageId") String messageId, @Bind("gp2gpErrorCode") String gp2gpErrorCode
);

@SqlQuery("select_migration_status_log")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
@Setter
@Builder
public class MigrationStatusLog {

private int id;
@EnumByName
private MigrationStatus migrationStatus;
private OffsetDateTime date;
private int migrationRequestId;
private String messageId;
private String gp2gpErrorCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@
@Service
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class MigrationStatusLogService {

private final PatientMigrationRequestDao patientMigrationRequestDao;
private final MigrationStatusLogDao migrationStatusLogDao;
private final DateUtils dateUtils;

public void addMigrationStatusLog(MigrationStatus migrationStatus, String conversationId, String messageId) {
public void addMigrationStatusLog(MigrationStatus migrationStatus, String conversationId, String messageId, String gp2gpErrorCode) {

int migrationRequestId = patientMigrationRequestDao.getMigrationRequestId(conversationId);
migrationStatusLogDao.addMigrationStatusLog(
migrationStatus,
dateUtils.getCurrentOffsetDateTime(),
migrationRequestId,
messageId
messageId,
gp2gpErrorCode
);

LOGGER.debug("Changed MigrationStatus of PatientMigrationRequest with id=[{}] to [{}]", migrationRequestId, migrationStatus.name());
}

Expand All @@ -45,6 +49,6 @@ public List<MigrationStatusLog> getMigrationStatusLogs(String conversationId) {
public void updatePatientMigrationRequestAndAddMigrationStatusLog(String conversationId, String bundle, String inboundMessage,
MigrationStatus migrationStatus, String messageId) {
patientMigrationRequestDao.saveBundleAndInboundMessageData(conversationId, bundle, inboundMessage);
addMigrationStatusLog(migrationStatus, conversationId, messageId);
addMigrationStatusLog(migrationStatus, conversationId, messageId, null);
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
INSERT INTO migration_status_log(status, date, migration_request_id, message_id)
VALUES (:status, :date, :migrationRequestId, :messageId);
INSERT INTO migration_status_log(status, date, migration_request_id, message_id, gp2gp_error_code)
VALUES (:status, :date, :migrationRequestId, :messageId, :gp2gpErrorCode);
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public void testAddMigrationStatusLog() {
when(patientMigrationRequestDao.getMigrationRequestId(nhsNumber)).thenReturn(MIGRATION_REQUEST_ID);
when(dateUtils.getCurrentOffsetDateTime()).thenReturn(now);

migrationStatusLogService.addMigrationStatusLog(MigrationStatus.MIGRATION_COMPLETED, nhsNumber, null);
migrationStatusLogService.addMigrationStatusLog(MigrationStatus.MIGRATION_COMPLETED, nhsNumber, null, null);

verify(migrationStatusLogDao).addMigrationStatusLog(MigrationStatus.MIGRATION_COMPLETED, now, MIGRATION_REQUEST_ID, null);
verify(migrationStatusLogDao).addMigrationStatusLog(MigrationStatus.MIGRATION_COMPLETED, now, MIGRATION_REQUEST_ID, null, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class AcknowledgeMessageHandlingIT {
private static final String ERROR_REASON_MESSAGE_PLACEHOLDER = "{{reasonMessage}}";
private static final String LOSING_ODS_CODE = "K547";
private static final String WINNING_ODS_CODE = "ABC";
public static final String TEST_ERROR_MESSAGE = "Test Error Message";

@Autowired
private PatientMigrationRequestDao patientMigrationRequestDao;
Expand All @@ -72,7 +73,7 @@ public class AcknowledgeMessageHandlingIT {
public void setUp() {
conversationId = generateConversationId().toUpperCase(Locale.ROOT);
patientMigrationRequestDao.addNewRequest(generatePatientNhsNumber(), conversationId, LOSING_ODS_CODE, WINNING_ODS_CODE);
migrationStatusLogService.addMigrationStatusLog(EHR_EXTRACT_REQUEST_ACCEPTED, conversationId, null);
migrationStatusLogService.addMigrationStatusLog(EHR_EXTRACT_REQUEST_ACCEPTED, conversationId, null, null);
}

@Test
Expand All @@ -93,63 +94,63 @@ public void handleNegativeAcknowledgeMessageFromQueue() {

@Test
public void handleNegativeAcknowledgeMessageWithUndeclairedErrorReasonFromQueue() {
sendAcknowledgementMessageToQueue("AE", "101", "Test Error Message");
sendAcknowledgementMessageToQueue("AE", "101", TEST_ERROR_MESSAGE);

// verify if correct status is set in the DB
await().until(() -> isCorrectStatusSet(EHR_EXTRACT_REQUEST_NEGATIVE_ACK_UNKNOWN));
}

@Test
public void handleNegativeAcknowledgeMessageWithUnknownErrorReasonFromQueue() {
sendAcknowledgementMessageToQueue("AE", "99", "Test Error Message");
sendAcknowledgementMessageToQueue("AE", "99", TEST_ERROR_MESSAGE);

// verify if correct status is set in the DB
await().until(() -> isCorrectStatusSet(EHR_EXTRACT_REQUEST_NEGATIVE_ACK_UNKNOWN));
}

@Test
public void handleNegativeAcknowledgeMessageWithPatientNotRegisteredErrorReasonFromQueue() {
sendAcknowledgementMessageToQueue("AE", "06", "Test Error Message");
sendAcknowledgementMessageToQueue("AE", "06", TEST_ERROR_MESSAGE);

// verify if correct status is set in the DB
await().until(() -> isCorrectStatusSet(EHR_EXTRACT_REQUEST_NEGATIVE_ACK_GP2GP_PATIENT_NOT_REGISTERED));
}

@Test
public void handleNegativeAcknowledgeMessageWithSENDERNOTCONFIGUREDErrorReasonFromQueue() {
sendAcknowledgementMessageToQueue("AE", "07", "Test Error Message");
sendAcknowledgementMessageToQueue("AE", "07", TEST_ERROR_MESSAGE);

// verify if correct status is set in the DB
await().until(() -> isCorrectStatusSet(EHR_EXTRACT_REQUEST_NEGATIVE_ACK_GP2GP_SENDER_NOT_CONFIGURED));
}

@Test
public void handleNegativeAcknowledgeMessageWithEHRGENERATIONErrorReasonFromQueue() {
sendAcknowledgementMessageToQueue("AE", "10", "Test Error Message");
sendAcknowledgementMessageToQueue("AE", "10", TEST_ERROR_MESSAGE);

// verify if correct status is set in the DB
await().until(() -> isCorrectStatusSet(EHR_EXTRACT_REQUEST_NEGATIVE_ACK_GP2GP_EHR_GENERATION_ERROR));
}

@Test
public void handleNegativeAcknowledgeMessageWithMISFORMEDREQUESTErrorReasonFromQueue() {
sendAcknowledgementMessageToQueue("AE", "18", "Test Error Message");
sendAcknowledgementMessageToQueue("AE", "18", TEST_ERROR_MESSAGE);

// verify if correct status is set in the DB
await().until(() -> isCorrectStatusSet(EHR_EXTRACT_REQUEST_NEGATIVE_ACK_GP2GP_MISFORMED_REQUEST));
}

@Test
public void handleNegativeAcknowledgeMessageWithNOTPRIMARYHEALTHCAREPROVIDERErrorReasonFromQueue() {
sendAcknowledgementMessageToQueue("AE", "19", "Test Error Message");
sendAcknowledgementMessageToQueue("AE", "19", TEST_ERROR_MESSAGE);

// verify if correct status is set in the DB
await().until(() -> isCorrectStatusSet(EHR_EXTRACT_REQUEST_NEGATIVE_ACK_GP2GP_NOT_PRIMARY_HEALTHCARE_PROVIDER));
}

@Test
public void handleNegativeAcknowledgeMessageWithMULTIORNORESPONSESErrorReasonFromQueue() {
sendAcknowledgementMessageToQueue("AE", "24", "Test Error Message");
sendAcknowledgementMessageToQueue("AE", "24", TEST_ERROR_MESSAGE);

// verify if correct status is set in the DB
await().until(() -> isCorrectStatusSet(EHR_EXTRACT_REQUEST_NEGATIVE_ACK_GP2GP_MULTI_OR_NO_RESPONSES));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package uk.nhs.adaptors.pss.translator;

import static org.assertj.core.api.Assertions.fail;
import static org.awaitility.Awaitility.await;

import static uk.nhs.adaptors.common.util.FileUtil.readResourceAsString;
import static uk.nhs.adaptors.pss.translator.util.XmlUnmarshallUtil.unmarshallString;
import static uk.nhs.adaptors.pss.util.JsonPathIgnoreGeneratorUtil.generateJsonPathIgnores;

import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Locale;
import java.util.stream.Stream;
Expand All @@ -21,10 +18,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.skyscreamer.jsonassert.Customization;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.skyscreamer.jsonassert.comparator.CustomComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand All @@ -47,7 +40,6 @@
public class E2EMappingIT extends BaseEhrHandler {

private static final boolean OVERWRITE_EXPECTED_JSON = false;
private static final int NHS_NUMBER_MIN_MAX_LENGTH = 10;
private static final String PSS_ADAPTOR_URL = "https://PSSAdaptor/";
private static final String EBXML_PART_PATH = "/xml/RCMR_IN030000UK06/ebxml_part.xml";
//these are programming language special characters, not to be confused with line endings
Expand Down Expand Up @@ -348,28 +340,4 @@ private String getOdsToBeReplaced(String expectedBundle) {
return expectedBundle.substring(startIndex, endIndex);
}

private String getLocationToBeReplaced(String expectedBundle) {
var startIndex = expectedBundle.toLowerCase().indexOf(PSS_ADAPTOR_URL.toLowerCase()) + PSS_ADAPTOR_URL.length();
var endIndex = expectedBundle.toLowerCase().indexOf("\"", startIndex);

return expectedBundle.substring(startIndex, endIndex);
}

@SneakyThrows
private void overwriteExpectJson(String newExpected) {
try (PrintWriter printWriter = new PrintWriter("src/integrationTest/resources/json/expectedBundle.json", StandardCharsets.UTF_8)) {
printWriter.print(newExpected);
}
fail("Re-run the tests with OVERWRITE_EXPECTED_JSON=false");
}

private void assertBundleContent(String actual, String expected, List<String> ignoredPaths) throws JSONException {
// when comparing json objects, this will ignore various json paths that contain random values like ids or timestamps
var customizations = ignoredPaths.stream()
.map(jsonPath -> new Customization(jsonPath, (o1, o2) -> true))
.toArray(Customization[]::new);

JSONAssert.assertEquals(expected, actual,
new CustomComparator(JSONCompareMode.STRICT, customizations));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void handleEhrExtractWithConfidentialityCodeFromQueue() throws JSONExcept

private void startPatientMigrationJourney() {
patientMigrationRequestDao.addNewRequest(patientNhsNumber, conversationId, LOSING_ODS_CODE, WINNING_ODS_CODE);
migrationStatusLogService.addMigrationStatusLog(EHR_EXTRACT_REQUEST_ACCEPTED, conversationId, null);
migrationStatusLogService.addMigrationStatusLog(EHR_EXTRACT_REQUEST_ACCEPTED, conversationId, null, null);
}

private String generatePatientNhsNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void When_ProcessFailedByIncumbent_With_CopcMessage_Expect_NotProcessed()

@Test
public void When_ProcessFailedByNME_With_EhrExtract_Expect_NotProcessed() {
migrationStatusLogService.addMigrationStatusLog(EHR_GENERAL_PROCESSING_ERROR, getConversationId(), null);
migrationStatusLogService.addMigrationStatusLog(EHR_GENERAL_PROCESSING_ERROR, getConversationId(), null, "99");

sendEhrExtractToQueue();

Expand All @@ -133,7 +133,7 @@ private void whenCopcSentExpectNackCode(MigrationStatus preCopcMigrationStatus,

await().until(this::isContinueRequestAccepted);

migrationStatusLogService.addMigrationStatusLog(preCopcMigrationStatus, getConversationId(), null);
migrationStatusLogService.addMigrationStatusLog(preCopcMigrationStatus, getConversationId(), null, null);

sendCopcToQueue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ private void sendRequestToPssQueue(String conversationId, String patientNhsNumbe
getPatientMigrationRequestDao()
.addNewRequest(patientNhsNumber, conversationId, getLosingODSCode(), getWiningODSCode());
getMigrationStatusLogService()
.addMigrationStatusLog(REQUEST_RECEIVED, conversationId, null);
.addMigrationStatusLog(REQUEST_RECEIVED, conversationId, null, null);

var transferRequestMessage = TransferRequestMessage.builder()
.patientNhsNumber(patientNhsNumber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void setUp() {

protected void startPatientMigrationJourney() {
patientMigrationRequestDao.addNewRequest(patientNhsNumber, conversationId, losingODSCode, winingODSCode);
migrationStatusLogService.addMigrationStatusLog(EHR_EXTRACT_REQUEST_ACCEPTED, conversationId, null);
migrationStatusLogService.addMigrationStatusLog(EHR_EXTRACT_REQUEST_ACCEPTED, conversationId, null, null);
}

protected boolean isEhrExtractTranslated() {
Expand Down Expand Up @@ -118,14 +118,14 @@ protected void verifyBundle(String path) throws JSONException {
}

@SneakyThrows
private void overwriteExpectJson(String newExpected) {
protected void overwriteExpectJson(String newExpected) {
try (PrintWriter printWriter = new PrintWriter("src/integrationTest/resources/json/expectedBundle.json", StandardCharsets.UTF_8)) {
printWriter.print(newExpected);
}
fail("Re-run the tests with OVERWRITE_EXPECTED_JSON=false");
}

private void assertBundleContent(String actual, String expected, List<String> ignoredPaths) throws JSONException {
protected void assertBundleContent(String actual, String expected, List<String> ignoredPaths) throws JSONException {
// when comparing json objects, this will ignore various json paths that contain random values like ids or timestamps
var customizations = ignoredPaths.stream()
.map(jsonPath -> new Customization(jsonPath, (o1, o2) -> true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static uk.nhs.adaptors.common.enums.MigrationStatus.EHR_EXTRACT_NEGATIVE_ACK_FAILED_TO_INTEGRATE;
import static uk.nhs.adaptors.common.enums.MigrationStatus.EHR_EXTRACT_NEGATIVE_ACK_NON_ABA_INCORRECT_PATIENT;
import static uk.nhs.adaptors.common.enums.MigrationStatus.EHR_EXTRACT_NEGATIVE_ACK_SUPPRESSED;
import static uk.nhs.adaptors.common.enums.MigrationStatus.EHR_EXTRACT_REQUEST_NEGATIVE_ACK_GP2GP_PATIENT_NOT_REGISTERED;
import static uk.nhs.adaptors.common.enums.MigrationStatus.EHR_GENERAL_PROCESSING_ERROR;
import static uk.nhs.adaptors.common.enums.MigrationStatus.ERROR_EXTRACT_CANNOT_BE_PROCESSED;
import static uk.nhs.adaptors.common.enums.MigrationStatus.ERROR_LRG_MSG_ATTACHMENTS_NOT_RECEIVED;
Expand All @@ -17,6 +18,7 @@

@RequiredArgsConstructor
public enum NACKReason {

LARGE_MESSAGE_REASSEMBLY_FAILURE("29"),
LARGE_MESSAGE_ATTACHMENTS_NOT_RECEIVED("31"),
LARGE_MESSAGE_GENERAL_FAILURE("30"),
Expand All @@ -26,6 +28,7 @@ public enum NACKReason {
UNEXPECTED_CONDITION("99"),
ABA_EHR_EXTRACT_REJECTED_WRONG_PATIENT("17"),
ABA_EHR_EXTRACT_SUPPRESSED("15"),
PATIENT_NOT_AT_SURGERY("06"),
NON_ABA_EHR_EXTRACT_REJECTED_WRONG_PATIENT("28");

@Getter
Expand All @@ -43,6 +46,7 @@ public MigrationStatus getMigrationStatus() {
case ABA_EHR_EXTRACT_SUPPRESSED -> EHR_EXTRACT_NEGATIVE_ACK_SUPPRESSED;
case ABA_EHR_EXTRACT_REJECTED_WRONG_PATIENT -> EHR_EXTRACT_NEGATIVE_ACK_ABA_INCORRECT_PATIENT;
case NON_ABA_EHR_EXTRACT_REJECTED_WRONG_PATIENT -> EHR_EXTRACT_NEGATIVE_ACK_NON_ABA_INCORRECT_PATIENT;
case PATIENT_NOT_AT_SURGERY -> EHR_EXTRACT_REQUEST_NEGATIVE_ACK_GP2GP_PATIENT_NOT_REGISTERED;
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void mergeAndBundleMessage(String conversationId) throws JAXBException, J
EHR_EXTRACT_TRANSLATED,
null
);
migrationStatusLogService.addMigrationStatusLog(MIGRATION_COMPLETED, conversationId, null);
migrationStatusLogService.addMigrationStatusLog(MIGRATION_COMPLETED, conversationId, null, null);

} catch (InlineAttachmentProcessingException | SAXException | TransformerException
| JAXBException | AttachmentNotFoundException | JsonProcessingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public boolean sendAckMessage(RCMRIN030000UK06Message payload, String conversati
LOGGER.debug("Sending Final ACK message for Conversation ID: [{}]", conversationId);

migrationStatusLogService.
addMigrationStatusLog(FINAL_ACK_SENT, conversationId, null);
addMigrationStatusLog(FINAL_ACK_SENT, conversationId, null, null);

return sendACKMessageHandler.prepareAndSendMessage(prepareAckMessageData(
payload,
Expand All @@ -46,7 +46,7 @@ public boolean sendAckMessage(COPCIN000001UK01Message payload, String conversati
LOGGER.debug("Sending ACK message for COPC message with Conversation ID: [{}]", conversationId);

migrationStatusLogService.
addMigrationStatusLog(COPC_ACKNOWLEDGED, conversationId, null);
addMigrationStatusLog(COPC_ACKNOWLEDGED, conversationId, null, null);

return sendACKMessageHandler.prepareAndSendMessage(prepareAckMessageData(
payload,
Expand Down Expand Up @@ -132,7 +132,7 @@ public boolean sendNackMessage(NACKReason reason, RCMRIN030000UKMessage payload,
LOGGER.debug("Sending NACK message with acknowledgement code [{}] for message EHR Extract message [{}]", reason.getCode(),
payload.getId().getRoot());

migrationStatusLogService.addMigrationStatusLog(reason.getMigrationStatus(), conversationId, null);
migrationStatusLogService.addMigrationStatusLog(reason.getMigrationStatus(), conversationId, null, reason.getCode());

return sendNACKMessageHandler.prepareAndSendMessage(prepareNackMessageData(
reason,
Expand All @@ -146,7 +146,7 @@ public boolean sendNackMessage(NACKReason reason, COPCIN000001UK01Message payloa
LOGGER.debug("Sending NACK message with acknowledgement code [{}] for message COPC message [{}]", reason.getCode(),
payload.getId().getRoot());

migrationStatusLogService.addMigrationStatusLog(reason.getMigrationStatus(), conversationId, null);
migrationStatusLogService.addMigrationStatusLog(reason.getMigrationStatus(), conversationId, null, reason.getCode());

return sendNACKMessageHandler.prepareAndSendMessage(prepareNackMessageData(
reason,
Expand Down
Loading