Skip to content

Commit d89ba43

Browse files
committed
#572 - Create SimpleResourceAssembler for Resource<T>.
Implement a ResourceAssembler based solely on a domain type, returning Resource<T> for the `toResource(obj)` method. Related issues: #416
1 parent a597960 commit d89ba43

File tree

5 files changed

+200
-7
lines changed

5 files changed

+200
-7
lines changed

src/main/java/org/springframework/hateoas/ResourceAssembler.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,15 +19,15 @@
1919
import java.util.List;
2020

2121
/**
22-
* Interface for components that convert a domain type into an {@link ResourceSupport}.
22+
* Interface for components that convert a domain type into a {@link ResourceSupport}.
2323
*
2424
* @author Oliver Gierke
2525
* @author Greg Turnquist
2626
*/
2727
public interface ResourceAssembler<T, D extends ResourceSupport> {
2828

2929
/**
30-
* Converts the given entity into an {@link ResourceSupport}.
30+
* Converts the given entity into a {@code D}, which extends {@link ResourceSupport}.
3131
*
3232
* @param entity
3333
* @return
@@ -38,10 +38,10 @@ public interface ResourceAssembler<T, D extends ResourceSupport> {
3838
* Converts an {@link Iterable} or {@code T}s into an {@link Iterable} of {@link ResourceSupport} and wraps
3939
* them in a {@link Resources} instance.
4040
*
41-
* @param entities
42-
* @return
41+
* @param entities must not be {@literal null}.
42+
* @return {@link Resources} containing {@code D}.
4343
*/
44-
default Resources<D> toResources(List<T> entities) {
44+
default Resources<D> toResources(Iterable<? extends T> entities) {
4545

4646
List<D> resources = new ArrayList<>();
4747
for (T entity : entities) {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2018 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;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
import org.springframework.util.Assert;
22+
23+
/**
24+
* A {@link ResourceAssembler} based purely on the domain type, using {@code Resource<T>} as the enclosing
25+
* "resource" type.
26+
*
27+
* @author Greg Turnquist
28+
* @since 1.0
29+
*/
30+
public interface SimpleResourceAssembler<T> extends ResourceAssembler<T, Resource<T>> {
31+
32+
/**
33+
* Converts the given entity into a {@link Resource}.
34+
*
35+
* @param entity
36+
* @return
37+
*/
38+
default Resource<T> toResource(T entity) {
39+
40+
Resource<T> resource = new Resource<>(entity);
41+
addLinks(resource);
42+
return resource;
43+
}
44+
45+
/**
46+
* Define links to add to every individual {@link Resource}.
47+
*
48+
* @param resource
49+
*/
50+
default void addLinks(Resource<T> resource) {
51+
// By default, add no links.
52+
}
53+
54+
/**
55+
* Converts all given entities into resources and wraps the collection as a resource as well.
56+
*
57+
* @see #toResource(Object)
58+
* @param entities must not be {@literal null}.
59+
* @return {@link Resources} containing {@link Resource} of {@code T}.
60+
*/
61+
@Override
62+
default Resources<Resource<T>> toResources(Iterable<? extends T> entities) {
63+
64+
Assert.notNull(entities, "entities must not be null!");
65+
List<Resource<T>> resourceList = new ArrayList<>();
66+
67+
for (T entity : entities) {
68+
resourceList.add(toResource(entity));
69+
}
70+
71+
Resources<Resource<T>> resources = new Resources<>(resourceList);
72+
addLinks(resources);
73+
return resources;
74+
}
75+
76+
/**
77+
* Define links to add to the {@link Resources} collection.
78+
*
79+
* @param resources
80+
*/
81+
default void addLinks(Resources<Resource<T>> resources) {
82+
// By default, add no links.
83+
}
84+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public ResourceAssemblerSupport(Class<?> controllerClass, Class<D> resourceType)
5454
}
5555

5656
@Override
57-
public Resources<D> toResources(List<T> entities) {
57+
public Resources<D> toResources(Iterable<? extends T> entities) {
5858
return this.map(entities).toResources();
5959
}
6060

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2018 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+
17+
package org.springframework.hateoas;
18+
19+
import static org.assertj.core.api.Assertions.*;
20+
21+
import lombok.Data;
22+
23+
import java.util.Collections;
24+
25+
import org.junit.Test;
26+
27+
/**
28+
* @author Greg Turnquist
29+
*/
30+
public class SimpleResourceAssemblerTest {
31+
32+
/**
33+
* @see #572
34+
*/
35+
@Test
36+
public void convertingToResourceShouldWork() {
37+
38+
TestResourceAssembler assembler = new TestResourceAssembler();
39+
Resource<Employee> resource = assembler.toResource(new Employee("Frodo"));
40+
41+
assertThat(resource.getContent().getName()).isEqualTo("Frodo");
42+
assertThat(resource.getLinks()).isEmpty();
43+
}
44+
45+
/**
46+
* @see #572
47+
*/
48+
@Test
49+
public void convertingToResourcesShouldWork() {
50+
51+
TestResourceAssembler assembler = new TestResourceAssembler();
52+
Resources<Resource<Employee>> resources = assembler.toResources(Collections.singletonList(new Employee("Frodo")));
53+
54+
assertThat(resources.getContent()).hasSize(1);
55+
assertThat(resources.getContent()).containsExactly(new Resource<>(new Employee("Frodo")));
56+
assertThat(resources.getLinks()).isEmpty();
57+
58+
assertThat(resources.getContent().iterator().next()).isEqualTo(new Resource<>(new Employee("Frodo")));
59+
}
60+
61+
/**
62+
* @see #572
63+
*/
64+
@Test
65+
public void convertingToResourceWithCustomLinksShouldWork() {
66+
67+
ResourceAssemblerWithCustomLink assembler = new ResourceAssemblerWithCustomLink();
68+
Resource<Employee> resource = assembler.toResource(new Employee("Frodo"));
69+
70+
assertThat(resource.getContent().getName()).isEqualTo("Frodo");
71+
assertThat(resource.getLinks()).hasSize(1);
72+
assertThat(resource.getLinks()).containsExactly(new Link("/employees").withRel("employees"));
73+
}
74+
75+
/**
76+
* @see #572
77+
*/
78+
@Test
79+
public void convertingToResourcesWithCustomLinksShouldWork() {
80+
81+
ResourceAssemblerWithCustomLink assembler = new ResourceAssemblerWithCustomLink();
82+
Resources<Resource<Employee>> resources = assembler.toResources(Collections.singletonList(new Employee("Frodo")));
83+
84+
assertThat(resources.getContent()).hasSize(1);
85+
assertThat(resources.getContent())
86+
.containsExactly(new Resource<>(new Employee("Frodo"), new Link("/employees").withRel("employees")));
87+
assertThat(resources.getLinks()).isEmpty();
88+
89+
assertThat(resources.getContent().iterator().next())
90+
.isEqualTo(new Resource<>(new Employee("Frodo"), new Link("/employees").withRel("employees")));
91+
}
92+
93+
94+
class TestResourceAssembler implements SimpleResourceAssembler<Employee> {}
95+
96+
class ResourceAssemblerWithCustomLink implements SimpleResourceAssembler<Employee> {
97+
98+
@Override
99+
public void addLinks(Resource<Employee> resource) {
100+
resource.add(new Link("/employees").withRel("employees"));
101+
}
102+
}
103+
104+
@Data
105+
class Employee {
106+
private final String name;
107+
}
108+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.junit.Before;
2626
import org.junit.Test;
27+
2728
import org.springframework.hateoas.Identifiable;
2829
import org.springframework.hateoas.Link;
2930
import org.springframework.hateoas.LinkBuilder;

0 commit comments

Comments
 (0)