Skip to content

Commit

Permalink
Adding integration test for different ISO date time formats for MySql db
Browse files Browse the repository at this point in the history
  • Loading branch information
souravroy committed Nov 30, 2024
1 parent 81cd4df commit 5751add
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@
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;
import org.junit.jupiter.api.Order;
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;

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;
Expand Down Expand Up @@ -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<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 + "/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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ void deleteActorByTimeStamp() throws Exception {
void createActorWithIsoDateTimeFormats(String isoDateTime) throws Exception {
// Prepare the request with datetime fields
Map<String, Object> actorRequestWithDateTime = new HashMap<>();
actorRequestWithDateTime.put("first_name", "Collective");
actorRequestWithDateTime.put("last_name", "Unconscious");
actorRequestWithDateTime.put("first_name", "Graeme");
actorRequestWithDateTime.put("last_name", "Smith");
actorRequestWithDateTime.put("last_update", isoDateTime);

var result = mockMvc.perform(post(VERSION + "/pgsqldb/actor")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 @@ -35,6 +37,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, "TIMESTAMP")) {
LocalDateTime v = convertToLocalDateTime((String) value);
data.put(columnName, v);
}

}
Expand All @@ -57,4 +62,12 @@ public String renderTableName(DbTable table, boolean containsWhere, boolean dele
public String renderTableNameWithoutAlias(DbTable table) {
return getQuotedName(table.schema()) + "." + getQuotedName(table.name());
}

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 5751add

Please sign in to comment.