diff --git a/api-rest/src/test/java/com/homihq/db2rest/BaseIntegrationTest.java b/api-rest/src/test/java/com/homihq/db2rest/BaseIntegrationTest.java index 58b21897..c7265593 100644 --- a/api-rest/src/test/java/com/homihq/db2rest/BaseIntegrationTest.java +++ b/api-rest/src/test/java/com/homihq/db2rest/BaseIntegrationTest.java @@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.provider.Arguments; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; @@ -19,6 +20,7 @@ import java.io.IOException; import java.nio.file.Paths; +import java.util.List; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; @@ -61,4 +63,16 @@ void setupEnv() { } } } + + static List isoDateTimeFormats() { + return List.of(Arguments.of("2011-12-03T10:15:30"), + Arguments.of("2011-12-03T10:15:30.123"), + Arguments.of("2011-12-03T10:15:30+01:00"), + Arguments.of("2011-12-03T10:15:30-05:00"), + Arguments.of("2011-12-03T10:15:30Z"), + Arguments.of("2011-12-03T10:15:30.123Z"), + Arguments.of("2011-12-03T10:15:30.123+05:30"), + Arguments.of("2011-12-03T10:15:30+01:00[Europe/Paris]"), + Arguments.of("2011-12-03T10:15:30.123+01:00[Europe/Paris]")); + } } diff --git a/api-rest/src/test/java/com/homihq/db2rest/rest/mariadb/MariaDBCreateControllerTest.java b/api-rest/src/test/java/com/homihq/db2rest/rest/mariadb/MariaDBCreateControllerTest.java index 9571583d..de0df325 100644 --- a/api-rest/src/test/java/com/homihq/db2rest/rest/mariadb/MariaDBCreateControllerTest.java +++ b/api-rest/src/test/java/com/homihq/db2rest/rest/mariadb/MariaDBCreateControllerTest.java @@ -60,8 +60,8 @@ void create() throws Exception { //.andDo(print()) .andExpect(status().isCreated()) .andExpect(jsonPath("$.row", equalTo(1))) - //.andExpect(jsonPath("$.keys.GENERATED_KEY").exists()) - //.andExpect(jsonPath("$.keys.GENERATED_KEY", equalTo(5))) + .andExpect(jsonPath("$.keys.insert_id").exists()) + .andExpect(jsonPath("$.keys.insert_id", equalTo(5))) .andDo(document("mariadb-create-a-film")); } @@ -99,8 +99,8 @@ void createDirectorWithTSIDEnabled() throws Exception { //.andDo(print()) .andExpect(status().isCreated()) .andExpect(jsonPath("$.row", equalTo(1))) - //.andExpect(jsonPath("$.keys.director_id").exists()) - // .andExpect(jsonPath("$.keys.director_id").isNumber()) + .andExpect(jsonPath("$.keys.director_id").exists()) + .andExpect(jsonPath("$.keys.director_id").isNumber()) .andDo(document("mariadb-create-a-director-tsid-enabled")); } @@ -128,8 +128,8 @@ void createVanityVanWithVarcharTsIdType() throws Exception { .andDo(print()) .andExpect(status().isCreated()) .andExpect(jsonPath("$.row", equalTo(1))) - //.andExpect(jsonPath("$.keys.vanity_van_id").exists()) - //.andExpect(jsonPath("$.keys.vanity_van_id").isString()) + .andExpect(jsonPath("$.keys.van_id").exists()) + .andExpect(jsonPath("$.keys.van_id").isString()) .andDo(document("mariadb-create-a-vanity-van-tsid-varchar")); } @@ -144,7 +144,7 @@ void createFilmWithSubsetOfColumns() throws Exception { .andExpect(status().isCreated()) .andExpect(jsonPath("$.row", equalTo(1))) .andExpect(jsonPath("$.keys").exists()) - // .andExpect(jsonPath("$.keys.GENERATED_KEY", equalTo(5))) + //.andExpect(jsonPath("$.keys.insert_id", equalTo(5))) .andExpect(jsonPath("$.keys.insert_id").isNumber()) .andDo(document("mariadb-create-a-film")) .andReturn(); diff --git a/api-rest/src/test/java/com/homihq/db2rest/rest/mariadb/MariadbDateTimeAllTest.java b/api-rest/src/test/java/com/homihq/db2rest/rest/mariadb/MariadbDateTimeAllTest.java index 0d39722a..42ccc350 100644 --- a/api-rest/src/test/java/com/homihq/db2rest/rest/mariadb/MariadbDateTimeAllTest.java +++ b/api-rest/src/test/java/com/homihq/db2rest/rest/mariadb/MariadbDateTimeAllTest.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.homihq.db2rest.MariaDBBaseIntegrationTest; import com.homihq.db2rest.rest.DateTimeUtil; +import com.jayway.jsonpath.JsonPath; import org.junit.jupiter.api.ClassOrderer; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.MethodOrderer; @@ -12,6 +13,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestClassOrder; import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import java.util.HashMap; import java.util.Map; @@ -19,6 +22,7 @@ import static com.homihq.db2rest.jdbc.rest.RdbmsRestApi.VERSION; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.springframework.http.MediaType.APPLICATION_JSON; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete; @@ -142,4 +146,28 @@ void deleteActorByTimeStamp() throws Exception { .andExpect(jsonPath("$.rows", equalTo(1))) .andDo(document("mariadb-delete-an-actor-by-timestamp")); } + + @ParameterizedTest + @MethodSource("isoDateTimeFormats") + @Order(5) + @DisplayName("Test ISO Date Time formats") + void createActorWithIsoDateTimeFormats(String isoDateTime) throws Exception { + // Prepare the request with datetime fields + Map actorRequestWithDateTime = new HashMap<>(); + actorRequestWithDateTime.put("first_name", "Graeme"); + actorRequestWithDateTime.put("last_name", "Smith"); + actorRequestWithDateTime.put("last_update", isoDateTime); + + var result = mockMvc.perform(post(VERSION + "/mariadb/actor") + .contentType(APPLICATION_JSON) + .accept(APPLICATION_JSON) + .content(objectMapper.writeValueAsString(actorRequestWithDateTime))) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.row", equalTo(1))) + .andDo(document("mariadb-create-an-actor-with-datetime")) + .andReturn(); + + var pk = JsonPath.read(result.getResponse().getContentAsString(), "$.keys.insert_id"); + assertTrue(deleteRow("actor", "actor_id", (int) pk)); + } } diff --git a/api-rest/src/test/java/com/homihq/db2rest/rest/mssql/MsSQLBaseIntegrationTest.java b/api-rest/src/test/java/com/homihq/db2rest/rest/mssql/MsSQLBaseIntegrationTest.java index 0b56f4c2..0227191a 100644 --- a/api-rest/src/test/java/com/homihq/db2rest/rest/mssql/MsSQLBaseIntegrationTest.java +++ b/api-rest/src/test/java/com/homihq/db2rest/rest/mssql/MsSQLBaseIntegrationTest.java @@ -5,7 +5,9 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.homihq.db2rest.BaseIntegrationTest; import com.homihq.db2rest.MsSQLServerContainerConfiguration; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Import; +import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.ActiveProfiles; import static com.homihq.db2rest.jdbc.rest.RdbmsRestApi.VERSION; @@ -17,10 +19,18 @@ class MsSQLBaseIntegrationTest extends BaseIntegrationTest { protected static final String TEST_JSON_FOLDER = "/testdata"; protected static final String DB_NAME = "mssql"; + @Autowired + private JdbcTemplate jdbcTemplate; + @WithJacksonMapper protected ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule()); protected String getPrefixApiUrl() { return VERSION + "/" + DB_NAME; } + + protected boolean deleteRow(String table, String column, int id) { + var query = "DELETE FROM " + table + " WHERE " + column + " = ?"; + return jdbcTemplate.update(query, id) == 1; + } } diff --git a/api-rest/src/test/java/com/homihq/db2rest/rest/mssql/MsSQLDateTimeAllTest.java b/api-rest/src/test/java/com/homihq/db2rest/rest/mssql/MsSQLDateTimeAllTest.java new file mode 100644 index 00000000..7608a8b3 --- /dev/null +++ b/api-rest/src/test/java/com/homihq/db2rest/rest/mssql/MsSQLDateTimeAllTest.java @@ -0,0 +1,53 @@ +package com.homihq.db2rest.rest.mssql; + +import com.jayway.jsonpath.JsonPath; +import org.junit.jupiter.api.ClassOrderer; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.TestClassOrder; +import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.HashMap; +import java.util.Map; + +import static com.homihq.db2rest.jdbc.rest.RdbmsRestApi.VERSION; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.springframework.http.MediaType.APPLICATION_JSON; +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@TestClassOrder(ClassOrderer.OrderAnnotation.class) +@Order(506) +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class MsSQLDateTimeAllTest extends MsSQLBaseIntegrationTest { + + @ParameterizedTest + @MethodSource("isoDateTimeFormats") + @Order(1) + @DisplayName("Test ISO Date Time formats") + void createActorWithIsoDateTimeFormats(String isoDateTime) throws Exception { + // Prepare the request with datetime fields + Map actorRequestWithDateTime = new HashMap<>(); + actorRequestWithDateTime.put("first_name", "Graeme"); + actorRequestWithDateTime.put("last_name", "Smith"); + actorRequestWithDateTime.put("last_update", isoDateTime); + + var result = mockMvc.perform(post(VERSION + "/mssql/actor") + .contentType(APPLICATION_JSON) + .accept(APPLICATION_JSON) + .content(objectMapper.writeValueAsString(actorRequestWithDateTime))) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.row", equalTo(1))) + .andDo(document("mssql-create-an-actor-with-datetime")) + .andReturn(); + + var pk = JsonPath.read(result.getResponse().getContentAsString(), "$.keys.GENERATED_KEYS"); + assertTrue(deleteRow("actor", "actor_id", (int) pk)); + } +} diff --git a/api-rest/src/test/java/com/homihq/db2rest/rest/mysql/MySQLDateTimeAllTest.java b/api-rest/src/test/java/com/homihq/db2rest/rest/mysql/MySQLDateTimeAllTest.java index 456edb27..640690a5 100644 --- a/api-rest/src/test/java/com/homihq/db2rest/rest/mysql/MySQLDateTimeAllTest.java +++ b/api-rest/src/test/java/com/homihq/db2rest/rest/mysql/MySQLDateTimeAllTest.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.homihq.db2rest.MySQLBaseIntegrationTest; import com.homihq.db2rest.rest.DateTimeUtil; +import com.jayway.jsonpath.JsonPath; import org.junit.jupiter.api.ClassOrderer; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.MethodOrderer; @@ -12,6 +13,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestClassOrder; import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import java.util.HashMap; import java.util.Map; @@ -19,6 +22,7 @@ import static com.homihq.db2rest.jdbc.rest.RdbmsRestApi.VERSION; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.springframework.http.MediaType.APPLICATION_JSON; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete; @@ -140,4 +144,28 @@ void deleteActorByTimeStamp() throws Exception { .andExpect(jsonPath("$.rows", equalTo(1))) .andDo(document("mysql-delete-an-actor-by-timestamp")); } + + @ParameterizedTest + @MethodSource("isoDateTimeFormats") + @Order(5) + @DisplayName("Test ISO Date Time formats") + void createActorWithIsoDateTimeFormats(String isoDateTime) throws Exception { + // Prepare the request with datetime fields + Map actorRequestWithDateTime = new HashMap<>(); + actorRequestWithDateTime.put("first_name", "Graeme"); + actorRequestWithDateTime.put("last_name", "Smith"); + actorRequestWithDateTime.put("last_update", isoDateTime); + + var result = mockMvc.perform(post(VERSION + "/mysqldb/actor") + .contentType(APPLICATION_JSON) + .accept(APPLICATION_JSON) + .content(objectMapper.writeValueAsString(actorRequestWithDateTime))) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.row", equalTo(1))) + .andDo(document("mysql-create-an-actor-with-datetime")) + .andReturn(); + + var pk = JsonPath.read(result.getResponse().getContentAsString(), "$.keys.GENERATED_KEY"); + assertTrue(deleteRow("actor", "actor_id", (int) pk)); + } } diff --git a/api-rest/src/test/java/com/homihq/db2rest/rest/oracle/OracleDateTimeAllTest.java b/api-rest/src/test/java/com/homihq/db2rest/rest/oracle/OracleDateTimeAllTest.java index 8d670136..1803d533 100644 --- a/api-rest/src/test/java/com/homihq/db2rest/rest/oracle/OracleDateTimeAllTest.java +++ b/api-rest/src/test/java/com/homihq/db2rest/rest/oracle/OracleDateTimeAllTest.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.homihq.db2rest.OracleBaseIntegrationTest; import com.homihq.db2rest.rest.DateTimeUtil; +import com.jayway.jsonpath.JsonPath; import org.junit.jupiter.api.ClassOrderer; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.MethodOrderer; @@ -12,13 +13,18 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestClassOrder; import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import java.util.HashMap; +import java.util.List; import java.util.Map; import static com.homihq.db2rest.jdbc.rest.RdbmsRestApi.VERSION; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.springframework.http.MediaType.APPLICATION_JSON; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete; @@ -144,4 +150,30 @@ void deleteActorByTimeStamp() throws Exception { .andExpect(jsonPath("$.rows", equalTo(1))) .andDo(document("oracle-delete-an-actor-by-timestamp")); } + + @ParameterizedTest + @MethodSource("isoDateTimeFormats") + @Order(5) + @DisplayName("Test ISO Date Time formats") + void createActorWithIsoDateTimeFormats(String isoDateTime) throws Exception { + // Prepare the request with datetime fields + Map actorRequestWithDateTime = new HashMap<>(); + actorRequestWithDateTime.put("first_name", "Graeme"); + actorRequestWithDateTime.put("last_name", "Smith"); + actorRequestWithDateTime.put("last_update", isoDateTime); + var result = mockMvc.perform(post(VERSION + "/oradb/ACTOR") + .queryParam("sequences", "actor_id:actor_sequence") + .contentType(APPLICATION_JSON) + .accept(APPLICATION_JSON) + .content(objectMapper.writeValueAsString(actorRequestWithDateTime))) + .andDo(print()) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.row", equalTo(1))) + .andDo(document("oracle-create-an-actor-with-datetime")) + .andReturn(); + + var pk = JsonPath.read(result.getResponse().getContentAsString(), "$.keys.ACTOR_ID"); + assertTrue(deleteRow("actor", "actor_id", (int) pk)); + } + } diff --git a/api-rest/src/test/java/com/homihq/db2rest/rest/pg/PGDateTimeAllTest.java b/api-rest/src/test/java/com/homihq/db2rest/rest/pg/PGDateTimeAllTest.java index acb4429e..10af6866 100644 --- a/api-rest/src/test/java/com/homihq/db2rest/rest/pg/PGDateTimeAllTest.java +++ b/api-rest/src/test/java/com/homihq/db2rest/rest/pg/PGDateTimeAllTest.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.homihq.db2rest.PostgreSQLBaseIntegrationTest; import com.homihq.db2rest.rest.DateTimeUtil; +import com.jayway.jsonpath.JsonPath; import org.junit.jupiter.api.ClassOrderer; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.MethodOrderer; @@ -12,6 +13,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestClassOrder; import org.junit.jupiter.api.TestMethodOrder; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import java.util.HashMap; import java.util.Map; @@ -19,6 +22,7 @@ import static com.homihq.db2rest.jdbc.rest.RdbmsRestApi.VERSION; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.springframework.http.MediaType.APPLICATION_JSON; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete; @@ -138,4 +142,30 @@ void deleteActorByTimeStamp() throws Exception { .andExpect(jsonPath("$.rows", equalTo(1))) .andDo(document("pg-delete-an-actor-by-timestamp")); } + + @ParameterizedTest + @MethodSource("isoDateTimeFormats") + @Order(5) + @DisplayName("Test ISO Date Time formats") + void createActorWithIsoDateTimeFormats(String isoDateTime) throws Exception { + // Prepare the request with datetime fields + Map actorRequestWithDateTime = new HashMap<>(); + actorRequestWithDateTime.put("first_name", "Graeme"); + actorRequestWithDateTime.put("last_name", "Smith"); + actorRequestWithDateTime.put("last_update", isoDateTime); + + var result = mockMvc.perform(post(VERSION + "/pgsqldb/actor") + .contentType(APPLICATION_JSON) + .accept(APPLICATION_JSON) + .content(objectMapper.writeValueAsString(actorRequestWithDateTime))) + .andDo(print()) + .andExpect(status().isCreated()) + .andExpect(jsonPath("$.row", equalTo(1))) + .andDo(document("pg-create-an-actor-with-datetime")) + .andReturn(); + + var pk = JsonPath.read(result.getResponse().getContentAsString(), "$.keys.actor_id"); + assertTrue(deleteRow("actor", "actor_id", (int) pk)); + } + } diff --git a/mariadb-dialect/src/main/java/com/homihq/db2rest/jdbc/config/dialect/MariaDBDialect.java b/mariadb-dialect/src/main/java/com/homihq/db2rest/jdbc/config/dialect/MariaDBDialect.java index 4ffc27b9..95e389c1 100644 --- a/mariadb-dialect/src/main/java/com/homihq/db2rest/jdbc/config/dialect/MariaDBDialect.java +++ b/mariadb-dialect/src/main/java/com/homihq/db2rest/jdbc/config/dialect/MariaDBDialect.java @@ -9,6 +9,8 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Map; @@ -35,6 +37,9 @@ public void processTypes(DbTable table, List insertableColumns, Map insertableColumns, Map insertableColumns, Map