75bd676b2f4ba2e59b5b36e44811f25cf7c9a363
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Nordix Foundation
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.policyexecutor.stub.controller
22
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import org.onap.cps.policyexecutor.stub.model.Operation
25 import org.onap.cps.policyexecutor.stub.model.PermissionRequest
26 import org.onap.cps.policyexecutor.stub.model.PermissionResponse
27 import org.spockframework.spring.SpringBean
28 import org.springframework.beans.factory.annotation.Autowired
29 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
30 import org.springframework.http.HttpStatus
31 import org.springframework.http.MediaType
32 import org.springframework.test.web.servlet.MockMvc
33 import spock.lang.Specification
34
35 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
36
37 @WebMvcTest(PolicyExecutorStubController)
38 class PolicyExecutorStubControllerSpec extends Specification {
39
40     @Autowired
41     MockMvc mockMvc
42
43     @Autowired
44     ObjectMapper objectMapper
45
46     @SpringBean
47     Sleeper sleeper = Spy()
48
49     def url = '/operation-permission/v1/permissions'
50
51     def setup() {
52         PolicyExecutorStubController.slowResponseTimeInSeconds = 1
53     }
54
55     def 'Permission request with #targetIdentifier.'() {
56         given: 'a permission request with target: #targetIdentifier'
57             def requestBody = createRequestBody(targetIdentifier)
58         when: 'request is posted'
59             def response = mockMvc.perform(post(url)
60                     .header('Authorization','some string')
61                     .contentType(MediaType.APPLICATION_JSON)
62                     .content(requestBody))
63                     .andReturn().response
64         then: 'response status is Ok'
65             assert response.status == HttpStatus.OK.value()
66         and: 'the response body has the expected decision details'
67             def responseBody = response.contentAsString
68             def permissionResponse = objectMapper.readValue(responseBody, PermissionResponse.class)
69             assert permissionResponse.id == expectedId
70             assert permissionResponse.permissionResult == expectedResult
71             assert permissionResponse.message == expectedMessage
72         where: 'the following targets are used'
73             targetIdentifier        || expectedId | expectedResult | expectedMessage
74             'some fdn'              || '1'        | 'deny'         | "Only FDNs containing 'cps-is-great' are allowed"
75             'fdn with cps-is-great' || '2'        | 'allow'        | 'All good'
76             'slow'                  || '3'        | 'deny'         | "Only FDNs containing 'cps-is-great' are allowed"
77     }
78
79     def 'Permission request with a HTTP error code.'() {
80         given: 'a permission request with a target fdn with a 3-digit error code'
81             def requestBody = createRequestBody('target with error code 418')
82         when: 'request is posted'
83             def response = mockMvc.perform(post(url)
84                 .header('Authorization','some string')
85                 .contentType(MediaType.APPLICATION_JSON)
86                 .content(requestBody))
87                 .andReturn().response
88         then: 'response status the same error code as in target fdn'
89             assert response.status == 418
90     }
91
92     def 'Permission request without authorization header.'() {
93         given: 'a valid permission request'
94             def requestBody = createRequestBody('some target')
95         when: 'request is posted without authorization header'
96             def response = mockMvc.perform(post(url)
97                 .contentType(MediaType.APPLICATION_JSON)
98                 .content(requestBody))
99                 .andReturn().response
100         then: 'response status is OK'
101             assert response.status == HttpStatus.OK.value()
102     }
103
104     def 'Permission request with no operations.'() {
105         given: 'a permission request with no operations'
106             def permissionRequest = new PermissionRequest('some decision type', [])
107             def requestBody = objectMapper.writeValueAsString(permissionRequest)
108         when: 'request is posted'
109             def response = mockMvc.perform(post(url)
110                 .header('Authorization','some string')
111                 .contentType(MediaType.APPLICATION_JSON)
112                 .content(requestBody))
113                 .andReturn().response
114         then: 'response status is Bad Request'
115             assert response.status == HttpStatus.BAD_REQUEST.value()
116     }
117
118     def 'Request with invalid json for request data.'() {
119         when: 'request with invalid json is posted'
120             def response = mockMvc.perform(post(url)
121                 .header('Authorization','some string')
122                 .contentType(MediaType.APPLICATION_JSON)
123                 .content('invalid json'))
124                 .andReturn().response
125         then: 'response status is Bad Request'
126             assert response.status == HttpStatus.BAD_REQUEST.value()
127     }
128
129     def 'Permission request with interrupted exception during slow response.'() {
130         given: 'a permission request with target: "slow" (stub will be slow)'
131             def requestBody = createRequestBody('slow')
132             sleeper.haveALittleRest(_) >> { throw new InterruptedException() }
133         when: 'request is posted'
134             mockMvc.perform(post(url)
135                 .header('Authorization','some string')
136                 .contentType(MediaType.APPLICATION_JSON)
137                 .content(requestBody))
138         then: 'response status is Bad Request'
139             noExceptionThrown()
140     }
141
142     def 'Permission request with missing or invalid attributes.'() {
143         given: 'Permission request with operation=#operation and targetIdentifier=#targetIdentifier'
144             def requestBody = createRequestBody(operation, targetIdentifier, changeRequest)
145         when: 'request is posted'
146             def response = mockMvc.perform(post(url)
147                 .header('Authorization','something')
148                 .contentType(MediaType.APPLICATION_JSON)
149                 .content(requestBody))
150                 .andReturn().response
151         then: 'response status as expected'
152             assert response.status == expectedStatus.value()
153         where: 'following parameters are used'
154             operation | targetIdentifier | changeRequest || expectedStatus
155             'delete'  | 'something'      | null          || HttpStatus.OK
156             'other'   | 'something'      | '{}'          || HttpStatus.OK
157             'delete'  | null             | null          || HttpStatus.BAD_REQUEST
158             'other'   | 'something'      | null          || HttpStatus.BAD_REQUEST
159     }
160
161     def createRequestBody(targetIdentifier) {
162         return createRequestBody('delete', targetIdentifier, '{}')
163     }
164
165     def createRequestBody(operationName, targetIdentifier, changeRequest) {
166         def operation = new Operation(operationName, targetIdentifier)
167         operation.setChangeRequest(changeRequest)
168         def permissionRequest = new PermissionRequest('cm-legacy', [operation])
169         return objectMapper.writeValueAsString(permissionRequest)
170     }
171
172 }