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.common.producer;
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;
24 import javax.jms.Connection;
25 import javax.jms.ConnectionFactory;
26 import javax.jms.Destination;
27 import javax.jms.JMSException;
28 import javax.jms.MessageProducer;
29 import javax.jms.ObjectMessage;
30 import javax.jms.Session;
31 import javax.jms.Topic;
32 import org.glassfish.hk2.api.IterableProvider;
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.ExpectedException;
37 import org.onap.holmes.common.api.stat.Alarm;
38 import org.onap.holmes.common.api.entity.CorrelationResult;
39 import org.onap.holmes.common.api.stat.VesAlarm;
40 import org.onap.holmes.common.config.MQConfig;
41 import org.powermock.api.easymock.PowerMock;
42 import org.powermock.modules.junit4.rule.PowerMockRule;
43 import org.powermock.reflect.Whitebox;
45 public class MQProducerTest {
48 public PowerMockRule powerMockRule = new PowerMockRule();
50 public ExpectedException thrown = ExpectedException.none();
52 private IterableProvider<MQConfig> mqConfigProvider;
54 private ConnectionFactory connectionFactory;
56 private MQProducer mqProducer;
59 public void before() throws Exception {
60 mqProducer = new MQProducer();
62 mqConfigProvider = PowerMock.createMock(IterableProvider.class);
63 connectionFactory = PowerMock.createMock(ConnectionFactory.class);
65 Whitebox.setInternalState(mqProducer, "mqConfigProvider", mqConfigProvider);
66 Whitebox.setInternalState(mqProducer, "connectionFactory", connectionFactory);
72 MQConfig mqConfig = new MQConfig();
73 mqConfig.brokerIp = "127.0.0.1";
74 mqConfig.brokerPort = 61616;
75 mqConfig.brokerPassword = "admin";
76 mqConfig.brokerUsername = "admin";
77 expect(mqConfigProvider.get()).andReturn(mqConfig).anyTimes();
79 PowerMock.replayAll();
83 PowerMock.verifyAll();
87 public void sendAlarmMQTopicMsg() throws Exception {
88 VesAlarm alarm = new VesAlarm();
89 Connection connection = PowerMock.createMock(Connection.class);
90 Session session = PowerMock.createMock(Session.class);
91 Destination destination = PowerMock.createMock(Topic.class);
92 MessageProducer messageProducer = PowerMock.createMock(MessageProducer.class);
93 ObjectMessage objMessage = PowerMock.createMock(ObjectMessage.class);
95 expect(connectionFactory.createConnection()).andReturn(connection);
97 expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session);
98 expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination);
99 expect(session.createProducer(anyObject(Destination.class))).andReturn(messageProducer);
101 expect(session.createObjectMessage(anyObject(Alarm.class))).andReturn(objMessage);
102 messageProducer.send(objMessage);
106 PowerMock.replayAll();
108 mqProducer.sendAlarmMQTopicMsg(alarm);
110 PowerMock.verifyAll();
115 public void sendAlarmMQTopicMsg_exception() throws Exception {
116 thrown.expect(JMSException.class);
117 VesAlarm alarm = new VesAlarm();
119 expect(connectionFactory.createConnection()).andThrow(new JMSException(""));
121 PowerMock.replayAll();
123 mqProducer.sendAlarmMQTopicMsg(alarm);
125 PowerMock.verifyAll();
129 public void sendCorrelationMQTopicMsg() throws Exception {
131 Connection connection = PowerMock.createMock(Connection.class);
132 Session session = PowerMock.createMock(Session.class);
133 Destination destination = PowerMock.createMock(Topic.class);
134 MessageProducer messageProducer = PowerMock.createMock(MessageProducer.class);
135 ObjectMessage objMessage = PowerMock.createMock(ObjectMessage.class);
137 expect(connectionFactory.createConnection()).andReturn(connection);
139 expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session);
140 expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination);
141 expect(session.createProducer(anyObject(Destination.class))).andReturn(messageProducer);
143 expect(session.createObjectMessage(anyObject(CorrelationResult.class)))
144 .andReturn(objMessage);
145 messageProducer.send(objMessage);
149 PowerMock.replayAll();
151 Alarm parentAlarm = new Alarm();
152 Alarm childAlarm = new Alarm();
153 mqProducer.sendCorrelationMQTopicMsg("ruleId", 123L, parentAlarm, childAlarm);
155 PowerMock.verifyAll();
160 public void sendCorrelationMQTopicMsg_exception() throws Exception {
161 thrown.expect(JMSException.class);
163 expect(connectionFactory.createConnection()).andThrow(new JMSException(""));
165 PowerMock.replayAll();
167 Alarm parentAlarm = new Alarm();
168 Alarm childAlarm = new Alarm();
169 mqProducer.sendCorrelationMQTopicMsg("ruleId", 123L, parentAlarm, childAlarm);
171 PowerMock.verifyAll();