From 2cd28fd0c320587c4592de88294d9c01cb780e3e Mon Sep 17 00:00:00 2001 From: Cara Fisher Date: Tue, 21 Nov 2023 12:48:07 +0000 Subject: [PATCH 1/3] SLVUU: Add tests for invalid JSON for definition in layout request --- .../integration/LayoutIntegrationTest.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) 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..93d4c5c01 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,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 { @@ -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 { From af3e6aeb4efcd189093181bfd95459ddfae7b232 Mon Sep 17 00:00:00 2001 From: Cara Fisher Date: Thu, 23 Nov 2023 13:59:26 +0000 Subject: [PATCH 2/3] SLVUU: Add tests for invalid JSON for definition in application layout request --- .../ApplicationLayoutIntegrationTest.java | 40 ++++++++++++++++++- .../integration/LayoutIntegrationTest.java | 1 - 2 files changed, 38 insertions(+), 3 deletions(-) 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..154b407bd 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 @@ -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; @@ -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"; 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 93d4c5c01..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 @@ -257,7 +257,6 @@ void createLayout_invalidRequestBodyDefinitionIsNotValidJSON_returns400AndDoesNo assertThat(metadataRepository.findAll()).isEmpty(); } - @Test void createLayout_invalidRequestBodyMetadataIsNull_returns400AndDoesNotCreateLayout() throws Exception { From 9ed74e25ea9740bb5e89d25db25d91780050ea49 Mon Sep 17 00:00:00 2001 From: Cara Fisher Date: Mon, 27 Nov 2023 10:56:00 +0000 Subject: [PATCH 3/3] SLVUU: Add database clearing before each ApplicationLayoutIT --- .../integration/ApplicationLayoutIntegrationTest.java | 6 ++++++ 1 file changed, 6 insertions(+) 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 154b407bd..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; @@ -48,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());