44e020381aae1ae6385fbdb5d07ec172e041149e
[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.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;
33
34 /**
35  * Test the ApexRestRequestorConsumer class.
36  *
37  */
38 public class ApexRestRequestorConsumerTest {
39     @Test
40     public void testApexRestRequestorConsumerSetup() {
41         ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer();
42         assertNotNull(consumer);
43
44         String consumerName = "ConsumerName";
45
46         EventHandlerParameters consumerParameters = new EventHandlerParameters();
47         ApexEventReceiver incomingEventReceiver = null;
48
49         try {
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)",
54                             aee.getMessage());
55         }
56
57         RestRequestorCarrierTechnologyParameters rrctp = new RestRequestorCarrierTechnologyParameters();
58         consumerParameters.setCarrierTechnologyParameters(rrctp);
59         try {
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());
65         }
66
67         consumerParameters.setPeeredMode(EventHandlerPeeredMode.REQUESTOR, true);
68         rrctp.setHttpMethod(null);
69         try {
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());
74         }
75
76         rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET);
77         rrctp.setUrl("ZZZZ");
78         try {
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());
83         }
84
85         rrctp.setHttpMethod(RestRequestorCarrierTechnologyParameters.HttpMethod.GET);
86         rrctp.setUrl("http://www.onap.org");
87         consumerParameters.setPeerTimeout(EventHandlerPeeredMode.REQUESTOR, 0);
88
89         try {
90             consumer.init(consumerName, consumerParameters, incomingEventReceiver);
91         } catch (ApexEventException aee) {
92             fail("test should not throw an exception");
93         }
94
95         try {
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)",
100                             ex.getMessage());
101         }
102
103         assertEquals("ConsumerName", consumer.getName());
104         assertEquals(0, consumer.getEventsReceived());
105         assertEquals(null, consumer.getPeeredReference(EventHandlerPeeredMode.REQUESTOR));
106     }
107
108     @Test
109     public void testApexRestRequestorConsumerRequest() {
110         ApexRestRequestorConsumer consumer = new ApexRestRequestorConsumer();
111         assertNotNull(consumer);
112
113         String consumerName = "ConsumerName";
114
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);
123
124         // Test should time out requests
125         try {
126             consumer.init(consumerName, consumerParameters, incomingEventReceiver);
127             consumer.start();
128             ApexRestRequest request = new ApexRestRequest(123, "EventName", "Event body");
129             consumer.processRestRequest(request);
130             ThreadUtilities.sleep(2000);
131             consumer.stop();
132             assertEquals(0, consumer.getEventsReceived());
133         } catch (ApexEventException aee) {
134             fail("test should not throw an exception");
135         }
136     }
137 }