diff options
Diffstat (limited to 'policy-executor-stub')
3 files changed, 13 insertions, 22 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 5b3a9931a0..cdd26c96e9 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 @@ -20,12 +20,12 @@ package org.onap.cps.policyexecutor.stub.controller; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Locale; import java.util.regex.Matcher; import java.util.regex.Pattern; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.onap.cps.policyexecutor.stub.api.PolicyExecutorApi; import org.onap.cps.policyexecutor.stub.model.NcmpDelete; import org.onap.cps.policyexecutor.stub.model.PolicyExecutionRequest; @@ -34,18 +34,15 @@ import org.onap.cps.policyexecutor.stub.model.Request; 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}") @RequiredArgsConstructor +@Slf4j public class PolicyExecutorStubController implements PolicyExecutorApi { private final ObjectMapper objectMapper; - private static final Pattern ERROR_CODE_PATTERN = Pattern.compile("(\\d{3})"); - private int decisionCounter = 0; @Override @@ -53,25 +50,24 @@ public class PolicyExecutorStubController implements PolicyExecutorApi { final String action, final PolicyExecutionRequest policyExecutionRequest, final String authorization) { + log.info("Stub Policy Executor Invoked (only supports 'delete' operations)"); if (policyExecutionRequest.getRequests().isEmpty()) { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } final Request firstRequest = policyExecutionRequest.getRequests().iterator().next(); - if ("ncmp-delete-schema:1.0.0".equals(firstRequest.getSchema())) { + log.info("1st Request Schema:{}", firstRequest.getSchema()); + if (firstRequest.getSchema().contains("ncmp-delete-schema:1.0.0")) { return handleNcmpDeleteSchema(firstRequest); } + log.warn("This stub only supports 'delete' operations"); return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } private ResponseEntity<PolicyExecutionResponse> handleNcmpDeleteSchema(final Request request) { - final NcmpDelete ncmpDelete; - try { - ncmpDelete = objectMapper.readValue((String) request.getData(), NcmpDelete.class); - } catch (final JsonProcessingException e) { - return new ResponseEntity<>(HttpStatus.BAD_REQUEST); - } + final NcmpDelete ncmpDelete = objectMapper.convertValue(request.getData(), NcmpDelete.class); final String targetIdentifier = ncmpDelete.getTargetIdentifier(); + if (targetIdentifier == null) { return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } @@ -97,7 +93,7 @@ public class PolicyExecutorStubController implements PolicyExecutorApi { decision = "deny"; message = "Only FDNs containing 'cps-is-great' are allowed"; } - + log.info("Decision: {} ({})", decision, message); final PolicyExecutionResponse policyExecutionResponse = new PolicyExecutionResponse(decisionId, decision, message); diff --git a/policy-executor-stub/src/main/resources/application.yml b/policy-executor-stub/src/main/resources/application.yml index f713a157fe..8ed5742a41 100644 --- a/policy-executor-stub/src/main/resources/application.yml +++ b/policy-executor-stub/src/main/resources/application.yml @@ -15,6 +15,5 @@ # SPDX-License-Identifier: Apache-2.0 # ============LICENSE_END========================================================= -rest: - api: - policy-executor-base-path: /policy-executor/api +server: + port: 8093 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 efb12ac619..064e0234a3 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 @@ -108,15 +108,11 @@ class PolicyExecutorStubControllerSpec extends Specification { } def 'Execute policy action with invalid json for request data.'() { - given: 'a policy execution request' - def request = new Request('ncmp-delete-schema:1.0.0', 'invalid json') - def policyExecutionRequest = new PolicyExecutionRequest('some decision type', [request]) - 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)) + .content('invalid json')) .andReturn().response then: 'response status is Bad Request' assert response.status == HttpStatus.BAD_REQUEST.value() @@ -143,7 +139,7 @@ class PolicyExecutorStubControllerSpec extends Specification { def createRequestBody(decisionType, schema, targetIdentifier) { def ncmpDelete = new NcmpDelete(targetIdentifier: targetIdentifier) - def request = new Request(schema, objectMapper.writeValueAsString(ncmpDelete)) + def request = new Request(schema, ncmpDelete) def policyExecutionRequest = new PolicyExecutionRequest(decisionType, [request]) return objectMapper.writeValueAsString(policyExecutionRequest) } |