aboutsummaryrefslogtreecommitdiffstats
path: root/policy-executor-stub/src/main
diff options
context:
space:
mode:
authorToineSiebelink <toine.siebelink@est.tech>2024-10-01 18:40:39 +0100
committerToineSiebelink <toine.siebelink@est.tech>2024-10-03 13:29:16 +0100
commit77e469b27708d2fabe6281082716a8c086f8107d (patch)
tree1780ac8f935ea4c2d0b9c283088fefaffe91d50e /policy-executor-stub/src/main
parent89bfabfda2afeeedd1e6cdcba41705469d406f48 (diff)
Policy Executor: handle errors, part 2
(fighting between IntelliJ and Checkstyle best practices) - non-2xx responses are processed using web client exceptions - handle unknown host exception - upgraded spotbugs (checkstyle and related mvn plugin) - fixed some small spotbugs due to upgrade - added commented instructions in docker compose to enable debugging - added some environment variables for policy executor configuration - extract out Sleeper in stub service to achieve 100% coverage - added cause to Policy Executor exceptions where applicable - ignored (new) spotbug rule about catch NPE because of issue in 3pp - ignored (new) spotbug rule about \n in string due to multiline string block Issue-ID: CPS-2412 Change-Id: I6835a73320c436cbeea12cc7a06f15899eec7bf1 Signed-off-by: ToineSiebelink <toine.siebelink@est.tech>
Diffstat (limited to 'policy-executor-stub/src/main')
-rw-r--r--policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/PolicyExecutorStubController.java11
-rw-r--r--policy-executor-stub/src/main/java/org/onap/cps/policyexecutor/stub/controller/Sleeper.java35
2 files changed, 45 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);
+ }
+}