Skip to content
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
@@ -1,6 +1,7 @@
package uk.nhs.digital.nhsconnect.nhais;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.reflect.FieldUtils;
Expand Down Expand Up @@ -45,6 +46,7 @@
@ExtendWith({SpringExtension.class, SoftAssertionsExtension.class, IntegrationTestsExtension.class})
@SpringBootTest
@Slf4j
@Getter
public abstract class IntegrationBaseTest {

public static final String DLQ_PREFIX = "DLQ.";
Expand All @@ -54,34 +56,35 @@ public abstract class IntegrationBaseTest {
private static final int JMS_RECEIVE_TIMEOUT = 500;
@Rule
public Timeout globalTimeout = Timeout.seconds(2);

@Autowired
protected JmsTemplate jmsTemplate;
private JmsTemplate jmsTemplate;
@Autowired
protected InboundStateRepository inboundStateRepository;
private InboundStateRepository inboundStateRepository;
@Autowired
protected OutboundStateRepository outboundStateRepository;
private OutboundStateRepository outboundStateRepository;
@Autowired
protected ObjectMapper objectMapper;
private ObjectMapper objectMapper;
@Autowired
protected MeshClient meshClient;
private MeshClient meshClient;
@Autowired
protected MeshConfig meshConfig;
private MeshConfig meshConfig;
@Autowired
private RecipientMailboxIdMappings recipientMailboxIdMappings;
@Autowired
private MeshHeaders meshHeaders;
@Autowired
private MeshHttpClientBuilder meshHttpClientBuilder;
@Value("${nhais.amqp.meshInboundQueueName}")
protected String meshInboundQueueName;
private String meshInboundQueueName;
@Value("${nhais.amqp.meshOutboundQueueName}")
protected String meshOutboundQueueName;
private String meshOutboundQueueName;
@Value("${nhais.amqp.gpSystemInboundQueueName}")
protected String gpSystemInboundQueueName;
private String gpSystemInboundQueueName;
@Autowired
private InboundQueueService inboundQueueService;
private long originalReceiveTimeout;
protected MeshClient nhaisMeshClient;
private MeshClient nhaisMeshClient;

@PostConstruct
private void postConstruct() {
Expand Down Expand Up @@ -190,9 +193,15 @@ private MeshClient buildMeshClientForNhaisMailbox() {
String endpointCert = (String) FieldUtils.readField(meshConfig, "endpointCert", true);
String endpointPrivateKey = (String) FieldUtils.readField(meshConfig, "endpointPrivateKey", true);
String subCaCert = (String) FieldUtils.readField(meshConfig, "subCAcert", true);
MeshConfig nhaisMailboxConfig = new MeshConfig(nhaisMailboxId, meshConfig.getMailboxPassword(),
meshConfig.getSharedKey(), meshConfig.getHost(), meshConfig.getCertValidation(), endpointCert,
endpointPrivateKey, subCaCert);
MeshConfig nhaisMailboxConfig = new MeshConfig()
.setMailboxId(nhaisMailboxId)
.setMailboxPassword(meshConfig.getMailboxPassword())
.setSharedKey(meshConfig.getSharedKey())
.setHost(meshConfig.getHost())
.setCertValidation(meshConfig.getCertValidation())
.setEndpointCert(endpointCert)
.setEndpointPrivateKey(endpointPrivateKey)
.setSubCAcert(subCaCert);
MeshHeaders meshHeaders = new MeshHeaders(nhaisMailboxConfig);
MeshRequests meshRequests = new MeshRequests(nhaisMailboxConfig, meshHeaders);
return new MeshClient(meshRequests, mockRecipientMailboxIdMappings, meshHttpClientBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
@AutoConfigureMockMvc
@DirtiesContext
public class TimeToLiveTest {
private static final long INTERCHANGE_SEQUENCE = 123L;
private static final int AWAIT_TIMEOUT = 90;

@Autowired
private InboundStateRepository inboundStateRepository;
Expand Down Expand Up @@ -75,14 +77,14 @@ void When_TimeToLiveHasPassedInInboundState_Expect_DocumentRemoved() {
.setWorkflowId(WorkflowId.RECEP)
.setSender("some_sender")
.setRecipient("some_recipient")
.setInterchangeSequence(123L)
.setInterchangeSequence(INTERCHANGE_SEQUENCE)
.setTranslationTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant());

assertThat(inboundStateRepository.findAll()).isEmpty();
inboundStateRepository.save(inboundState);
assertThat(inboundStateRepository.findAll()).isNotEmpty();
await()
.atMost(90, TimeUnit.SECONDS)
.atMost(AWAIT_TIMEOUT, TimeUnit.SECONDS)
.pollInterval(Durations.ONE_SECOND)
.untilAsserted(() -> assertThat(inboundStateRepository.findAll()).isEmpty());
}
Expand All @@ -94,14 +96,14 @@ void When_TimeToLiveHasPassedInOutboundState_Expect_DocumentRemoved() {
.setWorkflowId(WorkflowId.RECEP)
.setSender("some_sender")
.setRecipient("some_recipient")
.setInterchangeSequence(123L)
.setInterchangeSequence(INTERCHANGE_SEQUENCE)
.setTranslationTimestamp(Instant.now().atZone(ZoneId.systemDefault()).toInstant());

assertThat(outboundStateRepository.findAll()).isEmpty();
outboundStateRepository.save(inboundState);
assertThat(outboundStateRepository.findAll()).isNotEmpty();
await()
.atMost(90, TimeUnit.SECONDS)
.atMost(AWAIT_TIMEOUT, TimeUnit.SECONDS)
.pollInterval(Durations.ONE_SECOND)
.untilAsserted(() -> assertThat(outboundStateRepository.findAll()).isEmpty());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public class DeadLetterQueueTest extends IntegrationBaseTest {

@Test
public void When_SendingInvalidMessageToMeshInboundQueue_Expect_MessageIsSentToDeadLetterQueue() throws JMSException {
clearDeadLetterQueue(meshInboundQueueName);
clearDeadLetterQueue(super.getMeshInboundQueueName());
sendToMeshInboundQueue(MESSAGE_CONTENT);

var message = getDeadLetterMeshInboundQueueMessage(meshInboundQueueName);
var message = getDeadLetterMeshInboundQueueMessage(super.getMeshInboundQueueName());
var messageBody = parseTextMessage(message);

assertThat(messageBody).isEqualTo(MESSAGE_CONTENT);
Expand All @@ -59,10 +59,10 @@ public void When_MeshOutboundQueueMessageCannotBeProcessed_Expect_MessageIsSentT
);
doThrow(RuntimeException.class).when(meshClient).authenticate();

clearDeadLetterQueue(meshOutboundQueueName);
clearDeadLetterQueue(super.getMeshOutboundQueueName());
outboundQueueService.publish(meshMessage);

var message = getDeadLetterMeshInboundQueueMessage(meshOutboundQueueName);
var message = getDeadLetterMeshInboundQueueMessage(super.getMeshOutboundQueueName());

assertThat(message.getStringProperty(JmsHeaders.CONVERSATION_ID)).isEqualTo(conversationId);
assertThat(parseTextMessage(message)).isEqualTo(objectMapper.writeValueAsString(meshMessage));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -64,13 +65,21 @@ public class InboundMeshQueueMultiTransactionTest extends IntegrationBaseTest {
private static final String TRANSACTION_4_OPERATION_ID = OperationId.buildOperationId(RECIPIENT, TN_4);
private static final String TRANSACTION_5_OPERATION_ID = OperationId.buildOperationId(RECIPIENT, TN_5);
private static final String TRANSACTION_6_OPERATION_ID = OperationId.buildOperationId(RECIPIENT, TN_6);
private static final int TN1_INDEX = 0;
private static final int TN2_INDEX = 1;
private static final int TN3_INDEX = 2;
private static final int TN4_INDEX = 3;
private static final int TN5_INDEX = 4;
private static final int TN6_INDEX = 5;
private static final int INBOUND_STATE_EXPECTED_SIZE = 6;
private static final Instant INTERCHANGE_TRANSLATION_TIMESTAMP = ZonedDateTime
.of(2020, 1, 25, 12, 35, 0, 0, TimestampService.UK_ZONE)
.of(LocalDateTime.parse("2020-01-25T12:35:00"), TimestampService.UK_ZONE)
.toInstant();

private static final Instant GENERATED_TIMESTAMP = ZonedDateTime.of(2020, 6, 10, 14, 38, 0, 0, TimestampService.UK_ZONE)
private static final Instant GENERATED_TIMESTAMP = ZonedDateTime
.of(LocalDateTime.parse("2020-06-10T14:38:00"), TimestampService.UK_ZONE)
.toInstant();
private static final String ISO_GENERATED_TIMESTAMP = new TimestampService().formatInISO(GENERATED_TIMESTAMP);
private static final String ISO_GENERATED_TIMESTAMP = new TimestampService()
.formatInISO(GENERATED_TIMESTAMP);

@MockBean
private TimestampService timestampService;
Expand Down Expand Up @@ -132,19 +141,19 @@ private List<InboundState> getAllInboundStates() {
.flatMap(Optional::stream)
.collect(Collectors.toList());

if (inboundStates.size() == 6) {
if (inboundStates.size() == INBOUND_STATE_EXPECTED_SIZE) {
return inboundStates;
}
return null;
}

private Optional<InboundState> findInboundState(long sms, long tn) {
return inboundStateRepository.findBy(WorkflowId.REGISTRATION, SENDER, RECIPIENT, SIS, sms, tn);
return super.getInboundStateRepository().findBy(WorkflowId.REGISTRATION, SENDER, RECIPIENT, SIS, sms, tn);
}

private void assertOutboundRecepMessage(SoftAssertions softly) throws IOException {

var meshMessage = waitForMeshMessage(nhaisMeshClient);
var meshMessage = waitForMeshMessage(super.getNhaisMeshClient());

softly.assertThat(meshMessage.getContent()).isEqualTo(new String(Files.readAllBytes(recep.getFile().toPath())));
softly.assertThat(meshMessage.getWorkflowId()).isEqualTo(WorkflowId.RECEP);
Expand All @@ -154,22 +163,22 @@ private void assertOutboundRecepMessage(SoftAssertions softly) throws IOExceptio
}

private void assertGpSystemInboundQueueMessages(SoftAssertions softly) throws JMSException, IOException, JSONException {
var gpSystemInboundQueueMessages = IntStream.range(0, 6)
var gpSystemInboundQueueMessages = IntStream.range(0, INBOUND_STATE_EXPECTED_SIZE)
.mapToObj(x -> getGpSystemInboundQueueMessage())
.toList();

assertGpSystemInboundQueueMessages(
softly, gpSystemInboundQueueMessages.get(0), MESSAGE_1_TRANSACTION_TYPE, TRANSACTION_1_OPERATION_ID, fhirTN1);
softly, gpSystemInboundQueueMessages.get(TN1_INDEX), MESSAGE_1_TRANSACTION_TYPE, TRANSACTION_1_OPERATION_ID, fhirTN1);
assertGpSystemInboundQueueMessages(
softly, gpSystemInboundQueueMessages.get(1), MESSAGE_2_TRANSACTION_TYPE, TRANSACTION_2_OPERATION_ID, fhirTN2);
softly, gpSystemInboundQueueMessages.get(TN2_INDEX), MESSAGE_2_TRANSACTION_TYPE, TRANSACTION_2_OPERATION_ID, fhirTN2);
assertGpSystemInboundQueueMessages(
softly, gpSystemInboundQueueMessages.get(2), MESSAGE_2_TRANSACTION_TYPE, TRANSACTION_3_OPERATION_ID, fhirTN3);
softly, gpSystemInboundQueueMessages.get(TN3_INDEX), MESSAGE_2_TRANSACTION_TYPE, TRANSACTION_3_OPERATION_ID, fhirTN3);
assertGpSystemInboundQueueMessages(
softly, gpSystemInboundQueueMessages.get(3), MESSAGE_3_TRANSACTION_TYPE, TRANSACTION_4_OPERATION_ID, fhirTN4);
softly, gpSystemInboundQueueMessages.get(TN4_INDEX), MESSAGE_3_TRANSACTION_TYPE, TRANSACTION_4_OPERATION_ID, fhirTN4);
assertGpSystemInboundQueueMessages(
softly, gpSystemInboundQueueMessages.get(4), MESSAGE_4_TRANSACTION_TYPE, TRANSACTION_5_OPERATION_ID, fhirTN5);
softly, gpSystemInboundQueueMessages.get(TN5_INDEX), MESSAGE_4_TRANSACTION_TYPE, TRANSACTION_5_OPERATION_ID, fhirTN5);
assertGpSystemInboundQueueMessages(
softly, gpSystemInboundQueueMessages.get(5), MESSAGE_4_TRANSACTION_TYPE, TRANSACTION_6_OPERATION_ID, fhirTN6);
softly, gpSystemInboundQueueMessages.get(TN6_INDEX), MESSAGE_4_TRANSACTION_TYPE, TRANSACTION_6_OPERATION_ID, fhirTN6);
}

private void assertGpSystemInboundQueueMessages(
Expand Down Expand Up @@ -199,20 +208,20 @@ private void assertGpSystemInboundQueueMessages(
}

private void assertInboundStates(SoftAssertions softly, List<InboundState> inboundStates) {
softly.assertThat(inboundStates).hasSize(6);
softly.assertThat(inboundStates).hasSize(INBOUND_STATE_EXPECTED_SIZE);

assertInboundState(
softly, inboundStates.get(0), TRANSACTION_1_OPERATION_ID, SMS_1, TN_1, MESSAGE_1_TRANSACTION_TYPE);
softly, inboundStates.get(TN1_INDEX), TRANSACTION_1_OPERATION_ID, SMS_1, TN_1, MESSAGE_1_TRANSACTION_TYPE);
assertInboundState(
softly, inboundStates.get(1), TRANSACTION_2_OPERATION_ID, SMS_2, TN_2, MESSAGE_2_TRANSACTION_TYPE);
softly, inboundStates.get(TN2_INDEX), TRANSACTION_2_OPERATION_ID, SMS_2, TN_2, MESSAGE_2_TRANSACTION_TYPE);
assertInboundState(
softly, inboundStates.get(2), TRANSACTION_3_OPERATION_ID, SMS_2, TN_3, MESSAGE_2_TRANSACTION_TYPE);
softly, inboundStates.get(TN3_INDEX), TRANSACTION_3_OPERATION_ID, SMS_2, TN_3, MESSAGE_2_TRANSACTION_TYPE);
assertInboundState(
softly, inboundStates.get(3), TRANSACTION_4_OPERATION_ID, SMS_3, TN_4, MESSAGE_3_TRANSACTION_TYPE);
softly, inboundStates.get(TN4_INDEX), TRANSACTION_4_OPERATION_ID, SMS_3, TN_4, MESSAGE_3_TRANSACTION_TYPE);
assertInboundState(
softly, inboundStates.get(4), TRANSACTION_5_OPERATION_ID, SMS_4, TN_5, MESSAGE_4_TRANSACTION_TYPE);
softly, inboundStates.get(TN5_INDEX), TRANSACTION_5_OPERATION_ID, SMS_4, TN_5, MESSAGE_4_TRANSACTION_TYPE);
assertInboundState(
softly, inboundStates.get(5), TRANSACTION_6_OPERATION_ID, SMS_4, TN_6, MESSAGE_4_TRANSACTION_TYPE);
softly, inboundStates.get(TN6_INDEX), TRANSACTION_6_OPERATION_ID, SMS_4, TN_6, MESSAGE_4_TRANSACTION_TYPE);
}

