From 72647464c6b6d8509bf0d7255a9dc86cebe3a329 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sat, 11 Apr 2015 15:54:14 +0200 Subject: [PATCH] DATAREST-515 - RepositorySearchesResource now exposes domain type. RepositorySearchesResource now exposes the domain type for whom the searches are listed to allow implementations of ResourceProcessor add links by type more easily. --- .../webmvc/RepositorySearchController.java | 2 +- .../webmvc/RepositorySearchesResource.java | 26 ++++++++++- ...itorySearchControllerIntegrationTests.java | 11 +++++ .../RepositorySearchesResourceUnitTests.java | 46 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchesResourceUnitTests.java diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchController.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchController.java index 2db4bbb6a..f2b0c5bb5 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchController.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchController.java @@ -153,7 +153,7 @@ class RepositorySearchController extends AbstractRepositoryRestController { throw new ResourceNotFoundException(); } - RepositorySearchesResource result = new RepositorySearchesResource(); + RepositorySearchesResource result = new RepositorySearchesResource(resourceInformation.getDomainType()); result.add(queryMethodLinks); return result; diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchesResource.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchesResource.java index 1d8e81589..ad33a0f7c 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchesResource.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/RepositorySearchesResource.java @@ -17,6 +17,7 @@ package org.springframework.data.rest.webmvc; import org.springframework.hateoas.ResourceProcessor; import org.springframework.hateoas.ResourceSupport; +import org.springframework.util.Assert; /** * A custom {@link ResourceSupport} type to be able to write custom {@link ResourceProcessor}s to add additional links @@ -24,4 +25,27 @@ import org.springframework.hateoas.ResourceSupport; * * @author Oliver Gierke */ -public class RepositorySearchesResource extends ResourceSupport {} +public class RepositorySearchesResource extends ResourceSupport { + + private final Class domainType; + + /** + * Creates a new {@link RepositorySearchesResource} for the given domain type. + * + * @param domainType must not be {@literal null}. + */ + RepositorySearchesResource(Class domainType) { + + Assert.notNull(domainType, "Domain type must not be null!"); + this.domainType = domainType; + } + + /** + * Returns the domain type for which the resource lists searches. + * + * @return + */ + public Class getDomainType() { + return domainType; + } +} diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchControllerIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchControllerIntegrationTests.java index 7d523f9dc..436951aea 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchControllerIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchControllerIntegrationTests.java @@ -193,4 +193,15 @@ public class RepositorySearchControllerIntegrationTests extends AbstractControll assertThat(result.getBody(), is(instanceOf(Resources.class))); } + + /** + * @see DATAREST-515 + */ + @Test + public void repositorySearchResourceExposesDomainType() { + + RepositorySearchesResource searches = controller.listSearches(getResourceInformation(Person.class)); + + assertThat(searches.getDomainType(), is(typeCompatibleWith(Person.class))); + } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchesResourceUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchesResourceUnitTests.java new file mode 100644 index 000000000..78030fb72 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/RepositorySearchesResourceUnitTests.java @@ -0,0 +1,46 @@ +/* + * Copyright 2015 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.rest.webmvc; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; + +/** + * Unit tests for {@link RepositorySearchesResource} + * + * @author Oliver Gierke + * @soundtrack Bakkushan - Gefahr (Bakkushan) + */ +public class RepositorySearchesResourceUnitTests { + + /** + * @see DATAREST-515 + */ + @Test(expected = IllegalArgumentException.class) + public void rejectsNullDomainType() { + new RepositorySearchesResource(null); + } + + /** + * @see DATAREST-515 + */ + @Test + public void returnsConfiguredDomainType() { + assertThat(new RepositorySearchesResource(String.class).getDomainType(), is(typeCompatibleWith(String.class))); + } +}