From 27489899ee7c7f46789a94e6e05e149c2db0602a Mon Sep 17 00:00:00 2001 From: mattch1 Date: Fri, 22 Nov 2024 13:57:06 +0000 Subject: [PATCH] LP-218 Use transaction in the controller --- .../controller/PartnershipController.java | 6 +++++- .../limitedpartnershipsapi/utils/Constants.java | 2 ++ .../controller/PartnershipControllerTest.java | 11 ++++++++++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/controller/PartnershipController.java b/src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/controller/PartnershipController.java index 6ee33aa..259c64f 100644 --- a/src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/controller/PartnershipController.java +++ b/src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/controller/PartnershipController.java @@ -4,10 +4,12 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestAttribute; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import uk.gov.companieshouse.api.model.transaction.Transaction; import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionCreatedResponseDto; import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionDto; import uk.gov.companieshouse.limitedpartnershipsapi.service.LimitedPartnershipService; @@ -18,6 +20,7 @@ import static uk.gov.companieshouse.api.util.security.EricConstants.ERIC_IDENTITY; import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.ERIC_REQUEST_ID_KEY; +import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.TRANSACTION_KEY; import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.URL_PARAM_TRANSACTION_ID; @RestController @@ -35,11 +38,12 @@ public PartnershipController(LimitedPartnershipService limitedPartnershipService @PostMapping public ResponseEntity createPartnership( + @RequestAttribute(TRANSACTION_KEY) Transaction transaction, @RequestBody LimitedPartnershipSubmissionDto limitedPartnershipSubmissionDto, @RequestHeader(value = ERIC_REQUEST_ID_KEY) String requestId, @RequestHeader(value = ERIC_IDENTITY) String userId) { - var transactionId = 12321123; + var transactionId = transaction.getId(); var logMap = new HashMap(); logMap.put(URL_PARAM_TRANSACTION_ID, transactionId); diff --git a/src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/utils/Constants.java b/src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/utils/Constants.java index 54f7645..b8ea7a0 100644 --- a/src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/utils/Constants.java +++ b/src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/utils/Constants.java @@ -9,4 +9,6 @@ private Constants() { } // URL path parameters public static final String URL_PARAM_TRANSACTION_ID = "transactionId"; + + public static final String TRANSACTION_KEY = "transaction"; } diff --git a/src/test/java/uk/gov/companieshouse/limitedpartnershipsapi/controller/PartnershipControllerTest.java b/src/test/java/uk/gov/companieshouse/limitedpartnershipsapi/controller/PartnershipControllerTest.java index 2947503..660090c 100644 --- a/src/test/java/uk/gov/companieshouse/limitedpartnershipsapi/controller/PartnershipControllerTest.java +++ b/src/test/java/uk/gov/companieshouse/limitedpartnershipsapi/controller/PartnershipControllerTest.java @@ -8,6 +8,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; +import uk.gov.companieshouse.api.model.transaction.Transaction; import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.DataDto; import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionCreatedResponseDto; import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionDto; @@ -27,6 +28,7 @@ class PartnershipControllerTest { private static final String REQUEST_ID = "5346336"; private static final String USER_ID = "rjg736k791"; private static final String SUBMISSION_ID = "ABC123ABC456"; + private static final String TRANSACTION_ID = "12321123"; @InjectMocks private PartnershipController partnershipController; @@ -34,6 +36,9 @@ class PartnershipControllerTest { @Mock private LimitedPartnershipService limitedPartnershipService; + @Mock + private Transaction transaction; + private LimitedPartnershipSubmissionDto limitedPartnershipSubmissionDto; @BeforeEach @@ -51,7 +56,10 @@ void testCreatePartnership() { eq(USER_ID))) .thenReturn(SUBMISSION_ID); + when(transaction.getId()).thenReturn(TRANSACTION_ID); + var response = partnershipController.createPartnership( + transaction, limitedPartnershipSubmissionDto, REQUEST_ID, USER_ID); @@ -59,7 +67,7 @@ void testCreatePartnership() { assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value()); var responseHeaderLocation = Objects.requireNonNull(response.getHeaders().get(HttpHeaders.LOCATION)).getFirst(); assertEquals( - String.format(URL_GET_PARTNERSHIP, 12321123, SUBMISSION_ID), + String.format(URL_GET_PARTNERSHIP, TRANSACTION_ID, SUBMISSION_ID), responseHeaderLocation); LimitedPartnershipSubmissionCreatedResponseDto responseBody = (LimitedPartnershipSubmissionCreatedResponseDto) response.getBody(); assert responseBody != null; @@ -75,6 +83,7 @@ void testCreatePartnershipInternalServerError() { .thenThrow(new RuntimeException()); var response = partnershipController.createPartnership( + transaction, limitedPartnershipSubmissionDto, REQUEST_ID, USER_ID);