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.assertFalse;
24 import static org.junit.Assert.assertTrue;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.List;
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;
46 import com.att.aft.dme2.hazelcast.core.Message;
48 @RunWith(MockitoJUnitRunner.class)
49 public class EventBusTest {
51 public EventConsumer consumer;
54 public EventPublisher publisher;
57 public CamelContext context;
60 public Processor processor;
66 AbstractEventBusEndpoint endPoint;
69 * Test case initialization
71 * @throws Exception the exception
74 public void init() throws Exception {
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);
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);
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"));
103 public void validateConsumer() throws Exception {
104 EventBusComponent rc = new EventBusComponent();
105 EventBusEndPoint endpoint = new EventBusEndPoint("http://host.com:8443/endpoint", rc);
107 endpoint.setConsumer(consumer);
108 endpoint.setEventTopic("eventTopic");
109 endpoint.setPoolSize(45);
110 endpoint.setPollingDelay(10);
112 assertTrue(endpoint.getEventTopic().compareTo("eventTopic") == 0);
113 assertTrue(endpoint.getPoolSize() == 45);
114 assertTrue(endpoint.getPollingDelay() == 10);
115 assertFalse(endpoint.isSingleton());
117 EventBusConsumer consumer = (EventBusConsumer)endpoint.createConsumer(processor);
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");
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);
132 EventBusConsumer busConsumer = new EventBusConsumer(endPoint, processor, consumer);
133 int messages = busConsumer.poll();