Skip to content

Commit

Permalink
Merge branch 'main' into PRMT-3413-REFACTOR
Browse files Browse the repository at this point in the history
  • Loading branch information
martin-nhs authored Aug 15, 2023
2 parents 9b4b455 + e56d958 commit 765d4df
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 42 deletions.
18 changes: 12 additions & 6 deletions src/main/java/uk/nhs/prm/e2etests/enumeration/Gp2GpSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,32 @@
"into properties files. There are several cases in the codebase where the environments (dev, preprod, perf) etc" +
"are being referred to as strings. This enum would be an ideal candidate to expand out.", priority = MEDIUM)
public enum Gp2GpSystem {
REPO_DEV("B85002"),
REPO_TEST("B86041"),
EMIS_PTL_INT("N82668"),
TPP_PTL_INT("M85019");
REPO_DEV("B85002", "200000001613"),
REPO_TEST("B86041", "200000001694"),
EMIS_PTL_INT("N82668", "200000000631"),
TPP_PTL_INT("M85019", "200000000149");

private final String odsCode;
private final String asidCode;

Gp2GpSystem(String odsCode) {
Gp2GpSystem(String odsCode, String asidCode) {
this.odsCode = odsCode;
this.asidCode = asidCode;
}

public String odsCode() {
return odsCode;
}

public String asidCode() {
return asidCode;
}

public static Gp2GpSystem repoInEnv(String nhsEnvironment) {
switch (nhsEnvironment) {
case "dev" -> { return REPO_DEV; }
case "test" -> { return REPO_TEST; }
default -> throw new InvalidNhsEnvironmentException();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
public class TransferTrackerRecord {
private static final String DEFAULT_TIMESTAMP = LocalDateTime.now() + "Z";
private String conversationId;
private String largeEhrCoreMessageId;
@Builder.Default
private String largeEhrCoreMessageId = "";
private String nemsMessageId;
private String nhsNumber;
private String sourceGp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ public class EhrRequestTemplateContext implements TemplateContext {

@Pattern(regexp = ASID_REGEX, message = "An invalid ASID Code (new GP) was provided.")
private String asidCode;

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
@Getter
@Builder
public class SmallEhrTemplateContext implements TemplateContext {
@Builder.Default
@Pattern(regexp = UUID_REGEX, message = "An invalid Conversation ID was provided.")
private String conversationId;
private String inboundConversationId = UUID.randomUUID().toString().toUpperCase();

@Pattern(regexp = NHS_NUMBER_REGEX, message = "An invalid NHS Number was provided.")
private String nhsNumber;

@Builder.Default
@Pattern(regexp = UUID_REGEX, message = "An invalid Message ID was provided.")
private String messageId = UUID.randomUUID().toString();
}
private String messageId = UUID.randomUUID().toString().toUpperCase();
}
42 changes: 37 additions & 5 deletions src/main/java/uk/nhs/prm/e2etests/queue/AbstractMessageQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@
import lombok.extern.log4j.Log4j2;
import org.awaitility.core.ConditionTimeoutException;
import software.amazon.awssdk.services.sqs.model.PurgeQueueInProgressException;
import uk.nhs.prm.e2etests.model.SqsMessage;
import uk.nhs.prm.e2etests.model.nems.NemsResolutionMessage;
import uk.nhs.prm.e2etests.service.SqsService;
import org.awaitility.core.ConditionTimeoutException;
import uk.nhs.prm.e2etests.utility.MappingUtility;
import uk.nhs.prm.e2etests.service.SqsService;
import uk.nhs.prm.e2etests.model.SqsMessage;
import lombok.extern.log4j.Log4j2;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Optional;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;

@Log4j2
public abstract class AbstractMessageQueue {
Expand Down Expand Up @@ -61,7 +66,7 @@ public SqsMessage getMessageContaining(String substring) {
return foundMessage;
}

public List<SqsMessage> getAllMessageContaining(String substring, int expectedNumberOfMessages) {
public List<SqsMessage> getAllMessagesContaining(String substring, int expectedNumberOfMessages) {
// This method for now only try to get at least 2 messages from the queue. May need renaming or amending
log.info(CHECKING_QUEUE_LOG_MESSAGE, this.queueUri);
List<SqsMessage> allMessages = new ArrayList<>();
Expand Down Expand Up @@ -185,4 +190,31 @@ private boolean hasResolutionMessageNow(NemsResolutionMessage messageToCheck) {
protected void postAMessageWithAttributes(String message, Map<String, String> attributes) {
this.sqsService.postAMessage(queueUri, message, attributes);
}

public List<SqsMessage> attemptToGetAllMessagesContaining(String substring, int expectedNumberOfMessages, long secondsToPoll) {
if(expectedNumberOfMessages <= 0 || secondsToPoll <= 0)
throw new IllegalArgumentException("Expected number of messages or seconds to poll must be greater than 0.");

log.info("Attempting to get at least {} message(s) with substring {} on queue {}.", expectedNumberOfMessages, substring, this.queueUri);

List<SqsMessage> allMessages = new ArrayList<>();

try {
await().atMost(secondsToPoll, TimeUnit.SECONDS).with().pollInterval(100, TimeUnit.MILLISECONDS).untilAsserted(() -> {
Optional<SqsMessage> found = findMessageContaining(substring);

if (found.isPresent()) {
boolean isNewMessage = allMessages.stream()
.noneMatch(sqsMessage -> sqsMessage.getId().equals(found.get().getId()));
if (isNewMessage) allMessages.add(found.get());
}

assertThat("", allMessages.size() >= expectedNumberOfMessages);
});
} catch (ConditionTimeoutException exception) {
log.error("Failed to get at least {} message(s) with substring {} on queue {}, returning all found.", expectedNumberOfMessages, substring, this.queueUri);
}

return allMessages;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ public TransferTrackerDatabaseRepository(
}

public void save(TransferTrackerRecord transferTrackerDynamoDbEntry) {
// Update the conversation ID so that it can be saved into the Transfer Tracker DynamoDB.
final String conversationId = transferTrackerDynamoDbEntry.getConversationId();
transferTrackerDynamoDbEntry.setConversationId(conversationId.toLowerCase());

transferTrackerTable.putItem(transferTrackerDynamoDbEntry);
}

public Optional<TransferTrackerRecord> findByConversationId(String conversationId) {
return Optional.ofNullable(
transferTrackerTable.getItem(Key.builder().partitionValue(conversationId).build())
transferTrackerTable.getItem(Key.builder().partitionValue(conversationId.toLowerCase()).build())
);
}
}
Loading

0 comments on commit 765d4df

Please sign in to comment.