2 * Copyright 2017 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.holmes.engine.mqconsumer;
19 import static org.easymock.EasyMock.anyBoolean;
20 import static org.easymock.EasyMock.anyInt;
21 import static org.easymock.EasyMock.anyObject;
22 import static org.easymock.EasyMock.expect;
25 import javax.jms.Connection;
26 import javax.jms.ConnectionFactory;
27 import javax.jms.Destination;
28 import javax.jms.JMSException;
29 import javax.jms.MessageConsumer;
30 import javax.jms.Session;
31 import javax.jms.Topic;
32 import org.apache.activemq.command.ActiveMQObjectMessage;
33 import org.easymock.EasyMock;
34 import org.glassfish.hk2.api.IterableProvider;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.holmes.common.api.stat.Alarm;
38 import org.onap.holmes.common.config.MQConfig;
39 import org.powermock.api.easymock.PowerMock;
40 import org.powermock.reflect.Whitebox;
42 public class MQConsumerTest {
44 private IterableProvider<MQConfig> mqConfigProvider;
46 private ConnectionFactory connectionFactory;
48 private MQConsumer mqConsumer;
50 private MQConsumer mqConsumer1;
52 private MQConsumer mqConsumer2;
57 mqConsumer = new MQConsumer();
59 mqConfigProvider = PowerMock.createMock(IterableProvider.class);
60 connectionFactory = PowerMock.createMock(ConnectionFactory.class);
62 Whitebox.setInternalState(mqConsumer, "mqConfigProvider", mqConfigProvider);
63 Whitebox.setInternalState(mqConsumer, "connectionFactory", connectionFactory);
67 public void init() throws Exception {
68 MQConfig mqConfig = new MQConfig();
69 mqConfig.brokerIp = "127.0.0.1";
70 mqConfig.brokerPort = 4567;
71 mqConfig.brokerUsername = "admin";
72 mqConfig.brokerPassword = "admin";
74 expect(mqConfigProvider.get()).andReturn(mqConfig).anyTimes();
75 PowerMock.replayAll();
77 PowerMock.verifyAll();
81 public void listener_receive() throws JMSException {
82 MQConsumer.AlarmMqMessageListener listener = mqConsumer.new AlarmMqMessageListener();
84 Connection connection = PowerMock.createMock(Connection.class);
85 Session session = PowerMock.createMock(Session.class);
86 Destination destination = PowerMock.createMock(Topic.class);
87 MessageConsumer consumer = PowerMock.createMock(MessageConsumer.class);
89 Whitebox.setInternalState(listener, "connection", connection);
90 Whitebox.setInternalState(listener, "session", session);
91 Whitebox.setInternalState(listener, "destination", destination);
92 Whitebox.setInternalState(listener, "consumer", consumer);
96 expect(connectionFactory.createConnection()).andReturn(connection);
98 expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session);
99 expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination);
100 expect(session.createConsumer(anyObject(Destination.class))).andReturn(consumer);
101 consumer.setMessageListener(listener);
103 PowerMock.replayAll();
107 PowerMock.verifyAll();
111 public void listener_exception() throws JMSException {
112 MQConsumer.AlarmMqMessageListener listener = mqConsumer.new AlarmMqMessageListener();
114 Connection connection = PowerMock.createMock(Connection.class);
115 Session session = PowerMock.createMock(Session.class);
116 Destination destination = PowerMock.createMock(Topic.class);
117 MessageConsumer consumer = PowerMock.createMock(MessageConsumer.class);
119 Whitebox.setInternalState(listener, "connection", connection);
120 Whitebox.setInternalState(listener, "session", session);
121 Whitebox.setInternalState(listener, "destination", destination);
122 Whitebox.setInternalState(listener, "consumer", consumer);
126 expect(connectionFactory.createConnection()).andReturn(connection);
128 expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session);
129 expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination);
130 expect(session.createConsumer(anyObject(Destination.class))).andReturn(consumer);
131 consumer.setMessageListener(listener);
132 EasyMock.expectLastCall().andThrow(new JMSException(""));
138 PowerMock.replayAll();
142 PowerMock.verifyAll();
146 public void listener_close_exception() throws JMSException {
147 MQConsumer.AlarmMqMessageListener listener = mqConsumer.new AlarmMqMessageListener();
149 Connection connection = PowerMock.createMock(Connection.class);
150 Session session = PowerMock.createMock(Session.class);
151 Destination destination = PowerMock.createMock(Topic.class);
152 MessageConsumer consumer = PowerMock.createMock(MessageConsumer.class);
154 Whitebox.setInternalState(listener, "connection", connection);
155 Whitebox.setInternalState(listener, "session", session);
156 Whitebox.setInternalState(listener, "destination", destination);
157 Whitebox.setInternalState(listener, "consumer", consumer);
161 expect(connectionFactory.createConnection()).andReturn(connection);
163 expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session);
164 expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination);
165 expect(session.createConsumer(anyObject(Destination.class))).andReturn(consumer);
166 consumer.setMessageListener(listener);
167 EasyMock.expectLastCall().andThrow(new JMSException(""));
170 EasyMock.expectLastCall().andThrow(new JMSException(""));
172 PowerMock.replayAll();
176 PowerMock.verifyAll();
180 public void listener_on_message() throws JMSException {
181 MQConsumer.AlarmMqMessageListener listener = mqConsumer.new AlarmMqMessageListener();
182 Alarm alarm = new Alarm();
183 alarm.setAlarmKey("alarmKey");
184 ActiveMQObjectMessage objectMessage = new ActiveMQObjectMessage();
185 objectMessage.setObject(alarm);
187 listener.onMessage(objectMessage);