2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.event.carrier.restrequestor;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
27 import org.junit.Test;
28 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
29 import org.onap.policy.apex.service.engine.event.ApexEventException;
30 import org.onap.policy.apex.service.engine.event.ApexEventReceiver;
31 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
32 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
35 * Test the ApexRestRequestorConsumer class.
38 public class ApexRestRequestorConsumerTest {
40 public void testApexRestRequestorConsumerSetup() {
41 ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer();
42 assertNotNull(consumer);
44 String consumerName = "ConsumerName";
46 EventHandlerParameters consumerParameters = new EventHandlerParameters();
47 ApexEventReceiver incomingEventReceiver = null;
50 consumer.init(consumerName, consumerParameters, incomingEventReceiver);
51 fail("test should throw an exception here");
52 } catch (ApexEventException aee) {
53 assertEquals("specified consumer properties are not applicable to REST Requestor consumer (ConsumerName)",
57 RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();
58 consumerParameters.setCarrierTechnologyParameters(rrctp);
60 consumer.init(consumerName, consumerParameters, incomingEventReceiver);
61 fail("test should throw an exception here");
62 } catch (ApexEventException aee) {
63 assertEquals("REST Requestor consumer (ConsumerName) must run in peered requestor mode "
64 + "with a REST Requestor producer", aee.getMessage());
67 consumerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);
68 rrctp.setHttpMethod(null);
70 consumer.init(consumerName, consumerParameters, incomingEventReceiver);
71 fail("test should throw an exception here");
72 } catch (ApexEventException aee) {
73 assertEquals("no URL has been specified on REST Requestor consumer (ConsumerName)", aee.getMessage());
76 rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET);
79 consumer.init(consumerName, consumerParameters, incomingEventReceiver);
80 fail("test should throw an exception here");
81 } catch (ApexEventException aee) {
82 assertEquals("invalid URL has been specified on REST Requestor consumer (ConsumerName)", aee.getMessage());
85 rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET);
86 rrctp.setUrl("http://www.onap.org");
87 consumerParameters.setPeerTimeout(EventHandlerPeeredMode.REQUESTOR, 0);
90 consumer.init(consumerName, consumerParameters, incomingEventReceiver);
91 } catch (ApexEventException aee) {
92 fail("test should not throw an exception");
96 consumer.processRestRequest(null);
97 fail("test should throw an exception here");
98 } catch (Exception ex) {
99 assertEquals("could not queue request \"null\" on REST Requestor consumer (ConsumerName)",
103 assertEquals("ConsumerName", consumer.getName());
104 assertEquals(0, consumer.getEventsReceived());
105 assertEquals(null, consumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
109 public void testApexRestRequestorConsumerRequest() {
110 ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer();
111 assertNotNull(consumer);
113 String consumerName = "ConsumerName";
115 EventHandlerParameters consumerParameters = new EventHandlerParameters();
116 ApexEventReceiver incomingEventReceiver = null;
117 RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();
118 consumerParameters.setCarrierTechnologyParameters(rrctp);
119 consumerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);
120 rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET);
121 rrctp.setUrl("http://www.onap.org");
122 consumerParameters.setPeerTimeout(EventHandlerPeeredMode.REQUESTOR, 0);
124 // Test should time out requests
126 consumer.init(consumerName, consumerParameters, incomingEventReceiver);
128 ApexRestRequest request = new ApexRestRequest(123, "EventName", "Event body");
129 consumer.processRestRequest(request);
130 ThreadUtilities.sleep(2000);
132 assertEquals(0, consumer.getEventsReceived());
133 } catch (ApexEventException aee) {
134 fail("test should not throw an exception");