From ea70ba51f4dda0ee6e4952d2f00a45b86218c8db Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 28 Aug 2015 10:51:27 +0200 Subject: [PATCH] DATAREST-661 - Fixed non-existant resource detection for PATCH requests. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PATCH requests to non existing resource now correctly return 404 Not Found as PersistentEntityResourceHandlerMethodArgumentResolver.read(…) now correctly throws the ResourceNotFoundException. --- ...ResourceHandlerMethodArgumentResolver.java | 2 +- .../data/rest/webmvc/CommonWebTests.java | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/PersistentEntityResourceHandlerMethodArgumentResolver.java b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/PersistentEntityResourceHandlerMethodArgumentResolver.java index b02e12088..a953392cc 100644 --- a/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/PersistentEntityResourceHandlerMethodArgumentResolver.java +++ b/spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/PersistentEntityResourceHandlerMethodArgumentResolver.java @@ -155,7 +155,7 @@ public class PersistentEntityResourceHandlerMethodArgumentResolver implements Ha if (request.isPatchRequest() && converter instanceof MappingJackson2HttpMessageConverter) { if (objectToUpdate == null) { - new ResourceNotFoundException(); + throw new ResourceNotFoundException(); } ObjectMapper mapper = ((MappingJackson2HttpMessageConverter) converter).getObjectMapper(); diff --git a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java index 0fbc6049f..153da4ab7 100644 --- a/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java +++ b/spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/CommonWebTests.java @@ -21,6 +21,9 @@ import static org.junit.Assume.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import net.minidev.json.JSONArray; + +import java.net.URI; import java.util.List; import java.util.Map; @@ -29,6 +32,7 @@ import net.minidev.json.JSONArray; import org.junit.Test; import org.springframework.hateoas.Link; import org.springframework.hateoas.MediaTypes; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.ResultActions; @@ -207,4 +211,27 @@ public abstract class CommonWebTests extends AbstractWebIntegrationTests { mvc.perform(get("/index.html")).// andExpect(status().isNotFound()); } + + /** + * @see DATAREST-661 + */ + @Test + public void patchToNonExistingResourceReturnsNotFound() throws Exception { + + String rel = expectedRootLinkRels().iterator().next(); + String uri = client.discoverUnique(rel).expand().getHref().concat("/"); + String id = "4711"; + Integer status = null; + + do { + + // Try to find non existing resource + uri = uri.concat(id); + status = mvc.perform(get(URI.create(uri))).andReturn().getResponse().getStatus(); + + } while (status != HttpStatus.NOT_FOUND.value()); + + // PATCH to non-existing resource + mvc.perform(patch(URI.create(uri))).andExpect(status().isNotFound()); + } }