4fa9e915c3c9fb5cc0dbec46bdda8ddb16bbc955
[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.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30
31 import org.apache.camel.Endpoint;
32 import org.apache.camel.Exchange;
33 import org.apache.camel.ExtendedCamelContext;
34 import org.apache.camel.Processor;
35 import org.apache.camel.support.DefaultMessage;
36 import org.apache.camel.support.MessageSupport;
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 ExtendedCamelContext 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         String clientName =  "client name";
78         EventBusComponent rc = new EventBusComponent();
79         EventBusEndPoint endpoint = new EventBusEndPoint("http://host.com:8443/endpoint", rc);
80         endpoint.setName(clientName);
81         endpoint.setEventTopic("eventTopic");
82         endpoint.setPublisher(publisher);
83         endpoint.setPoolSize(45);
84         endpoint.setPollingDelay(10);
85
86         assertEquals(clientName, endpoint.getName());
87         assertTrue(endpoint.getEventTopic().compareTo("eventTopic") == 0);
88         assertTrue(endpoint.getPoolSize() == 45);
89         assertTrue(endpoint.getPollingDelay() == 10);
90         assertFalse(endpoint.isSingleton());
91         EventBusProducer producer = (EventBusProducer)endpoint.createProducer();
92         assertTrue(producer.getEndpoint() != null);
93         endpoint.end();
94     }
95     
96     @Test
97     public void validateEventBusComponent() throws Exception {
98         EventBusComponent rc = new EventBusComponent(context);
99         Endpoint endpoint = rc.createEndpoint("http://host.com:8443/endpoint", null, new HashMap<String, Object>());
100         assertTrue(endpoint.getEndpointUri().equals("http://host.com:8443/endpoint"));
101     }
102     
103     @Test
104     public void validateConsumer() throws Exception {
105         EventBusComponent rc = new EventBusComponent();
106         EventBusEndPoint endpoint = new EventBusEndPoint("http://host.com:8443/endpoint", rc);
107         
108         endpoint.setConsumer(consumer);
109         endpoint.setEventTopic("eventTopic");
110         endpoint.setPoolSize(45);
111         endpoint.setPollingDelay(10);
112         
113         assertTrue(endpoint.getEventTopic().compareTo("eventTopic") == 0);
114         assertTrue(endpoint.getPoolSize() == 45);
115         assertTrue(endpoint.getPollingDelay() == 10);
116         assertFalse(endpoint.isSingleton());
117         
118         EventBusConsumer consumer = (EventBusConsumer)endpoint.createConsumer(processor);
119     }
120     
121     @Test
122     public void validateConsumerPoll() throws Exception {
123         MessageSupport me = new DefaultMessage(context);
124         List<String> list = new ArrayList<>();
125         list.add("Message 1");
126         list.add("Message 2");
127         
128         Mockito.when(consumer.consumeAndCommit()).thenReturn(list);
129         Mockito.when(endPoint.createExchange()).thenReturn(exchange);
130         Mockito.when(exchange.getIn()).thenReturn(me);
131         Mockito.when(exchange.getOut()).thenReturn(me);
132         
133         EventBusConsumer busConsumer = new EventBusConsumer(endPoint, processor, consumer);
134         int messages = busConsumer.poll();
135     }
136 }