d168f2444bd730719da2c648ff21ca08687aced8
[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     // String constants
41     private static final String PRODUCER_NAME = "ProducerName";
42
43     @Test
44     public void testApexRestRequestorProducerMethods() throws ApexEventException {
45         ApexRestRequestorProducer producer = new ApexRestRequestorProducer();
46         assertNotNull(producer);
47
48         EventHandlerParameters producerParameters = new EventHandlerParameters();
49
50         try {
51             producer.init(PRODUCER_NAME, producerParameters);
52         } catch (ApexEventException aee) {
53             assertEquals("specified producer properties are not applicable to REST requestor producer (ProducerName)",
54                             aee.getMessage());
55         }
56
57         RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();
58         producerParameters.setCarrierTechnologyParameters(rrctp);
59         try {
60             producer.init(PRODUCER_NAME, producerParameters);
61             fail("test should throw an exception here");
62         } catch (ApexEventException aee) {
63             assertEquals("REST Requestor producer (ProducerName) must run in peered requestor mode "
64                             + "with a REST Requestor consumer", aee.getMessage());
65         }
66
67         producerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);
68         rrctp.setUrl("ZZZZ");
69         try {
70             producer.init(PRODUCER_NAME, producerParameters);
71             fail("test should throw an exception here");
72         } catch (ApexEventException aee) {
73             assertEquals("URL may not be specified on REST Requestor producer (ProducerName)", aee.getMessage());
74         }
75
76         rrctp.setUrl(null);
77         rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET);
78         try {
79             producer.init(PRODUCER_NAME, producerParameters);
80             fail("test should throw an exception here");
81         } catch (ApexEventException aee) {
82             assertEquals("HTTP method may not be specified on REST Requestor producer (ProducerName)",
83                             aee.getMessage());
84         }
85
86         rrctp.setHttpMethod(null);
87         producer.init(PRODUCER_NAME, producerParameters);
88         producer.stop();
89
90         assertEquals(PRODUCER_NAME, producer.getName());
91         assertEquals(0, producer.getEventsSent());
92         assertEquals(null, producer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
93     }
94
95     @Test
96     public void testApexRestRequestorProducerRequest() throws ApexEventException {
97         EventHandlerParameters producerParameters = new EventHandlerParameters();
98
99         RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();
100         producerParameters.setCarrierTechnologyParameters(rrctp);
101         producerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);
102         rrctp.setUrl(null);
103         rrctp.setHttpMethod(null);
104
105         ApexRestRequestorProducer producer = new ApexRestRequestorProducer();
106         producer.init(PRODUCER_NAME, producerParameters);
107         producer.stop();
108
109         String eventName = "EventName";
110         String event = "This is the event";
111
112         try {
113             producer.sendEvent(12345, null, eventName, event);
114             fail("test should throw an exception here");
115         } catch (Exception aee) {
116             assertEquals("send of event to URL \"null\" failed, REST response consumer is not defined\n"
117                             + "This is the event", aee.getMessage());
118         }
119
120         ApexEventConsumer consumer = new ApexFileEventConsumer();
121         SynchronousEventCache eventCache = new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer,
122                         producer, 1000);
123         producer.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, eventCache);
124
125         PeeredReference peeredReference = new PeeredReference(EventHandlerPeeredMode.REQUESTOR, consumer, producer);
126         producer.setPeeredReference(EventHandlerPeeredMode.REQUESTOR, peeredReference);
127         try {
128             producer.sendEvent(12345, null, eventName, event);
129             fail("test should throw an exception here");
130         } catch (Exception aee) {
131             assertEquals("send of event to URL \"null\" failed, REST response consumer "
132                             + "is not an instance of ApexRestRequestorConsumer\n" + "This is the event",
133                             aee.getMessage());
134         }
135     }
136 }