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.
This commit is contained in:
Oliver Gierke
2015-04-11 15:54:14 +02:00
parent 4bb7612094
commit ba9f1b36ab
4 changed files with 83 additions and 2 deletions

View File

@@ -144,7 +144,7 @@ class RepositorySearchController extends AbstractRepositoryRestController {
throw new ResourceNotFoundException();
}
RepositorySearchesResource result = new RepositorySearchesResource();
RepositorySearchesResource result = new RepositorySearchesResource(resourceInformation.getDomainType());
result.add(queryMethodLinks);
return result;

View File

@@ -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;
}
}

View File

@@ -169,4 +169,15 @@ public class RepositorySearchControllerIntegrationTests extends AbstractControll
assertAllowHeaders(response, HttpMethod.GET);
}
/**
* @see DATAREST-515
*/
@Test
public void repositorySearchResourceExposesDomainType() {
RepositorySearchesResource searches = controller.listSearches(getResourceInformation(Person.class));
assertThat(searches.getDomainType(), is(typeCompatibleWith(Person.class)));
}
}

View File

@@ -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)));
}
}