diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/PersistentEntityProjector.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/PersistentEntityProjector.java index 5530f17c4..b8e2cc650 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/PersistentEntityProjector.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/PersistentEntityProjector.java @@ -61,6 +61,6 @@ public class PersistentEntityProjector implements Projector { } Class projectionType = projectionDefinitions.getProjectionType(source.getClass(), projection); - return factory.createProjection(source, projectionType); + return projectionType == null ? source : factory.createProjection(source, projectionType); } } diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/Projector.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/Projector.java index 90bc90445..271640677 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/Projector.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/support/Projector.java @@ -16,23 +16,18 @@ package org.springframework.data.rest.webmvc.support; /** + * Interface for a component being able to create projections for objects. + * * @author Oliver Gierke */ public interface Projector { + /** + * Returns the projection object for the given source. This may result in the same object being returned or a + * completely different acting as projection for the source. + * + * @param source must not be {@literal null}. + * @return + */ public Object project(Object source); - - enum NoOpProjector implements Projector { - - INSTANCE; - - /* - * (non-Javadoc) - * @see org.springframework.data.rest.webmvc.support.Projector#project(java.lang.Object) - */ - @Override - public Object project(Object source) { - return source; - } - } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractControllerIntegrationTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractControllerIntegrationTests.java index ba5b8a553..d96869552 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractControllerIntegrationTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/AbstractControllerIntegrationTests.java @@ -53,7 +53,12 @@ public abstract class AbstractControllerIntegrationTests { @Bean public PersistentEntityResourceAssembler persistentEntityResourceAssembler() { - return new PersistentEntityResourceAssembler(repositories(), entityLinks(), Projector.NoOpProjector.INSTANCE); + return new PersistentEntityResourceAssembler(repositories(), entityLinks(), new Projector() { + @Override + public Object project(Object source) { + return source; + } + }); } } diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/PersistentEntityProjectorUnitTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/PersistentEntityProjectorUnitTests.java new file mode 100644 index 000000000..ec10beb02 --- /dev/null +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/support/PersistentEntityProjectorUnitTests.java @@ -0,0 +1,76 @@ +/* + * Copyright 2014 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.support; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.runners.MockitoJUnitRunner; +import org.springframework.data.rest.core.config.ProjectionDefinitionConfiguration; +import org.springframework.data.rest.core.projection.ProjectionFactory; + +/** + * Unit tests for {@link PersistentEntityProjector}. + * + * @author Oliver Gierke + */ +@RunWith(MockitoJUnitRunner.class) +public class PersistentEntityProjectorUnitTests { + + @Mock ProjectionFactory factory; + ProjectionDefinitionConfiguration configuration; + + @Before + public void setUp() { + configuration = new ProjectionDefinitionConfiguration(); + } + + /** + * @see DATAREST-221 + */ + @Test + public void returnsObjectAsIfNoProjectionTypeFound() { + + Projector projector = new PersistentEntityProjector(configuration, factory, "sample"); + + Object object = new Object(); + assertThat(projector.project(object), is(object)); + } + + /** + * @see DATAREST-221 + */ + @Test + public void invokesProjectionFactoryIfProjectionFound() { + + configuration.addProjection(Sample.class, Object.class); + + Projector projector = new PersistentEntityProjector(configuration, factory, "sample"); + Object source = new Object(); + projector.project(source); + + verify(factory, times(1)).createProjection(source, Sample.class); + } + + interface Sample { + + } +}