summaryrefslogtreecommitdiffstats
path: root/cps-rest/src
diff options
context:
space:
mode:
authorputhuparambil.aditya <aditya.puthuparambil@bell.ca>2020-12-14 15:46:30 +0000
committerputhuparambil.aditya <aditya.puthuparambil@bell.ca>2020-12-16 11:49:42 +0000
commit008c0adc3712ba0aee6410e8cd7efc61f4cbeeac (patch)
tree255b28e757eecda25bb900d4b041902787a18d24 /cps-rest/src
parentc040bcb4ae61177b1aa691f059d19c96c6cfff7b (diff)
Few Sonar Fixes
Add spock-spring testing Issue-ID: CPS-89 Signed-off-by: puthuparambil.aditya <aditya.puthuparambil@bell.ca> Change-Id: I1e12fb56916e2123fc1d29bdf1524accbb9eeffd
Diffstat (limited to 'cps-rest/src')
-rw-r--r--cps-rest/src/main/java/org/onap/cps/rest/exceptions/CpsRestExceptionHandler.java3
-rw-r--r--cps-rest/src/test/groovy/org/onap/cps/rest/exceptions/CpsRestExceptionHandlerSpec.groovy31
2 files changed, 21 insertions, 13 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 00e72a189e..54f3db4a08 100644
--- 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
@@ -37,6 +37,9 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice(assignableTypes = {CpsRestController.class})
public class CpsRestExceptionHandler {
+ private CpsRestExceptionHandler() {
+ }
+
/**
* Default exception handler.
*
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 e427c6063e..7a777bf380 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
@@ -20,14 +20,18 @@
package org.onap.cps.rest.exceptions
import groovy.json.JsonSlurper
+import org.modelmapper.ModelMapper
import org.onap.cps.api.CpsAdminService
import org.onap.cps.spi.exceptions.AnchorAlreadyDefinedException
import org.onap.cps.spi.exceptions.CpsException
import org.onap.cps.spi.exceptions.DataValidationException
import org.onap.cps.spi.exceptions.NotFoundInDataspaceException
import org.onap.cps.spi.exceptions.ModelValidationException
-import org.onap.cps.rest.controller.CpsRestController
import org.onap.cps.spi.exceptions.SchemaSetAlreadyDefinedException
+import org.spockframework.spring.SpringBean
+import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
+import org.springframework.test.web.servlet.MockMvc
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll
@@ -36,10 +40,19 @@ import static org.springframework.http.HttpStatus.BAD_REQUEST
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.setup.MockMvcBuilders.standaloneSetup
+@WebMvcTest
class CpsRestExceptionHandlerSpec extends Specification {
+ @SpringBean
+ CpsAdminService mockCpsAdminService = Mock()
+
+ @SpringBean
+ ModelMapper modelMapper = Mock()
+
+ @Autowired
+ MockMvc mvc
+
@Shared
def errorMessage = 'some error message'
@Shared
@@ -49,14 +62,6 @@ class CpsRestExceptionHandlerSpec extends Specification {
@Shared
def existingObjectName = 'MyAdminObject'
- def cpsRestController = new CpsRestController()
- def mockCpsAdminService = Mock(CpsAdminService.class)
- def objectUnderTest = new CpsRestExceptionHandler()
- def mockMvc = standaloneSetup(cpsRestController).setControllerAdvice(objectUnderTest).build()
-
- def setup() {
- cpsRestController.cpsAdminService = mockCpsAdminService
- }
def 'Get request with runtime exception returns HTTP Status Internal Server Error'() {
@@ -134,15 +139,15 @@ class CpsRestExceptionHandlerSpec extends Specification {
}
def performTestRequest() {
- return mockMvc.perform(get('/v1/dataspaces/dataspace-name/anchors')).andReturn().response
+ return mvc.perform(get('/v1/dataspaces/dataspace-name/anchors')).andReturn().response
}
- void assertTestResponse(response, expectedStatus, expectedErrorMessage, expectedErrorDetails) {
+ void assertTestResponse(response, expectedStatus,
+ expectedErrorMessage, expectedErrorDetails) {
assert response.status == expectedStatus.value()
def content = new JsonSlurper().parseText(response.contentAsString)
assert content['status'] == expectedStatus.toString()
assert content['message'] == expectedErrorMessage
assert expectedErrorDetails == null || content['details'] == expectedErrorDetails
}
-
}