Skip to content

Commit 6116cd1

Browse files
author
Gerald Unterrainer
committed
Merge branch 'develop'
2 parents 66bfb1f + 163e5ea commit 6116cd1

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<modelVersion>4.0.0</modelVersion>
1010
<artifactId>rdb-utils</artifactId>
11-
<version>0.2.2</version>
11+
<version>0.2.3</version>
1212
<name>RdbUtils</name>
1313
<packaging>jar</packaging>
1414

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package info.unterrainer.commons.rdbutils;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.time.LocalDateTime;
6+
import java.time.temporal.ChronoUnit;
7+
8+
import org.junit.jupiter.api.BeforeAll;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
12+
import info.unterrainer.commons.jreutils.DateUtils;
13+
import info.unterrainer.commons.rdbutils.converters.LocalDateTimeConverter;
14+
import info.unterrainer.commons.rdbutils.exceptions.RdbUtilException;
15+
import info.unterrainer.commons.rdbutils.jpas.TestJpa;
16+
import jakarta.persistence.EntityManagerFactory;
17+
import jakarta.persistence.Query;
18+
import jakarta.persistence.TypedQuery;
19+
import lombok.extern.slf4j.Slf4j;
20+
21+
@Slf4j
22+
public class LocalDateTimeConverterIntegrationTests {
23+
24+
LocalDateTimeConverter converter = new LocalDateTimeConverter();
25+
26+
public static EntityManagerFactory emf;
27+
28+
@BeforeAll
29+
public static void setupClass() {
30+
try {
31+
emf = RdbUtils.createAutoclosingEntityManagerFactory(ManualTests.class, "test");
32+
} catch (RdbUtilException e) {
33+
log.error("Error getting EntityManagerFactory", e);
34+
}
35+
}
36+
37+
@BeforeEach
38+
public void beforeMethod() {
39+
deleteTestTable();
40+
}
41+
42+
@Test
43+
public void persistingAndReadingEntityWorks() {
44+
LocalDateTime nowUtc = DateUtils.nowUtc();
45+
TestJpa testJpa = TestJpa.builder().message("test").createdOn(nowUtc).editedOn(nowUtc).build();
46+
47+
persistTestEntity(testJpa);
48+
TestJpa jpa = selectFirstTestEntity();
49+
assertThat(jpa.getEditedOn()).isEqualTo(testJpa.getEditedOn().truncatedTo(ChronoUnit.MICROS));
50+
}
51+
52+
private int deleteTestTable() {
53+
return Transactions.withNewTransactionReturning(emf, em -> {
54+
Query q = em.createQuery(String.format("DELETE FROM %s AS o", TestJpa.class.getSimpleName()));
55+
int entitiesDeleted = q.executeUpdate();
56+
log.info("[{}] entities deleted", entitiesDeleted);
57+
return entitiesDeleted;
58+
});
59+
}
60+
61+
private void persistTestEntity(final TestJpa jpa) {
62+
Transactions.withNewTransaction(emf, em -> {
63+
em.persist(jpa);
64+
});
65+
}
66+
67+
private TestJpa selectFirstTestEntity() {
68+
return Transactions.withNewTransactionReturning(emf, em -> {
69+
TypedQuery<TestJpa> q = em
70+
.createQuery(String.format("SELECT o FROM %s AS o", TestJpa.class.getSimpleName()), TestJpa.class);
71+
q.setMaxResults(1);
72+
return q.getSingleResult();
73+
});
74+
}
75+
}

src/test/java/info/unterrainer/commons/rdbutils/LocalDateTimeConverterTests.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
import info.unterrainer.commons.jreutils.DateUtils;
1212
import info.unterrainer.commons.rdbutils.converters.LocalDateTimeConverter;
13+
import lombok.extern.slf4j.Slf4j;
1314

15+
@Slf4j
1416
public class LocalDateTimeConverterTests {
1517

1618
LocalDateTimeConverter converter = new LocalDateTimeConverter();
@@ -26,11 +28,13 @@ public void conversionFromLocalDateTimeToTimestamp() {
2628
public void conversionFromTimestampToLocalDateTime() {
2729
LocalDateTime now = DateUtils.nowUtc();
2830
Timestamp ts = new Timestamp(DateUtils.utcLocalDateTimeToEpoch(now));
31+
ts.setNanos(now.truncatedTo(ChronoUnit.MICROS).getNano());
2932
LocalDateTime d = converter.convertToEntityAttribute(ts);
3033

3134
Timestamp timestamp = Timestamp.valueOf(d);
3235
timestamp.setNanos(d.truncatedTo(ChronoUnit.MICROS).getNano());
36+
LocalDateTime other = DateUtils.nowUtc();
3337

34-
assertThat(d).isEqualTo(ts.toLocalDateTime());
38+
assertThat(d).isEqualTo(other);
3539
}
3640
}

src/test/java/info/unterrainer/commons/rdbutils/jpas/TestJpa.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package info.unterrainer.commons.rdbutils.jpas;
22

3+
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
34
import jakarta.persistence.Entity;
45
import jakarta.persistence.Table;
5-
6-
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
76
import lombok.Data;
87
import lombok.EqualsAndHashCode;
98
import lombok.NoArgsConstructor;

0 commit comments

Comments
 (0)