44460daa7e5502c3a73ac32c09708edd8138e839
[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.NcmpDelete
25 import org.onap.cps.policyexecutor.stub.model.PolicyExecutionRequest
26 import org.onap.cps.policyexecutor.stub.model.PolicyExecutionResponse
27 import org.onap.cps.policyexecutor.stub.model.Request
28 import org.spockframework.spring.SpringBean
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
31 import org.springframework.http.HttpStatus
32 import org.springframework.http.MediaType
33 import org.springframework.test.web.servlet.MockMvc
34 import spock.lang.Specification
35
36 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
37
38 @WebMvcTest(PolicyExecutorStubController)
39 class PolicyExecutorStubControllerSpec extends Specification {
40
41     @Autowired
42     MockMvc mockMvc
43
44     @Autowired
45     ObjectMapper objectMapper
46
47     @SpringBean
48     Sleeper sleeper = Spy()
49
50     def url = '/policy-executor/api/v1/some-action'
51
52     def setup() {
53         PolicyExecutorStubController.slowResponseTimeInSeconds = 1
54     }
55
56     def 'Execute policy action.'() {
57         given: 'a policy execution request with target: #targetIdentifier'
58             def requestBody = createRequestBody(targetIdentifier)
59         when: 'request is posted'
60             def response = mockMvc.perform(post(url)
61                     .header('Authorization','some string')
62                     .contentType(MediaType.APPLICATION_JSON)
63                     .content(requestBody))
64                     .andReturn().response
65         then: 'response status is Ok'
66             assert response.status == HttpStatus.OK.value()
67         and: 'the response body has the expected decision details'
68             def responseBody = response.contentAsString
69             def policyExecutionResponse = objectMapper.readValue(responseBody, PolicyExecutionResponse.class)
70             assert policyExecutionResponse.decisionId == expectedDecsisonId
71             assert policyExecutionResponse.decision == expectedDecision
72             assert policyExecutionResponse.message == expectedMessage
73         where: 'the following targets are used'
74             targetIdentifier        || expectedDecsisonId | expectedDecision | expectedMessage
75             'some fdn'              || '1'                | 'deny'           | "Only FDNs containing 'cps-is-great' are allowed"
76             'fdn with cps-is-great' || '2'                | 'allow'          | 'All good'
77             'slow'                  || '3'                | 'deny'           | "Only FDNs containing 'cps-is-great' are allowed"
78     }
79
80     def 'Execute policy action with a HTTP error code.'() {
81         given: 'a policy execution request with a target fdn with a 3-digit error code'
82             def requestBody = createRequestBody('target with error code 418')
83         when: 'request is posted'
84             def response = mockMvc.perform(post(url)
85                 .header('Authorization','some string')
86                 .contentType(MediaType.APPLICATION_JSON)
87                 .content(requestBody))
88                 .andReturn().response
89         then: 'response status the same error code as in target fdn'
90             assert response.status == 418
91     }
92
93     def 'Execute policy action without authorization header.'() {
94         given: 'a valid policy execution request'
95             def requestBody = createRequestBody('some target')
96         when: 'request is posted without authorization header'
97             def response = mockMvc.perform(post(url)
98                 .contentType(MediaType.APPLICATION_JSON)
99                 .content(requestBody))
100                 .andReturn().response
101         then: 'response status is OK'
102             assert response.status == HttpStatus.OK.value()
103     }
104
105     def 'Execute policy action with no requests.'() {
106         given: 'a policy execution request'
107             def policyExecutionRequest = new PolicyExecutionRequest('some decision type', [])
108             def requestBody = objectMapper.writeValueAsString(policyExecutionRequest)
109         when: 'request is posted'
110             def response = mockMvc.perform(post(url)
111                 .header('Authorization','some string')
112                 .contentType(MediaType.APPLICATION_JSON)
113                 .content(requestBody))
114                 .andReturn().response
115         then: 'response status is Bad Request'
116             assert response.status == HttpStatus.BAD_REQUEST.value()
117     }
118
119     def 'Execute policy action with invalid json for request data.'() {
120         when: 'request is posted'
121             def response = mockMvc.perform(post(url)
122                 .header('Authorization','some string')
123                 .contentType(MediaType.APPLICATION_JSON)
124                 .content('invalid json'))
125                 .andReturn().response
126         then: 'response status is Bad Request'
127             assert response.status == HttpStatus.BAD_REQUEST.value()
128     }
129
130     def 'Execute policy action with interrupted exception during slow response.'() {
131         given: 'a policy execution request with target: "slow"'
132             def requestBody = createRequestBody('slow')
133             sleeper.haveALittleRest(_) >> { throw new InterruptedException() }
134         when: 'request is posted'
135             mockMvc.perform(post(url)
136                 .header('Authorization','some string')
137                 .contentType(MediaType.APPLICATION_JSON)
138                 .content(requestBody))
139         then: 'response status is Bad Request'
140             noExceptionThrown()
141     }
142
143     def 'Execute policy action with missing or invalid attributes.'() {
144         given: 'a policy execution request with decisionType=#decisionType, schema=#schema, targetIdentifier=#targetIdentifier'
145             def requestBody = createRequestBody(decisionType, schema, targetIdentifier)
146         when: 'request is posted'
147             def response = mockMvc.perform(post(url)
148                 .header('Authorization','something')
149                 .contentType(MediaType.APPLICATION_JSON)
150                 .content(requestBody))
151                 .andReturn().response
152         then: 'response status as expected'
153             assert response.status == expectedStatus.value()
154         where: 'following parameters are used'
155             decisionType | schema                     | targetIdentifier || expectedStatus
156             'something'  | 'ncmp-delete-schema:1.0.0' | 'something'      || HttpStatus.OK
157             null         | 'ncmp-delete-schema:1.0.0' | 'something'      || HttpStatus.BAD_REQUEST
158             'something'  | 'other schema'             | 'something'      || HttpStatus.BAD_REQUEST
159             'something'  | 'ncmp-delete-schema:1.0.0' | null             || HttpStatus.BAD_REQUEST
160     }
161
162     def createRequestBody(decisionType, schema, targetIdentifier) {
163         def ncmpDelete = new NcmpDelete(targetIdentifier: targetIdentifier)
164         def request = new Request(schema, ncmpDelete)
165         def policyExecutionRequest = new PolicyExecutionRequest(decisionType, [request])
166         return objectMapper.writeValueAsString(policyExecutionRequest)
167     }
168
169     def createRequestBody(targetIdentifier) {
170         return createRequestBody('some decision type', 'ncmp-delete-schema:1.0.0', targetIdentifier)
171     }
172
173 }