af0074794e432c369106f25be1d15aaa3271b776
[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() throws ApexEventException {
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         producer.init(producerName, producerParameters);
87         producer.stop();
88
89         assertEquals("ProducerName", producer.getName());
90         assertEquals(0, producer.getEventsSent());
91         assertEquals(null, producer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
92     }
93
94     @Test
95     public void testApexRestRequestorProducerRequest() throws ApexEventException {
96         ApexRestRequestorProducer producer = new ApexRestRequestorProducer();
97
98         String producerName = "ProducerName";
99         EventHandlerParameters producerParameters = new EventHandlerParameters();
100
101         RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();
102         producerParameters.setCarrierTechnologyParameters(rrctp);
103         producerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);
104         rrctp.setUrl(null);
105         rrctp.setHttpMethod(null);
106
107         producer.init(producerName, producerParameters);
108         producer.stop();
109
110         String eventName = "EventName";
111         String event = "This is the event";
112
113         try {
114             producer.sendEvent(12345, null, eventName, event);
115             fail("test should throw an exception here");
116         } catch (Exception aee) {
117             assertEquals("send of event to URL \"null\" failed, REST response consumer is not defined\n"
118                             + "This is the event", aee.getMessage());
119         }
120
121         ApexEventConsumer consumer = new ApexFileEventConsumer();
122         SynchronousEventCache eventCache = new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer,
123                         producer, 1000);
124         producer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, eventCache);
125
126         PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, consumer, producer);
127         producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference);
128         try {
129             producer.sendEvent(12345, null, eventName, event);
130             fail("test should throw an exception here");
131         } catch (Exception aee) {
132             assertEquals("send of event to URL \"null\" failed, REST response consumer "
133                             + "is not an instance of ApexRestRequestorConsumer\n" + "This is the event",
134                             aee.getMessage());
135         }
136     }
137 }