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.service.engine.event.ApexEventConsumer;
29 import org.onap.policy.apex.service.engine.event.ApexEventException;
30 import org.onap.policy.apex.service.engine.event.PeeredReference;
31 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
32 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer;
33 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
34 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
37 * Test the ApexRestRequestorProducer class.
39 public class ApexRestRequestorProducerTest {
42 public void testApexRestRequestorProducerMethods() {
43 ApexRestRequestorProducer producer = new ApexRestRequestorProducer();
44 assertNotNull(producer);
46 String producerName = "ProducerName";
47 EventHandlerParameters producerParameters = new EventHandlerParameters();
50 producer.init(producerName, producerParameters);
51 } catch (ApexEventException aee) {
52 assertEquals("specified producer properties are not applicable to REST requestor producer (ProducerName)",
56 RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();
57 producerParameters.setCarrierTechnologyParameters(rrctp);
59 producer.init(producerName, producerParameters);
60 fail("test should throw an exception here");
61 } catch (ApexEventException aee) {
62 assertEquals("REST Requestor producer (ProducerName) must run in peered requestor mode "
63 + "with a REST Requestor consumer", aee.getMessage());
66 producerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);
69 producer.init(producerName, producerParameters);
70 fail("test should throw an exception here");
71 } catch (ApexEventException aee) {
72 assertEquals("URL may not be specified on REST Requestor producer (ProducerName)", aee.getMessage());
76 rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET);
78 producer.init(producerName, producerParameters);
79 fail("test should throw an exception here");
80 } catch (ApexEventException aee) {
81 assertEquals("HTTP method may not be specified on REST Requestor producer (ProducerName)",
85 rrctp.setHttpMethod(null);
87 producer.init(producerName, producerParameters);
89 } catch (ApexEventException aee) {
90 fail("test should not throw an exception here");
93 assertEquals("ProducerName", producer.getName());
94 assertEquals(0, producer.getEventsSent());
95 assertEquals(null, producer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
99 public void testApexRestRequestorProducerRequest() {
100 ApexRestRequestorProducer producer = new ApexRestRequestorProducer();
102 String producerName = "ProducerName";
103 EventHandlerParameters producerParameters = new EventHandlerParameters();
105 RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();
106 producerParameters.setCarrierTechnologyParameters(rrctp);
107 producerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);
109 rrctp.setHttpMethod(null);
112 producer.init(producerName, producerParameters);
114 } catch (ApexEventException aee) {
115 fail("test should not throw an exception here");
118 String eventName = "EventName";
119 String event = "This is the event";
122 producer.sendEvent(12345, null, eventName, event);
123 fail("test should throw an exception here");
124 } catch (Exception aee) {
125 assertEquals("send of event to URL \"null\" failed, REST response consumer is not defined\n"
126 + "This is the event", aee.getMessage());
129 ApexEventConsumer consumer = new ApexFileEventConsumer();
130 SynchronousEventCache eventCache = new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer,
132 producer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, eventCache);
134 PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, consumer, producer);
135 producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference);
137 producer.sendEvent(12345, null, eventName, event);
138 fail("test should throw an exception here");
139 } catch (Exception aee) {
140 assertEquals("send of event to URL \"null\" failed, REST response consumer "
141 + "is not an instance of ApexRestRequestorConsumer\n" + "This is the event",