diff options
author | Sourabh Sourabh <sourabh.sourabh@est.tech> | 2024-10-08 15:51:01 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2024-10-08 15:51:01 +0000 |
commit | e2517a8b993ed884edb251b91ce600d0a1a9fefe (patch) | |
tree | e26cc91a338b296275ee6d579a3a3394c4eb0ade /policy-executor-stub | |
parent | c024f967a84719b0ce6d2c546be4c31d496b6e22 (diff) | |
parent | 77e469b27708d2fabe6281082716a8c086f8107d (diff) |
Merge "Policy Executor: handle errors, part 2 (fighting between IntelliJ and Checkstyle best practices)"
Diffstat (limited to 'policy-executor-stub')
3 files changed, 67 insertions, 1 deletions
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 index cdd26c96e9..88073c0a0f 100644 --- 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 @@ -41,9 +41,11 @@ import org.springframework.web.bind.annotation.RestController; @Slf4j public class PolicyExecutorStubController implements PolicyExecutorApi { + private final Sleeper sleeper; private final ObjectMapper objectMapper; private static final Pattern ERROR_CODE_PATTERN = Pattern.compile("(\\d{3})"); private int decisionCounter = 0; + private static int slowResponseTimeInSeconds = 40; @Override public ResponseEntity<PolicyExecutionResponse> executePolicyAction( @@ -85,7 +87,14 @@ public class PolicyExecutorStubController implements PolicyExecutorApi { final String decisionId = String.valueOf(++decisionCounter); final String decision; final String message; - + if (targetIdentifier.toLowerCase(Locale.getDefault()).contains("slow")) { + try { + sleeper.haveALittleRest(slowResponseTimeInSeconds); + } catch (final InterruptedException e) { + log.trace("Sleep interrupted, re-interrupting the thread"); + Thread.currentThread().interrupt(); // Re-interrupt the thread + } + } if (targetIdentifier.toLowerCase(Locale.getDefault()).contains("cps-is-great")) { decision = "allow"; message = "All good"; diff --git a/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/Sleeper.java b/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/Sleeper.java new file mode 100644 index 0000000000..8f904cc5f2 --- /dev/null +++ b/policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/Sleeper.java @@ -0,0 +1,35 @@ +/* + * ============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.concurrent.TimeUnit; +import org.springframework.stereotype.Service; + +/** + * This class is a successfull experiment to extract out sleep functionality so the interrupted exception handling can + * be covered with a test (e.g. using spy ion Sleeper) and help to get too 100% code coverage. + */ +@Service +public class Sleeper { + public void haveALittleRest(final int timeInSeconds) throws InterruptedException { + TimeUnit.SECONDS.sleep(timeInSeconds); + } +} 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 index 064e0234a3..44460daa7e 100644 --- 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 @@ -25,6 +25,7 @@ import org.onap.cps.policyexecutor.stub.model.NcmpDelete import org.onap.cps.policyexecutor.stub.model.PolicyExecutionRequest import org.onap.cps.policyexecutor.stub.model.PolicyExecutionResponse import org.onap.cps.policyexecutor.stub.model.Request +import org.spockframework.spring.SpringBean import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest import org.springframework.http.HttpStatus @@ -43,8 +44,15 @@ class PolicyExecutorStubControllerSpec extends Specification { @Autowired ObjectMapper objectMapper + @SpringBean + Sleeper sleeper = Spy() + def url = '/policy-executor/api/v1/some-action' + def setup() { + PolicyExecutorStubController.slowResponseTimeInSeconds = 1 + } + def 'Execute policy action.'() { given: 'a policy execution request with target: #targetIdentifier' def requestBody = createRequestBody(targetIdentifier) @@ -66,6 +74,7 @@ class PolicyExecutorStubControllerSpec extends Specification { targetIdentifier || expectedDecsisonId | expectedDecision | expectedMessage 'some fdn' || '1' | 'deny' | "Only FDNs containing 'cps-is-great' are allowed" 'fdn with cps-is-great' || '2' | 'allow' | 'All good' + 'slow' || '3' | 'deny' | "Only FDNs containing 'cps-is-great' are allowed" } def 'Execute policy action with a HTTP error code.'() { @@ -118,6 +127,19 @@ class PolicyExecutorStubControllerSpec extends Specification { assert response.status == HttpStatus.BAD_REQUEST.value() } + def 'Execute policy action with interrupted exception during slow response.'() { + given: 'a policy execution request with target: "slow"' + def requestBody = createRequestBody('slow') + sleeper.haveALittleRest(_) >> { throw new InterruptedException() } + when: 'request is posted' + mockMvc.perform(post(url) + .header('Authorization','some string') + .contentType(MediaType.APPLICATION_JSON) + .content(requestBody)) + then: 'response status is Bad Request' + noExceptionThrown() + } + def 'Execute policy action with missing or invalid attributes.'() { given: 'a policy execution request with decisionType=#decisionType, schema=#schema, targetIdentifier=#targetIdentifier' def requestBody = createRequestBody(decisionType, schema, targetIdentifier) |