Skip to content
This repository has been archived by the owner on Aug 13, 2020. It is now read-only.

Commit

Permalink
Add common JPA date time attribute converters
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Rich committed Apr 8, 2016
1 parent 2516dc8 commit d230243
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.gov.justice.services.common.jpa.converter;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.sql.Date;
import java.time.LocalDate;

import static java.sql.Date.valueOf;

@Converter(autoApply = true)
public class LocalDatePersistenceConverter implements AttributeConverter<LocalDate, Date> {

@Override
public Date convertToDatabaseColumn(final LocalDate entityValue) {
return entityValue != null ? valueOf(entityValue) : null;
}

@Override
public LocalDate convertToEntityAttribute(final Date databaseValue) {
return databaseValue != null ? databaseValue.toLocalDate() : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.gov.justice.services.common.jpa.converter;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.sql.Timestamp;
import java.time.LocalDateTime;

import static java.sql.Timestamp.valueOf;

@Converter(autoApply = true)
public class LocalDateTimePersistenceConverter implements AttributeConverter<LocalDateTime, Timestamp> {

@Override
public Timestamp convertToDatabaseColumn(final LocalDateTime entityValue) {
return entityValue != null ? valueOf(entityValue) : null;
}

@Override
public LocalDateTime convertToEntityAttribute(final Timestamp databaseValue) {
return databaseValue != null ? databaseValue.toLocalDateTime() : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package uk.gov.justice.services.common.jpa.converter;

import org.junit.Before;
import org.junit.Test;

import java.sql.Date;
import java.time.LocalDate;
import java.time.Month;
import java.util.Calendar;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNull;

public class LocalDatePersistenceConverterTest {

private static final int DAY = 25;
private static final Month MONTH = Month.DECEMBER;
private static final int YEAR = 2016;

private LocalDatePersistenceConverter localDatePersistenceConverter;

@Before
public void setup() {
localDatePersistenceConverter = new LocalDatePersistenceConverter();
}

@Test
public void shouldReturnValidConvertedDatabaseDate() {
LocalDate date = LocalDate.of(YEAR, MONTH, DAY);
Date convertedDate = localDatePersistenceConverter.convertToDatabaseColumn(date);

Calendar calendar = Calendar.getInstance();
calendar.setTime(convertedDate);
assertThat(calendar.get(Calendar.DATE), equalTo(DAY));
assertThat(calendar.get(Calendar.MONTH), equalTo(MONTH.getValue() - 1));
assertThat(calendar.get(Calendar.YEAR), equalTo(YEAR));
}

@Test
public void shouldReturnNullDatabaseValueWhenGivenNullDate() {
assertNull(localDatePersistenceConverter.convertToDatabaseColumn(null));
}

@Test
public void shouldReturnValidConvertedAttributeDate() {
Date date = Date.valueOf(String.format("%s-%s-%s", YEAR, MONTH.getValue() - 1, DAY));

LocalDate convertedDate = localDatePersistenceConverter.convertToEntityAttribute(date);

Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
assertThat(convertedDate.getDayOfMonth(), equalTo(calendar.get(Calendar.DATE)));
assertThat(convertedDate.getMonth().getValue(), equalTo(calendar.get(Calendar.MONTH) + 1));
assertThat(convertedDate.getYear(), equalTo(calendar.get(Calendar.YEAR)));
}

@Test
public void shouldReturnEntityAttributeNullWhenGivenNullDate() {
assertNull(localDatePersistenceConverter.convertToEntityAttribute(null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package uk.gov.justice.services.common.jpa.converter;

import org.junit.Before;
import org.junit.Test;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.Calendar;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNull;

public class LocalDateTimePersistenceConverterTest {

public static final int HOUR = 11;
public static final int MINUTE = 59;
public static final int SECOND = 58;
private static final int DAY = 25;
private static final Month MONTH = Month.DECEMBER;
private static final int YEAR = 2016;
private LocalDateTimePersistenceConverter localDateTimePersistenceConverter;

@Before
public void setup() {
localDateTimePersistenceConverter = new LocalDateTimePersistenceConverter();
}

@Test
public void shouldReturnValidConvertedDatabaseDate() {
LocalDateTime dateTime = LocalDateTime.of(YEAR, MONTH, DAY, HOUR, MINUTE, SECOND);
Timestamp timestamp = localDateTimePersistenceConverter.convertToDatabaseColumn(dateTime);

Calendar calendar = Calendar.getInstance();
calendar.setTime(timestamp);
assertThat(calendar.get(Calendar.DATE), equalTo(DAY));
assertThat(calendar.get(Calendar.MONTH), equalTo(MONTH.getValue() - 1));
assertThat(calendar.get(Calendar.YEAR), equalTo(YEAR));
assertThat(calendar.get(Calendar.HOUR), equalTo(HOUR));
assertThat(calendar.get(Calendar.MINUTE), equalTo(MINUTE));
assertThat(calendar.get(Calendar.SECOND), equalTo(SECOND));
}

@Test
public void shouldReturnNullDatabaseValueWhenGivenNullDate() {
assertNull(localDateTimePersistenceConverter.convertToDatabaseColumn(null));
}

@Test
public void shouldReturnValidConvertedAttributeDate() {
Timestamp timestamp = Timestamp.valueOf(String.format("%s-%s-%s %d:%d:%d", YEAR, MONTH.getValue() - 1, DAY, HOUR, MINUTE, SECOND));
LocalDateTime localDateTime = localDateTimePersistenceConverter.convertToEntityAttribute(timestamp);

Calendar calendar = Calendar.getInstance();
calendar.setTime(timestamp);
assertThat(localDateTime.getDayOfMonth(), equalTo(calendar.get(Calendar.DATE)));
assertThat(localDateTime.getMonth().getValue(), equalTo(calendar.get(Calendar.MONTH) + 1));
assertThat(localDateTime.getYear(), equalTo(calendar.get(Calendar.YEAR)));
assertThat(localDateTime.getHour(), equalTo(calendar.get(Calendar.HOUR)));
assertThat(localDateTime.getMinute(), equalTo(calendar.get(Calendar.MINUTE)));
assertThat(localDateTime.getSecond(), equalTo(calendar.get(Calendar.SECOND)));
}

@Test
public void shouldReturnEntityAttributeNullWhenGivenNullDate() {
assertNull(localDateTimePersistenceConverter.convertToEntityAttribute(null));
}
}
4 changes: 0 additions & 4 deletions event-sourcing/event-repository/event-repository-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
<artifactId>event-repository-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>

<!-- Test Dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@
</subsystem>
<subsystem xmlns="urn:jboss:domain:messaging-activemq:1.0">
<server name="default">
<journal type="NIO"/>
<security-setting name="#">
<role name="guest" delete-non-durable-queue="true" create-non-durable-queue="true" consume="true"
send="true"/>
Expand Down

0 comments on commit d230243

Please sign in to comment.