Skip to content

Commit

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

Expand Down Expand Up @@ -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"));
}

Expand Down Expand Up @@ -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"));
}

Expand All @@ -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();
Expand Down
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.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;
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 @@ -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<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 + "/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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
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;

Expand All @@ -22,10 +24,12 @@

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 Down Expand Up @@ -72,4 +77,12 @@ public String getAliasedName(DbColumn dbColumn, boolean deleteOp) {

return dbColumn.tableAlias() + "." + dbColumn.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 c0bd60f

Please sign in to comment.