From b639e6c2387cfbb9e776442acec2dcb4b65bcb77 Mon Sep 17 00:00:00 2001 From: Sourav Roy Date: Sun, 1 Dec 2024 17:26:02 +0000 Subject: [PATCH] Adding integration test for different ISO date time formats for MsSql db --- .../rest/mssql/MsSQLBaseIntegrationTest.java | 10 ++++ .../rest/mssql/MsSQLDateTimeAllTest.java | 49 +++++++++++++++++++ .../config/dialect/MsSQLServerDialect.java | 13 +++++ 3 files changed, 72 insertions(+) create mode 100644 api-rest/src/test/java/com/homihq/db2rest/rest/mssql/MsSQLDateTimeAllTest.java 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..58f6b6ab --- /dev/null +++ b/api-rest/src/test/java/com/homihq/db2rest/rest/mssql/MsSQLDateTimeAllTest.java @@ -0,0 +1,49 @@ +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.Order; +import org.junit.jupiter.api.TestClassOrder; +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) +class MsSQLDateTimeAllTest extends MsSQLBaseIntegrationTest { + + @ParameterizedTest + @MethodSource("isoDateTimeFormats") + @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/mssql-dialect/src/main/java/com/homihq/db2rest/jdbc/config/dialect/MsSQLServerDialect.java b/mssql-dialect/src/main/java/com/homihq/db2rest/jdbc/config/dialect/MsSQLServerDialect.java index 4f57c646..a41f06b1 100644 --- a/mssql-dialect/src/main/java/com/homihq/db2rest/jdbc/config/dialect/MsSQLServerDialect.java +++ b/mssql-dialect/src/main/java/com/homihq/db2rest/jdbc/config/dialect/MsSQLServerDialect.java @@ -7,6 +7,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; @@ -37,6 +39,9 @@ public void processTypes(DbTable table, List insertableColumns, Map