Skip to content

Commit

Permalink
Adding integration test for different ISO date time formats for MsSql db
Browse files Browse the repository at this point in the history
  • Loading branch information
souravroy committed Dec 1, 2024
1 parent 5751add commit b639e6c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -37,6 +39,9 @@ public void processTypes(DbTable table, List<String> insertableColumns, Map<Stri

if (StringUtils.equalsAnyIgnoreCase(columnDataTypeName, "json")) {
data.put(columnName, getObjectMapper().writeValueAsString(value));
} else if (StringUtils.equalsAnyIgnoreCase(columnDataTypeName, "datetime")) {
LocalDateTime v = convertToLocalDateTime((String) value);
data.put(columnName, v);
}
}
} catch (Exception exception) {
Expand Down Expand Up @@ -80,4 +85,12 @@ public String getReadSqlTemplate() {
public String getUpdateSqlTemplate() {
return "update-mssql";
}

private LocalDateTime convertToLocalDateTime(String value) {
try {
return LocalDateTime.parse(value, DateTimeFormatter.ISO_DATE_TIME);
} catch (Exception e) {
throw new GenericDataAccessException("Error converting to LocalDateTime type - " + e.getLocalizedMessage());
}
}
}

0 comments on commit b639e6c

Please sign in to comment.