Skip to content

Commit

Permalink
Add another test with another object
Browse files Browse the repository at this point in the history
  • Loading branch information
rainbowdashlabs committed May 25, 2024
1 parent 04df3fd commit 24cbb4e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -42,7 +45,11 @@ public T map(Row row) throws SQLException {
var type = meta.getColumnTypeName(index);
var forType = registry.findForType(type);
var mapper = forType.orElseThrow(() -> new UnknownTypeException(type));
map.put(meta.getColumnName(index), mapper.map(row, index));
Object mapped = mapper.map(row, index);
if (mapped instanceof Timestamp timestamp) {
mapped = DateTimeFormatter.ISO_INSTANT.format(timestamp.toInstant());
}
map.put(meta.getColumnName(index), mapped);
}

return objectMapper.convertValue(map, clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@
import java.util.UUID;

public record TestObj(int first, long second, String third, Instant fourth, UUID fifth) {
@JsonCreator
public TestObj(@JsonProperty("first") int first,
@JsonProperty("second") long second,
@JsonProperty("third") String third,
@JsonProperty("fourth") Instant fourth,
@JsonProperty("fifth") UUID fifth) {
this.first = first;
this.second = second;
this.third = third;
this.fourth = fourth;
this.fifth = fifth;
}

@Override
public boolean equals(Object o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* Copyright (C) RainbowDashLabs and Contributor
*/

package de.chojo.sadu.jackson;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.Objects;
import java.util.UUID;

public record TestObj2(int first, long second, String third, LocalDateTime fourth, UUID fifth) {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

TestObj2 testObj = (TestObj2) o;
return first == testObj.first && second == testObj.second && Objects.equals(fifth, testObj.fifth) && Objects.equals(third, testObj.third) && Objects.equals(fourth.truncatedTo(ChronoUnit.SECONDS), testObj.fourth.truncatedTo(ChronoUnit.SECONDS));
}

@Override
public int hashCode() {
int result = first;
result = 31 * result + Long.hashCode(second);
result = 31 * result + Objects.hashCode(third);
result = 31 * result + Objects.hashCode(fourth.truncatedTo(ChronoUnit.SECONDS));
result = 31 * result + Objects.hashCode(fifth);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@
package de.chojo.sadu.jackson.databases;

import de.chojo.sadu.jackson.TestObj;
import de.chojo.sadu.jackson.TestObj2;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.time.Instant;
import java.time.LocalDateTime;
import java.util.UUID;
import java.util.stream.Stream;

import static de.chojo.sadu.jackson.setup.PostgresDatabase.createContainer;
import static de.chojo.sadu.queries.api.call.Call.call;
import static de.chojo.sadu.queries.api.query.Query.query;
import static de.chojo.sadu.queries.converter.StandardValueConverter.INSTANT_TIMESTAMP;
import static de.chojo.sadu.queries.converter.StandardValueConverter.LOCAL_DATE_TIME;
import static de.chojo.sadu.queries.converter.StandardValueConverter.UUID_STRING;

public class TestPostgreSQL {
Expand All @@ -28,14 +31,19 @@ void setup() throws Exception {
createContainer();
}

public static Stream<TestObj> objects() {
public static Stream<TestObj> objects1() {
return Stream.of(
new TestObj(1324, 78644877545455L, "test", Instant.now(), UUID.randomUUID())
);
}
public static Stream<TestObj2> objects2() {
return Stream.of(
new TestObj2(1324, 78644877545455L, "test", LocalDateTime.now(), UUID.randomUUID())
);
}

@ParameterizedTest
@MethodSource("objects")
@MethodSource("objects1")
public void testRead(TestObj obj) {
query("INSERT INTO test(first, second, third, fourth, fifth) VALUES (?,?,?,?,?::UUID)")
.single(call().bind(obj.first()).bind(obj.second()).bind(obj.third()).bind(obj.fourth(), INSTANT_TIMESTAMP).bind(obj.fifth(), UUID_STRING))
Expand All @@ -49,4 +57,19 @@ public void testRead(TestObj obj) {

Assertions.assertEquals(obj, result);
}
@ParameterizedTest
@MethodSource("objects2")
public void testRead2(TestObj2 obj) {
query("INSERT INTO test(first, second, third, fourth, fifth) VALUES (?,?,?,?,?::UUID)")
.single(call().bind(obj.first()).bind(obj.second()).bind(obj.third()).bind(obj.fourth(), LOCAL_DATE_TIME).bind(obj.fifth(), UUID_STRING))
.insert();

var result = query("SELECT first, second, third, fourth, fifth::UUID FROM test")
.single()
.mapAs(TestObj2.class)
.first()
.get();

Assertions.assertEquals(obj, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ public static Database createContainer() throws IOException, SQLException {
SqlUpdater.builder(dc, PostgreSql.get())
.execute();

// Create a Registry for mapping the types of a database to java objects.
RowMapperRegistry registry = new RowMapperRegistry().register(PostgresqlMapper.getDefaultMapper());
// Wrap the database registry into a jackson registry
JacksonRowMapperRegistry jacksonRegistry = new JacksonRowMapperRegistry(new ObjectMapper().findAndRegisterModules(), registry);

// Register the jackson registry at the query configuration
QueryConfiguration.setDefault(QueryConfiguration.builder(dc)
.setRowMapperRegistry(jacksonRegistry)
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public static RowMapper<Boolean> createBoolean(List<SqlType> types) {
return create(Boolean.class, Row::getBoolean, types);
}

public static RowMapper<Instant> createTimestamp(List<SqlType> types) {
return create(Instant.class, (row, integer) -> row.get(integer, INSTANT_FROM_TIMESTAMP), types);
public static RowMapper<Timestamp> createTimestamp(List<SqlType> types) {
return create(Timestamp.class, Row::getTimestamp, types);
}

public static RowMapper<Byte[]> createBytes(List<SqlType> types) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class PostgresqlMapper {
public static final RowMapper<String> STRING_MAPPER = createString(List.of(PostgreSqlTypes.TEXT, PostgreSqlTypes.VARCHAR, PostgreSqlTypes.CHAR, PostgreSqlTypes.JSON, PostgreSqlTypes.JSONB));
public static final RowMapper<Byte[]> BYTES_MAPPER = createBytes(List.of(PostgreSqlTypes.BYTEA));
public static final RowMapper<UUID> UUID_MAPPER = createUuid(List.of(PostgreSqlTypes.TEXT, PostgreSqlTypes.UUID), List.of(PostgreSqlTypes.BYTEA));
public static final RowMapper<Instant> TIMESTAMP_MAPPER = createTimestamp(List.of(PostgreSqlTypes.TIMESTAMP));
public static final RowMapper<Timestamp> TIMESTAMP_MAPPER = createTimestamp(List.of(PostgreSqlTypes.TIMESTAMP));

private PostgresqlMapper() {
throw new UnsupportedOperationException("This is a utility class.");
Expand Down

0 comments on commit 24cbb4e

Please sign in to comment.