DATAREST-229 - RepositoryEntityLinks now exposes templated links.

If the resource exposed for a domain or repository type is considered a paging resource we now return a templated URI. Introduced ResourceMapping.isPagingResource() to allow clients to find out about whether the resource is actually capable of pagination. Adapted implementations to inspect the findAll(…) method as well as the search methods for a Pageable parameter.

Tweaked pom.xml to create correct classpaths if the IDE uses direct workspace project references.
This commit is contained in:
Oliver Gierke
2014-01-22 17:54:35 +01:00
parent 5778ff9940
commit 88b2f05edf
21 changed files with 309 additions and 313 deletions

View File

@@ -1,72 +0,0 @@
package org.springframework.data.rest.core.invoke;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import static org.springframework.util.ReflectionUtils.*;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.core.domain.jpa.PersonRepository;
import org.springframework.data.rest.core.invoke.RepositoryMethod;
import org.springframework.data.rest.core.support.Methods;
import org.springframework.util.ReflectionUtils;
/**
* Tests to verify the integrity of the {@link RepositoryMethod} abstraction.
*
* @author Jon Brisbin
*/
public class RepositoryMethodUnitTests {
Map<String, RepositoryMethod> methods = new HashMap<String, RepositoryMethod>();
RepositoryMethod method;
@Before
public void setup() {
doWithMethods(PersonRepository.class, new ReflectionUtils.MethodCallback() {
@Override
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
String name = method.getName();
RepositoryMethod repoMethod = new RepositoryMethod(method);
methods.put(name, repoMethod);
}
}, Methods.USER_METHODS);
method = methods.get("findByFirstName");
}
@Test
public void shouldFindSimpleQueryMethods() throws Exception {
assertThat(method, notNullValue());
}
@Test
public void shouldFindPageableInformationOnMethod() throws Exception {
assertThat(method, notNullValue());
assertThat(method.isPageable(), is(true));
}
@Test
public void shouldNotFindSortInformationOnMethod() throws Exception {
assertThat(method, notNullValue());
assertThat(method.isSortable(), is(false));
}
@Test
public void shouldProvideParameterClassTypes() throws Exception {
assertThat(method, notNullValue());
assertThat(method.getParameters().get(0).getParameterType(), is(typeCompatibleWith(String.class)));
assertThat(method.getParameters().get(1).getParameterType(), is(typeCompatibleWith(Pageable.class)));
}
@Test
public void shouldProvideParameterNames() throws Exception {
assertThat(method, notNullValue());
assertThat(method.getParameterNames(), contains("firstName", "arg1"));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,12 +19,13 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.data.rest.core.mapping.CollectionResourceMapping;
import org.springframework.data.rest.core.mapping.RepositoryCollectionResourceMapping;
import org.springframework.data.rest.core.mapping.ResourceMapping;
/**
* Unit tests for {@link RepositoryCollectionResourceMapping}.
@@ -36,7 +37,7 @@ public class RepositoryCollectionResourceMappingUnitTests {
@Test
public void buildsDefaultMappingForRepository() {
CollectionResourceMapping mapping = new RepositoryCollectionResourceMapping(PersonRepository.class);
CollectionResourceMapping mapping = getResourceMappingFor(PersonRepository.class);
assertThat(mapping.getPath(), is(new Path("persons")));
assertThat(mapping.getRel(), is("persons"));
@@ -47,7 +48,7 @@ public class RepositoryCollectionResourceMappingUnitTests {
@Test
public void honorsAnnotatedsMapping() {
CollectionResourceMapping mapping = new RepositoryCollectionResourceMapping(AnnotatedPersonRepository.class);
CollectionResourceMapping mapping = getResourceMappingFor(AnnotatedPersonRepository.class);
assertThat(mapping.getPath(), is(new Path("bar")));
assertThat(mapping.getRel(), is("foo"));
@@ -58,8 +59,7 @@ public class RepositoryCollectionResourceMappingUnitTests {
@Test
public void repositoryAnnotationTrumpsDomainTypeMapping() {
CollectionResourceMapping mapping = new RepositoryCollectionResourceMapping(
AnnotatedAnnotatedPersonRepository.class);
CollectionResourceMapping mapping = getResourceMappingFor(AnnotatedAnnotatedPersonRepository.class);
assertThat(mapping.getPath(), is(new Path("/trumpsAll")));
assertThat(mapping.getRel(), is("foo"));
@@ -70,16 +70,33 @@ public class RepositoryCollectionResourceMappingUnitTests {
@Test
public void doesNotExposeRepositoryForPublicDomainTypeIfRepoIsPackageProtected() {
ResourceMapping mapping = new RepositoryCollectionResourceMapping(PackageProtectedRepository.class);
ResourceMapping mapping = getResourceMappingFor(PackageProtectedRepository.class);
assertThat(mapping.isExported(), is(false));
}
/**
* @see DATAREST-229
*/
@Test
public void detectsPagingRepository() {
assertThat(getResourceMappingFor(PersonRepository.class).isPagingResource(), is(true));
}
private static CollectionResourceMapping getResourceMappingFor(Class<?> repositoryInterface) {
RepositoryMetadata metadata = new DefaultRepositoryMetadata(repositoryInterface);
return new RepositoryCollectionResourceMapping(metadata);
}
public static class Person {}
@RestResource(path = "bar", rel = "foo", exported = false)
static class AnnotatedPerson {}
public interface PersonRepository extends Repository<Person, Long> {}
public interface PersonRepository extends Repository<Person, Long> {
Page<Person> findAll(Pageable pageable);
}
interface AnnotatedPersonRepository extends Repository<AnnotatedPerson, Long> {}
@@ -88,5 +105,5 @@ public class RepositoryCollectionResourceMappingUnitTests {
public static class PublicClass {}
static interface PackageProtectedRepository extends Repository<PublicClass, Long> {}
interface PackageProtectedRepository extends Repository<PublicClass, Long> {}
}

View File

@@ -21,7 +21,11 @@ import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.junit.Test;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.RepositoryMetadata;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.Path;
import org.springframework.data.rest.core.annotation.RestResource;
@@ -33,7 +37,8 @@ import org.springframework.data.rest.core.annotation.RestResource;
*/
public class RepositoryMethodResourceMappingUnitTests {
RepositoryCollectionResourceMapping resourceMapping = new RepositoryCollectionResourceMapping(PersonRepository.class);
RepositoryMetadata metadata = new DefaultRepositoryMetadata(PersonRepository.class);
RepositoryCollectionResourceMapping resourceMapping = new RepositoryCollectionResourceMapping(metadata);
@Test
public void defaultsMappingToMethodName() throws Exception {
@@ -78,6 +83,18 @@ public class RepositoryMethodResourceMappingUnitTests {
assertThat(mapping.getParameterNames(), hasItem("firstname"));
}
/**
* @see DATAREST-229
*/
@Test
public void considersPagingFinderAPagingResource() throws Exception {
Method method = PersonRepository.class.getMethod("findByEmailAddress", String.class, Pageable.class);
MethodResourceMapping mapping = new RepositoryMethodResourceMapping(method, resourceMapping);
assertThat(mapping.isPagingResource(), is(true));
}
static class Person {}
interface PersonRepository extends Repository<Person, Long> {
@@ -89,5 +106,8 @@ public class RepositoryMethodResourceMappingUnitTests {
@RestResource(path = "foo")
Iterable<Person> findByEmailAddress(String email);
@RestResource(path = "fooPaged")
Page<Person> findByEmailAddress(String email, Pageable pageable);
}
}