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 @RunWith(MockitoJUnitRunner.class)
47 public class EventBusTest {
49 public EventConsumer consumer;
52 public EventPublisher publisher;
55 public CamelContext 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 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);
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);
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"));
101 public void validateConsumer() throws Exception {
102 EventBusComponent rc = new EventBusComponent();
103 EventBusEndPoint endpoint = new EventBusEndPoint("http://host.com:8443/endpoint", rc);
105 endpoint.setConsumer(consumer);
106 endpoint.setEventTopic("eventTopic");
107 endpoint.setPoolSize(45);
108 endpoint.setPollingDelay(10);
110 assertTrue(endpoint.getEventTopic().compareTo("eventTopic") == 0);
111 assertTrue(endpoint.getPoolSize() == 45);
112 assertTrue(endpoint.getPollingDelay() == 10);
113 assertFalse(endpoint.isSingleton());
115 EventBusConsumer consumer = (EventBusConsumer)endpoint.createConsumer(processor);
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");
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);
130 EventBusConsumer busConsumer = new EventBusConsumer(endPoint, processor, consumer);
131 int messages = busConsumer.poll();