Skip to content

Commit

Permalink
SLVUU: Add tests for invalid JSON for definition in layout request
Browse files Browse the repository at this point in the history
  • Loading branch information
cfisher-scottlogic committed Nov 23, 2023
1 parent 0badbdc commit 2cd28fd
Showing 1 changed file with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,37 @@ void createLayout_invalidRequestBodyDefinitionsIsNull_returns400AndDoesNotCreate
assertThat(metadataRepository.findAll()).isEmpty();
}

@Test
void createLayout_invalidRequestBodyDefinitionIsNotValidJSON_returns400AndDoesNotCreateLayout()
throws Exception {
String layoutRequestString =
"{\n"
+ " \"definition\": invalidJson,\n"
+ " \"metadata\": {\n"
+ " \"name\": \"string\",\n"
+ " \"group\": \"string\",\n"
+ " \"screenshot\": \"string\",\n"
+ " \"user\": \"string\"\n"
+ " }\n"
+ "}";

mockMvc.perform(
post("/layouts").content(layoutRequestString).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.messages", iterableWithSize(1)))
.andExpect(jsonPath("$.messages", contains(
"JSON parse error: Unrecognized token 'invalidJson': was expecting (JSON String, "
+ "Number, Array, Object or token 'null', 'true' or 'false'); nested "
+ "exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized "
+ "token 'invalidJson': was expecting (JSON String, Number, Array, Object or "
+ "token 'null', 'true' or 'false')\n at [Source: (org.springframework.util"
+ ".StreamUtils$NonClosingInputStream); line: 2, column: 29]")));

assertThat(layoutRepository.findAll()).isEmpty();
assertThat(metadataRepository.findAll()).isEmpty();
}


@Test
void createLayout_invalidRequestBodyMetadataIsNull_returns400AndDoesNotCreateLayout()
throws Exception {
Expand Down Expand Up @@ -318,6 +349,36 @@ void updateLayout_invalidRequestBodyDefinitionIsNull_returns400AndLayoutDoesNotC
assertThat(layoutRepository.findById(layout.getId()).orElseThrow()).isEqualTo(layout);
}

@Test
void updateLayout_invalidRequestBodyDefinitionIsNotValidJSON_returns400AndDoesNotUpdateLayout()
throws Exception {
String layoutRequestString =
"{\n"
+ " \"definition\": invalidJson,\n"
+ " \"metadata\": {\n"
+ " \"name\": \"string\",\n"
+ " \"group\": \"string\",\n"
+ " \"screenshot\": \"string\",\n"
+ " \"user\": \"string\"\n"
+ " }\n"
+ "}";

mockMvc.perform(put("/layouts/{id}", DEFAULT_LAYOUT_ID).content(layoutRequestString)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.messages", iterableWithSize(1)))
.andExpect(jsonPath("$.messages", contains(
"JSON parse error: Unrecognized token 'invalidJson': was expecting (JSON String, "
+ "Number, Array, Object or token 'null', 'true' or 'false'); nested "
+ "exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized "
+ "token 'invalidJson': was expecting (JSON String, Number, Array, Object or "
+ "token 'null', 'true' or 'false')\n at [Source: (org.springframework.util"
+ ".StreamUtils$NonClosingInputStream); line: 2, column: 29]")));

assertThat(layoutRepository.findAll()).isEmpty();
assertThat(metadataRepository.findAll()).isEmpty();
}

@Test
void updateLayout_invalidRequestBodyMetadataIsNull_returns400AndLayoutDoesNotChange()
throws Exception {
Expand Down

0 comments on commit 2cd28fd

Please sign in to comment.