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

VUU-333: Add Spring Boot integration tests for valid JSON in 'definition' field of layout requests #1028

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -6,6 +6,7 @@
import org.finos.vuu.layoutserver.model.ApplicationLayout;
import org.finos.vuu.layoutserver.repository.ApplicationLayoutRepository;
import org.finos.vuu.layoutserver.utils.DefaultApplicationLayoutLoader;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand All @@ -19,10 +20,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 All @@ -43,6 +49,11 @@ public class ApplicationLayoutIntegrationTest {
private DefaultApplicationLayoutLoader mockLoader;
private final DefaultApplicationLayoutLoader realLoader = new DefaultApplicationLayoutLoader();

@BeforeEach
public void setUp() {
repository.deleteAll();
}

@Test
public void getApplicationLayout_noLayoutExists_returns200WithDefaultLayout() throws Exception {
when(mockLoader.getDefaultLayout()).thenReturn(realLoader.getDefaultLayout());
Expand Down Expand Up @@ -140,6 +151,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