diff --git a/layout-server/src/test/java/org/finos/vuu/layoutserver/integration/ApplicationLayoutIntegrationTest.java b/layout-server/src/test/java/org/finos/vuu/layoutserver/integration/ApplicationLayoutIntegrationTest.java index 0f862b852..c65768159 100644 --- a/layout-server/src/test/java/org/finos/vuu/layoutserver/integration/ApplicationLayoutIntegrationTest.java +++ b/layout-server/src/test/java/org/finos/vuu/layoutserver/integration/ApplicationLayoutIntegrationTest.java @@ -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; @@ -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; @@ -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()); @@ -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"; diff --git a/layout-server/src/test/java/org/finos/vuu/layoutserver/integration/LayoutIntegrationTest.java b/layout-server/src/test/java/org/finos/vuu/layoutserver/integration/LayoutIntegrationTest.java index 94eedc8e3..9c4a8e552 100644 --- a/layout-server/src/test/java/org/finos/vuu/layoutserver/integration/LayoutIntegrationTest.java +++ b/layout-server/src/test/java/org/finos/vuu/layoutserver/integration/LayoutIntegrationTest.java @@ -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 { @@ -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 {