From 06ea4844bddd197796933775faf8999612daa63d Mon Sep 17 00:00:00 2001 From: lapentafd Date: Mon, 18 Oct 2021 10:06:31 +0100 Subject: HTTP code filter for RestClientProducer RestClient producer can now consider 2xx response status as success Issue-ID: POLICY-3743 Change-Id: I40c4dce01b437ea6b9f9bbc317835657cb7ebc8a Signed-off-by: lapentafd --- .../carrier/restclient/ApexRestClientConsumer.java | 3 --- .../carrier/restclient/ApexRestClientProducer.java | 17 +++++++++---- .../restclient/ApexRestClientProducerTest.java | 29 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) (limited to 'plugins') diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumer.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumer.java index 956345cd4..899d4de11 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumer.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConsumer.java @@ -75,7 +75,6 @@ public class ApexRestClientConsumer extends ApexPluginsEventConsumer { if (!(consumerParameters.getCarrierTechnologyParameters() instanceof RestClientCarrierTechnologyParameters)) { final String errorMessage = "specified consumer properties are not applicable to REST client consumer (" + this.name + ")"; - LOGGER.warn(errorMessage); throw new ApexEventException(errorMessage); } restConsumerProperties = @@ -92,7 +91,6 @@ public class ApexRestClientConsumer extends ApexPluginsEventConsumer { final String errorMessage = "specified HTTP method of \"" + restConsumerProperties.getHttpMethod() + "\" is invalid, only HTTP method \"GET\" " + "is supported for event reception on REST client consumer (" + this.name + ")"; - LOGGER.warn(errorMessage); throw new ApexEventException(errorMessage); } @@ -157,7 +155,6 @@ public class ApexRestClientConsumer extends ApexPluginsEventConsumer { final String errorMessage = "reception of event from URL \"" + restConsumerProperties.getUrl() + "\" failed with status code " + response.getStatus() + " and message \"" + response.readEntity(String.class) + "\""; - LOGGER.warn(errorMessage); throw new ApexEventRuntimeException(errorMessage); } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducer.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducer.java index 035bd6524..15b6dd3dd 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducer.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducer.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019-2020 Nordix Foundation. + * Modifications Copyright (C) 2019-2021 Nordix Foundation. * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ @@ -26,6 +26,7 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; import java.util.Optional; import java.util.Properties; import java.util.Set; +import java.util.regex.Pattern; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; @@ -59,6 +60,8 @@ public class ApexRestClientProducer extends ApexPluginsEventProducer { // The REST carrier properties private RestClientCarrierTechnologyParameters restProducerProperties; + private Pattern httpCodeFilterPattern = null; + /** * {@inheritDoc}. */ @@ -71,12 +74,13 @@ public class ApexRestClientProducer extends ApexPluginsEventProducer { if (!(producerParameters.getCarrierTechnologyParameters() instanceof RestClientCarrierTechnologyParameters)) { final String errorMessage = "specified producer properties are not applicable to REST client producer (" + this.name + ")"; - LOGGER.warn(errorMessage); throw new ApexEventException(errorMessage); } restProducerProperties = (RestClientCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters(); + this.httpCodeFilterPattern = Pattern.compile(restProducerProperties.getHttpCodeFilter()); + // Check if the HTTP method has been set if (restProducerProperties.getHttpMethod() == null) { restProducerProperties.setHttpMethod(RestPluginCarrierTechnologyParameters.HttpMethod.POST); @@ -88,7 +92,6 @@ public class ApexRestClientProducer extends ApexPluginsEventProducer { final String errorMessage = "specified HTTP method of \"" + restProducerProperties.getHttpMethod() + "\" is invalid, only HTTP methods \"POST\" and \"PUT\" are supported " + "for event sending on REST client producer (" + this.name + ")"; - LOGGER.warn(errorMessage); throw new ApexEventException(errorMessage); } @@ -128,8 +131,12 @@ public class ApexRestClientProducer extends ApexPluginsEventProducer { NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, untaggedUrl, response.readEntity(String.class)); - // Check that the request worked - if (response.getStatus() != Response.Status.OK.getStatusCode()) { + + // Match the return code + var isPass = httpCodeFilterPattern.matcher(String.valueOf(response.getStatus())); + + // Check that status code + if (!isPass.matches()) { final String errorMessage = "send of event to URL \"" + untaggedUrl + "\" using HTTP \"" + restProducerProperties.getHttpMethod() + "\" failed with status code " + response.getStatus(); throw new ApexEventRuntimeException(errorMessage); diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java index 85a6214fb..57f0c4342 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientProducerTest.java @@ -189,6 +189,35 @@ public class ApexRestClientProducerTest { arcp.stop(); } + @Test + public void testApexRestClientProducerPostEventAccepted() throws ApexEventException { + ApexRestClientProducer arcp = new ApexRestClientProducer(); + assertNotNull(arcp); + + EventHandlerParameters producerParameters = new EventHandlerParameters(); + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(rcctp); + + rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.PUT); + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod()); + assertEquals("RestClientConsumer", arcp.getName()); + + rcctp.setUrl("http://some.place.{key}.does.not/{tag}"); + Properties properties = new Properties(); + properties.put("tag", "exist"); + properties.put("key", "that"); + Mockito.doReturn(Response.Status.ACCEPTED.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn(responseMock).when(builderMock).put(Mockito.any()); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(builderMock).when(builderMock).headers(Mockito.any()); + Mockito.doReturn(targetMock).when(httpClientMock).target("http://some.place.that.does.not/exist"); + arcp.setClient(httpClientMock); + + arcp.sendEvent(123, properties, "EventName", "This is an ACCEPTED Event"); + arcp.stop(); + } + @Test public void testApexRestClientProducerPostEventCache() throws ApexEventException { ApexRestClientProducer arcp = new ApexRestClientProducer(); -- cgit 1.2.3-korg