Skip to content

Commit

Permalink
[PRMT-3523] Dependency injected for PdsAdaptorService
Browse files Browse the repository at this point in the history
  • Loading branch information
joefong-nhs committed Aug 1, 2023
1 parent 31846d7 commit b898583
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/main/java/uk/nhs/prm/e2etests/annotation/Debt.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public @interface Debt {
String comment() default "This has been marked as technical debt to be addressed.";
Priority priority() default Priority.LOW;
String ticket() default "Not assigned.";;
String ticket() default "Not assigned.";

enum Priority {
LOW,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package uk.nhs.prm.e2etests.exception;

public class InvalidPdsAdaptorUsernameException extends RuntimeException {
private static final String MESSAGE = "The given PDS Adaptor username was invalid, details: %s";

public InvalidPdsAdaptorUsernameException(String details) {
super(String.format(MESSAGE, details));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public String getRepoOdsCode() {
}

public List<String> getSafeListedPatientList() {
return Arrays.asList(safeListedPatients.split(","));
String rawSafeListedPatientListStringFromSsm = super.getAwsSsmParameterValue(safeListedPatients);
return Arrays.asList(rawSafeListedPatientListStringFromSsm.split(","));
}

public String getSyntheticPatientPrefix() {
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/uk/nhs/prm/e2etests/service/PdsAdaptorService.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,57 @@
package uk.nhs.prm.e2etests.service;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import uk.nhs.prm.e2etests.annotation.Debt;
import uk.nhs.prm.e2etests.exception.InvalidPdsAdaptorUsernameException;
import uk.nhs.prm.e2etests.model.request.PdsAdaptorRequest;
import uk.nhs.prm.e2etests.model.response.PdsAdaptorResponse;
import uk.nhs.prm.e2etests.property.PdsAdaptorProperties;

@Debt(comment = "This needs to be annotated as a service and autowired in. There's a nested structure involving heavy" +
"use of the 'new' keyword that we should aim to remove.", ticket = "PRMT-3532")
@Log4j2
@Service
public class PdsAdaptorService {

private final String apiKey;
private final String patientRootUrl;
private final String username;

private final RestTemplate restTemplate = new RestTemplate();

public PdsAdaptorService(String username, String apiKey, String pdsAdaptorUrl) {
@Autowired
public PdsAdaptorService(PdsAdaptorProperties pdsAdaptorProperties, @Value("${test.pds.username}") String username) {
this.username = username;
this.apiKey = apiKey;
this.patientRootUrl = pdsAdaptorUrl;
this.patientRootUrl = pdsAdaptorProperties.getPdsAdaptorUrl();

switch (username) {
case "live-test" -> this.apiKey = pdsAdaptorProperties.getLiveTestApiKey();
case "e2e-test" -> this.apiKey = pdsAdaptorProperties.getE2eTestApiKey();
case "performance-test" -> this.apiKey = pdsAdaptorProperties.getPerformanceApiKey();
default ->
throw new InvalidPdsAdaptorUsernameException(String.format("Received username: %s", username));
}

}

public PdsAdaptorResponse getSuspendedPatientStatus(String nhsNumber) {
var patientUrl = buildUrl(patientRootUrl, nhsNumber);
System.out.printf("Requesting patient status from pds adaptor: %s%n", patientRootUrl);
log.info("Requesting patient status from pds adaptor: {}", patientRootUrl);
ResponseEntity<PdsAdaptorResponse> response =
restTemplate.exchange(patientUrl, HttpMethod.GET, new HttpEntity<>(createHeaders(username, apiKey)), PdsAdaptorResponse.class);
System.out.printf("Response received from pds adaptor: %s%n", response.getBody());
log.info("Response received from pds adaptor: {}", response.getBody());
return response.getBody();
}

public PdsAdaptorResponse updateManagingOrganisation(String nhsNumber, String previousGp, String recordETag) {
var patientUrl = buildUrl(patientRootUrl, nhsNumber);
PdsAdaptorRequest request = new PdsAdaptorRequest(previousGp, recordETag);
System.out.printf("Request to update patient : url - %s , request - %s%n", patientUrl, request);
log.info("Request to update patient : url - {} , request - {}", patientUrl, request);
ResponseEntity<PdsAdaptorResponse> response =
restTemplate.exchange(patientUrl, HttpMethod.PUT, new HttpEntity<>(request, createHeaders(username, apiKey)), PdsAdaptorResponse.class);
System.out.printf("Response received from pds adaptor update request: %s%n", response.getBody());
log.info("Response received from pds adaptor update request: {}", response.getBody());
return response.getBody();
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ nhs:
test:
numberOfPerfNhsNumbers: ${NUMBER_OF_PERF_NHS_NUMBERS:40}
performanceTestTimeout: ${PERFORMANCE_TEST_TIMEOUT:90}
pds:
username: unset

# Spring Properties
spring:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package uk.nhs.prm.e2etests.live_technical_test.day1;

import org.springframework.test.context.TestPropertySource;
import uk.nhs.prm.e2etests.property.Gp2gpMessengerProperties;
import uk.nhs.prm.e2etests.property.NhsProperties;
import uk.nhs.prm.e2etests.property.PdsAdaptorProperties;
import uk.nhs.prm.e2etests.property.QueueProperties;
import uk.nhs.prm.e2etests.live_technical_test.helpers.TestPatientValidator;
import uk.nhs.prm.e2etests.service.Gp2GpMessengerService;
Expand All @@ -20,6 +20,7 @@
import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@TestPropertySource(properties = {"test.pds.username=live-test"})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ChangeOfGPMessageReceivedTest {
private SuspensionServiceSuspensionsQueue suspensionServiceSuspensionsQueue;
Expand All @@ -28,25 +29,24 @@ class ChangeOfGPMessageReceivedTest {
private Gp2gpMessengerProperties gp2GpMessengerProperties;
private QueueProperties queueProperties;
private NhsProperties nhsProperties;
private PdsAdaptorProperties pdsAdaptorProperties;

private SqsService sqsService;
private PdsAdaptorService pdsAdaptorService;

@Autowired
public ChangeOfGPMessageReceivedTest(
TestPatientValidator testPatientValidator,
Gp2gpMessengerProperties gp2GpMessengerProperties,
QueueProperties queueProperties,
NhsProperties nhsProperties,
PdsAdaptorProperties pdsAdaptorProperties,
SqsService sqsService
SqsService sqsService,
PdsAdaptorService pdsAdaptorService
) {
this.patientValidator = testPatientValidator;
this.gp2GpMessengerProperties = gp2GpMessengerProperties;
this.queueProperties = queueProperties;
this.nhsProperties = nhsProperties;
this.pdsAdaptorProperties = pdsAdaptorProperties;
this.sqsService = sqsService;
this.pdsAdaptorService = pdsAdaptorService;
}

@BeforeEach
Expand All @@ -59,6 +59,8 @@ public void setUp() {
void shouldHaveReceivedSingleSuspensionChangeOfGpMessageRelatedToTestPatient() {
var safeListPatients = nhsProperties.getSafeListedPatientList();

System.out.println(safeListPatients);

if (safeListPatients.size() > 0) {

System.out.println("Safe list patient has size " + safeListPatients.size());
Expand Down Expand Up @@ -90,18 +92,6 @@ void shouldHaveReceivedSingleSuspensionChangeOfGpMessageRelatedToTestPatient() {
}

private PdsAdaptorResponse getPatientStatusOnPDSForSyntheticPatient(String testPatientNhsNumber) {
String pdsAdaptorUsername = "live-test";
return fetchPdsPatientStatus(pdsAdaptorUsername, testPatientNhsNumber);
}

private PdsAdaptorResponse fetchPdsPatientStatus(String pdsAdaptorUsername, String testPatientNhsNumber) {

var pds = new PdsAdaptorService(
pdsAdaptorUsername,
pdsAdaptorProperties.getLiveTestApiKey(),
pdsAdaptorProperties.getPdsAdaptorUrl()
);

return pds.getSuspendedPatientStatus(testPatientNhsNumber);
return pdsAdaptorService.getSuspendedPatientStatus(testPatientNhsNumber);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package uk.nhs.prm.e2etests.live_technical_test.day2;

import org.springframework.test.context.TestPropertySource;
import uk.nhs.prm.e2etests.service.Gp2GpMessengerService;
import uk.nhs.prm.e2etests.property.Gp2gpMessengerProperties;
import uk.nhs.prm.e2etests.property.PdsAdaptorProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import uk.nhs.prm.e2etests.property.NhsProperties;
Expand All @@ -16,28 +16,26 @@


@SpringBootTest
@TestPropertySource(properties = {"test.pds.username=live-test"})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ValidateMOFUpdatedTest {

private final String gp2GpMessengerApiKey;
private final String gp2GpMessengerUrl;
private final List<String> safeListedPatientList;
private final String pdsAdaptorApiKey;
private final String pdsAdaptorUrl;
private String pdsAdaptorUsername = "live-test";
private Gp2GpMessengerService gp2GpMessengerService;
private final PdsAdaptorService pdsAdaptorService;

@Autowired
public ValidateMOFUpdatedTest(
Gp2gpMessengerProperties gp2GpMessengerProperties,
PdsAdaptorProperties pdsAdaptorProperties,
NhsProperties nhsProperties
NhsProperties nhsProperties,
PdsAdaptorService pdsAdaptorService
) {
gp2GpMessengerApiKey = gp2GpMessengerProperties.getLiveTestApiKey();
gp2GpMessengerUrl = gp2GpMessengerProperties.getGp2gpMessengerUrl();
safeListedPatientList = nhsProperties.getSafeListedPatientList();
pdsAdaptorApiKey = pdsAdaptorProperties.getLiveTestApiKey();
pdsAdaptorUrl = pdsAdaptorProperties.getPdsAdaptorUrl();
this.pdsAdaptorService = pdsAdaptorService;
}

@BeforeEach
Expand All @@ -51,7 +49,7 @@ void shouldMoveSingleSuspensionMessageFromMeshMailBoxToNemsIncomingQueue() {
System.out.println("Safe list patient has size " + safeListedPatientList.size()); // TODO PRMT-3574 refactor to a consistent logging approach

safeListedPatientList.forEach(nhsNumber -> {
var pdsResponse = fetchPdsPatientStatus(pdsAdaptorUsername, nhsNumber);
var pdsResponse = fetchPdsPatientStatus(nhsNumber);
System.out.println("Patient suspended status is:" + pdsResponse.getIsSuspended());

System.out.println("Checking patient status with hl7 pds request - see logs for more details");
Expand All @@ -60,9 +58,7 @@ void shouldMoveSingleSuspensionMessageFromMeshMailBoxToNemsIncomingQueue() {
}
}

// TODO: PRMT-3523 @TestPropertySource(properties = {"PDS_ADAPTOR_TEST_USERNAME = live-test", apiKey = something}) could be useful.
private PdsAdaptorResponse fetchPdsPatientStatus(String pdsAdaptorUsername, String testPatientNhsNumber) {
PdsAdaptorService pds = new PdsAdaptorService(pdsAdaptorUsername, pdsAdaptorApiKey, pdsAdaptorUrl);
return pds.getSuspendedPatientStatus(testPatientNhsNumber);
private PdsAdaptorResponse fetchPdsPatientStatus(String testPatientNhsNumber) {
return pdsAdaptorService.getSuspendedPatientStatus(testPatientNhsNumber);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import uk.nhs.prm.e2etests.configuration.ActiveRoleArn;
import uk.nhs.prm.e2etests.property.NhsProperties;
import uk.nhs.prm.e2etests.property.PdsAdaptorProperties;
import uk.nhs.prm.e2etests.property.QueueProperties;
import uk.nhs.prm.e2etests.live_technical_test.TestParameters;
import uk.nhs.prm.e2etests.queue.ehrtransfer.EhrTransferServiceRepoIncomingQueue;
Expand All @@ -24,10 +24,9 @@
import static org.hamcrest.Matchers.equalTo;

@SpringBootTest
@TestPropertySource(properties = {"test.pds.username=live-test"})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ValidateSyntheticEhrTransferInToRepoUsingMofTest {

private static final String PDS_ADAPTOR_TEST_USERNAME = "live-test";
private TestPatientValidator patientValidator;
private PdsAdaptorService pdsAdaptorService;
private EhrRepositoryService ehrRepositoryService;
Expand All @@ -41,7 +40,7 @@ class ValidateSyntheticEhrTransferInToRepoUsingMofTest {
@Autowired
public ValidateSyntheticEhrTransferInToRepoUsingMofTest(
TestPatientValidator testPatientValidator,
PdsAdaptorProperties pdsAdaptorProperties,
PdsAdaptorService pdsAdaptorService,
EhrRepositoryService ehrRepositoryService,
NhsProperties nhsProperties,
QueueProperties queueProperties,
Expand All @@ -50,10 +49,7 @@ public ValidateSyntheticEhrTransferInToRepoUsingMofTest(
) {
patientValidator = testPatientValidator;

pdsAdaptorService = new PdsAdaptorService(
PDS_ADAPTOR_TEST_USERNAME,
pdsAdaptorProperties.getLiveTestApiKey(),
pdsAdaptorProperties.getPdsAdaptorUrl());
this.pdsAdaptorService = pdsAdaptorService;
this.ehrRepositoryService = ehrRepositoryService;
repoOdsCode = nhsProperties.getRepoOdsCode();
safeListedPatientList = nhsProperties.getSafeListedPatientList();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,40 @@
package uk.nhs.prm.e2etests.live_technical_test.repo;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import uk.nhs.prm.e2etests.property.PdsAdaptorProperties;
import org.springframework.test.context.TestPropertySource;
import uk.nhs.prm.e2etests.model.response.PdsAdaptorResponse;
import uk.nhs.prm.e2etests.live_technical_test.TestParameters;
import uk.nhs.prm.e2etests.live_technical_test.helpers.TestPatientValidator;
import uk.nhs.prm.e2etests.service.PdsAdaptorService;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@TestPropertySource(properties = {"test.pds.username=live-test"})
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ValidateSyntheticPatientIsSuspendedTest {

private final TestPatientValidator patientValidator;
private final String pdsAdaptorApiKey;
private final String pdsAdaptorUrl;
private static final String PDS_ADAPTOR_TEST_USERNAME = "live-test";
private PdsAdaptorService pdsAdaptorService;
private final PdsAdaptorService pdsAdaptorService;

@Autowired
public ValidateSyntheticPatientIsSuspendedTest(
TestPatientValidator testPatientValidator,
PdsAdaptorProperties pdsAdaptorProperties
PdsAdaptorService pdsAdaptorService
) {
patientValidator = testPatientValidator;
pdsAdaptorApiKey = pdsAdaptorProperties.getLiveTestApiKey();
pdsAdaptorUrl = pdsAdaptorProperties.getPdsAdaptorUrl();
}

// TODO: PRMT-3523 @TestPropertySource(properties = {"PDS_ADAPTOR_TEST_USERNAME = live-test", apiKey = something}) could be useful.
@BeforeEach
void setUp() {
pdsAdaptorService = new PdsAdaptorService(PDS_ADAPTOR_TEST_USERNAME, pdsAdaptorApiKey, pdsAdaptorUrl);
this.pdsAdaptorService = pdsAdaptorService;
}

@Test
void shouldUpdateMofToRepoAndSuccessfullyRetrieveEhrFromPreviousPractise() {
var testPatientNhsNumber = TestParameters.fetchTestParameter("LIVE_TECHNICAL_TEST_NHS_NUMBER");
String testPatientNhsNumber = TestParameters.fetchTestParameter("LIVE_TECHNICAL_TEST_NHS_NUMBER");
assertThat(patientValidator.isIncludedInTheTest(testPatientNhsNumber)).isTrue();

var pdsResponse = pdsAdaptorService.getSuspendedPatientStatus(testPatientNhsNumber);
PdsAdaptorResponse pdsResponse = pdsAdaptorService.getSuspendedPatientStatus(testPatientNhsNumber);
assertThat(pdsResponse.getIsSuspended()).as("patient status should be suspended").isTrue();
}
}
Loading

0 comments on commit b898583

Please sign in to comment.