2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Pantheon.tech
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.rest.exceptions
22 import groovy.json.JsonSlurper
23 import org.onap.cps.api.CpService
24 import org.onap.cps.exceptions.CpsException
25 import org.onap.cps.exceptions.CpsNotFoundException
26 import org.onap.cps.exceptions.CpsValidationException
27 import org.onap.cps.rest.controller.CpsRestController
28 import spock.lang.Specification
30 import static org.springframework.http.HttpStatus.BAD_REQUEST
31 import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR
32 import static org.springframework.http.HttpStatus.NOT_FOUND
33 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
34 import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup
36 class CpsRestExceptionHandlerSpec extends Specification {
38 def cpsRestController = new CpsRestController();
39 def mockCpService = Mock(CpService.class)
40 def objectUnderTest = new CpsRestExceptionHandler();
41 def mockMvc = standaloneSetup(cpsRestController).setControllerAdvice(objectUnderTest).build()
44 cpsRestController.cpService = mockCpService
47 def 'Get request with runtime exception returns HTTP Status Internal Server Error'() {
49 when: 'runtime exception is thrown by the service'
50 def errorMessage = 'runtime error message'
51 setupTestException(new IllegalStateException(errorMessage))
52 def response = performTestRequest()
54 then: 'an HTTP Internal Server Error response is returned with the correct message'
55 assertTestResponse(response, INTERNAL_SERVER_ERROR, errorMessage, null)
58 def 'Get request with generic CPS exception returns HTTP Status Internal Server Error'() {
60 when: 'generic CPS exception is thrown by the service'
61 def errorMessage = 'cps generic error message'
62 def errorDetails = 'cps generic error details'
63 setupTestException(new CpsException(errorMessage, errorDetails))
64 def response = performTestRequest()
66 then: 'an HTTP Internal Server Error response is returned with the correct message'
67 assertTestResponse(response, INTERNAL_SERVER_ERROR, errorMessage, errorDetails)
70 def 'Get request with no data found CPS exception returns HTTP Status Not Found'() {
72 when: 'no data found CPS exception is thrown by the service'
73 def errorMessage = 'cps no data error message'
74 def errorDetails = 'cps no data error details'
75 setupTestException(new CpsNotFoundException(errorMessage, errorDetails))
76 def response = performTestRequest()
78 then: 'an HTTP Not Found response is returned with the correct message'
79 assertTestResponse(response, NOT_FOUND, errorMessage, errorDetails)
82 def 'Get request with CPS validation exception returns HTTP Status Bad Request'() {
84 when: 'CPS validation exception is thrown by the service'
85 def errorMessage = 'cps validation error message'
86 def errorDetails = 'cps validation error details'
87 setupTestException(new CpsValidationException(errorMessage, errorDetails))
88 def response = performTestRequest()
90 then: 'an HTTP Bad Request response is returned with the correct message'
91 assertTestResponse(response, BAD_REQUEST, errorMessage, errorDetails)
95 * NB. The test uses 'get JSON by id' endpoint and associated service method invocation
96 * to test the exception handling. The endpoint chosen is not a subject of test.
99 def setupTestException(exception) {
100 mockCpService.getJsonById(_) >> { throw exception }
103 def performTestRequest() {
104 return mockMvc.perform(get('/json-object/1')).andReturn().response
107 void assertTestResponse(response, expectedStatus, expectedErrorMessage, expectedErrorDetails) {
108 assert response.status == expectedStatus.value()
109 def content = new JsonSlurper().parseText(response.contentAsString)
110 assert content['status'] == expectedStatus.toString()
111 assert content['message'] == expectedErrorMessage
112 assert expectedErrorDetails == null || content['details'] == expectedErrorDetails