From d7914bc1f3c9505539304bd23b795c7b061dc6db Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Thu, 4 Jul 2024 15:15:36 +0100 Subject: Create PolicyExecutor Stub - Generate interface from OpenApi in RTD docs - Fixed Content Type in OpenAPI - Fixed Paths in OpenAPI - Made Authorization header compulsory in OpenAPI - All 'enum' values in OpenAPI lowercase (in linr with CPS/NCMP conventions) - Added impl with some basic functionality - Added testware - Pom includes docker image creation - Docker compose updated to deploy stub Issue-ID: CPS-2301 Change-Id: I462ad5c70474b2813fc04005c0d20a1b15b574ec Signed-off-by: ToineSiebelink Signed-off-by: sourabh_sourabh --- policy-executor-stub/pom.xml | 188 +++++++++++++++++++++ .../stub/PolicyExecutorApplication.java | 31 ++++ .../controller/PolicyExecutorStubController.java | 70 ++++++++ .../src/main/resources/application.yml | 20 +++ .../stub/PolicyExecutorApplicationSpec.groovy | 34 ++++ .../PolicyExecutorStubControllerSpec.groovy | 137 +++++++++++++++ 6 files changed, 480 insertions(+) create mode 100644 policy-executor-stub/pom.xml create mode 100644 policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/PolicyExecutorApplication.java create mode 100644 policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubController.java create mode 100644 policy-executor-stub/src/main/resources/application.yml create mode 100644 policy-executor-stub/src/test/groovy/org/onap/cps/policyexecutor/stub/PolicyExecutorApplicationSpec.groovy create mode 100644 policy-executor-stub/src/test/groovy/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubControllerSpec.groovy (limited to 'policy-executor-stub') diff --git a/policy-executor-stub/pom.xml b/policy-executor-stub/pom.xml new file mode 100644 index 0000000000..99e621d8a9 --- /dev/null +++ b/policy-executor-stub/pom.xml @@ -0,0 +1,188 @@ + + + 4.0.0 + + org.onap.cps + cps-parent + 3.5.1-SNAPSHOT + ../cps-parent/pom.xml + + + policy-executor-stub + + + org.onap.cps.policyexecutor.stub.PolicyExecutorApplication + yyyyMMdd'T'HHmmss'Z' + ${docker.pull.registry}/onap/integration-java17:12.0.0 + policy-executor-stub + ${project.version}-${maven.build.timestamp} + UTF-8 + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + org.springframework.boot + spring-boot-starter-jetty + + + org.springframework.boot + spring-boot-starter-validation + + + + io.swagger.core.v3 + swagger-annotations + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + + + + org.codehaus.groovy + groovy + test + + + org.codehaus.groovy + groovy-json + test + + + org.spockframework + spock-core + test + + + org.spockframework + spock-spring + test + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + + com.google.cloud.tools + jib-maven-plugin + + + ${app} + USE_CURRENT_TIMESTAMP + + + ${base.image} + + + + latest + + ${docker.push.registry}/onap/${image.name}:${image.tag} + + + + + package + build + + dockerBuild + + + + deploy + buildAndPush + + build + + + + + + + + + + org.openapitools + openapi-generator-maven-plugin + 6.6.0 + + + code-gen + + generate + + + ${project.parent.basedir}/../docs/api/swagger/policy-executor/openapi.yaml + org.onap.cps.policyexecutor.stub.model + org.onap.cps.policyexecutor.stub.api + spring + false + + src/gen/java + java11 + true + true + true + false + true + + + + + openapi-yaml-gen + + generate + + compile + + ${project.parent.basedir}/../docs/api/swagger/policy-executor/openapi.yaml + openapi-yaml + + openapi.yaml + + + + + + + + + + + docker + + true + + + + + com.google.cloud.tools + jib-maven-plugin + 3.3.2 + + + + + + + + diff --git a/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/PolicyExecutorApplication.java b/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/PolicyExecutorApplication.java new file mode 100644 index 0000000000..367a470e2d --- /dev/null +++ b/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/PolicyExecutorApplication.java @@ -0,0 +1,31 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.policyexecutor.stub; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class PolicyExecutorApplication { + public static void main(final String[] args) { + SpringApplication.run(PolicyExecutorApplication.class, args); + } +} diff --git a/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubController.java b/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubController.java new file mode 100644 index 0000000000..7989c3fc8c --- /dev/null +++ b/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubController.java @@ -0,0 +1,70 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.policyexecutor.stub.controller; + +import java.util.Locale; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.onap.cps.policyexecutor.stub.api.PolicyExecutorApi; +import org.onap.cps.policyexecutor.stub.model.PolicyExecutionRequest; +import org.onap.cps.policyexecutor.stub.model.PolicyExecutionResponse; +import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("${rest.api.policy-executor-base-path}") +public class PolicyExecutorStubController implements PolicyExecutorApi { + + private final Pattern errorCodePattern = Pattern.compile("(\\d{3})"); + private int decisionCounter = 0; + + @Override + public ResponseEntity executePolicyAction( + final String authorization, + final String action, + final PolicyExecutionRequest policyExecutionRequest) { + if (policyExecutionRequest.getPayload().isEmpty()) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); + } + + final String firstTargetFdn = policyExecutionRequest.getPayload().iterator().next().getTargetFdn(); + + final Matcher matcher = errorCodePattern.matcher(firstTargetFdn); + if (matcher.find()) { + final int errorCode = Integer.parseInt(matcher.group(1)); + return new ResponseEntity<>(HttpStatusCode.valueOf(errorCode)); + } + + final PolicyExecutionResponse policyExecutionResponse = new PolicyExecutionResponse(); + policyExecutionResponse.setDecisionId(String.valueOf(++decisionCounter)); + + if (firstTargetFdn.toLowerCase(Locale.getDefault()).contains("cps-is-great")) { + policyExecutionResponse.setDecision("permit"); + } else { + policyExecutionResponse.setDecision("deny"); + policyExecutionResponse.setMessage("Only FDNs containing 'cps-is-great' are permitted"); + } + return ResponseEntity.ok(policyExecutionResponse); + } +} diff --git a/policy-executor-stub/src/main/resources/application.yml b/policy-executor-stub/src/main/resources/application.yml new file mode 100644 index 0000000000..f713a157fe --- /dev/null +++ b/policy-executor-stub/src/main/resources/application.yml @@ -0,0 +1,20 @@ +# ============LICENSE_START======================================================= +# Copyright (C) 2024 Nordix +# ================================================================================ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END========================================================= + +rest: + api: + policy-executor-base-path: /policy-executor/api diff --git a/policy-executor-stub/src/test/groovy/org/onap/cps/policyexecutor/stub/PolicyExecutorApplicationSpec.groovy b/policy-executor-stub/src/test/groovy/org/onap/cps/policyexecutor/stub/PolicyExecutorApplicationSpec.groovy new file mode 100644 index 0000000000..565932d9f0 --- /dev/null +++ b/policy-executor-stub/src/test/groovy/org/onap/cps/policyexecutor/stub/PolicyExecutorApplicationSpec.groovy @@ -0,0 +1,34 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.policyexecutor.stub + +import spock.lang.Specification + +class PolicyExecutorApplicationSpec extends Specification { + + def 'Execute Policy Action.'() { + when: 'Starting the application (for coverage)' + PolicyExecutorApplication.main() + then: 'all goes well' + noExceptionThrown() + } + +} diff --git a/policy-executor-stub/src/test/groovy/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubControllerSpec.groovy b/policy-executor-stub/src/test/groovy/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubControllerSpec.groovy new file mode 100644 index 0000000000..593394fdc7 --- /dev/null +++ b/policy-executor-stub/src/test/groovy/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubControllerSpec.groovy @@ -0,0 +1,137 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2024 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.policyexecutor.stub.controller + +import com.fasterxml.jackson.databind.ObjectMapper +import org.onap.cps.policyexecutor.stub.model.Payload +import org.onap.cps.policyexecutor.stub.model.PolicyExecutionRequest +import org.onap.cps.policyexecutor.stub.model.PolicyExecutionResponse +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest +import org.springframework.http.HttpStatus +import org.springframework.http.MediaType +import org.springframework.test.web.servlet.MockMvc +import spock.lang.Specification + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post + +@WebMvcTest(PolicyExecutorStubController) +class PolicyExecutorStubControllerSpec extends Specification { + + @Autowired + MockMvc mockMvc + + @Autowired + ObjectMapper objectMapper + + def url = '/policy-executor/api/v1/some-action' + + def 'Execute Policy Actions.'() { + given: 'a policy execution request with target fdn: #targetFdn' + def payload = new Payload(targetFdn, 'some change request') + def policyExecutionRequest = new PolicyExecutionRequest('some payload type','some decision type', [payload]) + def requestBody = objectMapper.writeValueAsString(policyExecutionRequest) + when: 'request is posted' + def response = mockMvc.perform(post(url) + .header('Authorization','some string') + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + .andReturn().response + then: 'response status is Ok' + assert response.status == HttpStatus.OK.value() + and: 'the response body has the expected decision details' + def responseBody = response.contentAsString + def policyExecutionResponse = objectMapper.readValue(responseBody, PolicyExecutionResponse.class) + assert policyExecutionResponse.decisionId == expectedDecsisonId + assert policyExecutionResponse.decision == expectedDecsison + assert policyExecutionResponse.message == expectedMessage + where: 'the following targets are used' + targetFdn || expectedDecsisonId | expectedDecsison | expectedMessage + 'some fdn' || '1' | 'deny' | "Only FDNs containing 'cps-is-great' are permitted" + 'fdn with cps-is-great' || '2' | 'permit' | null + } + + def 'Execute Policy Action with a HTTP Error Code.'() { + given: 'a policy execution request with a target fdn with a 3-digit error code' + def payload = new Payload('fdn with error code 418', 'some change request') + def policyExecutionRequest = new PolicyExecutionRequest('some payload type','some decision type', [payload]) + def requestBody = objectMapper.writeValueAsString(policyExecutionRequest) + when: 'request is posted' + def response = mockMvc.perform(post(url) + .header('Authorization','some string') + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + .andReturn().response + then: 'response status the same error code as in target fdn' + assert response.status == 418 + } + + def 'Execute Policy Action without Authorization Header.'() { + given: 'a valid policy execution request' + def payload = new Payload('some fdn', 'some change request') + def policyExecutionRequest = new PolicyExecutionRequest('some payload type','some decision type', [payload]) + def requestBody = objectMapper.writeValueAsString(policyExecutionRequest) + when: 'request is posted without authorization header' + def response = mockMvc.perform(post(url) + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + .andReturn().response + then: 'response status is Bad Request' + assert response.status == HttpStatus.BAD_REQUEST.value() + } + + def 'Execute Policy Action with Empty Payload.'() { + given: 'a policy execution request with empty payload list' + def policyExecutionRequest = new PolicyExecutionRequest('some payload type','some decision type', []) + def requestBody = objectMapper.writeValueAsString(policyExecutionRequest) + when: 'request is posted' + def response = mockMvc.perform(post(url) + .header('Authorization','some string') + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + .andReturn().response + then: 'response status is Bad Request' + assert response.status == HttpStatus.BAD_REQUEST.value() + } + + def 'Execute Policy Action without other required attributes.'() { + given: 'a policy execution request with payloadType=#payloadType, decisionType=decisionType, targetFdn=#targetFdn, changeRequest=#changeRequest' + def payload = new Payload(targetFdn, changeRequest) + def policyExecutionRequest = new PolicyExecutionRequest(payloadType, decisionType, [payload]) + def requestBody = objectMapper.writeValueAsString(policyExecutionRequest) + when: 'request is posted' + def response = mockMvc.perform(post(url) + .header('Authorization','something') + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + .andReturn().response + then: 'response status as expected' + assert response.status == expectedStatus.value() + where: 'following parameters are populated or not' + payloadType | decisionType | targetFdn | changeRequest || expectedStatus + 'something' | 'something' | 'something' | 'something' || HttpStatus.OK + null | 'something' | 'something' | 'something' || HttpStatus.BAD_REQUEST + 'something' | null | 'something' | 'something' || HttpStatus.BAD_REQUEST + 'something' | 'something' | null | 'something' || HttpStatus.BAD_REQUEST + 'something' | 'something' | 'something' | null || HttpStatus.BAD_REQUEST + } + +} -- cgit 1.2.3-korg