diff options
Diffstat (limited to 'plugins/plugins-event/plugins-event-carrier')
20 files changed, 797 insertions, 557 deletions
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 49ca1e309..aaad52954 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,10 +25,13 @@ import java.util.EnumMap; import java.util.Map; import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.Response; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.apex.core.infrastructure.threading.ApplicationThreadFactory; import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; import org.onap.policy.apex.service.engine.event.ApexEventConsumer; @@ -52,6 +56,9 @@ public class ApexRestClientConsumer implements ApexEventConsumer, Runnable { // The amount of time to wait in milliseconds between checks that the consumer thread has stopped private static final long REST_CLIENT_WAIT_SLEEP_TIME = 50; + // The Key for property + private static final String HTTP_CODE_STATUS = "HTTP_CODE_STATUS"; + // The REST parameters read from the parameter service private RestClientCarrierTechnologyParameters restConsumerProperties; @@ -67,25 +74,30 @@ public class ApexRestClientConsumer implements ApexEventConsumer, Runnable { // The peer references for this event handler private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class); + // The pattern for filtering status code + private Pattern httpCodeFilterPattern = null; + // The consumer thread and stopping flag private Thread consumerThread; private boolean stopOrderedFlag = false; @Override public void init(final String consumerName, final EventHandlerParameters consumerParameters, - final ApexEventReceiver incomingEventReceiver) throws ApexEventException { + final ApexEventReceiver incomingEventReceiver) throws ApexEventException { this.eventReceiver = incomingEventReceiver; this.name = consumerName; // Check and get the REST Properties if (!(consumerParameters.getCarrierTechnologyParameters() instanceof RestClientCarrierTechnologyParameters)) { - final String errorMessage = "specified consumer properties are not applicable to REST client consumer (" - + this.name + ")"; + final String errorMessage = + "specified consumer properties are not applicable to REST client consumer (" + this.name + ")"; LOGGER.warn(errorMessage); throw new ApexEventException(errorMessage); } - restConsumerProperties = (RestClientCarrierTechnologyParameters) consumerParameters - .getCarrierTechnologyParameters(); + restConsumerProperties = + (RestClientCarrierTechnologyParameters) consumerParameters.getCarrierTechnologyParameters(); + + this.httpCodeFilterPattern = Pattern.compile(restConsumerProperties.getHttpCodeFilter()); // Check if the HTTP method has been set if (restConsumerProperties.getHttpMethod() == null) { @@ -94,8 +106,8 @@ public class ApexRestClientConsumer implements ApexEventConsumer, Runnable { if (!RestClientCarrierTechnologyParameters.HttpMethod.GET.equals(restConsumerProperties.getHttpMethod())) { 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 + ")"; + + "\" is invalid, only HTTP method \"GET\" " + + "is supported for event reception on REST client consumer (" + this.name + ")"; LOGGER.warn(errorMessage); throw new ApexEventException(errorMessage); } @@ -187,13 +199,17 @@ public class ApexRestClientConsumer implements ApexEventConsumer, Runnable { public void run() { try { final Response response = client.target(restConsumerProperties.getUrl()).request("application/json") - .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()).get(); + .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()).get(); - // Check that the event request worked - if (!Response.Status.Family.familyOf(response.getStatus()).equals(Response.Status.Family.SUCCESSFUL)) { + // Match the return code + Matcher isPass = httpCodeFilterPattern.matcher(String.valueOf(response.getStatus())); + + // Check that status code + if (!isPass.matches()) { final String errorMessage = "reception of event from URL \"" + restConsumerProperties.getUrl() - + "\" failed with status code " + response.getStatus() + " and message \"" - + response.readEntity(String.class) + "\""; + + "\" failed with status code " + response.getStatus() + " and message \"" + + response.readEntity(String.class) + "\""; + LOGGER.warn(errorMessage); throw new ApexEventRuntimeException(errorMessage); } @@ -201,14 +217,18 @@ public class ApexRestClientConsumer implements ApexEventConsumer, Runnable { final String eventJsonString = response.readEntity(String.class); // Check there is content - if (eventJsonString == null || eventJsonString.trim().length() == 0) { - final String errorMessage = "received an empty event from URL \"" + restConsumerProperties.getUrl() - + "\""; + if (StringUtils.isBlank(eventJsonString)) { + final String errorMessage = + "received an empty event from URL \"" + restConsumerProperties.getUrl() + "\""; throw new ApexEventRuntimeException(errorMessage); } + // build a key and value property in excutionProperties + Properties executionProperties = new Properties(); + executionProperties.put(HTTP_CODE_STATUS, response.getStatus()); + // Send the event into Apex - eventReceiver.receiveEvent(new Properties(), eventJsonString); + eventReceiver.receiveEvent(executionProperties, eventJsonString); } catch (final Exception e) { LOGGER.warn("error receiving events on thread {}", consumerThread.getName(), e); } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java index ca0084982..8d783c5cd 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java @@ -26,13 +26,22 @@ import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; + +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.common.parameters.ValidationStatus; import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +// @formatter:off /** * Apex parameters for REST as an event carrier technology with Apex as a REST client. * @@ -42,11 +51,21 @@ import org.onap.policy.common.utils.validation.ParameterValidationUtils; * parameter is mandatory. * <li>httpMethod: The HTTP method to use when sending events over REST, legal values are POST (default) and PUT. When * receiving events, the REST client plugin always uses the HTTP GET method. + * <li>httpHeaders, the HTTP headers to send on REST requests, optional parameter, defaults to none. + * <li>httpCodeFilter: a regular expression filter for returned HTTP codes, if the returned HTTP code passes this + * filter, then the request is assumed to have succeeded by the plugin, optional, defaults to allowing 2xx codes + * through, that is a regular expression of "[2][0-9][0-9]" * </ol> * * @author Joss Armstrong (joss.armstrong@ericsson.com) */ +//@formatter:on +@Setter +@Getter public class RestClientCarrierTechnologyParameters extends CarrierTechnologyParameters { + // Get a reference to the logger + private static final Logger LOGGER = LoggerFactory.getLogger(RestClientCarrierTechnologyParameters.class); + /** The supported HTTP methods. */ public enum HttpMethod { GET, PUT, POST, DELETE @@ -61,15 +80,22 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara /** The consumer plugin class for the REST carrier technology. */ public static final String RESTCLIENT_EVENT_CONSUMER_PLUGIN_CLASS = ApexRestClientConsumer.class.getName(); + /** The default HTTP code filter, allows 2xx HTTP codes through. */ + public static final String DEFAULT_HTTP_CODE_FILTER = "[2][0-9][0-9]"; + // Commonly occurring strings private static final String HTTP_HEADERS = "httpHeaders"; + private static final String HTTP_CODE_FILTER = "httpCodeFilter"; + + // Regular expression patterns for finding and checking keys in URLs + private static final Pattern patternProperKey = Pattern.compile("(?<=\\{)[^}]*(?=\\})"); + private static final Pattern patternErrorKey = + Pattern.compile("(\\{[^\\{}]*.?\\{)|(\\{[^\\{}]*$)|(\\}[^\\{}]*.?\\})|(^[^\\{}]*.?\\})|\\{\\s*\\}"); private String url = null; private HttpMethod httpMethod = null; private String[][] httpHeaders = null; - private static final Pattern patternProperKey = Pattern.compile("(?<=\\{)[^}]*(?=\\})"); - private static final Pattern patternErrorKey = Pattern.compile( - "(\\{[^\\{}]*.?\\{)|(\\{[^\\{}]*$)|(\\}[^\\{}]*.?\\})|(^[^\\{}]*.?\\})|\\{\\s*\\}"); + private String httpCodeFilter = DEFAULT_HTTP_CODE_FILTER; /** * Constructor to create a REST carrier technology parameters instance and register the instance with the parameter @@ -82,43 +108,6 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara this.setLabel(RESTCLIENT_CARRIER_TECHNOLOGY_LABEL); this.setEventProducerPluginClass(RESTCLIENT_EVENT_PRODUCER_PLUGIN_CLASS); this.setEventConsumerPluginClass(RESTCLIENT_EVENT_CONSUMER_PLUGIN_CLASS); - - } - - /** - * Gets the URL for the REST request. - * - * @return the URL - */ - public String getUrl() { - return url; - } - - /** - * Sets the URL for the REST request. - * - * @param incomingUrl the URL - */ - public void setUrl(final String incomingUrl) { - this.url = incomingUrl; - } - - /** - * Gets the HTTP method to use for the REST request. - * - * @return the HTTP method - */ - public HttpMethod getHttpMethod() { - return httpMethod; - } - - /** - * Sets the HTTP method to use for the REST request. - * - * @param httpMethod the HTTP method - */ - public void setHttpMethod(final HttpMethod httpMethod) { - this.httpMethod = httpMethod; } /** @@ -131,15 +120,6 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara } /** - * Gets the http headers for the REST request. - * - * @return the headers - */ - public String[][] getHttpHeaders() { - return httpHeaders; - } - - /** * Gets the http headers for the REST request as a multivalued map. * * @return the headers @@ -174,7 +154,7 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara * @return set of the tags */ public Set<String> getKeysFromUrl() { - Matcher matcher = patternProperKey.matcher(this.url); + Matcher matcher = patternProperKey.matcher(getUrl()); Set<String> key = new HashSet<>(); while (matcher.find()) { key.add(matcher.group()); @@ -183,34 +163,54 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara } /** - * Validate tags in url. + * {@inheritDoc}. + */ + @Override + public GroupValidationResult validate() { + GroupValidationResult result = super.validate(); + + result = validateUrl(result); + + result = validateHttpHeaders(result); + + return validateHttpCodeFilter(result); + } + + // @formatter:off + /** + * Validate the URL. + * + * <p>Checks: * http://www.blah.com/{par1/somethingelse (Missing end tag) use {[^\\{}]*$ * http://www.blah.com/{par1/{some}thingelse (Nested tag) use {[^}]*{ * http://www.blah.com/{par1}/some}thingelse (Missing start tag1) use }[^{}]*.} * http://www.blah.com/par1}/somethingelse (Missing start tag2) use }[^{}]*} * http://www.blah.com/{}/somethingelse (Empty tag) use {[\s]*} - * - * @return if url is legal + * @param result the result of the validation */ - public boolean validateTagInUrl() { - // Check url tag syntax error - Matcher matcher = patternErrorKey.matcher(this.url); - return (!matcher.find()); - } - - /** - * {@inheritDoc}. - */ - @Override - public GroupValidationResult validate() { - final GroupValidationResult result = super.validate(); - + // @formatter:on + private GroupValidationResult validateUrl(final GroupValidationResult result) { // Check if the URL has been set for event output if (getUrl() == null) { + result.setResult("url", ValidationStatus.INVALID, "no URL has been set for event sending on REST client"); + return result; + } + + Matcher matcher = patternErrorKey.matcher(getUrl()); + if (matcher.find()) { result.setResult("url", ValidationStatus.INVALID, - "no URL has been set for event sending on REST client"); + "no proper URL has been set for event sending on REST client"); } + return result; + } + + /** + * Validate the HTTP headers. + * + * @param result the result of the validation + */ + private GroupValidationResult validateHttpHeaders(final GroupValidationResult result) { if (httpHeaders == null) { return result; } @@ -220,21 +220,44 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, "HTTP header array entry is null"); } else if (httpHeader.length != 2) { result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, - "HTTP header array entries must have one key and one value: " - + Arrays.deepToString(httpHeader)); + "HTTP header array entries must have one key and one value: " + + Arrays.deepToString(httpHeader)); } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[0])) { result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, - "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader)); + "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader)); } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[1])) { result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, - "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader)); + "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader)); } } - if (!validateTagInUrl()) { - result.setResult("url", ValidationStatus.INVALID, - "no proper URL has been set for event sending on REST client"); + return result; + } + + /** + * Validate the HTTP code filter. + * + * @param result the result of the validation + */ + public GroupValidationResult validateHttpCodeFilter(final GroupValidationResult result) { + if (httpCodeFilter == null) { + httpCodeFilter = DEFAULT_HTTP_CODE_FILTER; + + } else if (StringUtils.isBlank(httpCodeFilter)) { + result.setResult(HTTP_CODE_FILTER, ValidationStatus.INVALID, + "HTTP code filter must be specified as a three digit regular expression"); + } else { + try { + Pattern.compile(httpCodeFilter); + } catch (PatternSyntaxException pse) { + String message = + "Invalid HTTP code filter, the filter must be specified as a three digit regular expression: " + + pse.getMessage(); + result.setResult(HTTP_CODE_FILTER, ValidationStatus.INVALID, message); + LOGGER.debug(message, pse); + } } + return result; } @@ -244,6 +267,6 @@ public class RestClientCarrierTechnologyParameters extends CarrierTechnologyPara @Override public String toString() { return "RestClientCarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + ", httpHeaders=" - + Arrays.deepToString(httpHeaders) + "]"; + + Arrays.deepToString(httpHeaders) + ", httpCodeFilter=" + httpCodeFilter + "]"; } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java index ae75dd7e5..6face9e50 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/ApexRestClientConusmerTest.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 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========================================================= */ @@ -33,6 +34,7 @@ import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response; +import org.junit.After; import org.junit.Test; import org.mockito.Mock; import org.mockito.Mockito; @@ -61,8 +63,13 @@ public class ApexRestClientConusmerTest { @Mock private Response responseMock; + @After + public void after() { + System.setOut(stdout); + } + @Test - public void testApexRestClientConusmerErrors() { + public void testApexRestClientConsumerErrors() throws ApexEventException { MockitoAnnotations.initMocks(this); ApexRestClientConsumer arcc = new ApexRestClientConsumer(); @@ -92,51 +99,41 @@ public class ApexRestClientConusmerTest { } rcctp.setHttpMethod(null); - try { - arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); + rcctp.setHttpCodeFilter("zzz"); - assertEquals("RestClientConsumer", arcc.getName()); + arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); - arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + assertEquals("RestClientConsumer", arcc.getName()); + + arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.BAD_REQUEST.getStatusCode()).when(responseMock).getStatus(); Mockito.doReturn(responseMock).when(builderMock).get(); Mockito.doReturn(builderMock).when(targetMock).request("application/json"); Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getHttpCodeFilter()); arcc.setClient(httpClientMock); - try { - // We have not set the URL, this test should not receive any events - arcc.start(); - ThreadUtilities.sleep(200); - arcc.stop(); - - assertEquals(0, incomingEventReceiver.getEventCount()); - } catch (Exception e) { - fail("test should not throw an exception"); - } + // We have not set the URL, this test should not receive any events + arcc.start(); + ThreadUtilities.sleep(200); + arcc.stop(); + assertEquals(0, incomingEventReceiver.getEventCount()); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); - try { - // We have not set the URL, this test should not receive any events - arcc.start(); - ThreadUtilities.sleep(200); - arcc.stop(); - assertEquals(0, incomingEventReceiver.getEventCount()); - } catch (Exception e) { - fail("test should not throw an exception"); - } + // We have not set the URL, this test should not receive any events + arcc.start(); + ThreadUtilities.sleep(200); + arcc.stop(); + assertEquals(0, incomingEventReceiver.getEventCount()); } @Test - public void testApexRestClientConusmerHttpError() { + public void testApexRestClientConsumerHttpError() throws ApexEventException { MockitoAnnotations.initMocks(this); ApexRestClientConsumer arcc = new ApexRestClientConsumer(); @@ -146,50 +143,39 @@ public class ApexRestClientConusmerTest { RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(rcctp); rcctp.setUrl("http://some.place.that.does.not/exist"); + rcctp.setHttpCodeFilter("[1-5][0][0-5]"); SupportApexEventReceiver incomingEventReceiver = new SupportApexEventReceiver(); - try { - arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); + arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); - assertEquals("RestClientConsumer", arcc.getName()); + assertEquals("[1-5][0][0-5]", rcctp.getHttpCodeFilter()); - arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + assertEquals("RestClientConsumer", arcc.getName()); + + arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); Mockito.doReturn(Response.Status.BAD_REQUEST.getStatusCode()).when(responseMock).getStatus(); Mockito.doReturn(responseMock).when(builderMock).get(); Mockito.doReturn(builderMock).when(targetMock).request("application/json"); Mockito.doReturn(builderMock).when(builderMock).headers(Mockito.any()); Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getHttpCodeFilter()); arcc.setClient(httpClientMock); ByteArrayOutputStream outContent = new ByteArrayOutputStream(); System.setOut(new PrintStream(outContent)); - try { - // We have not set the URL, this test should not receive any events - arcc.start(); - ThreadUtilities.sleep(200); - arcc.stop(); - - assertEquals(0, incomingEventReceiver.getEventCount()); - } catch (Exception e) { - fail("test should not throw an exception"); - } - - final String outString = outContent.toString(); - System.setOut(stdout); - - assertTrue(outString.contains( - "reception of event from URL \"http://some.place.that.does.not/exist\" failed with status code 400")); + // We have not set the URL, this test should not receive any events + arcc.start(); + ThreadUtilities.sleep(200); + arcc.stop(); + assertEquals(0, incomingEventReceiver.getEventCount()); } @Test - public void testApexRestClientConusmerJsonError() { + public void testApexRestClientConsumerJsonError() { MockitoAnnotations.initMocks(this); ApexRestClientConsumer arcc = new ApexRestClientConsumer(); @@ -199,6 +185,7 @@ public class ApexRestClientConusmerTest { SupportApexEventReceiver incomingEventReceiver = new SupportApexEventReceiver(); RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(rcctp); + rcctp.setHttpCodeFilter("[1-5][0][0-5]"); try { arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); @@ -207,6 +194,7 @@ public class ApexRestClientConusmerTest { assertEquals("RestClientConsumer", arcc.getName()); arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); } catch (ApexEventException e) { fail("test should not throw an exception"); @@ -218,30 +206,26 @@ public class ApexRestClientConusmerTest { Mockito.doReturn(builderMock).when(targetMock).request("application/json"); Mockito.doReturn(builderMock).when(builderMock).headers(Mockito.any()); Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getHttpCodeFilter()); arcc.setClient(httpClientMock); ByteArrayOutputStream outContent = new ByteArrayOutputStream(); System.setOut(new PrintStream(outContent)); - try { - // We have not set the URL, this test should not receive any events - arcc.start(); - ThreadUtilities.sleep(200); - arcc.stop(); + // We have not set the URL, this test should not receive any events + arcc.start(); + ThreadUtilities.sleep(200); + arcc.stop(); - assertEquals(0, incomingEventReceiver.getEventCount()); - } catch (Exception e) { - fail("test should not throw an exception"); - } + assertEquals(0, incomingEventReceiver.getEventCount()); final String outString = outContent.toString(); - System.setOut(stdout); assertTrue(outString.contains("received an empty event from URL \"http://some.place.that.does.not/exist\"")); } @Test - public void testApexRestClientConusmerJsonEmpty() { + public void testApexRestClientConsumerJsonEmpty() throws ApexEventException { MockitoAnnotations.initMocks(this); ApexRestClientConsumer arcc = new ApexRestClientConsumer(); @@ -251,18 +235,16 @@ public class ApexRestClientConusmerTest { SupportApexEventReceiver incomingEventReceiver = new SupportApexEventReceiver(); RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(rcctp); + rcctp.setHttpCodeFilter("[1-5][0][0-5]"); - try { - arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); + arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); - assertEquals("RestClientConsumer", arcc.getName()); + assertEquals("RestClientConsumer", arcc.getName()); - arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); @@ -271,30 +253,26 @@ public class ApexRestClientConusmerTest { Mockito.doReturn(builderMock).when(targetMock).request("application/json"); Mockito.doReturn(builderMock).when(builderMock).headers(Mockito.any()); Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getHttpCodeFilter()); arcc.setClient(httpClientMock); ByteArrayOutputStream outContent = new ByteArrayOutputStream(); System.setOut(new PrintStream(outContent)); - try { - // We have not set the URL, this test should not receive any events - arcc.start(); - ThreadUtilities.sleep(200); - arcc.stop(); + // We have not set the URL, this test should not receive any events + arcc.start(); + ThreadUtilities.sleep(200); + arcc.stop(); - assertEquals(0, incomingEventReceiver.getEventCount()); - } catch (Exception e) { - fail("test should not throw an exception"); - } + assertEquals(0, incomingEventReceiver.getEventCount()); final String outString = outContent.toString(); - System.setOut(stdout); assertTrue(outString.contains("received an empty event from URL \"http://some.place.that.does.not/exist\"")); } @Test - public void testApexRestClientConusmerJsonOk() { + public void testApexRestClientConsumerJsonOk() throws ApexEventException { MockitoAnnotations.initMocks(this); ApexRestClientConsumer arcc = new ApexRestClientConsumer(); @@ -304,18 +282,16 @@ public class ApexRestClientConusmerTest { SupportApexEventReceiver incomingEventReceiver = new SupportApexEventReceiver(); RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); consumerParameters.setCarrierTechnologyParameters(rcctp); + rcctp.setHttpCodeFilter("[1-5][0][0-5]"); - try { - arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); + arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); - assertEquals("RestClientConsumer", arcc.getName()); + assertEquals("RestClientConsumer", arcc.getName()); - arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); @@ -324,17 +300,60 @@ public class ApexRestClientConusmerTest { Mockito.doReturn(builderMock).when(targetMock).request("application/json"); Mockito.doReturn(builderMock).when(builderMock).headers(Mockito.any()); Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getHttpCodeFilter()); arcc.setClient(httpClientMock); + // We have not set the URL, this test should not receive any events + arcc.start(); + ThreadUtilities.sleep(200); + arcc.stop(); + + assertEquals("This is an event", incomingEventReceiver.getLastEvent()); + } + + @Test + public void testApexRestClientConsumerInvalidStatusCode() throws ApexEventException { + MockitoAnnotations.initMocks(this); + + ApexRestClientConsumer arcc = new ApexRestClientConsumer(); + assertNotNull(arcc); + + EventHandlerParameters consumerParameters = new EventHandlerParameters(); + SupportApexEventReceiver incomingEventReceiver = new SupportApexEventReceiver(); + RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(rcctp); + rcctp.setHttpCodeFilter("zzz"); + + arcc.init("RestClientConsumer", consumerParameters, incomingEventReceiver); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod()); + + assertEquals("RestClientConsumer", arcc.getName()); + + arcc.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + + assertEquals(null, arcc.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + + rcctp.setUrl("http://some.place.that.does.not/exist"); + Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); + Mockito.doReturn("This is an event").when(responseMock).readEntity(String.class); + Mockito.doReturn(responseMock).when(builderMock).get(); + Mockito.doReturn(builderMock).when(targetMock).request("application/json"); + Mockito.doReturn(builderMock).when(builderMock).headers(Mockito.any()); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); + Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getHttpCodeFilter()); + arcc.setClient(httpClientMock); + + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outContent)); + try { // We have not set the URL, this test should not receive any events arcc.start(); ThreadUtilities.sleep(200); arcc.stop(); - - assertEquals("This is an event", incomingEventReceiver.getLastEvent()); } catch (Exception e) { - fail("test should not throw an exception"); + // test invalid status code + assertEquals("received an invalid status code \"200\"", e.getMessage()); } } } 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 d57bdd769..b67a339aa 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 @@ -27,6 +27,8 @@ import static org.junit.Assert.fail; import ch.qos.logback.classic.Level; +import java.util.Properties; + import javax.ws.rs.client.Client; import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.client.WebTarget; @@ -42,13 +44,9 @@ import org.onap.policy.apex.service.engine.event.SynchronousEventCache; import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; -import org.onap.policy.common.parameters.GroupValidationResult; -import org.onap.policy.common.parameters.ParameterException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.Properties; - /** * Test the ApexRestClientProducer class. * @@ -69,7 +67,7 @@ public class ApexRestClientProducerTest { private Response responseMock; @Test - public void testApexRestClientProducerErrors() { + public void testApexRestClientProducerErrors() throws ApexEventException { ApexRestClientProducer arcp = new ApexRestClientProducer(); assertNotNull(arcp); @@ -96,53 +94,33 @@ public class ApexRestClientProducerTest { } rcctp.setHttpMethod(null); - try { - arcp.init("RestClientConsumer", producerParameters); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); - - assertEquals("RestClientConsumer", arcp.getName()); - - arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); - - arcp.stop(); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); + assertEquals("RestClientConsumer", arcp.getName()); + arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + arcp.stop(); rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST); - try { - arcp.init("RestClientConsumer", producerParameters); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); - - assertEquals("RestClientConsumer", arcp.getName()); + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); + assertEquals("RestClientConsumer", arcp.getName()); + arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + arcp.stop(); - arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); - - arcp.stop(); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.PUT); - try { - arcp.init("RestClientConsumer", producerParameters); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod()); - - assertEquals("RestClientConsumer", arcp.getName()); - - arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); - assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); - - arcp.stop(); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod()); + assertEquals("RestClientConsumer", arcp.getName()); + arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null); + assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); + arcp.stop(); } @Test - public void testApexRestClientProducerPutEvent() { + public void testApexRestClientProducerPutEvent() throws ApexEventException { MockitoAnnotations.initMocks(this); ApexRestClientProducer arcp = new ApexRestClientProducer(); @@ -153,14 +131,9 @@ public class ApexRestClientProducerTest { producerParameters.setCarrierTechnologyParameters(rcctp); rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.PUT); - try { - arcp.init("RestClientConsumer", producerParameters); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod()); - - assertEquals("RestClientConsumer", arcp.getName()); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod()); + assertEquals("RestClientConsumer", arcp.getName()); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); @@ -170,16 +143,12 @@ public class ApexRestClientProducerTest { Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); arcp.setClient(httpClientMock); - try { - arcp.sendEvent(123, null, "EventName", "This is an Event"); - arcp.stop(); - } catch (Exception ex) { - fail("test should not throw an exception"); - } + arcp.sendEvent(123, null, "EventName", "This is an Event"); + arcp.stop(); } @Test - public void testApexRestClientProducerPostEventFail() { + public void testApexRestClientProducerPostEventFail() throws ApexEventException { MockitoAnnotations.initMocks(this); ApexRestClientProducer arcp = new ApexRestClientProducer(); @@ -190,14 +159,9 @@ public class ApexRestClientProducerTest { producerParameters.setCarrierTechnologyParameters(rcctp); rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST); - try { - arcp.init("RestClientConsumer", producerParameters); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); - - assertEquals("RestClientConsumer", arcp.getName()); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); + assertEquals("RestClientConsumer", arcp.getName()); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); @@ -225,7 +189,7 @@ public class ApexRestClientProducerTest { } @Test - public void testApexRestClientProducerPostEventOK() { + public void testApexRestClientProducerPostEventOK() throws ApexEventException { MockitoAnnotations.initMocks(this); ApexRestClientProducer arcp = new ApexRestClientProducer(); @@ -236,14 +200,9 @@ public class ApexRestClientProducerTest { producerParameters.setCarrierTechnologyParameters(rcctp); rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.PUT); - try { - arcp.init("RestClientConsumer", producerParameters); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod()); - - assertEquals("RestClientConsumer", arcp.getName()); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod()); + assertEquals("RestClientConsumer", arcp.getName()); System.out.println("fail test"); rcctp.setUrl("http://some.place.{key}.does.not/{tag}"); @@ -257,16 +216,12 @@ public class ApexRestClientProducerTest { Mockito.doReturn(targetMock).when(httpClientMock).target("http://some.place.that.does.not/exist"); arcp.setClient(httpClientMock); - try { - arcp.sendEvent(123, properties, "EventName", "This is an Event"); - arcp.stop(); - } catch (Exception ex) { - fail("test should not throw an exception"); - } + arcp.sendEvent(123, properties, "EventName", "This is an Event"); + arcp.stop(); } @Test - public void testApexRestClientProducerPostEventCache() { + public void testApexRestClientProducerPostEventCache() throws ApexEventException { MockitoAnnotations.initMocks(this); ApexRestClientProducer arcp = new ApexRestClientProducer(); @@ -283,14 +238,9 @@ public class ApexRestClientProducerTest { 1000); arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, cache); assertEquals(cache, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); - try { - arcp.init("RestClientConsumer", producerParameters); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); - - assertEquals("RestClientConsumer", arcp.getName()); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); + assertEquals("RestClientConsumer", arcp.getName()); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); @@ -300,16 +250,12 @@ public class ApexRestClientProducerTest { Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); arcp.setClient(httpClientMock); - try { - arcp.sendEvent(123, null, "EventName", "This is an Event"); - arcp.stop(); - } catch (Exception e) { - fail("test should not throw an exception"); - } + arcp.sendEvent(123, null, "EventName", "This is an Event"); + arcp.stop(); } @Test - public void testApexRestClientProducerPostEventCacheTrace() { + public void testApexRestClientProducerPostEventCacheTrace() throws ApexEventException { MockitoAnnotations.initMocks(this); ch.qos.logback.classic.Logger classicLogger = (ch.qos.logback.classic.Logger) LOGGER; @@ -329,14 +275,9 @@ public class ApexRestClientProducerTest { 1000); arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, cache); assertEquals(cache, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS)); - try { - arcp.init("RestClientConsumer", producerParameters); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); - - assertEquals("RestClientConsumer", arcp.getName()); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); + assertEquals("RestClientConsumer", arcp.getName()); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus(); @@ -346,16 +287,12 @@ public class ApexRestClientProducerTest { Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl()); arcp.setClient(httpClientMock); - try { - arcp.sendEvent(123, null, "EventName", "This is an Event"); - arcp.stop(); - } catch (Exception e) { - fail("test should not throw an exception"); - } + arcp.sendEvent(123, null, "EventName", "This is an Event"); + arcp.stop(); } @Test - public void testApexRestClientProducerHttpError() { + public void testApexRestClientProducerHttpError() throws ApexEventException { MockitoAnnotations.initMocks(this); ApexRestClientProducer arcp = new ApexRestClientProducer(); @@ -366,14 +303,9 @@ public class ApexRestClientProducerTest { producerParameters.setCarrierTechnologyParameters(rcctp); rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST); - try { - arcp.init("RestClientConsumer", producerParameters); - assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); - - assertEquals("RestClientConsumer", arcp.getName()); - } catch (ApexEventException e) { - fail("test should not throw an exception"); - } + arcp.init("RestClientConsumer", producerParameters); + assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod()); + assertEquals("RestClientConsumer", arcp.getName()); rcctp.setUrl("http://some.place.that.does.not/exist"); Mockito.doReturn(Response.Status.BAD_REQUEST.getStatusCode()).when(responseMock).getStatus(); @@ -393,4 +325,4 @@ public class ApexRestClientProducerTest { e.getMessage()); } } -} +}
\ No newline at end of file diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java index f982059f7..9db565ba8 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParametersTest.java @@ -87,39 +87,69 @@ public class RestClientCarrierTechnologyParametersTest { } @Test - public void testRestClientCarrierTechnologyParametersOk() { + public void testRestClientCarrierTechnologyParameterFilterInvalid() { ApexCommandLineArguments arguments = new ApexCommandLineArguments(); - arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json"); + arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json"); arguments.setRelativeFileRoot("."); try { + new ApexParameterHandler().getParameters(arguments); ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); RestClientCarrierTechnologyParameters rrctp0 = (RestClientCarrierTechnologyParameters) parameters .getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters(); - assertEquals(0, rrctp0.getHttpHeaders().length); - - RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters - .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters(); - assertEquals(3, rrctp1.getHttpHeaders().length); - assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0)); - assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0)); - assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0)); - - rrctp1.setHttpHeaders(null); - assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap()); + fail("test should throw an exception here"); } catch (ParameterException pe) { - fail("test should not throw an exception"); + assertTrue(pe.getMessage().contains("Invalid HTTP code filter, the filter must be specified as a three digit regular expression: ")); } } @Test + public void testRestClientCarrierTechnologyParametersOk() throws ParameterException { + ApexCommandLineArguments arguments = new ApexCommandLineArguments(); + arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json"); + arguments.setRelativeFileRoot("."); + + ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + RestClientCarrierTechnologyParameters rrctp0 = (RestClientCarrierTechnologyParameters) parameters + .getEventInputParameters().get("RestClientConsumer0").getCarrierTechnologyParameters(); + assertEquals(0, rrctp0.getHttpHeaders().length); + + RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters + .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters(); + assertEquals(3, rrctp1.getHttpHeaders().length); + assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0)); + assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0)); + assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0)); + + rrctp1.setHttpHeaders(null); + assertEquals(null, rrctp1.getHttpHeadersAsMultivaluedMap()); + } + + @Test + public void testRestClientCarrierTechnologyHttpCodeFilterOk() throws ParameterException { + ApexCommandLineArguments arguments = new ApexCommandLineArguments(); + arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json"); + arguments.setRelativeFileRoot("."); + + ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + RestClientCarrierTechnologyParameters rrctp1 = (RestClientCarrierTechnologyParameters) parameters + .getEventInputParameters().get("RestClientConsumer1").getCarrierTechnologyParameters(); + assertEquals("[1-5][0][0-5]", rrctp1.getHttpCodeFilter()); + } + + @Test public void testGettersAndSetters() { RestClientCarrierTechnologyParameters rrctp = new RestClientCarrierTechnologyParameters(); rrctp.setUrl("http://some.where"); assertEquals("http://some.where", rrctp.getUrl()); + rrctp.setHttpCodeFilter("[1-5][0][0-5]"); + assertEquals("[1-5][0][0-5]", rrctp.getHttpCodeFilter()); + String[][] httpHeaders = new String[2][2]; httpHeaders[0][0] = "aaa"; httpHeaders[0][1] = "bbb"; @@ -146,8 +176,8 @@ public class RestClientCarrierTechnologyParametersTest { assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod()); assertEquals("RestClientCarrierTechnologyParameters " - + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]]]", - rrctp.toString()); + + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]], " + + "httpCodeFilter=[1-5][0][0-5]]", rrctp.toString()); } @Test @@ -170,10 +200,10 @@ public class RestClientCarrierTechnologyParametersTest { assertEquals("ccc", rrctp.getHttpHeaders()[1][0]); assertEquals("ddd", rrctp.getHttpHeaders()[1][1]); - assertEquals(true, rrctp.validateTagInUrl()); + assertEquals(true, rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/is{that}.{one}"); - assertEquals(true, rrctp.validateTagInUrl()); + assertEquals(true, rrctp.validate().isValid()); Set<String> keymap = rrctp.getKeysFromUrl(); assertEquals(true, keymap.contains("place")); @@ -181,19 +211,18 @@ public class RestClientCarrierTechnologyParametersTest { assertEquals(true, keymap.contains("one")); rrctp.setUrl("http://{place.{that}/{is}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/{is}.{not}/{what}.{exist"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); rrctp.setUrl("http://place.that/is.not/what.{exist"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); rrctp.setUrl("http://place}.{that}/{is}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/is}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/{}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/{ }.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validateTagInUrl()); - + assertEquals(false, rrctp.validate().isValid()); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json new file mode 100644 index 000000000..490767186 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json @@ -0,0 +1,75 @@ +{ + "engineServiceParameters": { + "name": "MyApexEngine", + "version": "0.0.1", + "id": 45, + "instanceCount": 4, + "deploymentPort": 12561, + "policyModelFileName": "src/test/resources/policymodels/RequestorModel.json", + "engineParameters": { + "executorParameters": { + "JAVASCRIPT": { + "parameterClassName": "org.onap.policy.apex.plugins.executor.javascript.JavascriptExecutorParameters" + } + } + } + }, + "eventInputParameters": { + "RestClientConsumer0": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTCLIENT", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restclient.RestClientCarrierTechnologyParameters", + "parameters": { + "url": "http://localhost:32801/TestRESTRequestor/apex/event/GetEvent", + "httpMethod": "GET", + "httpHeaders" : [ + ["aaa", "bbb"], + ["ccc", "ddd"] + ], + "httpCodeFilter" : "*\\.*" + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + } + }, + "RestClientConsumer1": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTCLIENT", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restclient.RestClientCarrierTechnologyParameters", + "parameters": { + "url": "http://localhost:32801/TestRESTRequestor/apex/event/GetEvent", + "httpMethod": "GET", + "httpHeaders" : [ + ["aaa", "bbb"], + ["ccc", "ddd"] + ], + "httpCodeFilter" : " " + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + } + } + }, + "eventOutputParameters": { + "RestClientProducer0": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTCLIENT", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restclient.RestClientCarrierTechnologyParameters" + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + } + }, + "RestClientProducer1": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTCLIENT", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restclient.RestClientCarrierTechnologyParameters" + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + } + } + } +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json index a3969aead..be0729f6c 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderBadList.json @@ -25,7 +25,8 @@ "httpHeaders" : [ ["aaa", "bbb"], ["ccc", "ddd"], - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { @@ -41,7 +42,8 @@ "httpMethod": "GET", "httpHeaders" : [ , - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json index 790a9bc38..c41ff1cb4 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderNotKvPairs.json @@ -24,7 +24,8 @@ "httpMethod": "GET", "httpHeaders" : [ ["aaa", "bbb", "ccc"] - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { @@ -40,7 +41,8 @@ "httpMethod": "GET", "httpHeaders" : [ ["aaa"] - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json index 4b99ca0c2..bc9988633 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderNulls.json @@ -25,7 +25,8 @@ "httpHeaders" : [ [null, "bbb"], ["ccc", "ddd"] - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } } }, @@ -40,7 +41,8 @@ ["aaa", "bbb"], ["ccc", null], ["eee", "fff"] - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json index 1c4a39d99..3586b4481 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/test/resources/prodcons/RESTClientWithHTTPHeaderOK.json @@ -23,7 +23,8 @@ "url": "http://localhost:32801/TestRESTRequestor/apex/event/GetEvent", "httpMethod": "GET", "httpHeaders" : [ - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { @@ -41,7 +42,8 @@ ["aaa", "bbb"], ["ccc", "ddd"], ["eee", "fff"] - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { @@ -58,7 +60,8 @@ "url": "http://localhost:32801/TestRESTRequestor/apex/event/GetEvent", "httpMethod": "GET", "httpHeaders" : [ - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { @@ -76,7 +79,8 @@ ["aaa", "bbb"], ["ccc", "ddd"], ["eee", "fff"] - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumer.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumer.java index 82d16273f..f60abc24c 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumer.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumer.java @@ -36,11 +36,15 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.core.Response; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.apex.core.infrastructure.threading.ApplicationThreadFactory; import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; import org.onap.policy.apex.service.engine.event.ApexEventConsumer; @@ -67,6 +71,9 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { // stopped private static final long REST_REQUESTOR_WAIT_SLEEP_TIME = 50; + // The Key for property + private static final String HTTP_CODE_STATUS = "HTTP_CODE_STATUS"; + // The REST parameters read from the parameter service private RestRequestorCarrierTechnologyParameters restConsumerProperties; @@ -104,27 +111,30 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { private String untaggedUrl = null; + // The pattern for filtering status code + private Pattern httpCodeFilterPattern = null; + @Override public void init(final String consumerName, final EventHandlerParameters consumerParameters, - final ApexEventReceiver incomingEventReceiver) throws ApexEventException { + final ApexEventReceiver incomingEventReceiver) throws ApexEventException { this.eventReceiver = incomingEventReceiver; this.name = consumerName; // Check and get the REST Properties if (!(consumerParameters - .getCarrierTechnologyParameters() instanceof RestRequestorCarrierTechnologyParameters)) { - final String errorMessage = "specified consumer properties are not applicable to REST Requestor consumer (" - + this.name + ")"; + .getCarrierTechnologyParameters() instanceof RestRequestorCarrierTechnologyParameters)) { + final String errorMessage = + "specified consumer properties are not applicable to REST Requestor consumer (" + this.name + ")"; LOGGER.warn(errorMessage); throw new ApexEventException(errorMessage); } - restConsumerProperties = (RestRequestorCarrierTechnologyParameters) consumerParameters - .getCarrierTechnologyParameters(); + restConsumerProperties = + (RestRequestorCarrierTechnologyParameters) consumerParameters.getCarrierTechnologyParameters(); // Check if we are in peered mode if (!consumerParameters.isPeeredMode(EventHandlerPeeredMode.REQUESTOR)) { final String errorMessage = "REST Requestor consumer (" + this.name - + ") must run in peered requestor mode with a REST Requestor producer"; + + ") must run in peered requestor mode with a REST Requestor producer"; LOGGER.warn(errorMessage); throw new ApexEventException(errorMessage); } @@ -132,7 +142,7 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { // Check if the HTTP method has been set if (restConsumerProperties.getHttpMethod() == null) { restConsumerProperties - .setHttpMethod(RestRequestorCarrierTechnologyParameters.DEFAULT_REQUESTOR_HTTP_METHOD); + .setHttpMethod(RestRequestorCarrierTechnologyParameters.DEFAULT_REQUESTOR_HTTP_METHOD); } // Check if the HTTP URL has been set @@ -151,6 +161,8 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { throw new ApexEventException(errorMessage, e); } + this.httpCodeFilterPattern = Pattern.compile(restConsumerProperties.getHttpCodeFilter()); + // Set the requestor timeout if (consumerParameters.getPeerTimeout(EventHandlerPeeredMode.REQUESTOR) != 0) { restRequestTimeout = consumerParameters.getPeerTimeout(EventHandlerPeeredMode.REQUESTOR); @@ -159,7 +171,7 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { // Check if HTTP headers has been set if (restConsumerProperties.checkHttpHeadersSet()) { LOGGER.debug("REST Requestor consumer has http headers ({}): {}", this.name, - Arrays.deepToString(restConsumerProperties.getHttpHeaders())); + Arrays.deepToString(restConsumerProperties.getHttpHeaders())); } // Initialize the HTTP client @@ -177,8 +189,8 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { try { incomingRestRequestQueue.add(restRequest); } catch (final Exception requestException) { - final String errorMessage = "could not queue request \"" + restRequest + "\" on REST Requestor consumer (" - + this.name + ")"; + final String errorMessage = + "could not queue request \"" + restRequest + "\" on REST Requestor consumer (" + this.name + ")"; LOGGER.warn(errorMessage, requestException); throw new ApexEventRuntimeException(errorMessage); } @@ -238,8 +250,8 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { while (consumerThread.isAlive() && !stopOrderedFlag) { try { // Take the next event from the queue - final ApexRestRequest restRequest = incomingRestRequestQueue.poll(REST_REQUESTOR_WAIT_SLEEP_TIME, - TimeUnit.MILLISECONDS); + final ApexRestRequest restRequest = + incomingRestRequestQueue.poll(REST_REQUESTOR_WAIT_SLEEP_TIME, TimeUnit.MILLISECONDS); if (restRequest == null) { // Poll timed out, check for request timeouts timeoutExpiredRequests(); @@ -256,8 +268,7 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { op.filter(inputProperty::contains) .orElseThrow(() -> new ApexEventRuntimeException( "key\"" + op.get() + "\"specified on url \"" + restConsumerProperties.getUrl() - + "\"not found in execution properties passed by the current policy")) - ); + + "\"not found in execution properties passed by the current policy"))); untaggedUrl = names.stream().reduce(untaggedUrl, (acc, str) -> acc.replace("{" + str + "}", (String) inputExecutionProperties.get(str))); @@ -301,8 +312,8 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { // Interrupt timed out requests and remove them from the ongoing map for (final ApexRestRequest timedoutRequest : timedoutRequestList) { - final String errorMessage = "REST Requestor consumer (" + this.name + "), REST request timed out: " - + timedoutRequest; + final String errorMessage = + "REST Requestor consumer (" + this.name + "), REST request timed out: " + timedoutRequest; LOGGER.warn(errorMessage); ongoingRestRequestMap.remove(timedoutRequest); @@ -356,12 +367,14 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { // Execute the REST request final Response response = sendEventAsRestRequest(untaggedUrl); - // Check that the event request worked - if (!Response.Status.Family.familyOf(response.getStatus()).equals(Response.Status.Family.SUCCESSFUL)) { - final String errorMessage = "reception of response to \"" + request + "\" from URL \"" - + untaggedUrl + "\" failed with status code " - + response.getStatus() + " and message \"" + response.readEntity(String.class) - + "\""; + // Match the return code + Matcher isPass = httpCodeFilterPattern.matcher(String.valueOf(response.getStatus())); + + // Check that the request worked + if (!isPass.matches()) { + final String errorMessage ="reception of event from URL \"" + restConsumerProperties.getUrl() + + "\" failed with status code " + response.getStatus() + " and message \"" + + response.readEntity(String.class) + "\""; throw new ApexEventRuntimeException(errorMessage); } @@ -369,14 +382,18 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { final String eventJsonString = response.readEntity(String.class); // Check there is content - if (eventJsonString == null || eventJsonString.trim().length() == 0) { - final String errorMessage = "received an enpty response to \"" + request + "\" from URL \"" - + untaggedUrl + "\""; + if (StringUtils.isBlank(eventJsonString)) { + final String errorMessage = + "received an empty response to \"" + request + "\" from URL \"" + untaggedUrl + "\""; throw new ApexEventRuntimeException(errorMessage); } + // build a key and value property in excutionProperties + Properties executionProperties = new Properties(); + executionProperties.put(HTTP_CODE_STATUS, response.getStatus()); + // Send the event into Apex - eventReceiver.receiveEvent(request.getExecutionId(), new Properties(), eventJsonString); + eventReceiver.receiveEvent(request.getExecutionId(), executionProperties, eventJsonString); synchronized (eventsReceivedLock) { eventsReceived++; @@ -403,24 +420,20 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { * @return the response to the REST request */ public Response sendEventAsRestRequest(String untaggedUrl) { + Builder headers = client.target(untaggedUrl).request(APPLICATION_JSON) + .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()); switch (restConsumerProperties.getHttpMethod()) { case GET: - return client.target(untaggedUrl).request(APPLICATION_JSON) - .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()).get(); + return headers.get(); case PUT: - return client.target(untaggedUrl).request(APPLICATION_JSON) - .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()) - .put(Entity.json(request.getEvent())); + return headers.put(Entity.json(request.getEvent())); case POST: - return client.target(untaggedUrl).request(APPLICATION_JSON) - .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()) - .post(Entity.json(request.getEvent())); + return headers.post(Entity.json(request.getEvent())); case DELETE: - return client.target(untaggedUrl).request(APPLICATION_JSON) - .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()).delete(); + return headers.delete(); default: break; diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java index d583b790e..81aa1a74a 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java @@ -26,14 +26,20 @@ import java.util.HashSet; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import javax.ws.rs.core.MultivaluedHashMap; import javax.ws.rs.core.MultivaluedMap; +import lombok.Getter; +import lombok.Setter; +import org.apache.commons.lang3.StringUtils; import org.onap.policy.apex.service.parameters.carriertechnology.CarrierTechnologyParameters; import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.common.parameters.ValidationStatus; import org.onap.policy.common.utils.validation.ParameterValidationUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; // @formatter:off /** @@ -46,14 +52,21 @@ import org.onap.policy.common.utils.validation.ParameterValidationUtils; * This parameter is mandatory. * <li>httpMethod: The HTTP method to use when making requests over REST, legal values are GET (default), * POST, PUT, and DELETE. - * <li>restRequestTimeout: The time in milliseconds to wait for a REST request to complete. - * <li>restRequestHeader: The necessary header needed + * <li>httpHeaders, the HTTP headers to send on REST requests, optional parameter, defaults to none. + * <li>httpCodeFilter: a regular expression filter for returned HTTP codes, if the returned HTTP code passes this + * filter, then the request is assumed to have succeeded by the plugin, optional, defaults to allowing 2xx codes + * through, that is a regular expression of "[2][0-9][0-9]" * </ol> * * @author Liam Fallon (liam.fallon@ericsson.com) */ //@formatter:on +@Getter +@Setter public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyParameters { + // Get a reference to the logger + private static final Logger LOGGER = LoggerFactory.getLogger(RestRequestorCarrierTechnologyParameters.class); + /** The supported HTTP methods. */ public enum HttpMethod { GET, PUT, POST, DELETE @@ -63,12 +76,12 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP public static final String RESTREQUESTOR_CARRIER_TECHNOLOGY_LABEL = "RESTREQUESTOR"; /** The producer plugin class for the REST carrier technology. */ - public static final String RESTREQUSTOR_EVENT_PRODUCER_PLUGIN_CLASS = ApexRestRequestorProducer.class - .getCanonicalName(); + public static final String RESTREQUSTOR_EVENT_PRODUCER_PLUGIN_CLASS = + ApexRestRequestorProducer.class.getCanonicalName(); /** The consumer plugin class for the REST carrier technology. */ - public static final String RESTREQUSTOR_EVENT_CONSUMER_PLUGIN_CLASS = ApexRestRequestorConsumer.class - .getCanonicalName(); + public static final String RESTREQUSTOR_EVENT_CONSUMER_PLUGIN_CLASS = + ApexRestRequestorConsumer.class.getCanonicalName(); /** The default HTTP method for request events. */ public static final HttpMethod DEFAULT_REQUESTOR_HTTP_METHOD = HttpMethod.GET; @@ -76,20 +89,26 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP /** The default timeout for REST requests. */ public static final long DEFAULT_REST_REQUEST_TIMEOUT = 500; + /** The default HTTP code filter, allows 2xx HTTP codes through. */ + public static final String DEFAULT_HTTP_CODE_FILTER = "[2][0-9][0-9]"; + // Commonly occurring strings private static final String HTTP_HEADERS = "httpHeaders"; + private static final String HTTP_CODE_FILTER = "httpCodeFilter"; + + // Regular expression patterns for finding and checking keys in URLs + private static final Pattern patternProperKey = Pattern.compile("(?<=\\{)[^}]*(?=\\})"); + private static final Pattern patternErrorKey = + Pattern.compile("(\\{[^\\{}]*.?\\{)|(\\{[^\\{}]*$)|(\\}[^\\{}]*.?\\})|(^[^\\{}]*.?\\})|\\{\\s*\\}"); private String url = null; private HttpMethod httpMethod = null; private String[][] httpHeaders = null; - - private static final Pattern patternProperKey = Pattern.compile("(?<=\\{)[^}]*(?=\\})"); - private static final Pattern patternErrorKey = Pattern.compile( - "(\\{[^\\{}]*.?\\{)|(\\{[^\\{}]*$)|(\\}[^\\{}]*.?\\})|(^[^\\{}]*.?\\})|\\{\\s*\\}"); + private String httpCodeFilter = DEFAULT_HTTP_CODE_FILTER; /** - * Constructor to create a REST carrier technology parameters instance and register the instance with the - * parameter service. + * Constructor to create a REST carrier technology parameters instance and register the instance with the parameter + * service. */ public RestRequestorCarrierTechnologyParameters() { super(); @@ -101,42 +120,6 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP } /** - * Gets the URL for the REST request. - * - * @return the URL - */ - public String getUrl() { - return url; - } - - /** - * Sets the URL for the REST request. - * - * @param incomingUrl the URL - */ - public void setUrl(final String incomingUrl) { - this.url = incomingUrl; - } - - /** - * Gets the HTTP method to use for the REST request. - * - * @return the HTTP method - */ - public HttpMethod getHttpMethod() { - return httpMethod; - } - - /** - * Sets the HTTP method to use for the REST request. - * - * @param httpMethod the HTTP method - */ - public void setHttpMethod(final HttpMethod httpMethod) { - this.httpMethod = httpMethod; - } - - /** * Check if http headers have been set for the REST request. * * @return true if headers have beenset @@ -146,15 +129,6 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP } /** - * Gets the http headers for the REST request. - * - * @return the headers - */ - public String[][] getHttpHeaders() { - return httpHeaders; - } - - /** * Gets the http headers for the REST request as a multivalued map. * * @return the headers @@ -189,7 +163,7 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP * @return set of the tags */ public Set<String> getKeysFromUrl() { - Matcher matcher = patternProperKey.matcher(this.url); + Matcher matcher = patternProperKey.matcher(getUrl()); Set<String> key = new HashSet<>(); while (matcher.find()) { key.add(matcher.group()); @@ -197,29 +171,55 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP return key; } + + /** + * {@inheritDoc}. + */ + @Override + public GroupValidationResult validate() { + GroupValidationResult result = super.validate(); + + result = validateUrl(result); + + result = validateHttpHeaders(result); + + return validateHttpCodeFilter(result); + } + + // @formatter:off /** - * Validate tags in url. + * Validate the URL. + * + * <p>Checks: * http://www.blah.com/{par1/somethingelse (Missing end tag) use {[^\\{}]*$ - * http://www.blah.com/{par1/{some}thingelse (Missing end tag2) use {[^}]*{ + * http://www.blah.com/{par1/{some}thingelse (Nested tag) use {[^}]*{ * http://www.blah.com/{par1}/some}thingelse (Missing start tag1) use }[^{}]*.} * http://www.blah.com/par1}/somethingelse (Missing start tag2) use }[^{}]*} * http://www.blah.com/{}/somethingelse (Empty tag) use {[\s]*} - * - * @return if url is legal + * @param result the result of the validation */ - public boolean validateTagInUrl() { - // Check url tag syntax error - Matcher matcher = patternErrorKey.matcher(this.url); - return (!matcher.find()); + // @formatter:on + private GroupValidationResult validateUrl(final GroupValidationResult result) { + // URL is only set on Requestor consumers + if (getUrl() == null) { + return result; + } + + Matcher matcher = patternErrorKey.matcher(getUrl()); + if (matcher.find()) { + result.setResult("url", ValidationStatus.INVALID, + "no proper URL has been set for event sending on REST requestor"); + } + + return result; } /** - * {@inheritDoc}. + * Validate the HTTP headers. + * + * @param result the result of the validation */ - @Override - public GroupValidationResult validate() { - final GroupValidationResult result = super.validate(); - + private GroupValidationResult validateHttpHeaders(final GroupValidationResult result) { if (httpHeaders == null) { return result; } @@ -229,22 +229,43 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, "HTTP header array entry is null"); } else if (httpHeader.length != 2) { result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, - "HTTP header array entries must have one key and one value: " - + Arrays.deepToString(httpHeader)); + "HTTP header array entries must have one key and one value: " + + Arrays.deepToString(httpHeader)); } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[0])) { result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, - "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader)); + "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader)); } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[1])) { result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, - "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader)); + "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader)); } } - if (!validateTagInUrl()) { - result.setResult("url", ValidationStatus.INVALID, - "no proper URL has been set for event sending on REST client"); - } + return result; + } + /** + * Validate the HTTP code filter. + * + * @param result the result of the validation + */ + public GroupValidationResult validateHttpCodeFilter(final GroupValidationResult result) { + if (httpCodeFilter == null) { + httpCodeFilter = DEFAULT_HTTP_CODE_FILTER; + + } else if (StringUtils.isBlank(httpCodeFilter)) { + result.setResult(HTTP_CODE_FILTER, ValidationStatus.INVALID, + "HTTP code filter must be specified as a three digit regular expression"); + } else { + try { + Pattern.compile(httpCodeFilter); + } catch (PatternSyntaxException pse) { + String message = + "Invalid HTTP code filter, the filter must be specified as a three digit regular expression: " + + pse.getMessage(); + result.setResult(HTTP_CODE_FILTER, ValidationStatus.INVALID, message); + LOGGER.debug(message, pse); + } + } return result; } @@ -255,6 +276,6 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP @Override public String toString() { return "RESTRequestorCarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + ", httpHeaders=" - + Arrays.deepToString(httpHeaders) + "]"; + + Arrays.deepToString(httpHeaders) + ", httpCodeFilter=" + httpCodeFilter + "]"; } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumerTest.java index ac1af67dd..07818d068 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumerTest.java @@ -25,23 +25,23 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; +import java.util.Properties; + import org.junit.Test; import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; import org.onap.policy.apex.service.engine.event.ApexEventException; import org.onap.policy.apex.service.engine.event.ApexEventReceiver; -import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters; import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; -import java.util.Properties; - /** * Test the ApexRestRequestorConsumer class. * */ public class ApexRestRequestorConsumerTest { + @Test - public void testApexRestRequestorConsumerSetup() { + public void testApexRestRequestorConsumerSetup() throws ApexEventException { ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer(); assertNotNull(consumer); @@ -88,13 +88,10 @@ public class ApexRestRequestorConsumerTest { rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET); rrctp.setUrl("http://www.onap.org"); + rrctp.setHttpCodeFilter("[1-5][0][0-5]"); consumerParameters.setPeerTimeout(EventHandlerPeeredMode.REQUESTOR, 0); - try { - consumer.init(consumerName, consumerParameters, incomingEventReceiver); - } catch (ApexEventException aee) { - fail("test should not throw an exception"); - } + consumer.init(consumerName, consumerParameters, incomingEventReceiver); try { consumer.processRestRequest(null); @@ -110,7 +107,7 @@ public class ApexRestRequestorConsumerTest { } @Test - public void testApexRestRequestorConsumerRequest() { + public void testApexRestRequestorConsumerRequest() throws ApexEventException { ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer(); assertNotNull(consumer); @@ -123,24 +120,21 @@ public class ApexRestRequestorConsumerTest { consumerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true); rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET); rrctp.setUrl("http://www.onap.org"); + rrctp.setHttpCodeFilter("[1-5][0][0-5]"); consumerParameters.setPeerTimeout(EventHandlerPeeredMode.REQUESTOR, 0); // Test should time out requests - try { - consumer.init(consumerName, consumerParameters, incomingEventReceiver); - consumer.start(); - ApexRestRequest request = new ApexRestRequest(123, null,"EventName", "Event body"); - consumer.processRestRequest(request); - ThreadUtilities.sleep(2000); - consumer.stop(); - assertEquals(0, consumer.getEventsReceived()); - } catch (ApexEventException aee) { - fail("test should not throw an exception"); - } + consumer.init(consumerName, consumerParameters, incomingEventReceiver); + consumer.start(); + ApexRestRequest request = new ApexRestRequest(123, null, "EventName", "Event body"); + consumer.processRestRequest(request); + ThreadUtilities.sleep(200); + consumer.stop(); + assertEquals(0, consumer.getEventsReceived()); } @Test - public void testApexRestRequestorConsumerUrlUpdate() { + public void testApexRestRequestorConsumerUrlUpdate() throws ApexEventException { ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer(); assertNotNull(consumer); @@ -158,21 +152,18 @@ public class ApexRestRequestorConsumerTest { Properties properties = new Properties(); properties.put("site", "onap"); properties.put("net", "org"); - try { - consumer.init(consumerName, consumerParameters, incomingEventReceiver); - consumer.start(); - ApexRestRequest request = new ApexRestRequest(123, properties,"EventName", "Event body"); - consumer.processRestRequest(request); - ThreadUtilities.sleep(2000); - consumer.stop(); - assertEquals(0, consumer.getEventsReceived()); - } catch (Exception aee) { - fail("test should not throw an exception"); - } + + consumer.init(consumerName, consumerParameters, incomingEventReceiver); + consumer.start(); + ApexRestRequest request = new ApexRestRequest(123, properties,"EventName", "Event body"); + consumer.processRestRequest(request); + ThreadUtilities.sleep(2000); + consumer.stop(); + assertEquals(0, consumer.getEventsReceived()); } @Test - public void testApexRestRequestorConsumerUrlUpdateError() { + public void testApexRestRequestorConsumerUrlUpdateError() throws ApexEventException { ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer(); assertNotNull(consumer); @@ -189,16 +180,13 @@ public class ApexRestRequestorConsumerTest { consumerParameters.setPeerTimeout(EventHandlerPeeredMode.REQUESTOR, 2000); Properties properties = new Properties(); properties.put("site", "onap"); - try { - consumer.init(consumerName, consumerParameters, incomingEventReceiver); - consumer.start(); - ApexRestRequest request = new ApexRestRequest(123, properties,"EventName", "Event body"); - consumer.processRestRequest(request); - ThreadUtilities.sleep(2000); - consumer.stop(); - assertEquals(0, consumer.getEventsReceived()); - } catch (Exception aee) { - fail("test should not throw an exception"); - } + + consumer.init(consumerName, consumerParameters, incomingEventReceiver); + consumer.start(); + ApexRestRequest request = new ApexRestRequest(123, properties,"EventName", "Event body"); + consumer.processRestRequest(request); + ThreadUtilities.sleep(2000); + consumer.stop(); + assertEquals(0, consumer.getEventsReceived()); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducerTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducerTest.java index 5222f5bdb..af0074794 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducerTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducerTest.java @@ -39,7 +39,7 @@ import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMo public class ApexRestRequestorProducerTest { @Test - public void testApexRestRequestorProducerMethods() { + public void testApexRestRequestorProducerMethods() throws ApexEventException { ApexRestRequestorProducer producer = new ApexRestRequestorProducer(); assertNotNull(producer); @@ -83,12 +83,8 @@ public class ApexRestRequestorProducerTest { } rrctp.setHttpMethod(null); - try { - producer.init(producerName, producerParameters); - producer.stop(); - } catch (ApexEventException aee) { - fail("test should not throw an exception here"); - } + producer.init(producerName, producerParameters); + producer.stop(); assertEquals("ProducerName", producer.getName()); assertEquals(0, producer.getEventsSent()); @@ -96,7 +92,7 @@ public class ApexRestRequestorProducerTest { } @Test - public void testApexRestRequestorProducerRequest() { + public void testApexRestRequestorProducerRequest() throws ApexEventException { ApexRestRequestorProducer producer = new ApexRestRequestorProducer(); String producerName = "ProducerName"; @@ -108,12 +104,8 @@ public class ApexRestRequestorProducerTest { rrctp.setUrl(null); rrctp.setHttpMethod(null); - try { - producer.init(producerName, producerParameters); - producer.stop(); - } catch (ApexEventException aee) { - fail("test should not throw an exception here"); - } + producer.init(producerName, producerParameters); + producer.stop(); String eventName = "EventName"; String event = "This is the event"; diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParametersTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParametersTest.java index 68c6811b4..3044907fb 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParametersTest.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParametersTest.java @@ -87,26 +87,40 @@ public class RestRequestorCarrierTechnologyParametersTest { } @Test - public void testRestRequestorCarrierTechnologyParametersOk() { + public void testRestRequestorCarrierTechnologyParametersOk() throws ParameterException { ApexCommandLineArguments arguments = new ApexCommandLineArguments(); arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderOK.json"); arguments.setRelativeFileRoot("."); + ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); + + RestRequestorCarrierTechnologyParameters rrctp0 = (RestRequestorCarrierTechnologyParameters) parameters + .getEventInputParameters().get("RestRequestorConsumer0").getCarrierTechnologyParameters(); + assertEquals(0, rrctp0.getHttpHeaders().length); + + RestRequestorCarrierTechnologyParameters rrctp1 = (RestRequestorCarrierTechnologyParameters) parameters + .getEventInputParameters().get("RestRequestorConsumer1").getCarrierTechnologyParameters(); + assertEquals(3, rrctp1.getHttpHeaders().length); + assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0)); + assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0)); + assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0)); + } + + @Test + public void testRestClientCarrierTechnologyParameterFilterInvalid() { + ApexCommandLineArguments arguments = new ApexCommandLineArguments(); + arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json"); + arguments.setRelativeFileRoot("."); + try { + new ApexParameterHandler().getParameters(arguments); ApexParameters parameters = new ApexParameterHandler().getParameters(arguments); RestRequestorCarrierTechnologyParameters rrctp0 = (RestRequestorCarrierTechnologyParameters) parameters - .getEventInputParameters().get("RestRequestorConsumer0").getCarrierTechnologyParameters(); - assertEquals(0, rrctp0.getHttpHeaders().length); - - RestRequestorCarrierTechnologyParameters rrctp1 = (RestRequestorCarrierTechnologyParameters) parameters - .getEventInputParameters().get("RestRequestorConsumer1").getCarrierTechnologyParameters(); - assertEquals(3, rrctp1.getHttpHeaders().length); - assertEquals("bbb", rrctp1.getHttpHeadersAsMultivaluedMap().get("aaa").get(0)); - assertEquals("ddd", rrctp1.getHttpHeadersAsMultivaluedMap().get("ccc").get(0)); - assertEquals("fff", rrctp1.getHttpHeadersAsMultivaluedMap().get("eee").get(0)); + .getEventInputParameters().get("RestRequestorConsumer0").getCarrierTechnologyParameters(); + fail("test should throw an exception here"); } catch (ParameterException pe) { - fail("test should not throw an exception"); + assertTrue(pe.getMessage().contains("Invalid HTTP code filter, the filter must be specified as a three digit regular expression: ")); } } @@ -114,9 +128,15 @@ public class RestRequestorCarrierTechnologyParametersTest { public void testGettersAndSetters() { RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters(); + rrctp.setHttpHeaders(null); + assertEquals(null,rrctp.getHttpHeadersAsMultivaluedMap()); + rrctp.setUrl("http://some.where"); assertEquals("http://some.where", rrctp.getUrl()); + rrctp.setHttpCodeFilter("[1-5][0][0-5]"); + assertEquals("[1-5][0][0-5]",rrctp.getHttpCodeFilter()); + String[][] httpHeaders = new String[2][2]; httpHeaders[0][0] = "aaa"; httpHeaders[0][1] = "bbb"; @@ -143,8 +163,8 @@ public class RestRequestorCarrierTechnologyParametersTest { assertEquals(RestRequestorCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod()); assertEquals("RESTRequestorCarrierTechnologyParameters " - + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]]]", - rrctp.toString()); + + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]]," + + " httpCodeFilter=[1-5][0][0-5]]", rrctp.toString()); } @Test @@ -167,10 +187,10 @@ public class RestRequestorCarrierTechnologyParametersTest { assertEquals("ccc", rrctp.getHttpHeaders()[1][0]); assertEquals("ddd", rrctp.getHttpHeaders()[1][1]); - assertEquals(true, rrctp.validateTagInUrl()); + assertEquals(true, rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/is{that}.{one}"); - assertEquals(true, rrctp.validateTagInUrl()); + assertEquals(true, rrctp.validate().isValid()); Set<String> keymap = rrctp.getKeysFromUrl(); assertEquals(true, keymap.contains("place")); @@ -178,19 +198,19 @@ public class RestRequestorCarrierTechnologyParametersTest { assertEquals(true, keymap.contains("one")); rrctp.setUrl("http://{place.{that}/{is}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/{is}.{not}/{what}.{exist"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); rrctp.setUrl("http://place.that/is.not/what.{exist"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); rrctp.setUrl("http://place}.{that}/{is}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/is}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/{}.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); rrctp.setUrl("http://{place}.{that}/{ }.{not}/{what}.{exist}"); - assertEquals(false, rrctp.validateTagInUrl()); + assertEquals(false, rrctp.validate().isValid()); } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json new file mode 100644 index 000000000..d7a076346 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTClientWithHTTPFilterInvalid.json @@ -0,0 +1,90 @@ +{ + "engineServiceParameters": { + "name": "MyApexEngine", + "version": "0.0.1", + "id": 45, + "instanceCount": 4, + "deploymentPort": 12561, + "policyModelFileName": "src/test/resources/policymodels/RequestorModel.json", + "engineParameters": { + "executorParameters": { + "JAVASCRIPT": { + "parameterClassName": "org.onap.policy.apex.plugins.executor.javascript.JavascriptExecutorParameters" + } + } + } + }, + "eventInputParameters": { + "RestRequestorConsumer0": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTREQUESTOR", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restrequestor.RestRequestorCarrierTechnologyParameters", + "parameters": { + "url": "http://localhost:32801/TestRESTRequestor/apex/event/GetEvent", + "httpMethod": "GET", + "httpHeaders" : [ + ], + "httpCodeFilter" : "*\\.*" + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + }, + "eventNameFilter": "ResponseEvent", + "requestorMode": true, + "requestorPeer": "RestRequestorProducer0", + "requestorTimeout": 2000 + }, + "RestRequestorConsumer1": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTREQUESTOR", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restrequestor.RestRequestorCarrierTechnologyParameters", + "parameters": { + "url": "http://localhost:32801/TestRESTRequestor/apex/event/GetEvent", + "httpMethod": "GET", + "httpHeaders" : [ + ["aaa", "bbb"], + ["ccc", "ddd"], + ["eee", "fff"] + ], + "httpCodeFilter" : " " + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + }, + "eventNameFilter": "ResponseEvent", + "requestorMode": true, + "requestorPeer": "RestRequestorProducer1", + "requestorTimeout": 2000 + } + }, + "eventOutputParameters": { + "RestRequestorProducer0": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTREQUESTOR", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restrequestor.RestRequestorCarrierTechnologyParameters" + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + }, + "eventNameFilter": "RequestEvent", + "requestorMode": true, + "requestorPeer": "RestRequestorConsumer0", + "requestorTimeout": 2000 + }, + "RestRequestorProducer1": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTREQUESTOR", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restrequestor.RestRequestorCarrierTechnologyParameters" + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + }, + "eventNameFilter": "RequestEvent", + "requestorMode": true, + "requestorPeer": "RestRequestorConsumer1", + "requestorTimeout": 2000 + } + } +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderBadList.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderBadList.json index ffe04422b..5fbee4404 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderBadList.json +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderBadList.json @@ -25,7 +25,8 @@ "httpHeaders" : [ ["aaa", "bbb"], ["ccc", "ddd"], - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { @@ -45,7 +46,8 @@ "httpMethod": "GET", "httpHeaders" : [ , - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNotKvPairs.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNotKvPairs.json index c1a195b43..f3fbab801 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNotKvPairs.json +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNotKvPairs.json @@ -24,7 +24,8 @@ "httpMethod": "GET", "httpHeaders" : [ ["aaa", "bbb", "ccc"] - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { @@ -44,7 +45,8 @@ "httpMethod": "GET", "httpHeaders" : [ ["aaa"] - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNulls.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNulls.json index dd0484e8e..26059be00 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNulls.json +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNulls.json @@ -25,7 +25,8 @@ "httpHeaders" : [ [null, "bbb"], ["ccc", "ddd"] - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { @@ -47,7 +48,8 @@ ["aaa", "bbb"], ["ccc", null], ["eee", "fff"] - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderOK.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderOK.json index 504c77379..d1dc7d403 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderOK.json +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderOK.json @@ -23,7 +23,8 @@ "url": "http://localhost:32801/TestRESTRequestor/apex/event/GetEvent", "httpMethod": "GET", "httpHeaders" : [ - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { @@ -45,7 +46,8 @@ ["aaa", "bbb"], ["ccc", "ddd"], ["eee", "fff"] - ] + ], + "httpCodeFilter" : "[1-5][0][0-5]" } }, "eventProtocolParameters": { |