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.Payload
25 import org.onap.cps.policyexecutor.stub.model.PolicyExecutionRequest
26 import org.onap.cps.policyexecutor.stub.model.PolicyExecutionResponse
27 import org.springframework.beans.factory.annotation.Autowired
28 import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
29 import org.springframework.http.HttpStatus
30 import org.springframework.http.MediaType
31 import org.springframework.test.web.servlet.MockMvc
32 import spock.lang.Specification
34 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post
36 @WebMvcTest(PolicyExecutorStubController)
37 class PolicyExecutorStubControllerSpec extends Specification {
43 ObjectMapper objectMapper
45 def url = '/policy-executor/api/v1/some-action'
47 def 'Execute Policy Actions.'() {
48 given: 'a policy execution request with target fdn: #targetFdn'
49 def payload = new Payload(targetFdn, 'some change request')
50 def policyExecutionRequest = new PolicyExecutionRequest('some payload type','some decision type', [payload])
51 def requestBody = objectMapper.writeValueAsString(policyExecutionRequest)
52 when: 'request is posted'
53 def response = mockMvc.perform(post(url)
54 .header('Authorization','some string')
55 .contentType(MediaType.APPLICATION_JSON)
56 .content(requestBody))
58 then: 'response status is Ok'
59 assert response.status == HttpStatus.OK.value()
60 and: 'the response body has the expected decision details'
61 def responseBody = response.contentAsString
62 def policyExecutionResponse = objectMapper.readValue(responseBody, PolicyExecutionResponse.class)
63 assert policyExecutionResponse.decisionId == expectedDecsisonId
64 assert policyExecutionResponse.decision == expectedDecsison
65 assert policyExecutionResponse.message == expectedMessage
66 where: 'the following targets are used'
67 targetFdn || expectedDecsisonId | expectedDecsison | expectedMessage
68 'some fdn' || '1' | 'deny' | "Only FDNs containing 'cps-is-great' are permitted"
69 'fdn with cps-is-great' || '2' | 'permit' | "All good"
72 def 'Execute Policy Action with a HTTP Error Code.'() {
73 given: 'a policy execution request with a target fdn with a 3-digit error code'
74 def payload = new Payload('fdn with error code 418', 'some change request')
75 def policyExecutionRequest = new PolicyExecutionRequest('some payload type','some decision type', [payload])
76 def requestBody = objectMapper.writeValueAsString(policyExecutionRequest)
77 when: 'request is posted'
78 def response = mockMvc.perform(post(url)
79 .header('Authorization','some string')
80 .contentType(MediaType.APPLICATION_JSON)
81 .content(requestBody))
83 then: 'response status the same error code as in target fdn'
84 assert response.status == 418
87 def 'Execute Policy Action without Authorization Header.'() {
88 given: 'a valid policy execution request'
89 def payload = new Payload('some fdn', 'some change request')
90 def policyExecutionRequest = new PolicyExecutionRequest('some payload type','some decision type', [payload])
91 def requestBody = objectMapper.writeValueAsString(policyExecutionRequest)
92 when: 'request is posted without authorization header'
93 def response = mockMvc.perform(post(url)
94 .contentType(MediaType.APPLICATION_JSON)
95 .content(requestBody))
97 then: 'response status is OK'
98 assert response.status == HttpStatus.OK.value()
101 def 'Execute Policy Action with Empty Payload.'() {
102 given: 'a policy execution request with empty payload list'
103 def policyExecutionRequest = new PolicyExecutionRequest('some payload type','some decision type', [])
104 def requestBody = objectMapper.writeValueAsString(policyExecutionRequest)
105 when: 'request is posted'
106 def response = mockMvc.perform(post(url)
107 .header('Authorization','some string')
108 .contentType(MediaType.APPLICATION_JSON)
109 .content(requestBody))
110 .andReturn().response
111 then: 'response status is Bad Request'
112 assert response.status == HttpStatus.BAD_REQUEST.value()
115 def 'Execute Policy Action without other required attributes.'() {
116 given: 'a policy execution request with payloadType=#payloadType, decisionType=decisionType, targetFdn=#targetFdn, changeRequest=#changeRequest'
117 def payload = new Payload(targetFdn, changeRequest)
118 def policyExecutionRequest = new PolicyExecutionRequest(payloadType, decisionType, [payload])
119 def requestBody = objectMapper.writeValueAsString(policyExecutionRequest)
120 when: 'request is posted'
121 def response = mockMvc.perform(post(url)
122 .header('Authorization','something')
123 .contentType(MediaType.APPLICATION_JSON)
124 .content(requestBody))
125 .andReturn().response
126 then: 'response status as expected'
127 assert response.status == expectedStatus.value()
128 where: 'following parameters are populated or not'
129 payloadType | decisionType | targetFdn | changeRequest || expectedStatus
130 'something' | 'something' | 'something' | 'something' || HttpStatus.OK
131 null | 'something' | 'something' | 'something' || HttpStatus.BAD_REQUEST
132 'something' | null | 'something' | 'something' || HttpStatus.BAD_REQUEST
133 'something' | 'something' | null | 'something' || HttpStatus.BAD_REQUEST
134 'something' | 'something' | 'something' | null || HttpStatus.BAD_REQUEST