5222f5bdbc12f7110addc65130e145367e84e933
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.event.carrier.restrequestor;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
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;
35
36 /**
37  * Test the ApexRestRequestorProducer class.
38  */
39 public class ApexRestRequestorProducerTest {
40
41     @Test
42     public void testApexRestRequestorProducerMethods() {
43         ApexRestRequestorProducer producer = new ApexRestRequestorProducer();
44         assertNotNull(producer);
45
46         String producerName = "ProducerName";
47         EventHandlerParameters producerParameters = new EventHandlerParameters();
48
49         try {
50             producer.init(producerName, producerParameters);
51         } catch (ApexEventException aee) {
52             assertEquals("specified producer properties are not applicable to REST requestor producer (ProducerName)",
53                             aee.getMessage());
54         }
55
56         RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();
57         producerParameters.setCarrierTechnologyParameters(rrctp);
58         try {
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());
64         }
65
66         producerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);
67         rrctp.setUrl("ZZZZ");
68         try {
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());
73         }
74
75         rrctp.setUrl(null);
76         rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET);
77         try {
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)",
82                             aee.getMessage());
83         }
84
85         rrctp.setHttpMethod(null);
86         try {
87             producer.init(producerName, producerParameters);
88             producer.stop();
89         } catch (ApexEventException aee) {
90             fail("test should not throw an exception here");
91         }
92
93         assertEquals("ProducerName", producer.getName());
94         assertEquals(0, producer.getEventsSent());
95         assertEquals(null, producer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
96     }
97
98     @Test
99     public void testApexRestRequestorProducerRequest() {
100         ApexRestRequestorProducer producer = new ApexRestRequestorProducer();
101
102         String producerName = "ProducerName";
103         EventHandlerParameters producerParameters = new EventHandlerParameters();
104
105         RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();
106         producerParameters.setCarrierTechnologyParameters(rrctp);
107         producerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);
108         rrctp.setUrl(null);
109         rrctp.setHttpMethod(null);
110
111         try {
112             producer.init(producerName, producerParameters);
113             producer.stop();
114         } catch (ApexEventException aee) {
115             fail("test should not throw an exception here");
116         }
117
118         String eventName = "EventName";
119         String event = "This is the event";
120
121         try {
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());
127         }
128
129         ApexEventConsumer consumer = new ApexFileEventConsumer();
130         SynchronousEventCache eventCache = new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer,
131                         producer, 1000);
132         producer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, eventCache);
133
134         PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, consumer, producer);
135         producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference);
136         try {
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",
142                             aee.getMessage());
143         }
144     }
145 }