aboutsummaryrefslogtreecommitdiffstats
path: root/cps-rest
diff options
context:
space:
mode:
authorRuslan Kashapov <ruslan.kashapov@pantheon.tech>2021-05-19 10:43:39 +0300
committerRuslan Kashapov <ruslan.kashapov@pantheon.tech>2021-05-20 12:42:40 +0300
commitb42a3ced30877da19ce205c66539e4a286ccd458 (patch)
treef0d49cd725ee067ec0b07b7046c6709aeddd6428 /cps-rest
parentbd35b4d03a474ce425040cc4656c7b94a87ef399 (diff)
Response code fix (Bad Request instead of Not Found) when modifying non-existent node.
Issue-ID: CPS-422 Change-Id: I6652f8bcafb9938ce588be3d0a0d2bb1672723b0 Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Diffstat (limited to 'cps-rest')
-rwxr-xr-xcps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java8
-rw-r--r--cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy24
2 files changed, 30 insertions, 2 deletions
diff --git a/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java b/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java
index 2a89ef223..c61400d0c 100755
--- a/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java
+++ b/cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java
@@ -20,6 +20,7 @@
package org.onap.cps.rest.exceptions;
+import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.onap.cps.rest.controller.AdminRestController;
import org.onap.cps.rest.controller.DataRestController;
@@ -34,6 +35,7 @@ import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
import org.onap.cps.spi.exceptions.DataValidationException;
import org.onap.cps.spi.exceptions.ModelValidationException;
import org.onap.cps.spi.exceptions.NotFoundInDataspaceException;
+import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
@@ -65,8 +67,10 @@ public class CpsRestExceptionHandler {
}
@ExceptionHandler({NotFoundInDataspaceException.class, DataNodeNotFoundException.class})
- public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception) {
- return buildErrorResponse(HttpStatus.NOT_FOUND, exception);
+ public static ResponseEntity<Object> handleNotFoundExceptions(final CpsException exception,
+ final HttpServletRequest request) {
+ return buildErrorResponse(HttpMethod.GET.matches(request.getMethod())
+ ? HttpStatus.NOT_FOUND : HttpStatus.BAD_REQUEST, exception);
}
@ExceptionHandler({DataInUseException.class, AlreadyDefinedException.class})
diff --git a/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy b/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy
index 6c14dde46..b4872908f 100644
--- a/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy
+++ b/cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy
@@ -26,6 +26,7 @@ import static org.springframework.http.HttpStatus.CONFLICT
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
import static org.springframework.http.HttpStatus.NOT_FOUND
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
import groovy.json.JsonSlurper
import org.modelmapper.ModelMapper
@@ -37,6 +38,7 @@ import org.onap.cps.spi.exceptions.AlreadyDefinedException
import org.onap.cps.spi.exceptions.CpsException
import org.onap.cps.spi.exceptions.CpsPathException
import org.onap.cps.spi.exceptions.DataInUseException
+import org.onap.cps.spi.exceptions.DataNodeNotFoundException
import org.onap.cps.spi.exceptions.DataValidationException
import org.onap.cps.spi.exceptions.ModelValidationException
import org.onap.cps.spi.exceptions.NotFoundInDataspaceException
@@ -45,6 +47,7 @@ import org.spockframework.spring.SpringBean
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
+import org.springframework.http.MediaType
import org.springframework.test.web.servlet.MockMvc
import spock.lang.Shared
import spock.lang.Specification
@@ -144,9 +147,30 @@ class CpsRestExceptionHandlerSpec extends Specification {
}
/*
+ * NB. This method tests the expected behavior for POST request only;
+ * testing of PUT and PATCH requests omitted due to same NOT 'GET' condition is being used.
+ */
+ def 'Post request with #exceptionThrown.class.simpleName returns HTTP Status Bad Request.'() {
+ given: '#exception is thrown the service indicating data is not found'
+ mockCpsDataService.saveData(_, _, _, _) >> { throw exceptionThrown }
+ when: 'data update request is performed'
+ def response = mvc.perform(
+ post("$basePath/v1/dataspaces/dataspace-name/anchors/anchor-name/nodes")
+ .contentType(MediaType.APPLICATION_JSON)
+ .param('xpath', 'parent node xpath')
+ .content('json data')
+ ).andReturn().response
+ then: 'response code indicates bad input parameters'
+ response.status == BAD_REQUEST.value()
+ where: 'the following exceptions are thrown'
+ exceptionThrown << [new DataNodeNotFoundException('', ''), new NotFoundInDataspaceException('', '')]
+ }
+
+ /*
* NB. The test uses 'get anchors' endpoint and associated service method invocation
* to test the exception handling. The endpoint chosen is not a subject of test.
*/
+
def setupTestException(exception) {
mockCpsAdminService.getAnchors(_) >> { throw exception }
}