DATAREST-34 - Polishing.
Moved decision logic on whether to return response bodies int RepositoryRestConfig to ease testability of the controller. Added more unit tests and simplified the controller integration tests accordingly. Had to deactivate some Cassandra related tests as they don't seem to handle nulling of properties correctly. Changed configuration to rely on the presence of the Accept header by default. Deprecated parameterless isReturnBodyFor…(…) methods in favor of the ones taking the Accept header to avoid the null checks on the calling side. Polished JavaDoc for the isReturnBodyOn(Create|Update) methods. Polished formatting in RepositoryEntityController. Original pull request: #167.
This commit is contained in:
@@ -28,8 +28,11 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Spring Data REST configuration options.
|
||||
*
|
||||
* @author Jon Brisbin
|
||||
* @author Oliver Gierke
|
||||
* @author Jeremy Rickard
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
public class RepositoryRestConfiguration {
|
||||
@@ -46,8 +49,8 @@ public class RepositoryRestConfiguration {
|
||||
private String sortParamName = "sort";
|
||||
private MediaType defaultMediaType = MediaTypes.HAL_JSON;
|
||||
private boolean useHalAsDefaultJsonMediaType = true;
|
||||
private Boolean returnBodyOnCreate = Boolean.FALSE;
|
||||
private Boolean returnBodyOnUpdate = Boolean.FALSE;
|
||||
private Boolean returnBodyOnCreate = null;
|
||||
private Boolean returnBodyOnUpdate = null;
|
||||
private List<Class<?>> exposeIdsFor = new ArrayList<Class<?>>();
|
||||
private ResourceMappingConfiguration domainMappings = new ResourceMappingConfiguration();
|
||||
private ResourceMappingConfiguration repoMappings = new ResourceMappingConfiguration();
|
||||
@@ -293,43 +296,85 @@ public class RepositoryRestConfiguration {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to activate returning response bodies for all {@code PUT} and {@code POST} requests, i.e. both
|
||||
* creating and updating entities.
|
||||
*
|
||||
* @param returnBody can be {@literal null}, expressing the decision shall be derived from the presence of an
|
||||
* {@code Accept} header in the request.
|
||||
* @return
|
||||
*/
|
||||
public RepositoryRestConfiguration setReturnBodyForPutAndPost(Boolean returnBody) {
|
||||
|
||||
setReturnBodyOnCreate(returnBody);
|
||||
setReturnBodyOnUpdate(returnBody);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to return a response body after creating an entity.
|
||||
*
|
||||
* @return {@link java.lang.Boolean#TRUE} to return a body on create, {@link java.lang.Boolean#FALSE} otherwise.
|
||||
* If {@literal null}, defer to HTTP Accept header
|
||||
* @return {@link java.lang.Boolean#TRUE} to enforce returning a body on create, {@link java.lang.Boolean#FALSE}
|
||||
* otherwise. If {@literal null} and an {@code Accept} header present in the request will cause a body being
|
||||
* returned. If the {@code Accept} header is not present, no body will be rendered.
|
||||
* @deprecated use {@link #returnBodyOnCreate(String)}
|
||||
*/
|
||||
@Deprecated
|
||||
public Boolean isReturnBodyOnCreate() {
|
||||
return returnBodyOnCreate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to return a response body after creating an entity considering the given accept header.
|
||||
*
|
||||
* @param acceptHeader can be {@literal null} or empty.
|
||||
* @return
|
||||
*/
|
||||
public boolean returnBodyOnCreate(String acceptHeader) {
|
||||
return returnBodyOnCreate == null ? StringUtils.hasText(acceptHeader) : returnBodyOnCreate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to return a response body after creating an entity.
|
||||
*
|
||||
* @param returnBodyOnCreate {@link java.lang.Boolean#TRUE} to return a body on create, {@link java.lang.Boolean#FALSE} otherwise.
|
||||
* If {@literal null}, defer to HTTP Accept header
|
||||
* @param returnBody can be {@literal null}, expressing the decision shall be derived from the presence of an
|
||||
* {@code Accept} header in the request.
|
||||
* @return {@literal this}
|
||||
*/
|
||||
public RepositoryRestConfiguration setReturnBodyOnCreate(Boolean returnBodyOnCreate) {
|
||||
this.returnBodyOnCreate = returnBodyOnCreate;
|
||||
public RepositoryRestConfiguration setReturnBodyOnCreate(Boolean returnBody) {
|
||||
this.returnBodyOnCreate = returnBody;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to return a response body after updating an entity.
|
||||
*
|
||||
* @return {@link java.lang.Boolean#TRUE} to return a body on update, {@link java.lang.Boolean#FALSE} otherwise.
|
||||
* If {@literal null}, defer to HTTP Accept header
|
||||
* @return {@link java.lang.Boolean#TRUE} to enforce returning a body on create, {@link java.lang.Boolean#FALSE}
|
||||
* otherwise. If {@literal null} and an {@code Accept} header present in the request will cause a body being
|
||||
* returned. If the {@code Accept} header is not present, no body will be rendered.
|
||||
* @deprecated use {@link #returnBodyOnUpdate(String)}
|
||||
*/
|
||||
@Deprecated
|
||||
public Boolean isReturnBodyOnUpdate() {
|
||||
return returnBodyOnUpdate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether to return a response body after updating an entity considering the given accept header.
|
||||
*
|
||||
* @param acceptHeader can be {@literal null} or empty.
|
||||
* @return
|
||||
*/
|
||||
public boolean returnBodyOnUpdate(String acceptHeader) {
|
||||
return returnBodyOnUpdate == null ? StringUtils.hasText(acceptHeader) : returnBodyOnUpdate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to return a response body after updating an entity.
|
||||
*
|
||||
* @param returnBodyOnUpdate {@link java.lang.Boolean#TRUE} to return a body on update, {@link java.lang.Boolean#FALSE} otherwise.
|
||||
* If {@literal null}, defer to HTTP Accept header
|
||||
* @param returnBody can be {@literal null}, expressing the decision shall be derived from the presence of an
|
||||
* {@code Accept} header in the request.
|
||||
* @return {@literal this}
|
||||
*/
|
||||
public RepositoryRestConfiguration setReturnBodyOnUpdate(Boolean returnBodyOnUpdate) {
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright 2015 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.core;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RepositoryRestConfiguration}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @soundtrack Adam F - Circles (Colors)
|
||||
*/
|
||||
public class RepositoryRestConfigurationUnitTests {
|
||||
|
||||
/**
|
||||
* @see DATAREST-34
|
||||
*/
|
||||
@Test
|
||||
public void returnsBodiesIfAcceptHeaderPresentByDefault() {
|
||||
|
||||
RepositoryRestConfiguration configuration = new RepositoryRestConfiguration();
|
||||
|
||||
assertThat(configuration.returnBodyOnCreate(MediaType.APPLICATION_JSON_VALUE), is(true));
|
||||
assertThat(configuration.returnBodyOnUpdate(MediaType.APPLICATION_JSON_VALUE), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-34
|
||||
*/
|
||||
@Test
|
||||
public void doesNotReturnBodiesIfNoAcceptHeaderPresentByDefault() {
|
||||
|
||||
RepositoryRestConfiguration configuration = new RepositoryRestConfiguration();
|
||||
|
||||
assertThat(configuration.returnBodyOnCreate(null), is(false));
|
||||
assertThat(configuration.returnBodyOnUpdate(null), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-34
|
||||
*/
|
||||
@Test
|
||||
public void doesNotReturnBodiesIfEmptyAcceptHeaderPresentByDefault() {
|
||||
|
||||
RepositoryRestConfiguration configuration = new RepositoryRestConfiguration();
|
||||
|
||||
assertThat(configuration.returnBodyOnCreate(""), is(false));
|
||||
assertThat(configuration.returnBodyOnUpdate(""), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-34
|
||||
*/
|
||||
@Test
|
||||
public void doesNotReturnBodyForUpdateIfExplicitlyDeactivated() {
|
||||
|
||||
RepositoryRestConfiguration configuration = new RepositoryRestConfiguration();
|
||||
configuration.setReturnBodyOnUpdate(false);
|
||||
|
||||
assertThat(configuration.returnBodyOnUpdate(null), is(false));
|
||||
assertThat(configuration.returnBodyOnUpdate(""), is(false));
|
||||
assertThat(configuration.returnBodyOnUpdate(MediaType.APPLICATION_JSON_VALUE), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-34
|
||||
*/
|
||||
@Test
|
||||
public void doesNotReturnBodyForCreateIfExplicitlyDeactivated() {
|
||||
|
||||
RepositoryRestConfiguration configuration = new RepositoryRestConfiguration();
|
||||
configuration.setReturnBodyOnCreate(false);
|
||||
|
||||
assertThat(configuration.returnBodyOnCreate(null), is(false));
|
||||
assertThat(configuration.returnBodyOnCreate(""), is(false));
|
||||
assertThat(configuration.returnBodyOnCreate(MediaType.APPLICATION_JSON_VALUE), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-34
|
||||
*/
|
||||
@Test
|
||||
public void returnsBodyForUpdateIfExplicitlyActivated() {
|
||||
|
||||
RepositoryRestConfiguration configuration = new RepositoryRestConfiguration();
|
||||
configuration.setReturnBodyOnUpdate(true);
|
||||
|
||||
assertThat(configuration.returnBodyOnUpdate(null), is(true));
|
||||
assertThat(configuration.returnBodyOnUpdate(""), is(true));
|
||||
assertThat(configuration.returnBodyOnUpdate(MediaType.APPLICATION_JSON_VALUE), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-34
|
||||
*/
|
||||
@Test
|
||||
public void returnsBodyForCreateIfExplicitlyActivated() {
|
||||
|
||||
RepositoryRestConfiguration configuration = new RepositoryRestConfiguration();
|
||||
configuration.setReturnBodyOnCreate(true);
|
||||
|
||||
assertThat(configuration.returnBodyOnCreate(null), is(true));
|
||||
assertThat(configuration.returnBodyOnCreate(""), is(true));
|
||||
assertThat(configuration.returnBodyOnCreate(MediaType.APPLICATION_JSON_VALUE), is(true));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user