private void assertInboundState(
Expand Down Expand Up @@ -241,8 +250,8 @@ private void assertInboundState(
}

private void assertOutboundState(SoftAssertions softly) {
waitForCondition(() -> outboundStateRepository.findAll().iterator().hasNext());
Iterable<OutboundState> outboundStates = outboundStateRepository.findAll();
waitForCondition(() -> super.getOutboundStateRepository().findAll().iterator().hasNext());
Iterable<OutboundState> outboundStates = super.getOutboundStateRepository().findAll();

assertThat(outboundStates).hasSize(1);
var outboundState = outboundStates.iterator().next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;

Expand All @@ -40,7 +41,7 @@ public class InboundMeshQueueRecepTest extends IntegrationBaseTest {
private static final String SENDER = "FHS1";
private static final String RECIPIENT = "GP05";
private static final Instant TRANSLATION_TIMESTAMP = ZonedDateTime
.of(2020, 6, 20, 14, 0, 0, 0, TimestampService.UK_ZONE)
.of(LocalDateTime.parse("2020-06-20T14:00:00"), TimestampService.UK_ZONE)
.toInstant();
// Mongo only supports millis precision
private static final Instant PROCESSED_TIMESTAMP = Instant.now().truncatedTo(ChronoUnit.MILLIS);
Expand Down Expand Up @@ -81,7 +82,7 @@ private void assertOutboundStateRecepUpdates(SoftAssertions softly) {
var expectedOutboundStateRef2 = buildExpectedOutboundState(REF_MESSAGE_SEQUENCE_2, ReferenceMessageRecep.RecepCode.ERROR);

var outboundStates = waitFor(() -> {
var all = Lists.newArrayList(outboundStateRepository.findAll());
var all = Lists.newArrayList(super.getOutboundStateRepository().findAll());
if (all.stream().allMatch(outboundState -> outboundState.getRecep() != null)) {
return all;
}
Expand All @@ -105,7 +106,7 @@ private OutboundState buildExpectedOutboundState(long refMessageSequence1, Refer

private void assertInboundState(SoftAssertions softly) {
var inboundState = waitFor(
() -> inboundStateRepository
() -> super.getInboundStateRepository()
.findBy(WorkflowId.RECEP, SENDER, RECIPIENT, INTERCHANGE_SEQUENCE, MESSAGE_SEQUENCE, null)
.orElse(null));

Expand All @@ -123,8 +124,8 @@ private void assertInboundState(SoftAssertions softly) {
}

private void createOutboundStateRecords() {
outboundStateRepository.save(buildOutboundState(REF_MESSAGE_SEQUENCE_1));
outboundStateRepository.save(buildOutboundState(REF_MESSAGE_SEQUENCE_2));
super.getOutboundStateRepository().save(buildOutboundState(REF_MESSAGE_SEQUENCE_1));
super.getOutboundStateRepository().save(buildOutboundState(REF_MESSAGE_SEQUENCE_2));
}

private OutboundState buildOutboundState(long refMessageSequence1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;

import static org.mockito.Mockito.when;
Expand All @@ -42,11 +43,13 @@ public class InboundMeshQueueRegistrationTest extends IntegrationBaseTest {
private static final ReferenceTransactionType.Inbound TRANSACTION_TYPE = ReferenceTransactionType.Inbound.APPROVAL;
private static final String OPERATION_ID = OperationId.buildOperationId(RECIPIENT, TN);
private static final Instant TRANSLATION_TIMESTAMP = ZonedDateTime
.of(2020, 1, 25, 12, 35, 0, 0, TimestampService.UK_ZONE)
.of(LocalDateTime.parse("2020-01-25T12:35:00"), TimestampService.UK_ZONE)
.toInstant();
private static final Instant GENERATED_TIMESTAMP = ZonedDateTime.of(2020, 6, 10, 14, 38, 00, 0, TimestampService.UK_ZONE)
private static final Instant GENERATED_TIMESTAMP = ZonedDateTime
.of(LocalDateTime.parse("2020-06-10T14:38:00"), TimestampService.UK_ZONE)
.toInstant();
private static final String ISO_GENERATED_TIMESTAMP = new TimestampService().formatInISO(GENERATED_TIMESTAMP);
private static final String ISO_GENERATED_TIMESTAMP = new TimestampService()
.formatInISO(GENERATED_TIMESTAMP);

@MockBean
private TimestampService timestampService;
Expand Down Expand Up @@ -83,7 +86,7 @@ void When_MeshInboundQueueRegistrationMessageIsReceived_Expect_MessageIsHandled(
}

private void assertOutboundRecepMessage(SoftAssertions softly) throws IOException {
var meshMessage = waitForMeshMessage(nhaisMeshClient);
var meshMessage = waitForMeshMessage(super.getNhaisMeshClient());

softly.assertThat(meshMessage.getContent()).isEqualTo(new String(Files.readAllBytes(recep.getFile().toPath())));
softly.assertThat(meshMessage.getWorkflowId()).isEqualTo(WorkflowId.RECEP);
Expand All @@ -104,7 +107,7 @@ private void assertGpSystemInboundQueueMessage(SoftAssertions softly) throws JMS

private void assertInboundState(SoftAssertions softly) {
var inboundState = waitFor(
() -> inboundStateRepository
() -> super.getInboundStateRepository()
.findBy(WorkflowId.REGISTRATION, SENDER, RECIPIENT, SIS, SMS, TN)
.orElse(null));

Expand All @@ -123,7 +126,7 @@ private void assertInboundState(SoftAssertions softly) {
}

private void assertOutboundState(SoftAssertions softly) {
Iterable<OutboundState> outboundStates = outboundStateRepository.findAll();
Iterable<OutboundState> outboundStates = super.getOutboundStateRepository().findAll();

softly.assertThat(outboundStates).hasSize(1);
var outboundState = outboundStates.iterator().next();
Expand Down
Loading