Skip to content

Commit

Permalink
Merge pull request #64 from companieshouse/lp-415-add-post-endpoint-f…
Browse files Browse the repository at this point in the history
…or-lp-incorpoaration

LP-415 Implement Incorporation POST endpoint
  • Loading branch information
dlloyd1-ch authored Jan 21, 2025
2 parents b951a6e + 2fb4105 commit 910df85
Show file tree
Hide file tree
Showing 8 changed files with 342 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package uk.gov.companieshouse.limitedpartnershipsapi.controller;

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_INCORPORATION;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.URL_PARAM_TRANSACTION_ID;

import java.net.URI;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
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.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.service.LimitedPartnershipIncorporationService;
import uk.gov.companieshouse.limitedpartnershipsapi.utils.ApiLogger;

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

private final LimitedPartnershipIncorporationService incorporationService;

@Autowired
public IncorporationController(LimitedPartnershipIncorporationService incorporationService) {
this.incorporationService = incorporationService;
}

@PostMapping
public ResponseEntity<Object> createIncorporation(
@RequestAttribute(TRANSACTION_KEY) Transaction transaction,
@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);
ApiLogger.infoContext(requestId, "Calling service to create a Limited Partnership Incorporation", logMap);

String submissionId = incorporationService.createIncorporation(userId, transactionId);
var location = URI.create(String.format(URL_GET_INCORPORATION, transactionId, submissionId));
var response = new LimitedPartnershipSubmissionCreatedResponseDto(submissionId);
return ResponseEntity.created(location).body(response);
}

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

import org.springframework.data.mongodb.core.mapping.Field;

public class IncorporationDataDao {

@Field("kind")
private String kind;

public String getKind() {
return kind;
}

public void setKind(String kind) {
this.kind = kind;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package uk.gov.companieshouse.limitedpartnershipsapi.model.dao;

import java.time.LocalDateTime;
import java.util.Map;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;

@Document(collection = "limited_partnership_incorporations")
public class LimitedPartnershipIncorporationDao {
@Id
private String id;

@Field("created_at")
private LocalDateTime createdAt;

@Field("created_by_user_id")
private String createdBy;

@Field("updated_at")
private LocalDateTime updatedAt;

@Field("updated_by_user_id")
private String updatedBy;

@Field("data")
private IncorporationDataDao data;

@Field("links")
private Map<String, String> links;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public LocalDateTime getCreatedAt() {
return createdAt;
}

public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}

public String getCreatedBy() {
return createdBy;
}

public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}

public LocalDateTime getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(LocalDateTime updatedAt) {
this.updatedAt = updatedAt;
}

public String getUpdatedBy() {
return updatedBy;
}

public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy;
}

public IncorporationDataDao getData() {
if (data == null) {
data = new IncorporationDataDao();
}
return data;
}

public void setData(IncorporationDataDao data) {
this.data = data;
}

public Map<String, String> getLinks() {
return links;
}

