Skip to content

Commit

Permalink
LP-218 Use transaction in the controller
Browse files Browse the repository at this point in the history
  • Loading branch information
mattch1 committed Nov 22, 2024
1 parent e48700b commit 2748989
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -35,11 +38,12 @@ public PartnershipController(LimitedPartnershipService limitedPartnershipService

@PostMapping
public ResponseEntity<Object> 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<String, Object>();
logMap.put(URL_PARAM_TRANSACTION_ID, transactionId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,13 +28,17 @@ 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;

@Mock
private LimitedPartnershipService limitedPartnershipService;

@Mock
private Transaction transaction;

private LimitedPartnershipSubmissionDto limitedPartnershipSubmissionDto;

@BeforeEach
Expand All @@ -51,15 +56,18 @@ void testCreatePartnership() {
eq(USER_ID)))
.thenReturn(SUBMISSION_ID);

when(transaction.getId()).thenReturn(TRANSACTION_ID);

var response = partnershipController.createPartnership(
transaction,
limitedPartnershipSubmissionDto,
REQUEST_ID,
USER_ID);

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;
Expand All @@ -75,6 +83,7 @@ void testCreatePartnershipInternalServerError() {
.thenThrow(new RuntimeException());

var response = partnershipController.createPartnership(
transaction,
limitedPartnershipSubmissionDto,
REQUEST_ID,
USER_ID);
Expand Down

0 comments on commit 2748989

Please sign in to comment.