diff --git a/src/main/java/org/springframework/hateoas/ResourceAssembler.java b/src/main/java/org/springframework/hateoas/ResourceAssembler.java index a5b50c39..2b57a84c 100755 --- a/src/main/java/org/springframework/hateoas/ResourceAssembler.java +++ b/src/main/java/org/springframework/hateoas/ResourceAssembler.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 the original author or authors. + * Copyright 2012-2018 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,7 +19,7 @@ import java.util.ArrayList; import java.util.List; /** - * Interface for components that convert a domain type into an {@link ResourceSupport}. + * Interface for components that convert a domain type into a {@link ResourceSupport}. * * @author Oliver Gierke * @author Greg Turnquist @@ -27,7 +27,7 @@ import java.util.List; public interface ResourceAssembler { /** - * Converts the given entity into an {@link ResourceSupport}. + * Converts the given entity into a {@code D}, which extends {@link ResourceSupport}. * * @param entity * @return @@ -38,10 +38,10 @@ public interface ResourceAssembler { * Converts an {@link Iterable} or {@code T}s into an {@link Iterable} of {@link ResourceSupport} and wraps * them in a {@link Resources} instance. * - * @param entities - * @return + * @param entities must not be {@literal null}. + * @return {@link Resources} containing {@code D}. */ - default Resources toResources(List entities) { + default Resources toResources(Iterable entities) { List resources = new ArrayList<>(); for (T entity : entities) { diff --git a/src/main/java/org/springframework/hateoas/SimpleResourceAssembler.java b/src/main/java/org/springframework/hateoas/SimpleResourceAssembler.java new file mode 100644 index 00000000..5f0db8b5 --- /dev/null +++ b/src/main/java/org/springframework/hateoas/SimpleResourceAssembler.java @@ -0,0 +1,80 @@ +/* + * Copyright 2018 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.hateoas; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.util.Assert; + +/** + * A {@link ResourceAssembler} based purely on the domain type, using {@code Resource} as the enclosing + * "resource" type. + * + * @author Greg Turnquist + * @since 1.0 + */ +public interface SimpleResourceAssembler extends ResourceAssembler> { + + /** + * Converts the given entity into a {@link Resource}. + * + * @param entity + * @return + */ + default Resource toResource(T entity) { + + Resource resource = new Resource<>(entity); + addLinks(resource); + return resource; + } + + /** + * Define links to add to every individual {@link Resource}. + * + * @param resource + */ + void addLinks(Resource resource); + + /** + * Converts all given entities into resources and wraps the collection as a resource as well. + * + * @see #toResource(Object) + * @param entities must not be {@literal null}. + * @return {@link Resources} containing {@link Resource} of {@code T}. + */ + @Override + default Resources> toResources(Iterable entities) { + + Assert.notNull(entities, "entities must not be null!"); + List> resourceList = new ArrayList<>(); + + for (T entity : entities) { + resourceList.add(toResource(entity)); + } + + Resources> resources = new Resources<>(resourceList); + addLinks(resources); + return resources; + } + + /** + * Define links to add to the {@link Resources} collection. + * + * @param resources + */ + void addLinks(Resources> resources); +} diff --git a/src/main/java/org/springframework/hateoas/mvc/ResourceAssemblerSupport.java b/src/main/java/org/springframework/hateoas/mvc/ResourceAssemblerSupport.java index 126782d2..b75e3fd6 100755 --- a/src/main/java/org/springframework/hateoas/mvc/ResourceAssemblerSupport.java +++ b/src/main/java/org/springframework/hateoas/mvc/ResourceAssemblerSupport.java @@ -54,7 +54,7 @@ public abstract class ResourceAssemblerSupport imp } @Override - public Resources toResources(List entities) { + public Resources toResources(Iterable entities) { return this.map(entities).toResources(); } diff --git a/src/test/java/org/springframework/hateoas/SimpleResourceAssemblerTest.java b/src/test/java/org/springframework/hateoas/SimpleResourceAssemblerTest.java new file mode 100644 index 00000000..ed090d83 --- /dev/null +++ b/src/test/java/org/springframework/hateoas/SimpleResourceAssemblerTest.java @@ -0,0 +1,120 @@ +/* + * Copyright 2018 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.hateoas; + +import static org.assertj.core.api.Assertions.*; + +import lombok.Data; + +import java.util.Collections; + +import org.junit.Test; + +/** + * @author Greg Turnquist + */ +public class SimpleResourceAssemblerTest { + + /** + * @see #572 + */ + @Test + public void convertingToResourceShouldWork() { + + TestResourceAssembler assembler = new TestResourceAssembler(); + Resource resource = assembler.toResource(new Employee("Frodo")); + + assertThat(resource.getContent().getName()).isEqualTo("Frodo"); + assertThat(resource.getLinks()).isEmpty(); + } + + /** + * @see #572 + */ + @Test + public void convertingToResourcesShouldWork() { + + TestResourceAssembler assembler = new TestResourceAssembler(); + Resources> resources = assembler.toResources(Collections.singletonList(new Employee("Frodo"))); + + assertThat(resources.getContent()).hasSize(1); + assertThat(resources.getContent()).containsExactly(new Resource<>(new Employee("Frodo"))); + assertThat(resources.getLinks()).isEmpty(); + + assertThat(resources.getContent().iterator().next()).isEqualTo(new Resource<>(new Employee("Frodo"))); + } + + /** + * @see #572 + */ + @Test + public void convertingToResourceWithCustomLinksShouldWork() { + + ResourceAssemblerWithCustomLink assembler = new ResourceAssemblerWithCustomLink(); + Resource resource = assembler.toResource(new Employee("Frodo")); + + assertThat(resource.getContent().getName()).isEqualTo("Frodo"); + assertThat(resource.getLinks()).hasSize(1); + assertThat(resource.getLinks()).containsExactly(new Link("/employees").withRel("employees")); + } + + /** + * @see #572 + */ + @Test + public void convertingToResourcesWithCustomLinksShouldWork() { + + ResourceAssemblerWithCustomLink assembler = new ResourceAssemblerWithCustomLink(); + Resources> resources = assembler.toResources(Collections.singletonList(new Employee("Frodo"))); + + assertThat(resources.getContent()).hasSize(1); + assertThat(resources.getContent()) + .containsExactly(new Resource<>(new Employee("Frodo"), new Link("/employees").withRel("employees"))); + assertThat(resources.getLinks()).isEmpty(); + + assertThat(resources.getContent().iterator().next()) + .isEqualTo(new Resource<>(new Employee("Frodo"), new Link("/employees").withRel("employees"))); + } + + class TestResourceAssembler implements SimpleResourceAssembler { + + @Override + public void addLinks(Resource resource) { + } + + @Override + public void addLinks(Resources> resources) { + } + } + + class ResourceAssemblerWithCustomLink implements SimpleResourceAssembler { + + @Override + public void addLinks(Resource resource) { + resource.add(new Link("/employees").withRel("employees")); + } + + @Override + public void addLinks(Resources> resources) { + } + } + + @Data + class Employee { + private final String name; + } +} \ No newline at end of file