From e8be1c1dedd1cd6f04f2ab2ac2cc4ad0bc837e6b Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Tue, 9 Oct 2012 09:15:39 -0500 Subject: [PATCH] Added timestamp to Person for testing Date conversion. --- .../rest/webmvc/spec/QueryMethodsSpec.groovy | 2 +- .../data/rest/test/webmvc/Person.java | 13 ++++++++++ .../rest/test/webmvc/PersonRepository.java | 6 +++++ .../rest/test/webmvc/PersonValidator.java | 3 +-- .../test/webmvc/StringToISODateConverter.java | 24 +++++++++++++++++++ 5 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/StringToISODateConverter.java diff --git a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/QueryMethodsSpec.groovy b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/QueryMethodsSpec.groovy index c003173c7..a607aef1a 100644 --- a/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/QueryMethodsSpec.groovy +++ b/spring-data-rest-webmvc/src/test/groovy/org/springframework/data/rest/webmvc/spec/QueryMethodsSpec.groovy @@ -29,7 +29,7 @@ class QueryMethodsSpec extends BaseSpec { then: response.statusCode == HttpStatus.OK - body.links.size() == 4 + body.links.size() == 5 } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/Person.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/Person.java index 8b69bed33..d332a1640 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/Person.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/Person.java @@ -1,5 +1,7 @@ package org.springframework.data.rest.test.webmvc; +import java.util.Calendar; +import java.util.Date; import java.util.List; import java.util.Map; import javax.persistence.CascadeType; @@ -8,6 +10,7 @@ import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.MapKey; import javax.persistence.OneToMany; +import javax.persistence.PrePersist; import javax.persistence.Version; import org.codehaus.jackson.annotate.JsonManagedReference; @@ -28,6 +31,7 @@ public class Person { @OneToMany(cascade = CascadeType.REMOVE) @MapKey(name = "type") private Map profiles; + private Date created; public Person() { } @@ -90,4 +94,13 @@ public class Person { this.profiles = profiles; } + public Date getCreated() { + return created; + } + + @PrePersist + private void setCreated() { + this.created = Calendar.getInstance().getTime(); + } + } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/PersonRepository.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/PersonRepository.java index 049ad9976..1f8a612dd 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/PersonRepository.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/PersonRepository.java @@ -1,5 +1,6 @@ package org.springframework.data.rest.test.webmvc; +import java.util.Date; import java.util.List; import org.springframework.data.domain.Page; @@ -7,6 +8,7 @@ import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; +import org.springframework.data.rest.repository.annotation.ConvertWith; import org.springframework.data.rest.repository.annotation.RestResource; /** @@ -35,4 +37,8 @@ public interface PersonRepository extends PagingAndSortingRepository findById(@Param("ids") List ids, Pageable pageable); + @RestResource(path = "created") List findByCreatedGreaterThan( + @Param("startDate") @ConvertWith(StringToISODateConverter.class) Date startDate + ); + } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/PersonValidator.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/PersonValidator.java index 5ad826390..a452f1523 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/PersonValidator.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/PersonValidator.java @@ -10,8 +10,7 @@ import org.springframework.validation.Validator; /** * @author Jon Brisbin */ -public class PersonValidator - implements Validator { +public class PersonValidator implements Validator { private static final Logger LOG = LoggerFactory.getLogger(PersonValidator.class); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/StringToISODateConverter.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/StringToISODateConverter.java new file mode 100644 index 000000000..935def3ec --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/test/webmvc/StringToISODateConverter.java @@ -0,0 +1,24 @@ +package org.springframework.data.rest.test.webmvc; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.springframework.core.convert.converter.Converter; + +/** + * @author Jon Brisbin + */ +public class StringToISODateConverter implements Converter { + @Override public Date convert(String[] s) { + if(s.length == 1) { + try { + return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(s[0]); + } catch(ParseException e) { + throw new IllegalArgumentException(e); + } + } + + throw new IllegalArgumentException("Can only parse a single date in the parameter."); + } +} \ No newline at end of file