Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for invalid JSON for definition in request #119

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -227,6 +227,36 @@ 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 +348,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
Loading