-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from companieshouse/lp-415-add-post-endpoint-f…
…or-lp-incorpoaration LP-415 Implement Incorporation POST endpoint
- Loading branch information
Showing
8 changed files
with
342 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...java/uk/gov/companieshouse/limitedpartnershipsapi/controller/IncorporationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
...ain/java/uk/gov/companieshouse/limitedpartnershipsapi/model/dao/IncorporationDataDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...v/companieshouse/limitedpartnershipsapi/model/dao/LimitedPartnershipIncorporationDao.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ieshouse/limitedpartnershipsapi/repository/LimitedPartnershipIncorporationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
47 changes: 47 additions & 0 deletions
47
...companieshouse/limitedpartnershipsapi/service/LimitedPartnershipIncorporationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
.../uk/gov/companieshouse/limitedpartnershipsapi/controller/IncorporationControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...anieshouse/limitedpartnershipsapi/service/LimitedPartnershipIncorporationServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |