Skip to content

Commit 6dda30d

Browse files
committed
#445 - Make ResourceAssemblerSupport extensible
Adds getters to allow implementers access to controllerClass and resourceType. Related issues: #416
1 parent 4807ae0 commit 6dda30d

File tree

3 files changed

+121
-6
lines changed

3 files changed

+121
-6
lines changed

src/main/java/org/springframework/hateoas/mvc/ResourceAssemblerSupport.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import org.springframework.beans.BeanUtils;
2424
import org.springframework.hateoas.ResourceAssembler;
2525
import org.springframework.hateoas.ResourceSupport;
26+
import org.springframework.hateoas.Resources;
2627
import org.springframework.util.Assert;
2728

2829
/**
2930
* Base class to implement {@link ResourceAssembler}s. Will automate {@link ResourceSupport} instance creation and make
3031
* sure a self-link is always added.
3132
*
3233
* @author Oliver Gierke
34+
* @author Greg Turnquist
3335
*/
3436
public abstract class ResourceAssemblerSupport<T, D extends ResourceSupport> implements ResourceAssembler<T, D> {
3537

@@ -58,7 +60,7 @@ public ResourceAssemblerSupport(Class<?> controllerClass, Class<D> resourceType)
5860
* @param entities must not be {@literal null}.
5961
* @return
6062
*/
61-
public List<D> toResources(Iterable<? extends T> entities) {
63+
public Resources<D> toResources(Iterable<? extends T> entities) {
6264

6365
Assert.notNull(entities, "Entities must not be null!");
6466
List<D> result = new ArrayList<D>();
@@ -67,7 +69,7 @@ public List<D> toResources(Iterable<? extends T> entities) {
6769
result.add(toResource(entity));
6870
}
6971

70-
return result;
72+
return new Resources<D>(result);
7173
}
7274

7375
/**
@@ -87,7 +89,7 @@ protected D createResourceWithId(Object id, T entity, Object... parameters) {
8789
Assert.notNull(id, "Id must not be null!");
8890

8991
D instance = instantiateResource(entity);
90-
instance.add(linkTo(controllerClass, parameters).slash(id).withSelfRel());
92+
instance.add(linkTo(this.controllerClass, parameters).slash(id).withSelfRel());
9193
return instance;
9294
}
9395

@@ -102,4 +104,12 @@ protected D createResourceWithId(Object id, T entity, Object... parameters) {
102104
protected D instantiateResource(T entity) {
103105
return BeanUtils.instantiateClass(resourceType);
104106
}
107+
108+
public Class<?> getControllerClass() {
109+
return this.controllerClass;
110+
}
111+
112+
public Class<D> getResourceType() {
113+
return this.resourceType;
114+
}
105115
}

src/test/java/org/springframework/hateoas/mvc/IdentifiableResourceAssemblerSupportUnitTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
2121

2222
import java.util.Arrays;
23-
import java.util.List;
2423

2524
import org.junit.Before;
2625
import org.junit.Test;
26+
2727
import org.springframework.hateoas.Identifiable;
2828
import org.springframework.hateoas.Link;
2929
import org.springframework.hateoas.LinkBuilder;
3030
import org.springframework.hateoas.ResourceSupport;
31+
import org.springframework.hateoas.Resources;
3132
import org.springframework.hateoas.TestUtils;
3233
import org.springframework.web.bind.annotation.RequestMapping;
3334

@@ -85,7 +86,7 @@ public void convertsEntitiesToResources() {
8586
Person second = new Person();
8687
second.id = 2L;
8788

88-
List<PersonResource> result = assembler.toResources(Arrays.asList(first, second));
89+
Resources<PersonResource> result = assembler.toResources(Arrays.asList(first, second));
8990

9091
LinkBuilder builder = linkTo(PersonController.class);
9192

@@ -95,7 +96,7 @@ public void convertsEntitiesToResources() {
9596
PersonResource secondResource = new PersonResource();
9697
secondResource.add(builder.slash(1L).withSelfRel());
9798

98-
assertThat(result.size(), is(2));
99+
assertThat(result.getContent().size(), is(2));
99100
assertThat(result, hasItems(firstResource, secondResource));
100101
}
101102

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.hateoas.mvc;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.hamcrest.MatcherAssert.assertThat;
20+
21+
import java.util.Arrays;
22+
23+
import lombok.Data;
24+
import org.junit.Test;
25+
26+
import org.springframework.hateoas.ResourceSupport;
27+
import org.springframework.hateoas.Resources;
28+
import org.springframework.stereotype.Controller;
29+
import org.springframework.web.bind.annotation.GetMapping;
30+
31+
/**
32+
* @author Greg Turnquist
33+
*/
34+
public class ResourceAssemblerSupportTest {
35+
36+
@Test
37+
public void resourceAssemblerSupportShouldConvertEntitiesToResources() {
38+
39+
EmployeeAssembler assembler = new EmployeeAssembler();
40+
Employee employee = new Employee("Frodo");
41+
42+
EmployeeResource resource = assembler.toResource(employee);
43+
44+
assertThat(resource.getEmployee(), is(employee));
45+
}
46+
47+
@Test
48+
public void resourceAssemblerSupportShouldLetYouLookUpItsTypes() {
49+
50+
EmployeeAssembler assembler = new EmployeeAssembler();
51+
52+
assertThat(assembler.getControllerClass().getCanonicalName(), is(EmployeeController.class.getCanonicalName()));
53+
}
54+
55+
@Test
56+
public void resourceAssemblerShouldConvertCollectionOfEntitiesIntoResources() {
57+
58+
EmployeeAssembler assembler = new EmployeeAssembler();
59+
Employee employee1 = new Employee("Frodo");
60+
Employee employee2 = new Employee("Bilbo");
61+
62+
Resources<EmployeeResource> employees = assembler.toResources(Arrays.asList(employee1, employee2));
63+
64+
assertThat(employees, is(notNullValue()));
65+
assertThat(employees.getContent(), hasItems(new EmployeeResource(employee1), new EmployeeResource(employee2)));
66+
assertThat(employees.getLinks().size(), is(0));
67+
}
68+
69+
//
70+
// Support classes
71+
//
72+
73+
class EmployeeAssembler extends ResourceAssemblerSupport<Employee, EmployeeResource> {
74+
75+
public EmployeeAssembler() {
76+
super(EmployeeController.class, EmployeeResource.class);
77+
}
78+
79+
@Override
80+
public EmployeeResource toResource(Employee entity) {
81+
return new EmployeeResource(entity);
82+
}
83+
}
84+
85+
@Data
86+
class Employee {
87+
private final String name;
88+
}
89+
90+
@Data
91+
class EmployeeResource extends ResourceSupport {
92+
private final Employee employee;
93+
}
94+
95+
@Controller
96+
class EmployeeController {
97+
98+
@GetMapping("/employees/{id}")
99+
Employee findOne(String id) {
100+
return new Employee("Frodo");
101+
}
102+
}
103+
104+
}

0 commit comments

Comments
 (0)