diff options
author | puthuparambil.aditya <aditya.puthuparambil@bell.ca> | 2020-12-14 15:46:30 +0000 |
---|---|---|
committer | puthuparambil.aditya <aditya.puthuparambil@bell.ca> | 2020-12-16 11:49:42 +0000 |
commit | 008c0adc3712ba0aee6410e8cd7efc61f4cbeeac (patch) | |
tree | 255b28e757eecda25bb900d4b041902787a18d24 /cps-rest | |
parent | c040bcb4ae61177b1aa691f059d19c96c6cfff7b (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')
3 files changed, 37 insertions, 24 deletions
diff --git a/cps-rest/pom.xml b/cps-rest/pom.xml index 3a82ca3770..f2f643ac23 100755 --- a/cps-rest/pom.xml +++ b/cps-rest/pom.xml @@ -54,17 +54,6 @@ <groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
</dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- <exclusions>
- <exclusion>
- <groupId>org.junit.vintage</groupId>
- <artifactId>junit-vintage-engine</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
<!-- T E S T D E P E N D E N C I E S -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
@@ -77,10 +66,26 @@ <scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.spockframework</groupId>
+ <artifactId>spock-spring</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-test</artifactId>
+ <scope>test</scope>
+ <exclusions>
+ <exclusion>
+ <groupId>org.junit.vintage</groupId>
+ <artifactId>junit-vintage-engine</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
</dependencies>
<build>
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 } - } |