3ef1724273a530adce20734529e7637cafd507ae
[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.restclient;
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 ch.qos.logback.classic.Level;
28
29 import javax.ws.rs.client.Client;
30 import javax.ws.rs.client.Invocation.Builder;
31 import javax.ws.rs.client.WebTarget;
32 import javax.ws.rs.core.Response;
33
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
39 import org.onap.policy.apex.service.engine.event.ApexEventException;
40 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
41 import org.onap.policy.apex.service.engine.event.impl.filecarrierplugin.consumer.ApexFileEventConsumer;
42 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
43 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * Test the ApexRestClientProducer class.
49  *
50  */
51 public class ApexRestClientProducerTest {
52     private static final Logger LOGGER = LoggerFactory.getLogger(ApexRestClientProducer.class);
53
54     @Mock
55     private Client httpClientMock;
56
57     @Mock
58     private WebTarget targetMock;
59
60     @Mock
61     private Builder builderMock;
62
63     @Mock
64     private Response responseMock;
65
66     @Test
67     public void testApexRestClientProducerErrors() {
68         ApexRestClientProducer arcp = new ApexRestClientProducer();
69         assertNotNull(arcp);
70
71         EventHandlerParameters producerParameters = new EventHandlerParameters();
72         try {
73             arcp.init("RestClientProducer", producerParameters);
74             fail("test should throw an exception here");
75         } catch (ApexEventException e) {
76             assertEquals(
77                 "specified producer properties are not applicable to REST client producer (RestClientProducer)",
78                 e.getMessage());
79         }
80
81         RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters();
82         producerParameters.setCarrierTechnologyParameters(rcctp);
83         rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.DELETE);
84         try {
85             arcp.init("RestClientConsumer", producerParameters);
86             assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.GET, rcctp.getHttpMethod());
87             fail("test should throw an exception here");
88         } catch (ApexEventException e) {
89             assertEquals("specified HTTP method of \"DELETE\" is invalid, only HTTP methods \"POST\" and \"PUT\" "
90                 + "are supproted for event sending on REST client producer (RestClientConsumer)", e.getMessage());
91         }
92
93         rcctp.setHttpMethod(null);
94         try {
95             arcp.init("RestClientConsumer", producerParameters);
96             assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod());
97
98             assertEquals("RestClientConsumer", arcp.getName());
99
100             arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null);
101             assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS));
102
103             arcp.stop();
104         } catch (ApexEventException e) {
105             fail("test should not throw an exception");
106         }
107
108         rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST);
109         try {
110             arcp.init("RestClientConsumer", producerParameters);
111             assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod());
112
113             assertEquals("RestClientConsumer", arcp.getName());
114
115             arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null);
116             assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS));
117
118             arcp.stop();
119         } catch (ApexEventException e) {
120             fail("test should not throw an exception");
121         }
122
123         rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.PUT);
124         try {
125             arcp.init("RestClientConsumer", producerParameters);
126             assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod());
127
128             assertEquals("RestClientConsumer", arcp.getName());
129
130             arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, null);
131             assertEquals(null, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS));
132
133             arcp.stop();
134         } catch (ApexEventException e) {
135             fail("test should not throw an exception");
136         }
137     }
138
139     @Test
140     public void testApexRestClientProducerPutEvent() {
141         MockitoAnnotations.initMocks(this);
142
143         ApexRestClientProducer arcp = new ApexRestClientProducer();
144         assertNotNull(arcp);
145
146         EventHandlerParameters producerParameters = new EventHandlerParameters();
147         RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters();
148         producerParameters.setCarrierTechnologyParameters(rcctp);
149
150         rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.PUT);
151         try {
152             arcp.init("RestClientConsumer", producerParameters);
153             assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.PUT, rcctp.getHttpMethod());
154
155             assertEquals("RestClientConsumer", arcp.getName());
156         } catch (ApexEventException e) {
157             fail("test should not throw an exception");
158         }
159
160         rcctp.setUrl("http://some.place.that.does.not/exist");
161         Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus();
162         Mockito.doReturn(responseMock).when(builderMock).put(Mockito.any());
163         Mockito.doReturn(builderMock).when(targetMock).request("application/json");
164         Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl());
165         arcp.setClient(httpClientMock);
166
167         try {
168             arcp.sendEvent(123, "EventName", "This is an Event");
169             arcp.stop();
170         } catch (Exception e) {
171             fail("test should not throw an exception");
172         }
173     }
174
175     @Test
176     public void testApexRestClientProducerPostEvent() {
177         MockitoAnnotations.initMocks(this);
178
179         ApexRestClientProducer arcp = new ApexRestClientProducer();
180         assertNotNull(arcp);
181
182         EventHandlerParameters producerParameters = new EventHandlerParameters();
183         RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters();
184         producerParameters.setCarrierTechnologyParameters(rcctp);
185
186         rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST);
187         try {
188             arcp.init("RestClientConsumer", producerParameters);
189             assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod());
190
191             assertEquals("RestClientConsumer", arcp.getName());
192         } catch (ApexEventException e) {
193             fail("test should not throw an exception");
194         }
195
196         rcctp.setUrl("http://some.place.that.does.not/exist");
197         Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus();
198         Mockito.doReturn(responseMock).when(builderMock).post(Mockito.any());
199         Mockito.doReturn(builderMock).when(targetMock).request("application/json");
200         Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl());
201         arcp.setClient(httpClientMock);
202
203         try {
204             arcp.sendEvent(123, "EventName", "This is an Event");
205             arcp.stop();
206         } catch (Exception e) {
207             fail("test should not throw an exception");
208         }
209     }
210
211     @Test
212     public void testApexRestClientProducerPostEventCache() {
213         MockitoAnnotations.initMocks(this);
214
215         ApexRestClientProducer arcp = new ApexRestClientProducer();
216         assertNotNull(arcp);
217
218         EventHandlerParameters producerParameters = new EventHandlerParameters();
219         RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters();
220         producerParameters.setCarrierTechnologyParameters(rcctp);
221
222         rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST);
223
224         ApexEventConsumer consumer = new ApexFileEventConsumer();
225         SynchronousEventCache cache = new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, arcp,
226             1000);
227         arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, cache);
228         assertEquals(cache, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS));
229         try {
230             arcp.init("RestClientConsumer", producerParameters);
231             assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod());
232
233             assertEquals("RestClientConsumer", arcp.getName());
234         } catch (ApexEventException e) {
235             fail("test should not throw an exception");
236         }
237
238         rcctp.setUrl("http://some.place.that.does.not/exist");
239         Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus();
240         Mockito.doReturn(responseMock).when(builderMock).post(Mockito.any());
241         Mockito.doReturn(builderMock).when(targetMock).request("application/json");
242         Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl());
243         arcp.setClient(httpClientMock);
244
245         try {
246             arcp.sendEvent(123, "EventName", "This is an Event");
247             arcp.stop();
248         } catch (Exception e) {
249             fail("test should not throw an exception");
250         }
251     }
252
253
254     @Test
255     public void testApexRestClientProducerPostEventCacheTrace() {
256         MockitoAnnotations.initMocks(this);
257
258         ch.qos.logback.classic.Logger classicLogger = (ch.qos.logback.classic.Logger) LOGGER;
259         classicLogger.setLevel(Level.TRACE);
260         
261         ApexRestClientProducer arcp = new ApexRestClientProducer();
262         assertNotNull(arcp);
263
264         EventHandlerParameters producerParameters = new EventHandlerParameters();
265         RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters();
266         producerParameters.setCarrierTechnologyParameters(rcctp);
267
268         rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST);
269
270         ApexEventConsumer consumer = new ApexFileEventConsumer();
271         SynchronousEventCache cache = new SynchronousEventCache(EventHandlerPeeredMode.SYNCHRONOUS, consumer, arcp,
272             1000);
273         arcp.setPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS, cache);
274         assertEquals(cache, arcp.getPeeredReference(EventHandlerPeeredMode.SYNCHRONOUS));
275         try {
276             arcp.init("RestClientConsumer", producerParameters);
277             assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod());
278
279             assertEquals("RestClientConsumer", arcp.getName());
280         } catch (ApexEventException e) {
281             fail("test should not throw an exception");
282         }
283
284         rcctp.setUrl("http://some.place.that.does.not/exist");
285         Mockito.doReturn(Response.Status.OK.getStatusCode()).when(responseMock).getStatus();
286         Mockito.doReturn(responseMock).when(builderMock).post(Mockito.any());
287         Mockito.doReturn(builderMock).when(targetMock).request("application/json");
288         Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl());
289         arcp.setClient(httpClientMock);
290
291         try {
292             arcp.sendEvent(123, "EventName", "This is an Event");
293             arcp.stop();
294         } catch (Exception e) {
295             fail("test should not throw an exception");
296         }
297     }
298
299     @Test
300     public void testApexRestClientProducerHttpError() {
301         MockitoAnnotations.initMocks(this);
302
303         ApexRestClientProducer arcp = new ApexRestClientProducer();
304         assertNotNull(arcp);
305
306         EventHandlerParameters producerParameters = new EventHandlerParameters();
307         RestClientCarrierTechnologyParameters rcctp = new RestClientCarrierTechnologyParameters();
308         producerParameters.setCarrierTechnologyParameters(rcctp);
309
310         rcctp.setHttpMethod(RestClientCarrierTechnologyParameters.HttpMethod.POST);
311         try {
312             arcp.init("RestClientConsumer", producerParameters);
313             assertEquals(RestClientCarrierTechnologyParameters.HttpMethod.POST, rcctp.getHttpMethod());
314
315             assertEquals("RestClientConsumer", arcp.getName());
316         } catch (ApexEventException e) {
317             fail("test should not throw an exception");
318         }
319
320         rcctp.setUrl("http://some.place.that.does.not/exist");
321         Mockito.doReturn(Response.Status.BAD_REQUEST.getStatusCode()).when(responseMock).getStatus();
322         Mockito.doReturn(responseMock).when(builderMock).post(Mockito.any());
323         Mockito.doReturn(builderMock).when(targetMock).request("application/json");
324         Mockito.doReturn(targetMock).when(httpClientMock).target(rcctp.getUrl());
325         arcp.setClient(httpClientMock);
326
327         try {
328             arcp.sendEvent(123, "EventName", "This is an Event");
329             fail("test should throw an exception here");
330         } catch (Exception e) {
331             assertEquals(
332                 "send of event to URL \"http://some.place.that.does.not/exist\" using HTTP \"POST\" "
333                     + "failed with status code 400 and message \"null\", event:\n" + "This is an Event",
334                 e.getMessage());
335         }
336     }
337 }