2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.event;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
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;
46 @RunWith(MockitoJUnitRunner.class)
47 public class EventBusTest {
49 public EventConsumer consumer;
52 public EventPublisher publisher;
55 public ExtendedCamelContext context;
58 public Processor processor;
64 AbstractEventBusEndpoint endPoint;
67 * Test case initialization
69 * @throws Exception the exception
72 public void init() throws Exception {
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);
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);
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"));
104 public void validateConsumer() throws Exception {
105 EventBusComponent rc = new EventBusComponent();
106 EventBusEndPoint endpoint = new EventBusEndPoint("http://host.com:8443/endpoint", rc);
108 endpoint.setConsumer(consumer);
109 endpoint.setEventTopic("eventTopic");
110 endpoint.setPoolSize(45);
111 endpoint.setPollingDelay(10);
113 assertTrue(endpoint.getEventTopic().compareTo("eventTopic") == 0);
114 assertTrue(endpoint.getPoolSize() == 45);
115 assertTrue(endpoint.getPollingDelay() == 10);
116 assertFalse(endpoint.isSingleton());
118 EventBusConsumer consumer = (EventBusConsumer)endpoint.createConsumer(processor);
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");
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);
133 EventBusConsumer busConsumer = new EventBusConsumer(endPoint, processor, consumer);
134 int messages = busConsumer.poll();