DATAREST-221 - Further refinements in Projector.
Prevent NullPointerException in case we don't find a requested projection. We simply return the source object if so. Added unit tests to make sure we don't introduce a regression. Removed NoOpProjector from production code as it is only needed in test cases anyway.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user