From 2bcccff7e2891f708cedc08cdbf969025d63019e Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Thu, 19 Sep 2024 17:23:58 +0100 Subject: Policy Executor: handle errors - configurable default answer - apply default answer upon non 2xx response - delayed default webclient read timeout - add custom timeout method with original read timeout in seconds - apply default answer upon timeout - add integration test with short timeout error scenario Issue-ID: CPS-2412 Change-Id: I62527a27e426c2f01fda2182ebd2513242c29ac1 Signed-off-by: ToineSiebelink --- .../cps/integration/base/PolicyDispatcher.groovy | 7 ++++++- .../ncmp/PolicyExecutorIntegrationSpec.groovy | 23 ++++++++++++++++------ 2 files changed, 23 insertions(+), 7 deletions(-) (limited to 'integration-test/src/test/groovy/org/onap') diff --git a/integration-test/src/test/groovy/org/onap/cps/integration/base/PolicyDispatcher.groovy b/integration-test/src/test/groovy/org/onap/cps/integration/base/PolicyDispatcher.groovy index 69792d7ed8..c93a5274e6 100644 --- a/integration-test/src/test/groovy/org/onap/cps/integration/base/PolicyDispatcher.groovy +++ b/integration-test/src/test/groovy/org/onap/cps/integration/base/PolicyDispatcher.groovy @@ -24,10 +24,12 @@ package org.onap.cps.integration.base import okhttp3.mockwebserver.Dispatcher import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.RecordedRequest +import org.springframework.beans.factory.annotation.Value import org.springframework.http.HttpHeaders import org.springframework.http.HttpStatus import org.springframework.http.MediaType import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper +import java.util.concurrent.TimeUnit /** * This class simulates responses from the Policy Execution server in NCMP integration tests. @@ -53,11 +55,14 @@ class PolicyDispatcher extends Dispatcher { def targetIdentifier = body.get('requests').get(0).get('data').get('targetIdentifier') def responseAsMap = [:] responseAsMap.put('decisionId',1) + if (targetIdentifier == "mock slow response") { + TimeUnit.SECONDS.sleep(2) // One second more then configured readTimeoutInSeconds + } if (allowAll || targetIdentifier == 'fdn1') { responseAsMap.put('decision','allow') responseAsMap.put('message','') } else { - responseAsMap.put('decision','deny') + responseAsMap.put('decision','deny from mock server (dispatcher)') responseAsMap.put('message','I only like fdn1') } def responseAsString = objectMapper.writeValueAsString(responseAsMap) diff --git a/integration-test/src/test/groovy/org/onap/cps/integration/functional/ncmp/PolicyExecutorIntegrationSpec.groovy b/integration-test/src/test/groovy/org/onap/cps/integration/functional/ncmp/PolicyExecutorIntegrationSpec.groovy index 99f245ae8c..1d4d19bee0 100644 --- a/integration-test/src/test/groovy/org/onap/cps/integration/functional/ncmp/PolicyExecutorIntegrationSpec.groovy +++ b/integration-test/src/test/groovy/org/onap/cps/integration/functional/ncmp/PolicyExecutorIntegrationSpec.groovy @@ -20,6 +20,7 @@ package org.onap.cps.integration.functional.ncmp +import com.fasterxml.jackson.databind.ObjectMapper import org.onap.cps.integration.base.CpsIntegrationSpecBase import org.springframework.http.HttpHeaders import org.springframework.http.MediaType @@ -29,18 +30,22 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilder class PolicyExecutorIntegrationSpec extends CpsIntegrationSpecBase { + def objectMapper = new ObjectMapper() + def setup() { // Enable mocked policy executor logic policyDispatcher.allowAll = false; - //minimum setup for 2 cm handles with alternate ids - dmiDispatcher1.moduleNamesPerCmHandleId = ['ch-1': [], 'ch-2': []] + //minimum setup for cm handles with alternate ids + dmiDispatcher1.moduleNamesPerCmHandleId = ['ch-1': [], 'ch-2': [], 'ch-3':[]] registerCmHandle(DMI1_URL, 'ch-1', NO_MODULE_SET_TAG, 'fdn1') registerCmHandle(DMI1_URL, 'ch-2', NO_MODULE_SET_TAG, 'fdn2') + registerCmHandle(DMI1_URL, 'ch-3', NO_MODULE_SET_TAG, 'mock slow response') } def cleanup() { deregisterCmHandle(DMI1_URL, 'ch-1') deregisterCmHandle(DMI1_URL, 'ch-2') + deregisterCmHandle(DMI1_URL, 'ch-3') } def 'Policy Executor create request with #scenario.'() { @@ -53,11 +58,17 @@ class PolicyExecutorIntegrationSpec extends CpsIntegrationSpecBase { .andReturn().response then: 'the expected status code is returned' response.getStatus() == execpectedStatusCode + and: 'when not allowed the response body contains the expected message' + if (expectedMessage!='allow') { + def bodyAsMap = objectMapper.readValue(response.getContentAsByteArray(), Map.class) + assert bodyAsMap.get('message').endsWith(expectedMessage) + } where: 'following parameters are used' - scenario | cmHandle | authorization || execpectedStatusCode - 'accepted cm handle' | 'ch-1' | 'mock expects "ABC"' || 201 - 'un-accepted cm handle' | 'ch-2' | 'mock expects "ABC"' || 409 - 'invalid authorization' | 'ch-1' | 'something else' || 500 + scenario | cmHandle | authorization || execpectedStatusCode || expectedMessage + 'accepted cm handle' | 'ch-1' | 'mock expects "ABC"' || 201 || 'allow' + 'un-accepted cm handle' | 'ch-2' | 'mock expects "ABC"' || 409 || 'deny from mock server (dispatcher)' + 'timeout' | 'ch-3' | 'mock expects "ABC"' || 409 || 'test default decision' + 'invalid authorization' | 'ch-1' | 'something else' || 500 || '401 Unauthorized from POST http://localhost:8790/policy-executor/api/v1/execute' } } -- cgit 1.2.3-korg