From d8fe0db329c8859ad45ea8208a8db9f7250397b2 Mon Sep 17 00:00:00 2001 From: grabdoc Date: Sat, 13 Jan 2024 01:06:19 -0600 Subject: [PATCH] #135 - mysql bulk insert, minor other fixes --- .../db2rest/MySQLBaseIntegrationTest.java | 31 +---- .../PostgreSQLBaseIntegrationTest.java | 29 ----- .../rest/MySQLBulkCreateControllerTest.java | 115 ++++++++++++++++++ .../rest/MySQLCreateControllerTest.java | 4 +- .../db2rest/rest/MySQLReadControllerTest.java | 6 +- .../db2rest/rest/PgCreateControllerTest.java | 30 +++++ 6 files changed, 152 insertions(+), 63 deletions(-) create mode 100644 src/test/java/com/homihq/db2rest/rest/MySQLBulkCreateControllerTest.java diff --git a/src/test/java/com/homihq/db2rest/MySQLBaseIntegrationTest.java b/src/test/java/com/homihq/db2rest/MySQLBaseIntegrationTest.java index cebac1a8..e7457d51 100644 --- a/src/test/java/com/homihq/db2rest/MySQLBaseIntegrationTest.java +++ b/src/test/java/com/homihq/db2rest/MySQLBaseIntegrationTest.java @@ -1,35 +1,8 @@ package com.homihq.db2rest; -import org.springframework.test.context.jdbc.Sql; +import org.springframework.context.annotation.Import; -import static org.assertj.core.api.Assertions.assertThat; - -@Sql(scripts = {"classpath:mysql/mysql-sakila.sql", "classpath:mysql/mysql-sakila-insert-data.sql"}) +@Import(MySQLContainerConfiguration.class) public class MySQLBaseIntegrationTest extends BaseIntegrationTest{ - /* - @ServiceConnection - private static final MySQLContainer mySQLContainer = new MySQLContainer("mysql:8.2"); - - static { - mySQLContainer - .withDatabaseName("sakila") - .withUsername("mysql") - .withPassword("mysql") - .withUrlParam("serverTimezone", "UTC"); - - - } - - - @BeforeAll - static void beforeAll() { - mySQLContainer.start(); - } - - @AfterAll - static void afterAll() { - mySQLContainer.stop(); - } - */ } diff --git a/src/test/java/com/homihq/db2rest/PostgreSQLBaseIntegrationTest.java b/src/test/java/com/homihq/db2rest/PostgreSQLBaseIntegrationTest.java index 72417142..e1d84932 100644 --- a/src/test/java/com/homihq/db2rest/PostgreSQLBaseIntegrationTest.java +++ b/src/test/java/com/homihq/db2rest/PostgreSQLBaseIntegrationTest.java @@ -1,37 +1,8 @@ package com.homihq.db2rest; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.springframework.boot.testcontainers.service.connection.ServiceConnection; import org.springframework.context.annotation.Import; -import org.springframework.test.context.jdbc.Sql; -import org.testcontainers.containers.MySQLContainer; -import org.testcontainers.containers.PostgreSQLContainer; -//@Sql(scripts = {"classpath:pg/postgres-sakila-schema.sql", "classpath:pg/postgres-sakila-insert-data.sql"}) @Import(PostgreSQLContainerConfiguration.class) public class PostgreSQLBaseIntegrationTest extends BaseIntegrationTest{ - /* - @ServiceConnection - private static final PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:15-alpine"); - - static { - postgreSQLContainer.withUsername("postgres") - .withPassword("postgres") - .withDatabaseName("postgres").withReuse(true); - } - - - @BeforeAll - static void beforeAll() { - postgreSQLContainer.start(); - } - - @AfterAll - static void afterAll() { - postgreSQLContainer.stop(); - } - - */ } diff --git a/src/test/java/com/homihq/db2rest/rest/MySQLBulkCreateControllerTest.java b/src/test/java/com/homihq/db2rest/rest/MySQLBulkCreateControllerTest.java new file mode 100644 index 00000000..9033fed6 --- /dev/null +++ b/src/test/java/com/homihq/db2rest/rest/MySQLBulkCreateControllerTest.java @@ -0,0 +1,115 @@ +package com.homihq.db2rest.rest; + +import com.homihq.db2rest.MySQLBaseIntegrationTest; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.http.MediaType; + +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +class MySQLBulkCreateControllerTest extends MySQLBaseIntegrationTest { + + @Test + @DisplayName("Create many films.") + void create() throws Exception { + + var json = """ + [ + { + "title" : "Dunki", + "description" : "Film about illegal immigration" , + "release_year" : 2023, + "language_id" : 6, + "original_language_id" : 6, + "rental_duration" : 6, + "rental_rate" : 0.99 , + "length" : 150, + "replacement_cost" : 20.99 , + "rating" : "PG-13" , + "special_features" : "Commentaries" + + }, + + { + "title" : "Jawan", + "description" : "Socio-econmic problems and corruption" , + "release_year" : 2023, + "language_id" : 6, + "original_language_id" : 6, + "rental_duration" : 5, + "rental_rate" : 0.99 , + "length" : 160, + "replacement_cost" : 20.99 , + "rating" : "PG-13" , + "special_features" : "Commentaries" + + } + ] + """; + + + mockMvc.perform(post("/film/bulk").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8") + .header("Content-Profile", "public") + .content(json).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isCreated()) + //.andExpect(jsonPath("$.rows", Matchers.equalTo(1))) + .andDo(print()) + .andDo(document("pg-bulk-create-films")); + + } + + + @Test + @DisplayName("Create many films with failure.") + void createError() throws Exception { + + var json = """ + [ + { + "title" : "Dunki", + "description" : "Film about illegal immigration" , + "release_year" : 2023, + "language_id" : 6, + "original_language_id" : 6, + "rental_duration" : 6, + "rental_rate" : 0.99 , + "length" : 150, + "replacement_cost" : 20.99 , + "rating" : "PG-13" , + "special_features" : "Commentaries" + "country" : "INDIA" + + }, + + { + "title" : "Jawan", + "description" : "Socio-econmic problems and corruption" , + "release_year" : 2023, + "language_id" : 6, + "original_language_id" : 6, + "rental_duration" : 5, + "rental_rate" : 0.99 , + "length" : 160, + "replacement_cost" : 20.99 , + "rating" : "PG-13" , + "special_features" : "Commentaries" + + } + ] + """; + + + mockMvc.perform(post("/film/bulk").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8") + .header("Content-Profile", "public") + .content(json).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()) + .andDo(print()) + .andDo(document("pg-bulk-create-films-error")); + + } + + +} diff --git a/src/test/java/com/homihq/db2rest/rest/MySQLCreateControllerTest.java b/src/test/java/com/homihq/db2rest/rest/MySQLCreateControllerTest.java index c21bd69a..f6e2853a 100644 --- a/src/test/java/com/homihq/db2rest/rest/MySQLCreateControllerTest.java +++ b/src/test/java/com/homihq/db2rest/rest/MySQLCreateControllerTest.java @@ -1,6 +1,6 @@ package com.homihq.db2rest.rest; -import com.homihq.db2rest.PostgreSQLBaseIntegrationTest; +import com.homihq.db2rest.MySQLBaseIntegrationTest; import org.hamcrest.Matchers; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -12,7 +12,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -class MySQLCreateControllerTest extends PostgreSQLBaseIntegrationTest { +class MySQLCreateControllerTest extends MySQLBaseIntegrationTest { @Test @DisplayName("Create a film.") diff --git a/src/test/java/com/homihq/db2rest/rest/MySQLReadControllerTest.java b/src/test/java/com/homihq/db2rest/rest/MySQLReadControllerTest.java index 789901fd..240bcac1 100644 --- a/src/test/java/com/homihq/db2rest/rest/MySQLReadControllerTest.java +++ b/src/test/java/com/homihq/db2rest/rest/MySQLReadControllerTest.java @@ -11,13 +11,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; class MySQLReadControllerTest extends MySQLBaseIntegrationTest { - //@Test + @Test @DisplayName("Get all fields.") void findAllFilms() throws Exception { - mockMvc.perform(get("/films").header("Accept-Profile", "sakila") + mockMvc.perform(get("/film").header("Accept-Profile", "sakila") .accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isBadRequest()) + .andExpect(status().isOk()) .andDo(print()) .andDo(document("mysql-get-all-films")); } diff --git a/src/test/java/com/homihq/db2rest/rest/PgCreateControllerTest.java b/src/test/java/com/homihq/db2rest/rest/PgCreateControllerTest.java index c3f1d82b..f1eb5e95 100644 --- a/src/test/java/com/homihq/db2rest/rest/PgCreateControllerTest.java +++ b/src/test/java/com/homihq/db2rest/rest/PgCreateControllerTest.java @@ -76,4 +76,34 @@ void createError() throws Exception { .andDo(document("pg-create-a-film-error")); } + + @Test + @DisplayName("Create a film - non existent table.") + void createNonExistentTable() throws Exception { + + var json = """ + { + "title" : "Dunki", + "description" : "Film about illegal immigration" , + "release_year" : 2023, + "language_id" : 1, + "original_language_id" : null, + "rental_duration" : 6, + "rental_rate" : 0.99 , + "length" : 150, + "replacement_cost" : 20.99 , + "rating" : "PG-13" , + "special_features" : "Commentaries" + + } + """; + + mockMvc.perform(post("/films").contentType(MediaType.APPLICATION_JSON).characterEncoding("utf-8") + .header("Content-Profile", "public") + .content(json).accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isBadRequest()) + .andDo(print()) + .andDo(document("pg-create-a-film-no-table")); + + } }