Skip to content

Commit

Permalink
Merge pull request #54 from companieshouse/feature/create-get-endpoint
Browse files Browse the repository at this point in the history
LP-304 Implement GET Endpoint to retrieve submission resource
  • Loading branch information
dlloyd1-ch authored Dec 19, 2024
2 parents 117263e + 1051468 commit af960b6
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -15,6 +16,7 @@
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.exception.ResourceNotFoundException;
import uk.gov.companieshouse.limitedpartnershipsapi.exception.ServiceException;
import uk.gov.companieshouse.limitedpartnershipsapi.model.DataType;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionCreatedResponseDto;
Expand All @@ -29,15 +31,14 @@
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_GET_PARTNERSHIP;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.URL_PARAM_SUBMISSION_ID;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.URL_PARAM_TRANSACTION_ID;

@RestController
@RequestMapping("/transactions/{" + URL_PARAM_TRANSACTION_ID + "}/limited-partnership/partnership")
public class PartnershipController {

static final String URL_GET_PARTNERSHIP = "/transactions/%s/limited-partnership/partnership/%s";

private final LimitedPartnershipService limitedPartnershipService;

@Autowired
Expand Down Expand Up @@ -98,11 +99,30 @@ public ResponseEntity<Object> updatePartnership(
}
}

@GetMapping("/{" + URL_PARAM_SUBMISSION_ID + "}")
public ResponseEntity<Object> getPartnership(
@RequestAttribute(TRANSACTION_KEY) Transaction transaction,
@PathVariable(URL_PARAM_SUBMISSION_ID) String submissionId,
@RequestHeader(value = ERIC_REQUEST_ID_KEY) String requestId,
@RequestHeader(value = ERIC_IDENTITY) String userId
){
var transactionId = transaction.getId();
var logMap = new HashMap<String, Object>();
logMap.put(URL_PARAM_TRANSACTION_ID, transactionId);

try {
LimitedPartnershipSubmissionDto dto = limitedPartnershipService.getLimitedPartnership(transaction, submissionId);
return ResponseEntity.ok().body(dto);
} catch (ResourceNotFoundException e){
ApiLogger.errorContext(requestId, e.getMessage(), e, logMap);
return ResponseEntity.notFound().build();
}
}

