Make generic router core implementations
[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 import com.att.aft.dme2.hazelcast.core.Message;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class EventBusTest {
50         @Mock
51     public EventConsumer consumer;
52         
53         @Mock
54     public EventPublisher publisher;
55         
56         @Mock
57         public CamelContext context;
58         
59         @Mock
60         public Processor processor;
61
62         @Mock
63         Exchange exchange;
64         
65         @Mock
66         AbstractEventBusEndpoint endPoint;
67         
68         /**
69      * Test case initialization
70      * 
71      * @throws Exception the exception
72      */
73     @Before
74     public void init() throws Exception {
75     }
76
77     @Test
78     public void validateProducer() throws Exception {
79         EventBusComponent rc = new EventBusComponent();
80         EventBusEndPoint endpoint = new EventBusEndPoint("http://host.com:8443/endpoint", rc);
81         endpoint.setEventTopic("eventTopic");
82         endpoint.setPublisher(publisher);
83         endpoint.setPoolSize(45);
84         endpoint.setPollingDelay(10);
85         
86         assertTrue(endpoint.getEventTopic().compareTo("eventTopic") == 0);
87         assertTrue(endpoint.getPoolSize() == 45);
88         assertTrue(endpoint.getPollingDelay() == 10);
89         assertFalse(endpoint.isSingleton());
90         EventBusProducer producer = (EventBusProducer)endpoint.createProducer();
91         assertTrue(producer.getEndpoint() != null);
92         endpoint.close();
93     }
94     
95     @Test
96     public void validateEventBusComponent() throws Exception {
97         EventBusComponent rc = new EventBusComponent(context);
98         Endpoint endpoint = rc.createEndpoint("http://host.com:8443/endpoint", null, new HashMap<String, Object>());
99         assertTrue(endpoint.getEndpointUri().equals("http://host.com:8443/endpoint"));
100     }
101     
102     @Test
103     public void validateConsumer() throws Exception {
104         EventBusComponent rc = new EventBusComponent();
105         EventBusEndPoint endpoint = new EventBusEndPoint("http://host.com:8443/endpoint", rc);
106         
107         endpoint.setConsumer(consumer);
108         endpoint.setEventTopic("eventTopic");
109         endpoint.setPoolSize(45);
110         endpoint.setPollingDelay(10);
111         
112         assertTrue(endpoint.getEventTopic().compareTo("eventTopic") == 0);
113         assertTrue(endpoint.getPoolSize() == 45);
114         assertTrue(endpoint.getPollingDelay() == 10);
115         assertFalse(endpoint.isSingleton());
116         
117         EventBusConsumer consumer = (EventBusConsumer)endpoint.createConsumer(processor);
118     }
119     
120     @Test
121     public void validateConsumerPoll() throws Exception {
122         MessageSupport me = new DefaultMessage(context);
123         List<String> list = new ArrayList<>();
124         list.add("Message 1");
125         list.add("Message 2");
126         
127         Mockito.when(consumer.consumeAndCommit()).thenReturn(list);
128         Mockito.when(endPoint.createExchange()).thenReturn(exchange);
129         Mockito.when(exchange.getIn()).thenReturn(me);
130         Mockito.when(exchange.getOut()).thenReturn(me);
131         
132         EventBusConsumer busConsumer = new EventBusConsumer(endPoint, processor, consumer);
133         int messages = busConsumer.poll();
134     }
135 }