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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.policyexecutor.stub.controller
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
36 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
38 @WebMvcTest(PolicyExecutorStubController)
39 class PolicyExecutorStubControllerSpec extends Specification {
45 ObjectMapper objectMapper
48 Sleeper sleeper = Spy()
50 def url = '/policy-executor/api/v1/some-action'
53 PolicyExecutorStubController.slowResponseTimeInSeconds = 1
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))
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"
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))
89 then: 'response status the same error code as in target fdn'
90 assert response.status == 418
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()
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()
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()
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'
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
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)
169 def createRequestBody(targetIdentifier) {
170 return createRequestBody('some decision type', 'ncmp-delete-schema:1.0.0', targetIdentifier)