private static Map<String, Object> extractData(Map<String, Object> body) throws JsonProcessingException {
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
var json = ow.writeValueAsString(body.get("data"));

return new ObjectMapper().readValue(json, Map.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uk.gov.companieshouse.limitedpartnershipsapi.exception;

public class ResourceNotFoundException extends ServiceException{
public ResourceNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import org.springframework.stereotype.Service;
import uk.gov.companieshouse.api.model.transaction.Resource;
import uk.gov.companieshouse.api.model.transaction.Transaction;
import uk.gov.companieshouse.limitedpartnershipsapi.exception.ResourceNotFoundException;
import uk.gov.companieshouse.limitedpartnershipsapi.exception.ServiceException;
import uk.gov.companieshouse.limitedpartnershipsapi.mapper.LimitedPartnershipMapper;
import uk.gov.companieshouse.limitedpartnershipsapi.model.DataType;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dao.LimitedPartnershipSubmissionDao;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionDto;
import uk.gov.companieshouse.limitedpartnershipsapi.repository.LimitedPartnershipSubmissionsRepository;
import uk.gov.companieshouse.limitedpartnershipsapi.utils.ApiLogger;
import uk.gov.companieshouse.limitedpartnershipsapi.utils.TransactionUtils;

import java.time.LocalDateTime;
import java.util.Collections;
Expand All @@ -19,22 +21,25 @@

import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.FILING_KIND_LIMITED_PARTNERSHIP;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.LINK_SELF;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.SUBMISSION_URI_PATTERN;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.URL_GET_PARTNERSHIP;

@Service
public class LimitedPartnershipService {

private final LimitedPartnershipMapper mapper;
private final LimitedPartnershipSubmissionsRepository repository;
private final TransactionService transactionService;
private final TransactionUtils transactionUtils;

@Autowired
public LimitedPartnershipService(LimitedPartnershipMapper mapper,
LimitedPartnershipSubmissionsRepository repository,
TransactionService transactionService) {
TransactionService transactionService,
TransactionUtils transactionUtils) {
this.mapper = mapper;
this.repository = repository;
this.transactionService = transactionService;
this.transactionUtils = transactionUtils;
}

public String createLimitedPartnership(Transaction transaction,
Expand Down Expand Up @@ -106,7 +111,7 @@ private Resource createLimitedPartnershipTransactionResource(String submissionUr
}

private String getSubmissionUri(String transactionId, String submissionId) {
return String.format(SUBMISSION_URI_PATTERN, transactionId, submissionId);
return String.format(URL_GET_PARTNERSHIP, transactionId, submissionId);
}

private void updateTransactionWithLinksAndPartnershipName(Transaction transaction,
Expand All @@ -133,4 +138,16 @@ private void updateLimitedPartnershipSubmissionWithSelfLink(LimitedPartnershipSu
submission.setLinks(Collections.singletonMap(LINK_SELF, submissionUri));
repository.save(submission);
}

public LimitedPartnershipSubmissionDto getLimitedPartnership(Transaction transaction, String submissionId) throws ResourceNotFoundException{
String submissionUri = getSubmissionUri(transaction.getId(), submissionId);
if (!transactionUtils.isTransactionLinkedToLimitedPartnershipSubmission(transaction, submissionUri)) {
throw new ResourceNotFoundException(String.format(
"Transaction id: %s does not have a resource that matches submission id: %s", transaction.getId(), submissionId));
}

var submission = repository.findById(submissionId);
LimitedPartnershipSubmissionDao submissionDao = submission.orElseThrow(() -> new ResourceNotFoundException(String.format("Submission with id %s not found", submissionId)));
return mapper.daoToDto(submissionDao);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ private Constants() {
// URIs
public static final String TRANSACTIONS_PRIVATE_API_URI_PREFIX = "/private/transactions/";
public static final String SUBMISSION_URI_PATTERN = "/transactions/%s/limited-partnership/%s";
public static final String URL_GET_PARTNERSHIP = "/transactions/%s/limited-partnership/partnership/%s";

// Filings
public static final String FILING_KIND_LIMITED_PARTNERSHIP = "limited-partnership";

public static final String LINK_SELF = "self";
public static final String LINK_RESOURCE = "resource";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package uk.gov.companieshouse.limitedpartnershipsapi.utils;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import uk.gov.companieshouse.api.model.transaction.Transaction;

import java.util.Objects;

import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.FILING_KIND_LIMITED_PARTNERSHIP;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.LINK_RESOURCE;

@Component
public class TransactionUtils {

public boolean isTransactionLinkedToLimitedPartnershipSubmission(Transaction transaction, String limitedPartnershipSubmissionSelfLink) {
if (StringUtils.isBlank(limitedPartnershipSubmissionSelfLink)) {
return false;
}

if (Objects.isNull(transaction) || Objects.isNull(transaction.getResources())) {
return false;
}

return transaction.getResources().entrySet().stream()
.filter(resource -> FILING_KIND_LIMITED_PARTNERSHIP.equals(resource.getValue().getKind()))
.anyMatch(resource -> limitedPartnershipSubmissionSelfLink.equals(resource.getValue().getLinks().get(LINK_RESOURCE)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import uk.gov.companieshouse.api.model.transaction.Transaction;
import uk.gov.companieshouse.limitedpartnershipsapi.exception.ResourceNotFoundException;
import uk.gov.companieshouse.limitedpartnershipsapi.exception.ServiceException;
import uk.gov.companieshouse.limitedpartnershipsapi.model.DataType;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.DataDto;
Expand All @@ -25,7 +26,7 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
import static uk.gov.companieshouse.limitedpartnershipsapi.controller.PartnershipController.URL_GET_PARTNERSHIP;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.URL_GET_PARTNERSHIP;

@ExtendWith(MockitoExtension.class)
class PartnershipControllerTest {
Expand Down Expand Up @@ -141,4 +142,45 @@ void testUpdatePartnershipInternalServerError() throws ServiceException, JsonPro

assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), response.getStatusCode().value());
}

@Test
void testGetPartnership() throws ResourceNotFoundException {
// given
LimitedPartnershipSubmissionDto dto = createDto();
when(transaction.getId()).thenReturn(TRANSACTION_ID);
when(limitedPartnershipService.getLimitedPartnership(transaction, SUBMISSION_ID)).thenReturn(dto);

// when
var response = partnershipController.getPartnership(
transaction,
SUBMISSION_ID,
REQUEST_ID,
USER_ID);

// then
assertEquals(HttpStatus.OK.value(), response.getStatusCode().value());
assertEquals(dto, response.getBody());
}

@Test
void testGetPartnershipReturnsStatusNotFound() throws ResourceNotFoundException {
// given
when(transaction.getId()).thenReturn(TRANSACTION_ID);
when(limitedPartnershipService.getLimitedPartnership(transaction, SUBMISSION_ID)).thenThrow(new ResourceNotFoundException("error"));

// when
var response = partnershipController.getPartnership(
transaction,
SUBMISSION_ID,
REQUEST_ID,
USER_ID);

// then
assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatusCode().value());
assertEquals(null, response.getBody());
}

private LimitedPartnershipSubmissionDto createDto() {
return new LimitedPartnershipSubmissionDto();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.companieshouse.api.model.transaction.Resource;
import uk.gov.companieshouse.api.model.transaction.Transaction;
import uk.gov.companieshouse.limitedpartnershipsapi.exception.ResourceNotFoundException;
import uk.gov.companieshouse.limitedpartnershipsapi.exception.ServiceException;
import uk.gov.companieshouse.limitedpartnershipsapi.mapper.LimitedPartnershipMapper;
import uk.gov.companieshouse.limitedpartnershipsapi.model.DataType;
Expand All @@ -18,6 +19,7 @@
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.DataDto;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionDto;
import uk.gov.companieshouse.limitedpartnershipsapi.repository.LimitedPartnershipSubmissionsRepository;
import uk.gov.companieshouse.limitedpartnershipsapi.utils.TransactionUtils;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -27,10 +29,12 @@
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.FILING_KIND_LIMITED_PARTNERSHIP;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.URL_GET_PARTNERSHIP;

@ExtendWith(MockitoExtension.class)
class LimitedPartnershipServiceTest {
Expand All @@ -53,6 +57,9 @@ class LimitedPartnershipServiceTest {
@Mock
private TransactionService transactionService;

@Mock
private TransactionUtils transactionUtils;

@Captor
private ArgumentCaptor<Transaction> transactionApiCaptor;

Expand Down Expand Up @@ -84,7 +91,7 @@ void givenDto_whenCreateLP_thenLPCreatedWithSubmissionIdAndTransactionUpdated()
Transaction sentTransaction = transactionApiCaptor.getValue();
assertEquals(limitedPartnershipSubmissionDto.getData().getPartnershipName(), sentTransaction.getCompanyName());
assertNull(sentTransaction.getCompanyNumber());
String submissionUri = String.format("/transactions/%s/limited-partnership/%s", transaction.getId(), limitedPartnershipSubmissionDao.getId());
String submissionUri = String.format(URL_GET_PARTNERSHIP, transaction.getId(), limitedPartnershipSubmissionDao.getId());
assertEquals(submissionUri, sentTransaction.getResources().get(submissionUri).getLinks().get("resource"));
// assert dao submission self link is correct
LimitedPartnershipSubmissionDao sentSubmission = submissionCaptor.getValue();
Expand Down Expand Up @@ -142,6 +149,47 @@ void givenWrongSubmissionId_whenUpdateLP_thenServiceExceptionThrown() throws Ser
assertThrows(ServiceException.class, () -> service.updateLimitedPartnership("wrong-id", DataType.EMAIL, data));
}

@Test
void giveSubmissionId_whenGetLp_ThenLPRetrieved() throws ResourceNotFoundException {
// given
LimitedPartnershipSubmissionDto limitedPartnershipSubmissionDto = createDto();
LimitedPartnershipSubmissionDao limitedPartnershipSubmissionDao = createDao();
Transaction transaction = buildTransaction();

when(transactionUtils.isTransactionLinkedToLimitedPartnershipSubmission(eq(transaction), any(String.class))).thenReturn(true);
when(repository.findById(limitedPartnershipSubmissionDao.getId())).thenReturn(Optional.of(limitedPartnershipSubmissionDao));
when(mapper.daoToDto(limitedPartnershipSubmissionDao)).thenReturn(limitedPartnershipSubmissionDto);

// when
LimitedPartnershipSubmissionDto retrievedDto = service.getLimitedPartnership(transaction, SUBMISSION_ID);

// then
verify(repository, times(1)).findById(limitedPartnershipSubmissionDao.getId());
verify(mapper, times(1)).daoToDto(limitedPartnershipSubmissionDao);
assertEquals(limitedPartnershipSubmissionDto.getData(), retrievedDto.getData());
}

@Test
void giveInvalidSubmissionId_whenGetLp_ThenResourceNotFoundExceptionThrown() throws ResourceNotFoundException {
// given
Transaction transaction = buildTransaction();
when(transactionUtils.isTransactionLinkedToLimitedPartnershipSubmission(eq(transaction), any(String.class))).thenReturn(true);
when(repository.findById("wrong-id")).thenReturn(Optional.empty());

// when + then
assertThrows(ResourceNotFoundException.class, () -> service.getLimitedPartnership(transaction, "wrong-id"));
}

@Test
void giveSubmissionIdAndTransactionIdDoNotMatch_whenGetLp_ThenResourceNotFoundExceptionThrown() throws ResourceNotFoundException {
// given
Transaction transaction = buildTransaction();
when(transactionUtils.isTransactionLinkedToLimitedPartnershipSubmission(eq(transaction), any(String.class))).thenReturn(false);

// when + then
assertThrows(ResourceNotFoundException.class, () -> service.getLimitedPartnership(transaction, SUBMISSION_ID));
}

private Transaction buildTransaction() {
Transaction transaction = new Transaction();
transaction.setId(TRANSACTION_ID);
Expand Down
Loading

0 comments on commit af960b6

Please sign in to comment.