Skip to content

Commit

Permalink
DCB-162: Add validation for application id and name fields (#3)
Browse files Browse the repository at this point in the history
Add validation to ensure that:
- application id and name fields contain a non-empty string when
adding a new application.
- application name field contains a non-empty string when updating
an existing application.

(cherry picked from commit 67fd87f8320808818205bedc8238e697ee9f3360)
  • Loading branch information
samuel-walsh-red authored and lukasz-andrzejak committed Oct 2, 2023
1 parent 0b9947d commit dd6c917
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,7 @@ components:
example: https://libertyglobal.com/s/apps/com.libertyglobal.app.awesome/1.2.3/image/1920x1080/icon.png
name:
type: string
pattern: '^(?!\s*$).+' # validate for non-empty string
description: Application name
example: Awesome Application
description:
Expand Down Expand Up @@ -707,6 +708,7 @@ components:
properties:
id:
type: string
pattern: '^(?!\s*$).+' # validate for non-empty string
description: Reverse Domain Notation ID
example: com.libertyglobal.app.awesome
version:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,80 @@ void cannotAddApplicationWithBlankOciImageUrl(String ociImageUrl) throws Excepti
assertThat(response.getContentAsString()).isEqualTo("{\"message\":\"[header.ociImageUrl: must match \\\"^(?!\\\\s*$).+\\\"]\"}");
}

@Test
void cannotAddApplicationWithoutApplicationName() throws Exception {
// given
String aNull = CORRECT_APPLICATION_AS_JSON.replace("\"Awesome Application\"", "null");
// when
MockHttpServletResponse response = mvc
.perform(
post("/maintainers/{maintainerCode}/apps", MAINTAINER_CODE)
.content(aNull)
.contentType(MediaType.APPLICATION_JSON))
.andReturn()
.getResponse();

// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
assertThat(response.getContentAsString()).isEqualTo("{\"message\":\"[header.name: must not be null]\"}");
}

@ParameterizedTest
@ValueSource(strings = {"", " ", " "})
void cannotAddApplicationWithBlankApplicationName(String applicationName) throws Exception {
// given
String aNull = CORRECT_APPLICATION_AS_JSON.replace("\"Awesome Application\"", "\"" + applicationName + "\"");
// when
MockHttpServletResponse response = mvc
.perform(
post("/maintainers/{maintainerCode}/apps", MAINTAINER_CODE)
.content(aNull)
.contentType(MediaType.APPLICATION_JSON))
.andReturn()
.getResponse();

// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
assertThat(response.getContentAsString()).isEqualTo("{\"message\":\"[header.name: must match \\\"^(?!\\\\s*$).+\\\"]\"}");
}

@Test
void cannotAddApplicationWithoutApplicationId() throws Exception {
// given
String aNull = CORRECT_APPLICATION_AS_JSON.replaceFirst("\"com.libertyglobal.app.awesome\"", "null");
// when
MockHttpServletResponse response = mvc
.perform(
post("/maintainers/{maintainerCode}/apps", MAINTAINER_CODE)
.content(aNull)
.contentType(MediaType.APPLICATION_JSON))
.andReturn()
.getResponse();

// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
assertThat(response.getContentAsString()).isEqualTo("{\"message\":\"[header.id: must not be null]\"}");
}

@ParameterizedTest
@ValueSource(strings = {"", " ", " "})
void cannotAddApplicationWithBlankApplicationId(String applicationName) throws Exception {
// given
String aNull = CORRECT_APPLICATION_AS_JSON.replaceFirst("\"com.libertyglobal.app.awesome\"", "\"" + applicationName + "\"");
// when
MockHttpServletResponse response = mvc
.perform(
post("/maintainers/{maintainerCode}/apps", MAINTAINER_CODE)
.content(aNull)
.contentType(MediaType.APPLICATION_JSON))
.andReturn()
.getResponse();

// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value());
assertThat(response.getContentAsString()).isEqualTo("{\"message\":\"[header.id: must match \\\"^(?!\\\\s*$).+\\\"]\"}");
}

@Test
void cannotGetApplicationDetailsWithoutPlatformName() throws Exception {
// given
Expand Down

0 comments on commit dd6c917

Please sign in to comment.