Skip to content

Commit fed3c0a

Browse files
authored
Merge pull request #34 from companieshouse/lp-206-create-post-endpoint-2
LP-206 create the partnership POST endpoint
2 parents a152eba + dd6752f commit fed3c0a

File tree

8 files changed

+150
-57
lines changed

8 files changed

+150
-57
lines changed

pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>uk.gov.companieshouse</groupId>
99
<artifactId>companies-house-parent</artifactId>
10-
<version>2.1.6</version> <!-- lookup parent from repository -->
10+
<version>2.1.6</version>
1111
</parent>
1212

1313
<groupId>groupId</groupId>
@@ -19,6 +19,7 @@
1919
<maven.compiler.source>${java.version}</maven.compiler.source>
2020
<maven.compiler.target>${java.version}</maven.compiler.target>
2121
<maven-compiler-plugin.version>3.13.0</maven-compiler-plugin.version>
22+
<maven-surefire-plugin.version>3.5.0</maven-surefire-plugin.version>
2223
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2324
<spring-boot-dependencies.version>3.3.4</spring-boot-dependencies.version>
2425
<spring-boot-maven-plugin.version>3.3.4</spring-boot-maven-plugin.version>
@@ -161,6 +162,15 @@
161162
</to>
162163
</configuration>
163164
</plugin>
165+
<plugin>
166+
<groupId>org.apache.maven.plugins</groupId>
167+
<artifactId>maven-surefire-plugin</artifactId>
168+
<version>${maven-surefire-plugin.version}</version>
169+
</plugin>
170+
<plugin>
171+
<groupId>org.jacoco</groupId>
172+
<artifactId>jacoco-maven-plugin</artifactId>
173+
</plugin>
164174
</plugins>
165175
</build>
166176
</project>

