diff options
author | liamfallon <liam.fallon@est.tech> | 2018-12-04 22:36:39 +0000 |
---|---|---|
committer | Liam Fallon <liam.fallon@est.tech> | 2019-01-07 10:41:41 +0000 |
commit | 8ad058976d132e6e8288c8ebca8879329743a620 (patch) | |
tree | a4267b6add8e331ed3736650c7944b93c902251f /plugins/plugins-event/plugins-event-carrier | |
parent | 34dc53376f9be969fdeebc201ee3b12127e78cc1 (diff) |
Support HTTP headers in REST Requestor
Change-Id: Iea4211e7d3324f5ea6244f45c91241a4067d8d33
Issue-ID: POLICY-1222
Signed-off-by: liamfallon <liam.fallon@est.tech>
(cherry picked from commit c01eff1ee4aff0cc4b177a0bab5c6b96a9efb647)
Diffstat (limited to 'plugins/plugins-event/plugins-event-carrier')
15 files changed, 1670 insertions, 19 deletions
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 7d6c60ca1..746fc85c4 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 @@ -22,6 +22,7 @@ package org.onap.policy.apex.plugins.event.carrier.restrequestor; import java.net.URL; import java.util.ArrayList; +import java.util.Arrays; import java.util.EnumMap; import java.util.List; import java.util.Map; @@ -144,11 +145,17 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { throw new ApexEventException(errorMessage, e); } - // Set the peer timeout to the default value if its not set + // Set the requestor timeout if (consumerParameters.getPeerTimeout(EventHandlerPeeredMode.REQUESTOR) != 0) { restRequestTimeout = consumerParameters.getPeerTimeout(EventHandlerPeeredMode.REQUESTOR); } + // Check if HTTP headers has been set + if (restConsumerProperties.checkHttpHeadersSet()) { + LOGGER.debug("REST Requestor consumer has http headers ({}): {}", this.name, + Arrays.deepToString(restConsumerProperties.getHttpHeaders())); + } + // Initialize the HTTP client client = ClientBuilder.newClient(); } @@ -163,10 +170,10 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { // Push the event onto the queue for handling try { incomingRestRequestQueue.add(restRequest); - } catch (final Exception e) { + } catch (final Exception requestException) { final String errorMessage = "could not queue request \"" + restRequest + "\" on REST Requestor consumer (" + this.name + ")"; - LOGGER.warn(errorMessage, e); + LOGGER.warn(errorMessage, requestException); throw new ApexEventRuntimeException(errorMessage); } } @@ -386,23 +393,28 @@ public class ApexRestRequestorConsumer implements ApexEventConsumer, Runnable { /** * Execute the REST request. * + * * @return the response to the REST request */ public Response sendEventAsRestRequest() { switch (restConsumerProperties.getHttpMethod()) { case GET: - return client.target(restConsumerProperties.getUrl()).request(APPLICATION_JSON).get(); + return client.target(restConsumerProperties.getUrl()).request(APPLICATION_JSON) + .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()).get(); case PUT: return client.target(restConsumerProperties.getUrl()).request(APPLICATION_JSON) + .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()) .put(Entity.json(request.getEvent())); case POST: return client.target(restConsumerProperties.getUrl()).request(APPLICATION_JSON) + .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()) .post(Entity.json(request.getEvent())); case DELETE: - return client.target(restConsumerProperties.getUrl()).request(APPLICATION_JSON).delete(); + return client.target(restConsumerProperties.getUrl()).request(APPLICATION_JSON) + .headers(restConsumerProperties.getHttpHeadersAsMultivaluedMap()).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/ApexRestRequestorProducer.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducer.java index 69ad05b27..4985fe16f 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducer.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducer.java @@ -70,7 +70,7 @@ public class ApexRestRequestorProducer implements ApexEventProducer { if (!(producerParameters .getCarrierTechnologyParameters() instanceof RestRequestorCarrierTechnologyParameters)) { final String errorMessage = - "specified consumer properties are not applicable to REST requestor producer (" + this.name + ")"; + "specified producer properties are not applicable to REST requestor producer (" + this.name + ")"; LOGGER.warn(errorMessage); throw new ApexEventException(errorMessage); } 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 acd5e52e8..d8009eeef 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 @@ -20,23 +20,34 @@ package org.onap.policy.apex.plugins.event.carrier.restrequestor; +import java.util.Arrays; + +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; + 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; +// @formatter:off /** * Apex parameters for REST as an event carrier technology with Apex issuing a REST request and receiving a REST * response. * * <p>The parameters for this plugin are: * <ol> - * <li>url: The URL that the Apex Rest Requestor will connect to over REST for REST request sending. 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>url: The URL that the Apex Rest Requestor will connect to over REST for REST request sending. + * 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 * </ol> * * @author Liam Fallon (liam.fallon@ericsson.com) */ +//@formatter:on public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyParameters { /** The supported HTTP methods. */ public enum HttpMethod { @@ -47,12 +58,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; @@ -60,12 +71,16 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP /** The default timeout for REST requests. */ public static final long DEFAULT_REST_REQUEST_TIMEOUT = 500; + // Commonly occurring strings + private static final String HTTP_HEADERS = "httpHeaders"; + private String url = null; private HttpMethod httpMethod = null; + private String[][] httpHeaders = null; /** - * 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 regiaaaster the instance with the + * parameter service. */ public RestRequestorCarrierTechnologyParameters() { super(); @@ -112,14 +127,92 @@ public class RestRequestorCarrierTechnologyParameters extends CarrierTechnologyP this.httpMethod = httpMethod; } + /** + * Check if http headers have been set for the REST request. + * + * @return true if headers have beenset + */ + public boolean checkHttpHeadersSet() { + return httpHeaders != null && httpHeaders.length > 0; + } + + /** + * 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 + */ + public MultivaluedMap<String, Object> getHttpHeadersAsMultivaluedMap() { + if (httpHeaders == null) { + return null; + } + + // Load the HTTP headers into the map + MultivaluedMap<String, Object> httpHeaderMap = new MultivaluedHashMap<>(); + + for (String[] httpHeader : httpHeaders) { + httpHeaderMap.putSingle(httpHeader[0], httpHeader[1]); + } + + return httpHeaderMap; + } + + /** + * Sets the header for the REST request. + * + * @param httpHeaders the incoming HTTP headers + */ + public void setHttpHeaders(final String[][] httpHeaders) { + this.httpHeaders = httpHeaders; + } + + /** + * {@inheritDoc} + */ + @Override + public GroupValidationResult validate() { + final GroupValidationResult result = super.validate(); + + if (httpHeaders == null) { + return result; + } + + for (String[] httpHeader : httpHeaders) { + if (httpHeader == null) { + 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)); + } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[0])) { + result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, + "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)); + } + } + + return result; + } + /* * (non-Javadoc) * - * @see java.lang.Object#toString() + * @see new LinkedHashMap<>()java.lang.Object#toString() */ @Override public String toString() { - return "RESTRequestorCarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + "]"; + return "RESTRequestorCarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + ", httpHeaders=" + + Arrays.deepToString(httpHeaders) + "]"; } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestTest.java new file mode 100644 index 000000000..559d2dba8 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestTest.java @@ -0,0 +1,49 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.carrier.restrequestor; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +/** + * Test the ApexRestRequest class. + */ +public class ApexRestRequestTest { + + @Test + public void testApexRestRequest() { + final String eventName = "EventName"; + final String eventString = "The Event String"; + + ApexRestRequest rr = new ApexRestRequest(1, eventName, eventString); + + assertEquals(1, rr.getExecutionId()); + assertEquals(eventName, rr.getEventName()); + assertEquals(eventString, rr.getEvent()); + + rr.setTimestamp(1234567); + assertEquals(1234567, rr.getTimestamp()); + + assertEquals("ApexRestRequest [executionId=1, eventName=EventName, event=The Event String, timestamp=1234567]", + rr.toString()); + } +} 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 new file mode 100644 index 000000000..44e020381 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorConsumerTest.java @@ -0,0 +1,137 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.carrier.restrequestor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +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.parameters.eventhandler.EventHandlerParameters; +import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode; + +/** + * Test the ApexRestRequestorConsumer class. + * + */ +public class ApexRestRequestorConsumerTest { + @Test + public void testApexRestRequestorConsumerSetup() { + ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer(); + assertNotNull(consumer); + + String consumerName = "ConsumerName"; + + EventHandlerParameters consumerParameters = new EventHandlerParameters(); + ApexEventReceiver incomingEventReceiver = null; + + try { + consumer.init(consumerName, consumerParameters, incomingEventReceiver); + fail("test should throw an exception here"); + } catch (ApexEventException aee) { + assertEquals("specified consumer properties are not applicable to REST Requestor consumer (ConsumerName)", + aee.getMessage()); + } + + RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(rrctp); + try { + consumer.init(consumerName, consumerParameters, incomingEventReceiver); + fail("test should throw an exception here"); + } catch (ApexEventException aee) { + assertEquals("REST Requestor consumer (ConsumerName) must run in peered requestor mode " + + "with a REST Requestor producer", aee.getMessage()); + } + + consumerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true); + rrctp.setHttpMethod(null); + try { + consumer.init(consumerName, consumerParameters, incomingEventReceiver); + fail("test should throw an exception here"); + } catch (ApexEventException aee) { + assertEquals("no URL has been specified on REST Requestor consumer (ConsumerName)", aee.getMessage()); + } + + rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET); + rrctp.setUrl("ZZZZ"); + try { + consumer.init(consumerName, consumerParameters, incomingEventReceiver); + fail("test should throw an exception here"); + } catch (ApexEventException aee) { + assertEquals("invalid URL has been specified on REST Requestor consumer (ConsumerName)", aee.getMessage()); + } + + rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET); + rrctp.setUrl("http://www.onap.org"); + consumerParameters.setPeerTimeout(EventHandlerPeeredMode.REQUESTOR, 0); + + try { + consumer.init(consumerName, consumerParameters, incomingEventReceiver); + } catch (ApexEventException aee) { + fail("test should not throw an exception"); + } + + try { + consumer.processRestRequest(null); + fail("test should throw an exception here"); + } catch (Exception ex) { + assertEquals("could not queue request \"null\" on REST Requestor consumer (ConsumerName)", + ex.getMessage()); + } + + assertEquals("ConsumerName", consumer.getName()); + assertEquals(0, consumer.getEventsReceived()); + assertEquals(null, consumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); + } + + @Test + public void testApexRestRequestorConsumerRequest() { + ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer(); + assertNotNull(consumer); + + String consumerName = "ConsumerName"; + + EventHandlerParameters consumerParameters = new EventHandlerParameters(); + ApexEventReceiver incomingEventReceiver = null; + RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters(); + consumerParameters.setCarrierTechnologyParameters(rrctp); + consumerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true); + rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET); + rrctp.setUrl("http://www.onap.org"); + consumerParameters.setPeerTimeout(EventHandlerPeeredMode.REQUESTOR, 0); + + // Test should time out requests + try { + consumer.init(consumerName, consumerParameters, incomingEventReceiver); + consumer.start(); + ApexRestRequest request = new ApexRestRequest(123, "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"); + } + } +} 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 new file mode 100644 index 000000000..136c189db --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/ApexRestRequestorProducerTest.java @@ -0,0 +1,145 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.carrier.restrequestor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.apex.service.engine.event.ApexEventConsumer; +import org.onap.policy.apex.service.engine.event.ApexEventException; +import org.onap.policy.apex.service.engine.event.PeeredReference; +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; + +/** + * Test the ApexRestRequestorProducer class. + */ +public class ApexRestRequestorProducerTest { + + @Test + public void testApexRestRequestorProducerMethods() { + ApexRestRequestorProducer producer = new ApexRestRequestorProducer(); + assertNotNull(producer); + + String producerName = "ProducerName"; + EventHandlerParameters producerParameters = new EventHandlerParameters(); + + try { + producer.init(producerName, producerParameters); + } catch (ApexEventException aee) { + assertEquals("specified producer properties are not applicable to REST requestor producer (ProducerName)", + aee.getMessage()); + } + + RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(rrctp); + try { + producer.init(producerName, producerParameters); + fail("test should throw an exception here"); + } catch (ApexEventException aee) { + assertEquals("REST Requestor producer (ProducerName) must run in peered requestor mode " + + "with a REST Requestor consumer", aee.getMessage()); + } + + producerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true); + rrctp.setUrl("ZZZZ"); + try { + producer.init(producerName, producerParameters); + fail("test should throw an exception here"); + } catch (ApexEventException aee) { + assertEquals("URL may not be specified on REST Requestor producer (ProducerName)", aee.getMessage()); + } + + rrctp.setUrl(null); + rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET); + try { + producer.init(producerName, producerParameters); + fail("test should throw an exception here"); + } catch (ApexEventException aee) { + assertEquals("HTTP method may not be specified on REST Requestor producer (ProducerName)", + aee.getMessage()); + } + + rrctp.setHttpMethod(null); + try { + producer.init(producerName, producerParameters); + producer.stop(); + } catch (ApexEventException aee) { + fail("test should not throw an exception here"); + } + + assertEquals("ProducerName", producer.getName()); + assertEquals(0, producer.getEventsSent()); + assertEquals(null, producer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR)); + } + + @Test + public void testApexRestRequestorProducerRequest() { + ApexRestRequestorProducer producer = new ApexRestRequestorProducer(); + + String producerName = "ProducerName"; + EventHandlerParameters producerParameters = new EventHandlerParameters(); + + RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters(); + producerParameters.setCarrierTechnologyParameters(rrctp); + producerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true); + rrctp.setUrl(null); + rrctp.setHttpMethod(null); + + try { + producer.init(producerName, producerParameters); + producer.stop(); + } catch (ApexEventException aee) { + fail("test should not throw an exception here"); + } + + String eventName = "EventName"; + String event = "This is the event"; + + try { + producer.sendEvent(12345, eventName, event); + fail("test should throw an exception here"); + } catch (Exception aee) { + assertEquals("send of event to URL \"null\" failed, REST response consumer is not defined\n" + + "This is the event", aee.getMessage()); + } + + ApexEventConsumer consumer = new ApexFileEventConsumer(); + SynchronousEventCache eventCache = new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, + producer, 1000); + producer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, eventCache); + + PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, consumer, producer); + producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference); + try { + producer.sendEvent(12345, eventName, event); + fail("test should throw an exception here"); + } catch (Exception aee) { + assertEquals("send of event to URL \"null\" failed, REST response consumer " + + "is not an instance of ApexRestRequestorConsumer\n" + "This is the event", + aee.getMessage()); + } + } +} 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 new file mode 100644 index 000000000..7be42e264 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParametersTest.java @@ -0,0 +1,146 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.carrier.restrequestor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.junit.Test; +import org.onap.policy.apex.service.engine.main.ApexCommandLineArguments; +import org.onap.policy.apex.service.parameters.ApexParameterHandler; +import org.onap.policy.apex.service.parameters.ApexParameters; +import org.onap.policy.common.parameters.ParameterException; + +/** + * Test REST Requestor carrier technology parameters. + */ +public class RestRequestorCarrierTechnologyParametersTest { + + @Test + public void testRestRequestorCarrierTechnologyParametersBadList() { + ApexCommandLineArguments arguments = new ApexCommandLineArguments(); + arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderBadList.json"); + arguments.setRelativeFileRoot("."); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("test should throw an exception here"); + } catch (ParameterException pe) { + assertTrue(pe.getMessage().contains("HTTP header array entry is null\n parameter")); + assertTrue(pe.getMessage().trim().endsWith("HTTP header array entry is null")); + } + } + + @Test + public void testRestRequestorCarrierTechnologyParametersNotKvPairs() { + ApexCommandLineArguments arguments = new ApexCommandLineArguments(); + arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNotKvPairs.json"); + arguments.setRelativeFileRoot("."); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("test should throw an exception here"); + } catch (ParameterException pe) { + assertTrue(pe.getMessage() + .contains("HTTP header array entries must have one key and one value: [aaa, bbb, ccc]")); + assertTrue(pe.getMessage().trim() + .endsWith("HTTP header array entries must have one key and one value: [aaa]")); + } + } + + @Test + public void testRestRequestorCarrierTechnologyParametersNulls() { + ApexCommandLineArguments arguments = new ApexCommandLineArguments(); + arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNulls.json"); + arguments.setRelativeFileRoot("."); + + try { + new ApexParameterHandler().getParameters(arguments); + fail("test should throw an exception here"); + } catch (ParameterException pe) { + assertTrue(pe.getMessage().contains("HTTP header key is null or blank: [null, bbb]")); + assertTrue(pe.getMessage().trim().endsWith("HTTP header value is null or blank: [ccc, null]")); + } + } + + @Test + public void testRestRequestorCarrierTechnologyParametersOk() { + ApexCommandLineArguments arguments = new ApexCommandLineArguments(); + arguments.setConfigurationFilePath("src/test/resources/prodcons/RESTRequestorWithHTTPHeaderOK.json"); + arguments.setRelativeFileRoot("."); + + try { + 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)); + } catch (ParameterException pe) { + fail("test should not throw an exception"); + } + } + + @Test + public void testGettersAndSetters() { + RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters(); + + rrctp.setUrl("http://some.where"); + assertEquals("http://some.where", rrctp.getUrl()); + + String[][] httpHeaders = new String[2][2]; + httpHeaders[0][0] = "aaa"; + httpHeaders[0][1] = "bbb"; + httpHeaders[1][0] = "ccc"; + httpHeaders[1][1] = "ddd"; + + rrctp.setHttpHeaders(httpHeaders); + assertEquals("aaa", rrctp.getHttpHeaders()[0][0]); + assertEquals("bbb", rrctp.getHttpHeaders()[0][1]); + assertEquals("ccc", rrctp.getHttpHeaders()[1][0]); + assertEquals("ddd", rrctp.getHttpHeaders()[1][1]); + + rrctp.setHttpHeaders(null); + assertFalse(rrctp.checkHttpHeadersSet()); + + String[][] httpHeadersZeroLength = new String[0][0]; + rrctp.setHttpHeaders(httpHeadersZeroLength); + assertFalse(rrctp.checkHttpHeadersSet()); + + rrctp.setHttpHeaders(httpHeaders); + assertTrue(rrctp.checkHttpHeadersSet()); + + rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.DELETE); + assertEquals(RestRequestorCarrierTechnologyParameters.HttpMethod.DELETE, rrctp.getHttpMethod()); + + assertEquals("RESTRequestorCarrierTechnologyParameters " + + "[url=http://some.where, httpMethod=DELETE, httpHeaders=[[aaa, bbb], [ccc, ddd]]]", + rrctp.toString()); + } +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java new file mode 100644 index 000000000..a9dfd8dc5 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorTest.java @@ -0,0 +1,422 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.carrier.restrequestor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.gson.Gson; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.net.URI; +import java.util.Map; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.core.Response; + +import org.glassfish.grizzly.http.server.HttpServer; +import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; +import org.glassfish.jersey.server.ResourceConfig; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.onap.policy.apex.core.infrastructure.messaging.MessagingException; +import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities; +import org.onap.policy.apex.model.basicmodel.concepts.ApexException; +import org.onap.policy.apex.service.engine.main.ApexMain; + +/** + * The Class TestRestRequestor. + */ +public class RestRequestorTest { + private static final String BASE_URI = "http://localhost:32801/TestRESTRequestor"; + private static HttpServer server; + + private ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + + private final PrintStream stdout = System.out; + private final PrintStream stderr = System.err; + + /** + * Sets the up. + * + * @throws Exception the exception + */ + @BeforeClass + public static void setUp() throws Exception { + final ResourceConfig rc = new ResourceConfig(SupportRestRequestorEndpoint.class); + server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc); + + while (!server.isStarted()) { + ThreadUtilities.sleep(50); + } + } + + /** + * Tear down. + * + * @throws Exception the exception + */ + @AfterClass + public static void tearDown() throws Exception { + server.shutdownNow(); + + new File("src/test/resources/events/EventsOut.json").delete(); + new File("src/test/resources/events/EventsOutMulti0.json").delete(); + new File("src/test/resources/events/EventsOutMulti1.json").delete(); + } + + /** + * Reset counters. + */ + @Before + public void resetCounters() { + SupportRestRequestorEndpoint.resetCounters(); + } + + /** + * Test rest requestor get. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorGet() throws MessagingException, ApexException, IOException { + final Client client = ClientBuilder.newClient(); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FileGet.json" }; + final ApexMain apexMain = new ApexMain(args); + + Response response = null; + + // Wait for the required amount of events to be received or for 10 seconds + Double getsSoFar = 0.0; + for (int i = 0; i < 40; i++) { + ThreadUtilities.sleep(100); + + response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") + .request("application/json").get(); + + if (Response.Status.OK.getStatusCode() != response.getStatus()) { + break; + } + + final String responseString = response.readEntity(String.class); + + @SuppressWarnings("unchecked") + final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class); + getsSoFar = Double.valueOf(jsonMap.get("GET").toString()); + + if (getsSoFar >= 50.0) { + break; + } + } + + apexMain.shutdown(); + client.close(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + + assertEquals(Double.valueOf(50.0), getsSoFar); + } + + /** + * Test rest requestor get empty. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorGetEmpty() throws MessagingException, ApexException, IOException { + final Client client = ClientBuilder.newClient(); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json" }; + final ApexMain apexMain = new ApexMain(args); + + Response response = null; + + // Wait for the required amount of events to be received or for 10 seconds + Double getsSoFar = 0.0; + for (int i = 0; i < 40; i++) { + ThreadUtilities.sleep(100); + + response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") + .request("application/json").get(); + + if (Response.Status.OK.getStatusCode() != response.getStatus()) { + break; + } + + final String responseString = response.readEntity(String.class); + + @SuppressWarnings("unchecked") + final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class); + getsSoFar = Double.valueOf(jsonMap.get("GET").toString()); + + if (getsSoFar >= 50.0) { + break; + } + } + + apexMain.shutdown(); + client.close(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + } + + /** + * Test REST requestor put. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorPut() throws MessagingException, ApexException, IOException { + final Client client = ClientBuilder.newClient(); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FilePut.json" }; + final ApexMain apexMain = new ApexMain(args); + + // Wait for the required amount of events to be received or for 10 seconds + Double putsSoFar = 0.0; + + Response response = null; + for (int i = 0; i < 40; i++) { + ThreadUtilities.sleep(100); + + response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") + .request("application/json").get(); + + if (Response.Status.OK.getStatusCode() != response.getStatus()) { + break; + } + + final String responseString = response.readEntity(String.class); + + @SuppressWarnings("unchecked") + final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class); + putsSoFar = Double.valueOf(jsonMap.get("PUT").toString()); + + if (putsSoFar >= 50.0) { + break; + } + } + + apexMain.shutdown(); + client.close(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + assertEquals(Double.valueOf(50.0), putsSoFar); + } + + /** + * Test REST requestor post. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorPost() throws MessagingException, ApexException, IOException { + final Client client = ClientBuilder.newClient(); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FilePost.json" }; + final ApexMain apexMain = new ApexMain(args); + + // Wait for the required amount of events to be received or for 10 seconds + Double postsSoFar = 0.0; + for (int i = 0; i < 40; i++) { + ThreadUtilities.sleep(100); + + final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") + .request("application/json").get(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + final String responseString = response.readEntity(String.class); + + @SuppressWarnings("unchecked") + final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class); + postsSoFar = Double.valueOf(jsonMap.get("POST").toString()); + + if (postsSoFar >= 50.0) { + break; + } + } + + apexMain.shutdown(); + client.close(); + + assertEquals(Double.valueOf(50.0), postsSoFar); + } + + /** + * Test REST requestor delete. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorDelete() throws MessagingException, ApexException, IOException { + final Client client = ClientBuilder.newClient(); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FileDelete.json" }; + final ApexMain apexMain = new ApexMain(args); + + // Wait for the required amount of events to be received or for 10 seconds + Double deletesSoFar = 0.0; + for (int i = 0; i < 40; i++) { + ThreadUtilities.sleep(100); + + final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") + .request("application/json").get(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + final String responseString = response.readEntity(String.class); + + @SuppressWarnings("unchecked") + final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class); + deletesSoFar = Double.valueOf(jsonMap.get("DELETE").toString()); + + if (deletesSoFar >= 50.0) { + break; + } + } + + apexMain.shutdown(); + client.close(); + + assertEquals(Double.valueOf(50.0), deletesSoFar); + } + + /** + * Test REST requestor multi inputs. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorMultiInputs() throws MessagingException, ApexException, IOException { + final Client client = ClientBuilder.newClient(); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FileGetMulti.json" }; + final ApexMain apexMain = new ApexMain(args); + + // Wait for the required amount of events to be received or for 10 seconds + Double getsSoFar = 0.0; + for (int i = 0; i < 40; i++) { + ThreadUtilities.sleep(100); + + final Response response = client.target("http://localhost:32801/TestRESTRequestor/apex/event/Stats") + .request("application/json").get(); + + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + final String responseString = response.readEntity(String.class); + + @SuppressWarnings("unchecked") + final Map<String, Object> jsonMap = new Gson().fromJson(responseString, Map.class); + getsSoFar = Double.valueOf(jsonMap.get("GET").toString()); + + if (getsSoFar >= 8.0) { + break; + } + } + + apexMain.shutdown(); + client.close(); + + assertEquals(Double.valueOf(8.0), getsSoFar); + + ThreadUtilities.sleep(1000); + } + + /** + * Test REST requestor producer alone. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorProducerAlone() throws MessagingException, ApexException, IOException { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FileGetProducerAlone.json" }; + + final ApexMain apexMain = new ApexMain(args); + ThreadUtilities.sleep(200); + apexMain.shutdown(); + + final String outString = outContent.toString(); + + System.setOut(stdout); + System.setErr(stderr); + + assertTrue(outString.contains("REST Requestor producer (RestRequestorProducer) " + + "must run in peered requestor mode with a REST Requestor consumer")); + } + + /** + * Test REST requestor consumer alone. + * + * @throws MessagingException the messaging exception + * @throws ApexException the apex exception + * @throws IOException Signals that an I/O exception has occurred. + */ + @Test + public void testRestRequestorConsumerAlone() throws MessagingException, ApexException, IOException { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + + final String[] args = + { "src/test/resources/prodcons/File2RESTRequest2FileGetConsumerAlone.json" }; + + final ApexMain apexMain = new ApexMain(args); + ThreadUtilities.sleep(200); + apexMain.shutdown(); + + final String outString = outContent.toString(); + + System.setOut(stdout); + System.setErr(stderr); + + assertTrue(outString.contains("peer \"RestRequestorProducer for peered mode REQUESTOR " + + "does not exist or is not defined with the same peered mode")); + } +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/SupportRestRequestorEndpoint.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/SupportRestRequestorEndpoint.java new file mode 100644 index 000000000..8380d0a61 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/SupportRestRequestorEndpoint.java @@ -0,0 +1,207 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.apex.plugins.event.carrier.restrequestor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import com.google.gson.Gson; + +import java.util.Map; + +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; + +/** + * The Class TestRestRequestorEndpoint. + */ +@Path("/apex") +public class SupportRestRequestorEndpoint { + + private static Object counterLock = new Object(); + private static int postMessagesReceived = 0; + private static int putMessagesReceived = 0; + private static int statMessagesReceived = 0; + private static int getMessagesReceived = 0; + private static int deleteMessagesReceived = 0; + + private static String EVENT_STRING = "{\n" + "\"nameSpace\": \"org.onap.policy.apex.events\",\n" + + "\"name\": \"ResponseEvent\",\n" + "\"version\": \"0.0.1\",\n" + "\"source\": \"REST_" + + getMessagesReceived + "\",\n" + "\"target\": \"apex\",\n" + "\"intPar\": 9080\n" + "}"; + + /** + * Reset counters. + */ + public static void resetCounters() { + postMessagesReceived = 0; + putMessagesReceived = 0; + statMessagesReceived = 0; + getMessagesReceived = 0; + deleteMessagesReceived = 0; + } + + /** + * Service get stats. + * + * @return the response + */ + @Path("/event/Stats") + @GET + public Response serviceGetStats() { + synchronized (counterLock) { + statMessagesReceived++; + } + return Response.status(200) + .entity("{\"GET\": " + getMessagesReceived + ",\"STAT\": " + statMessagesReceived + + ",\"POST\": " + postMessagesReceived + ",\"PUT\": " + putMessagesReceived + + ",\"DELETE\": " + deleteMessagesReceived + "}") + .build(); + } + + /** + * Service get event. + * + * @return the response + */ + @Path("/event/GetEvent") + @GET + public Response serviceGetEvent() { + synchronized (counterLock) { + getMessagesReceived++; + } + + return Response.status(200).entity(EVENT_STRING).build(); + } + + /** + * Service get empty event. + * + * @return the response + */ + @Path("/event/GetEmptyEvent") + @GET + public Response serviceGetEmptyEvent() { + return Response.status(200).build(); + } + + /** + * Service get event bad response. + * + * @return the response + */ + @Path("/event/GetEventBadResponse") + @GET + public Response serviceGetEventBadResponse() { + return Response.status(400).build(); + } + + /** + * Service post request. + * + * @param jsonString the json string + * @return the response + */ + @Path("/event/PostEvent") + @POST + public Response servicePostRequest(final String jsonString) { + synchronized (counterLock) { + postMessagesReceived++; + } + + @SuppressWarnings("unchecked") + final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class); + assertTrue(jsonMap.containsKey("name")); + assertEquals("0.0.1", jsonMap.get("version")); + assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace")); + assertEquals("Act", jsonMap.get("source")); + assertEquals("Outside", jsonMap.get("target")); + + return Response.status(200).entity(EVENT_STRING).build(); + } + + /** + * Service post request bad response. + * + * @param jsonString the json string + * @return the response + */ + @Path("/event/PostEventBadResponse") + @POST + public Response servicePostRequestBadResponse(final String jsonString) { + return Response.status(400).build(); + } + + /** + * Service put request. + * + * @param jsonString the json string + * @return the response + */ + @Path("/event/PutEvent") + @PUT + public Response servicePutRequest(final String jsonString) { + synchronized (counterLock) { + putMessagesReceived++; + } + + @SuppressWarnings("unchecked") + final Map<String, Object> jsonMap = new Gson().fromJson(jsonString, Map.class); + assertTrue(jsonMap.containsKey("name")); + assertEquals("0.0.1", jsonMap.get("version")); + assertEquals("org.onap.policy.apex.sample.events", jsonMap.get("nameSpace")); + assertEquals("Act", jsonMap.get("source")); + assertEquals("Outside", jsonMap.get("target")); + + return Response.status(200).entity(EVENT_STRING).build(); + } + + /** + * Service delete request. + * + * @param jsonString the json string + * @return the response + */ + @Path("/event/DeleteEvent") + @DELETE + public Response serviceDeleteRequest(final String jsonString) { + synchronized (counterLock) { + deleteMessagesReceived++; + } + + return Response.status(200).entity(EVENT_STRING).build(); + } + + /** + * Service delete request bad response. + * + * @param jsonString the json string + * @return the response + */ + @Path("/event/DeleteEventBadResponse") + @DELETE + public Response serviceDeleteRequestBadResponse(final String jsonString) { + return Response.status(400).build(); + } +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.json new file mode 100644 index 000000000..16baced07 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/File2RESTRequest2FileGetEmpty.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": { + "FileConsumer": { + "carrierTechnologyParameters": { + "carrierTechnology": "FILE", + "parameters": { + "fileName": "src/test/resources/events/EventsIn.json" + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + } + }, + "RestRequestorConsumer": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTREQUESTOR", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restrequestor.RestRequestorCarrierTechnologyParameters", + "parameters": { + "url": "http://localhost:32801/TestRESTRequestor/apex/event/GetEmptyEvent", + "httpMethod": "GET", + "restRequestTimeout": 2000 + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + }, + "eventNameFilter": "ResponseEvent", + "requestorMode": true, + "requestorPeer": "RestRequestorProducer", + "requestorTimeout": 2000 + } + }, + "eventOutputParameters": { + "RestRequestorProducer": { + "carrierTechnologyParameters": { + "carrierTechnology": "RESTREQUESTOR", + "parameterClassName": "org.onap.policy.apex.plugins.event.carrier.restrequestor.RestRequestorCarrierTechnologyParameters" + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + }, + "eventNameFilter": "RequestEvent", + "requestorMode": true, + "requestorPeer": "RestRequestorConsumer", + "requestorTimeout": 2000 + }, + "FileProducer": { + "carrierTechnologyParameters": { + "carrierTechnology": "FILE", + "parameters": { + "fileName": "src/test/resources/events/EventsOut.json" + } + }, + "eventProtocolParameters": { + "eventProtocol": "JSON" + }, + "eventNameFilter": "BasicEvent" + } + } +} diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/File2RESTRequest2FilePut.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/File2RESTRequest2FilePut.json index ac418e623..c66051ce5 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/File2RESTRequest2FilePut.json +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/File2RESTRequest2FilePut.json @@ -33,7 +33,11 @@ "parameters": { "url": "http://localhost:32801/TestRESTRequestor/apex/event/PutEvent", "httpMethod": "PUT", - "restRequestTimeout": 2000 + "restRequestTimeout": 2000, + "httpHeaders" : [ + ["Content-Type", "application/json"], + ["Date", "Tue, 4 Dec 2018 14:57:12 GMT"] + ] } }, "eventProtocolParameters": { 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 new file mode 100644 index 000000000..c71911aab --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderBadList.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", + "restRequestTimeout": 2000, + "httpHeaders" : [ + ["aaa", "bbb"], + ["ccc", "ddd"], + ] + } + }, + "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", + "restRequestTimeout": 2000, + "httpHeaders" : [ + , + ] + } + }, + "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/RESTRequestorWithHTTPHeaderNotKvPairs.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNotKvPairs.json new file mode 100644 index 000000000..dc63c5884 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNotKvPairs.json @@ -0,0 +1,89 @@ +{ + "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", + "restRequestTimeout": 2000, + "httpHeaders" : [ + ["aaa", "bbb", "ccc"] + ] + } + }, + "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", + "restRequestTimeout": 2000, + "httpHeaders" : [ + ["aaa"] + ] + } + }, + "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/RESTRequestorWithHTTPHeaderNulls.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNulls.json new file mode 100644 index 000000000..9992e4f28 --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderNulls.json @@ -0,0 +1,92 @@ +{ + "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", + "restRequestTimeout": 2000, + "httpHeaders" : [ + [null, "bbb"], + ["ccc", "ddd"] + ] + } + }, + "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", + "restRequestTimeout": 2000, + "httpHeaders" : [ + ["aaa", "bbb"], + ["ccc", null], + ["eee", "fff"] + ] + } + }, + "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/RESTRequestorWithHTTPHeaderOK.json b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderOK.json new file mode 100644 index 000000000..e8a25c9fb --- /dev/null +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/test/resources/prodcons/RESTRequestorWithHTTPHeaderOK.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", + "restRequestTimeout": 2000, + "httpHeaders" : [ + ] + } + }, + "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", + "restRequestTimeout": 2000, + "httpHeaders" : [ + ["aaa", "bbb"], + ["ccc", "ddd"], + ["eee", "fff"] + ] + } + }, + "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 + } + } +} |