e535bf3ef8c245ebad61c4a08b4283c84bed9c9f
[aai/router-core.git] / src / test / java / org / onap / aai / event / EventBusTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.aai.event;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
29
30 import org.apache.camel.CamelContext;
31 import org.apache.camel.Endpoint;
32 import org.apache.camel.Exchange;
33 import org.apache.camel.Processor;
34 import org.apache.camel.impl.DefaultMessage;
35 import org.apache.camel.impl.MessageSupport;
36 import org.apache.kafka.clients.consumer.ConsumerRecords;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.mockito.runners.MockitoJUnitRunner;
43 import org.onap.aai.event.api.EventConsumer;
44 import org.onap.aai.event.api.EventPublisher;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class EventBusTest {
48         @Mock
49     public EventConsumer consumer;
50         
51         @Mock
52     public EventPublisher publisher;
53         
54         @Mock
55         public CamelContext context;
56         
57         @Mock
58         public Processor processor;
59
60         @Mock
61         Exchange exchange;
62         
63         @Mock
64         AbstractEventBusEndpoint endPoint;
65         
66         /**
67      * Test case initialization
68      * 
69      * @throws Exception the exception
70      */
71     @Before
72     public void init() throws Exception {
73     }
74
75     @Test
76     public void validateProducer() throws Exception {
77         EventBusComponent rc = new EventBusComponent();
78         EventBusEndPoint endpoint = new EventBusEndPoint("http://host.com:8443/endpoint", rc);
79         endpoint.setEventTopic("eventTopic");
80         endpoint.setPublisher(publisher);
81         endpoint.setPoolSize(45);
82         endpoint.setPollingDelay(10);
83         
84         assertTrue(endpoint.getEventTopic().compareTo("eventTopic") == 0);
85         assertTrue(endpoint.getPoolSize() == 45);
86         assertTrue(endpoint.getPollingDelay() == 10);
87         assertFalse(endpoint.isSingleton());
88         EventBusProducer producer = (EventBusProducer)endpoint.createProducer();
89         assertTrue(producer.getEndpoint() != null);
90         endpoint.close();
91     }
92     
93     @Test
94     public void validateEventBusComponent() throws Exception {
95         EventBusComponent rc = new EventBusComponent(context);
96         Endpoint endpoint = rc.createEndpoint("http://host.com:8443/endpoint", null, new HashMap<String, Object>());
97         assertTrue(endpoint.getEndpointUri().equals("http://host.com:8443/endpoint"));
98     }
99     
100     @Test
101     public void validateConsumer() throws Exception {
102         EventBusComponent rc = new EventBusComponent();
103         EventBusEndPoint endpoint = new EventBusEndPoint("http://host.com:8443/endpoint", rc);
104         
105         endpoint.setConsumer(consumer);
106         endpoint.setEventTopic("eventTopic");
107         endpoint.setPoolSize(45);
108         endpoint.setPollingDelay(10);
109         
110         assertTrue(endpoint.getEventTopic().compareTo("eventTopic") == 0);
111         assertTrue(endpoint.getPoolSize() == 45);
112         assertTrue(endpoint.getPollingDelay() == 10);
113         assertFalse(endpoint.isSingleton());
114         
115         EventBusConsumer consumer = (EventBusConsumer)endpoint.createConsumer(processor);
116     }
117     
118     @Test
119     public void validateConsumerPoll() throws Exception {
120         MessageSupport me = new DefaultMessage(context);
121         List<String> list = new ArrayList<>();
122         list.add("Message 1");
123         list.add("Message 2");
124         
125         Mockito.when(consumer.consumeAndCommit()).thenReturn(list);
126         Mockito.when(endPoint.createExchange()).thenReturn(exchange);
127         Mockito.when(exchange.getIn()).thenReturn(me);
128         Mockito.when(exchange.getOut()).thenReturn(me);
129         
130         EventBusConsumer busConsumer = new EventBusConsumer(endPoint, processor, consumer);
131         int messages = busConsumer.poll();
132     }
133 }