spec/schema.json

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
{
2-
"openapi": "3.0.1",
2+
"swagger": "2.0",
33
"info": {
44
"title": "Companies House - Limited Partnerships API",
55
"description": "An API to allow the registration, transition and update of Limited Partnerships",
66
"version": "1.0.0"
77
},
8-
"servers": [
9-
{
10-
"url": "https://api.companieshouse.gov.uk/"
11-
}
8+
"host": "api.companieshouse.gov.uk",
9+
"schemes": [
10+
"https"
1211
],
1312
"tags": [
1413
{
@@ -19,28 +18,30 @@
1918
"/transactions/{transaction_id}/limited-partnership/partnership": {
2019
"post": {
2120
"tags": [
22-
"Limited Partnership"
21+
"Limited Partnerships"
2322
],
2423
"summary": "Create a Limited Partnership submission",
2524
"parameters": [
2625
{
2726
"name": "transaction_id",
2827
"in": "path",
2928
"required": true,
29+
"type": "string"
30+
},
31+
{
32+
"name": "body",
33+
"in": "body",
34+
"required": true,
3035
"schema": {
31-
"type": "string"
36+
"$ref": "#/definitions/LimitedPartnershipSubmission"
3237
}
3338
}
3439
],
3540
"responses": {
3641
"201": {
3742
"description": "Limited Partnership Submission has been successfully created.",
38-
"content": {
39-
"application/json": {
40-
"schema": {
41-
"$ref": "#/components/schemas/LimitedPartnershipSubmissionCreatedResponse"
42-
}
43-
}
43+
"schema": {
44+
"$ref": "#/definitions/LimitedPartnershipSubmissionCreatedResponse"
4445
}
4546
},
4647
"400": {
@@ -59,31 +60,49 @@
5960
"get": {
6061
"summary": "Health check URL returns 200 if service is running",
6162
"tags": [
62-
"Limited Partnership"
63+
"Limited Partnerships"
6364
],
6465
"responses": {
6566
"200": {
6667
"description": "Request has been received",
67-
"content": {
68-
"application/json": {
69-
"schema": {
70-
"type": "object"
71-
}
72-
}
68+
"schema": {
69+
"type": "object"
7370
}
7471
}
7572
}
7673
}
7774
}
7875
},
79-
"components": {
80-
"schemas": {
81-
"LimitedPartnershipSubmissionCreatedResponse": {
82-
"type": "object",
83-
"properties": {
84-
"id": {
85-
"type": "string"
86-
}
76+
"definitions": {
77+
"LimitedPartnershipSubmission": {
78+
"type": "object",
79+
"required": [
80+
"data"
81+
],
82+
83+
"properties": {
84+
"data": {
85+
"$ref": "#/definitions/Data"
86+
}
87+
}
88+
},
89+
"Data": {
90+
"type": "object",
91+
"properties": {
92+
"partnership_name": {
93+
"type": "string"
94+
},
95+
"name_ending": {
96+
"type": "string",
97+
"enum": ["Limited Partnership", "LP", "L.P.", "Partneriaeth Cyfyngedig", "PC", "P.C."]
98+
}
99+
}
100+
},
101+
"LimitedPartnershipSubmissionCreatedResponse": {
102+
"type": "object",
103+
"properties": {
104+
"id": {
105+
"type": "string"
87106
}
88107
}
89108
}
Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,60 @@
11
package uk.gov.companieshouse.limitedpartnershipsapi.controller;
22

3-
import jakarta.servlet.http.HttpServletRequest;
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.http.HttpStatus;
45
import org.springframework.http.ResponseEntity;
56
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.RequestBody;
8+
import org.springframework.web.bind.annotation.RequestHeader;
69
import org.springframework.web.bind.annotation.RequestMapping;
710
import org.springframework.web.bind.annotation.RestController;
811
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionCreatedResponseDto;
12+
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionDto;
13+
import uk.gov.companieshouse.limitedpartnershipsapi.service.LimitedPartnershipService;
914
import uk.gov.companieshouse.limitedpartnershipsapi.utils.ApiLogger;
1015

1116
import java.net.URI;
17+
import java.util.HashMap;
1218

19+
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.ERIC_IDENTITY;
20+
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.ERIC_REQUEST_ID_KEY;
1321
import static uk.gov.companieshouse.limitedpartnershipsapi.utils.Constants.URL_PARAM_TRANSACTION_ID;
1422

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

27+
static final String URL_GET_PARTNERSHIP = "/transactions/%s/limited-partnership/partnership/%s";
28+
29+
private final LimitedPartnershipService limitedPartnershipService;
30+
31+
@Autowired
32+
public PartnershipController(LimitedPartnershipService limitedPartnershipService) {
33+
this.limitedPartnershipService = limitedPartnershipService;
34+
}
35+
1936
@PostMapping
2037
public ResponseEntity<Object> createPartnership(
21-
HttpServletRequest request) {
38+
@RequestBody LimitedPartnershipSubmissionDto limitedPartnershipSubmissionDto,
39+
@RequestHeader(value = ERIC_REQUEST_ID_KEY) String requestId,
40+
@RequestHeader(value = ERIC_IDENTITY) String userId) {
41+
42+
var transactionId = 12321123;
43+
var logMap = new HashMap<String, Object>();
44+
logMap.put(URL_PARAM_TRANSACTION_ID, transactionId);
45+
46+
try {
47+
ApiLogger.infoContext(requestId, "Calling service to create Partnership Submission", logMap);
48+
49+
var submissionId = limitedPartnershipService.createLimitedPartnership(limitedPartnershipSubmissionDto, requestId, userId);
2250

23-
ApiLogger.debug("createPartnership");
51+
var location = URI.create(String.format(URL_GET_PARTNERSHIP, transactionId, submissionId));
52+
var response = new LimitedPartnershipSubmissionCreatedResponseDto(submissionId);
2453

25-
URI location = URI.create("/transactions/12321123/limited-partnership/partnership/3235233232");
26-
LimitedPartnershipSubmissionCreatedResponseDto response = new LimitedPartnershipSubmissionCreatedResponseDto();
27-
response.setId("3235233232");
28-
return ResponseEntity.created(location).body(response);
54+
return ResponseEntity.created(location).body(response);
55+
} catch (Exception e) {
56+
ApiLogger.errorContext(requestId, "Error Creating Limited Partnership Submission", e, logMap);
57+
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
58+
}
2959
}
3060
}

src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/mapper/LimitedPartnershipMapper.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import org.mapstruct.factory.Mappers;
55
import org.springframework.stereotype.Component;
66
import uk.gov.companieshouse.limitedpartnershipsapi.model.PartnershipNameEnding;
7-
import uk.gov.companieshouse.limitedpartnershipsapi.model.dao.DataDao;
87
import uk.gov.companieshouse.limitedpartnershipsapi.model.dao.LimitedPartnershipSubmissionDao;
9-
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.DataDto;
108
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionDto;
119

1210
@Component

src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/model/PartnershipNameEnding.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public enum PartnershipNameEnding {
1313

1414
PartnershipNameEnding(String description) {
1515
this.description = description;
16-
};
16+
}
1717

1818
public String getDescription() {
1919
return description;

src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/model/dto/LimitedPartnershipSubmissionCreatedResponseDto.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,4 @@
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
44

5-
public class LimitedPartnershipSubmissionCreatedResponseDto {
6-
7-
@JsonProperty("id")
8-
private String id;
9-
10-
public String getId() {
11-
return id;
12-
}
13-
14-
public void setId(String id) {
15-
this.id = id;
16-
}
17-
}
5+
public record LimitedPartnershipSubmissionCreatedResponseDto(@JsonProperty("id") String id) {}

src/main/java/uk/gov/companieshouse/limitedpartnershipsapi/utils/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ private Constants() { }
66

77
// Request header names
88
public static final String ERIC_REQUEST_ID_KEY = "X-Request-Id";
9+
public static final String ERIC_IDENTITY = "ERIC-identity";
910

1011
// URL path parameters
1112
public static final String URL_PARAM_TRANSACTION_ID = "transaction_id";
Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,84 @@
11
package uk.gov.companieshouse.limitedpartnershipsapi.controller;
22

3+
import org.junit.jupiter.api.BeforeEach;
34
import org.junit.jupiter.api.Test;
45
import org.junit.jupiter.api.extension.ExtendWith;
56
import org.mockito.InjectMocks;
67
import org.mockito.Mock;
78
import org.mockito.junit.jupiter.MockitoExtension;
89
import org.springframework.http.HttpHeaders;
910
import org.springframework.http.HttpStatus;
10-
import org.springframework.mock.web.MockHttpServletRequest;
11+
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.DataDto;
1112
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionCreatedResponseDto;
13+
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionDto;
14+
import uk.gov.companieshouse.limitedpartnershipsapi.service.LimitedPartnershipService;
1215

1316
import java.util.Objects;
1417

1518
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.mockito.ArgumentMatchers.any;
20+
import static org.mockito.ArgumentMatchers.eq;
21+
import static org.mockito.Mockito.when;
22+
import static uk.gov.companieshouse.limitedpartnershipsapi.controller.PartnershipController.URL_GET_PARTNERSHIP;
1623

1724
@ExtendWith(MockitoExtension.class)
1825
class PartnershipControllerTest {
1926

27+
private static final String REQUEST_ID = "5346336";
28+
private static final String USER_ID = "rjg736k791";
29+
private static final String SUBMISSION_ID = "ABC123ABC456";
30+
2031
@InjectMocks
2132
private PartnershipController partnershipController;
2233

2334
@Mock
24-
private MockHttpServletRequest mockHttpServletRequest;
35+
private LimitedPartnershipService limitedPartnershipService;
36+
37+
private LimitedPartnershipSubmissionDto limitedPartnershipSubmissionDto;
38+
39+
@BeforeEach
40+
void init() {
41+
DataDto data = new DataDto();
42+
limitedPartnershipSubmissionDto = new LimitedPartnershipSubmissionDto();
43+
limitedPartnershipSubmissionDto.setData(data);
44+
}
2545

2646
@Test
2747
void testCreatePartnership() {
28-
var response = partnershipController.createPartnership(mockHttpServletRequest);
48+
when(limitedPartnershipService.createLimitedPartnership(
49+
any(LimitedPartnershipSubmissionDto.class),
50+
eq(REQUEST_ID),
51+
eq(USER_ID)))
52+
.thenReturn(SUBMISSION_ID);
53+
54+
var response = partnershipController.createPartnership(
55+
limitedPartnershipSubmissionDto,
56+
REQUEST_ID,
57+
USER_ID);
2958

3059
assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value());
3160
var responseHeaderLocation = Objects.requireNonNull(response.getHeaders().get(HttpHeaders.LOCATION)).getFirst();
32-
assertEquals("/transactions/12321123/limited-partnership/partnership/3235233232", responseHeaderLocation);
61+
assertEquals(
62+
String.format(URL_GET_PARTNERSHIP, 12321123, SUBMISSION_ID),
63+
responseHeaderLocation);
3364
LimitedPartnershipSubmissionCreatedResponseDto responseBody = (LimitedPartnershipSubmissionCreatedResponseDto) response.getBody();
3465
assert responseBody != null;
35-
assertEquals("3235233232", responseBody.getId());
66+
assertEquals(SUBMISSION_ID, responseBody.id());
67+
}
68+
69+
@Test
70+
void testCreatePartnershipInternalServerError() {
71+
when(limitedPartnershipService.createLimitedPartnership(
72+
any(LimitedPartnershipSubmissionDto.class),
73+
eq(REQUEST_ID),
74+
eq(USER_ID)))
75+
.thenThrow(new RuntimeException());
76+
77+
var response = partnershipController.createPartnership(
78+
limitedPartnershipSubmissionDto,
79+
REQUEST_ID,
80+
USER_ID);
81+
82+
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), response.getStatusCode().value());
3683
}
3784
}

0 commit comments

Comments
 (0)