public void setLinks(Map<String, String> links) {
this.links = links;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package uk.gov.companieshouse.limitedpartnershipsapi.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dao.LimitedPartnershipIncorporationDao;

@Repository
public interface LimitedPartnershipIncorporationRepository extends MongoRepository<LimitedPartnershipIncorporationDao, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package uk.gov.companieshouse.limitedpartnershipsapi.service;

import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.LINK_SELF;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.URL_GET_INCORPORATION;

import java.time.LocalDateTime;
import java.util.Collections;
import org.springframework.stereotype.Service;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dao.LimitedPartnershipIncorporationDao;
import uk.gov.companieshouse.limitedpartnershipsapi.repository.LimitedPartnershipIncorporationRepository;

@Service
public class LimitedPartnershipIncorporationService {

public static final String LIMITED_PARTNERSHIP_REGISTRATION_KIND = "limited-partnership-registration";
private final LimitedPartnershipIncorporationRepository repository;

public LimitedPartnershipIncorporationService(
LimitedPartnershipIncorporationRepository repository) {
this.repository = repository;
}

public String createIncorporation(String userId, String transaction) {
var dao = new LimitedPartnershipIncorporationDao();
dao.getData().setKind(LIMITED_PARTNERSHIP_REGISTRATION_KIND);
// TODO set etag
dao.setCreatedAt(LocalDateTime.now());
dao.setCreatedBy(userId);
LimitedPartnershipIncorporationDao insertedIncorporation = repository.insert(dao);

String incorporationUri = getSubmissionUri(transaction, insertedIncorporation.getId());
updateIncorporationTypeWithSelfLink(dao, incorporationUri);

// TODO Update transaction with master_resource
return insertedIncorporation.getId();
}

private void updateIncorporationTypeWithSelfLink(LimitedPartnershipIncorporationDao incorporationDao,
String submissionUri) {
incorporationDao.setLinks(Collections.singletonMap(LINK_SELF, submissionUri));
repository.save(incorporationDao);
}

private String getSubmissionUri(String transactionId, String submissionId) {
return String.format(URL_GET_INCORPORATION, transactionId, submissionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ private Constants() {
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";
public static final String URL_GET_INCORPORATION = "/transactions/%s/incorporation/limited-partnership/%s";

// Filings
public static final String FILING_KIND_LIMITED_PARTNERSHIP = "limited-partnership";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package uk.gov.companieshouse.limitedpartnershipsapi.controller;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.URL_GET_INCORPORATION;

import java.util.Objects;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
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.exception.ServiceException;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionCreatedResponseDto;
import uk.gov.companieshouse.limitedpartnershipsapi.service.LimitedPartnershipIncorporationService;

@ExtendWith(MockitoExtension.class)
class IncorporationControllerTest {

@InjectMocks
IncorporationController incorporationController;

@Mock
private LimitedPartnershipIncorporationService incorporationService;

@Mock
private Transaction transaction;

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";

@Test
void testCreateIncorporationIsSuccessful() throws ServiceException {
// given
when(incorporationService.createIncorporation(
USER_ID,
TRANSACTION_ID))
.thenReturn(SUBMISSION_ID);

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

// when
var response = incorporationController.createIncorporation(
transaction,
REQUEST_ID,
USER_ID);

// then
assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value());
var responseHeaderLocation = Objects.requireNonNull(response.getHeaders().get(HttpHeaders.LOCATION)).getFirst();
assertEquals(
String.format(URL_GET_INCORPORATION, TRANSACTION_ID, SUBMISSION_ID),
responseHeaderLocation);
LimitedPartnershipSubmissionCreatedResponseDto responseBody = (LimitedPartnershipSubmissionCreatedResponseDto) response.getBody();
assert responseBody != null;
assertEquals(SUBMISSION_ID, responseBody.id());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package uk.gov.companieshouse.limitedpartnershipsapi.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
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.LINK_SELF;
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.URL_GET_INCORPORATION;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.companieshouse.limitedpartnershipsapi.model.dao.LimitedPartnershipIncorporationDao;
import uk.gov.companieshouse.limitedpartnershipsapi.repository.LimitedPartnershipIncorporationRepository;

@ExtendWith(MockitoExtension.class)
class LimitedPartnershipIncorporationServiceTest {

@InjectMocks
LimitedPartnershipIncorporationService incorporationService;

@Mock
LimitedPartnershipIncorporationRepository repository;

@Captor
private ArgumentCaptor<LimitedPartnershipIncorporationDao> incorporationCaptor;

private static final String USER_ID = "xbJf0l";
private static final String SUBMISSION_ID = "abc-123";
private static final String TRANSACTION_ID = "12321123";

@Test
void testCreateIncorporationIsSuccessful() {
// given
LimitedPartnershipIncorporationDao limitedPartnershipIncorporationDao = createLimitedPartnershipIncorporationDao();
when(repository.insert(any(LimitedPartnershipIncorporationDao.class))).thenReturn(limitedPartnershipIncorporationDao);

// when
var submissionId = incorporationService.createIncorporation(USER_ID, TRANSACTION_ID);

// then
verify(repository, times(1)).insert(incorporationCaptor.capture());
assertEquals(SUBMISSION_ID, submissionId);

// assert dao incorporation self link is correct
String submissionUri = String.format(URL_GET_INCORPORATION, TRANSACTION_ID, submissionId);
LimitedPartnershipIncorporationDao sentSubmission = incorporationCaptor.getValue();
String sentSubmissionUri = sentSubmission.getLinks().get(LINK_SELF);
assertEquals(submissionUri, sentSubmissionUri);
}

private LimitedPartnershipIncorporationDao createLimitedPartnershipIncorporationDao() {
LimitedPartnershipIncorporationDao dao = new LimitedPartnershipIncorporationDao();
dao.setId(SUBMISSION_ID);
return dao;
}
}

0 comments on commit 910df85

Please sign in to comment.