Skip to content

Commit

Permalink
SLVUU: Add tests for invalid JSON for definition in application layou…
Browse files Browse the repository at this point in the history
…t request
  • Loading branch information
cfisher-scottlogic committed Nov 23, 2023
1 parent 2cd28fd commit af3e6ae
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.iterableWithSize;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -140,6 +145,37 @@ public void persistApplicationLayout_noUserInHeader_returns400() throws Exceptio
assertThat(actualError).isEqualTo(MISSING_USERNAME_ERROR_MESSAGE);
}

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

mockMvc.perform(put(BASE_URL).header("username", user)
.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(repository.findAll()).isEmpty();
}

@Test
public void deleteApplicationLayout_noLayoutExists_returns404() throws Exception {
String user = "user";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,6 @@ void createLayout_invalidRequestBodyDefinitionIsNotValidJSON_returns400AndDoesNo
assertThat(metadataRepository.findAll()).isEmpty();
}


@Test
void createLayout_invalidRequestBodyMetadataIsNull_returns400AndDoesNotCreateLayout()
throws Exception {
Expand Down

0 comments on commit af3e6ae

Please sign in to comment.