-
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 #62 from companieshouse/feature/lp-374-patch-valid…
…ation Feature/lp 374 patch validation
- Loading branch information
Showing
9 changed files
with
116 additions
and
66 deletions.
There are no files selected for viewing
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
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
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
32 changes: 19 additions & 13 deletions
32
...va/uk/gov/companieshouse/limitedpartnershipsapi/model/dto/LimitedPartnershipPatchDto.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 |
---|---|---|
@@ -1,52 +1,58 @@ | ||
package uk.gov.companieshouse.limitedpartnershipsapi.model.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import org.openapitools.jackson.nullable.JsonNullable; | ||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.Size; | ||
import uk.gov.companieshouse.limitedpartnershipsapi.model.PartnershipNameEnding; | ||
import uk.gov.companieshouse.limitedpartnershipsapi.model.PartnershipType; | ||
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.validator.NameSize; | ||
|
||
@NameSize | ||
public class LimitedPartnershipPatchDto { | ||
@JsonProperty("partnership_name") | ||
private JsonNullable<String> partnershipName; | ||
@Size(min = DataDto.NAME_MIN_SIZE, message = DataDto.NAME_MIN_SIZE_MESSAGE) | ||
@Size(max = DataDto.NAME_MAX_SIZE, message = DataDto.NAME_MAX_SIZE_MESSAGE) | ||
private String partnershipName; | ||
|
||
@JsonProperty("name_ending") | ||
private JsonNullable<PartnershipNameEnding> nameEnding; | ||
private PartnershipNameEnding nameEnding; | ||
|
||
@JsonProperty("email") | ||
private JsonNullable<String> email; | ||
private String email; | ||
|
||
@JsonProperty("partnership_type") | ||
private JsonNullable<PartnershipType> partnershipType; | ||
private PartnershipType partnershipType; | ||
|
||
public JsonNullable<String> getPartnershipName() { | ||
public String getPartnershipName() { | ||
return partnershipName; | ||
} | ||
|
||
public void setPartnershipName(JsonNullable<String> partnershipName) { | ||
public void setPartnershipName(String partnershipName) { | ||
this.partnershipName = partnershipName; | ||
} | ||
|
||
public JsonNullable<PartnershipNameEnding> getNameEnding() { | ||
public PartnershipNameEnding getNameEnding() { | ||
return nameEnding; | ||
} | ||
|
||
public void setNameEnding(JsonNullable<PartnershipNameEnding> nameEnding) { | ||
public void setNameEnding(PartnershipNameEnding nameEnding) { | ||
this.nameEnding = nameEnding; | ||
} | ||
|
||
public JsonNullable<String> getEmail() { | ||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(JsonNullable<String> email) { | ||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public JsonNullable<PartnershipType> getPartnershipType() { | ||
public PartnershipType getPartnershipType() { | ||
return partnershipType; | ||
} | ||
|
||
public void setPartnershipType(JsonNullable<PartnershipType> partnershipType) { | ||
public void setPartnershipType(PartnershipType partnershipType) { | ||
this.partnershipType = partnershipType; | ||
} | ||
} |
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
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
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package uk.gov.companieshouse.limitedpartnershipsapi.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
@@ -22,7 +23,9 @@ | |
import uk.gov.companieshouse.limitedpartnershipsapi.model.dto.LimitedPartnershipSubmissionDto; | ||
import uk.gov.companieshouse.limitedpartnershipsapi.service.LimitedPartnershipService; | ||
|
||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@ExtendWith(SpringExtension.class) | ||
|
@@ -48,7 +51,6 @@ class PartnershipControllerValidationTest { | |
@MockBean | ||
private LimitedPartnershipService service; | ||
|
||
|
||
@BeforeEach | ||
void setUp() { | ||
httpHeaders = new HttpHeaders(); | ||
|
@@ -79,6 +81,7 @@ void testCreatePartnershipShouldReturn201() throws Exception { | |
|
||
mockMvc.perform(post("/transactions/863851-951242-143528/limited-partnership/partnership") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.characterEncoding("utf-8") | ||
.headers(httpHeaders) | ||
.requestAttr("transaction", transaction) | ||
.content(body)) | ||
|
@@ -100,10 +103,67 @@ void testCreatePartnershipShouldReturnBadRequestErrorIfPartnershipNameIsLessThan | |
|
||
mockMvc.perform(post("/transactions/863851-951242-143528/limited-partnership/partnership") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.characterEncoding("utf-8") | ||
.headers(httpHeaders) | ||
.requestAttr("transaction", transaction) | ||
.content(body)) | ||
.andExpect(status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
void testUpdatePartnershipShouldReturn200() throws Exception { | ||
String body = "{\"email\":\"[email protected]\"}"; | ||
|
||
mockMvc.perform(patch("/transactions/863851-951242-143528/limited-partnership/partnership/93702824-9062-4c63-a694-716acffccdd5") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.characterEncoding("utf-8") | ||
.headers(httpHeaders) | ||
.requestAttr("transaction", transaction) | ||
.content(body)) | ||
.andDo(print()) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
void testUpdatePartnershipShouldReturnBadRequestErrorIfEmailBadlyFormated() throws Exception { | ||
String body = "{\"email\":\"test@email.\"}"; | ||
|
||
mockMvc.perform(patch("/transactions/863851-951242-143528/limited-partnership/partnership/93702824-9062-4c63-a694-716acffccdd5") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.characterEncoding("utf-8") | ||
.headers(httpHeaders) | ||
.requestAttr("transaction", transaction) | ||
.content(body)) | ||
.andDo(print()) | ||
.andExpect(status().isBadRequest()); | ||
} | ||
|
||
@Test | ||
void testUpdatePartnershipShouldReturn200IfNameSizeIsCorrect() throws Exception { | ||
String body = "{\"partnership_name\":\"Correct name size\",\"name_ending\":\"Limited Partnership\"}"; | ||
|
||
mockMvc.perform(patch("/transactions/863851-951242-143528/limited-partnership/partnership/93702824-9062-4c63-a694-716acffccdd5") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.characterEncoding("utf-8") | ||
.headers(httpHeaders) | ||
.requestAttr("transaction", transaction) | ||
.content(body)) | ||
.andDo(print()) | ||
.andExpect(status().isOk()); | ||
} | ||
|
||
@Test | ||
void testUpdatePartnershipShouldReturnBadRequestErrorIfNameSizeIsTooLong() throws Exception { | ||
String longName = StringUtils.repeat("A", 160); | ||
String body = "{\"partnership_name\":\"" + longName + "\",\"name_ending\":\"Limited Partnership\"}"; | ||
|
||
mockMvc.perform(patch("/transactions/863851-951242-143528/limited-partnership/partnership/93702824-9062-4c63-a694-716acffccdd5") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.characterEncoding("utf-8") | ||
.headers(httpHeaders) | ||
.requestAttr("transaction", transaction) | ||
.content(body)) | ||
.andDo(print()) | ||
.andExpect(status().isBadRequest()); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,7 +5,6 @@ | |
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
import org.openapitools.jackson.nullable.JsonNullable; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import uk.gov.companieshouse.limitedpartnershipsapi.model.PartnershipNameEnding; | ||
|
@@ -31,23 +30,21 @@ class LimitedPartnershipPatchMapperTest { | |
|
||
@Test | ||
void testObjectMapperCanHandleJsonNullableFields() throws JsonProcessingException { | ||
assertEquals(JsonNullable.of("some description"), mapper.readValue("{\"partnership_name\":\"some description\"}", | ||
assertEquals("some description", mapper.readValue("{\"partnership_name\":\"some description\"}", | ||
LimitedPartnershipPatchDto.class).getPartnershipName()); | ||
assertEquals(JsonNullable.of(null), mapper.readValue("{\"partnership_name\":null}", | ||
assertNull(mapper.readValue("{\"partnership_name\":null}", | ||
LimitedPartnershipPatchDto.class).getPartnershipName()); | ||
assertNull(mapper.readValue("{}", LimitedPartnershipPatchDto.class).getPartnershipName()); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource(value = { | ||
// Field NOT present in the JSON - no update: | ||
"{\"email\":\"[email protected]\"}$ Asset Strippers", | ||
"{\"email\":\"[email protected]\"}$ Asset Strippers $ [email protected]", | ||
// Field IS present in the JSON - set the new string value: | ||
"{\"partnership_name\":\"Asset Adders\", \"email\":\"[email protected]\"}$ Asset Adders", | ||
// Field IS present in the JSON with value null - set to null: | ||
"{\"partnership_name\":null, \"email\":\"[email protected]\"}$ NULL" | ||
"{\"partnership_name\":\"Asset Adders\", \"email\":\"[email protected]\"}$ Asset Adders $ [email protected]" | ||
}, delimiter = '$') | ||
void testMapStructMappingWhenEmailValueSentAndNameUnchanged(String incomingJson, String expectedPartnershipName) | ||
void testMapStructMappingWhenEmailValueSentAndNameUnchanged(String incomingJson, String expectedPartnershipName, String expectedEmail) | ||
throws JsonProcessingException { | ||
// Given | ||
LimitedPartnershipPatchDto patchDto = mapper.readValue(incomingJson, LimitedPartnershipPatchDto.class); | ||
|
@@ -58,35 +55,7 @@ void testMapStructMappingWhenEmailValueSentAndNameUnchanged(String incomingJson, | |
patchMapper.update(patchDto, mongoDto); | ||
|
||
// Then | ||
checkExpectedFieldValues(mongoDto, expectedPartnershipName.equals("NULL") ? null : expectedPartnershipName); | ||
} | ||
|
||
@Test | ||
void testMapStructMappingWhenEmailValueSentAndNameSetToUndefined() { | ||
|
||
// Field IS present in the JSON with value undefined - no update: | ||
|
||
// Given | ||
|
||
/* Need to create the patch DTO a bit differently, as this doesn't work:: | ||
String incomingJson = "{\"partnership_name\":undefined, \"email\":\"[email protected]\"}"; | ||
PatchDto patchDto = mapper.readValue(incomingJson, PatchDto.class); | ||
Error - "JsonParseException: Unrecognized token 'undefined'") | ||
*/ | ||
|
||
LimitedPartnershipPatchDto patchDto = new LimitedPartnershipPatchDto(); | ||
patchDto.setPartnershipName(JsonNullable.undefined()); | ||
patchDto.setEmail(JsonNullable.of("[email protected]")); | ||
|
||
DataDto mongoDto = createMongoDto(); | ||
|
||
// When | ||
patchMapper.update(patchDto, mongoDto); | ||
|
||
// Then | ||
checkExpectedFieldValues(mongoDto, "Asset Strippers"); | ||
checkExpectedFieldValues(mongoDto, expectedPartnershipName, expectedEmail); | ||
} | ||
|
||
private DataDto createMongoDto() { | ||
|
@@ -98,10 +67,10 @@ private DataDto createMongoDto() { | |
return mongoDto; | ||
} | ||
|
||
private void checkExpectedFieldValues(DataDto mongoDto, String expectedPartnershipName) { | ||
private void checkExpectedFieldValues(DataDto mongoDto, String expectedPartnershipName, String expectedEmail) { | ||
assertEquals(expectedPartnershipName, mongoDto.getPartnershipName()); | ||
assertEquals(PartnershipNameEnding.L_DOT_P_DOT.getDescription(), mongoDto.getNameEnding()); | ||
assertEquals(PartnershipType.PFLP, mongoDto.getPartnershipType()); | ||
assertEquals("[email protected]", mongoDto.getEmail()); | ||
assertEquals(expectedEmail, mongoDto.getEmail()); | ||
} | ||
} |
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