Change the package name from openo to onap
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / producer / MQProducerTest.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.holmes.common.producer;
18
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;
23
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.config.MQConfig;
40 import org.powermock.api.easymock.PowerMock;
41 import org.powermock.modules.junit4.rule.PowerMockRule;
42 import org.powermock.reflect.Whitebox;
43
44 public class MQProducerTest {
45
46     @Rule
47     public PowerMockRule powerMockRule = new PowerMockRule();
48     @Rule
49     public ExpectedException thrown = ExpectedException.none();
50
51     private IterableProvider<MQConfig> mqConfigProvider;
52
53     private ConnectionFactory connectionFactory;
54
55     private MQProducer mqProducer;
56
57     @Before
58     public void before() throws Exception {
59         mqProducer = new MQProducer();
60
61         mqConfigProvider = PowerMock.createMock(IterableProvider.class);
62         connectionFactory = PowerMock.createMock(ConnectionFactory.class);
63
64         Whitebox.setInternalState(mqProducer, "mqConfigProvider", mqConfigProvider);
65         Whitebox.setInternalState(mqProducer, "connectionFactory", connectionFactory);
66         PowerMock.resetAll();
67     }
68
69     @Test
70     public void init() {
71         MQConfig mqConfig = new MQConfig();
72         mqConfig.brokerIp = "127.0.0.1";
73         mqConfig.brokerPort = 61616;
74         mqConfig.brokerPassword = "admin";
75         mqConfig.brokerUsername = "admin";
76         expect(mqConfigProvider.get()).andReturn(mqConfig).anyTimes();
77
78         PowerMock.replayAll();
79
80         mqProducer.init();
81
82         PowerMock.verifyAll();
83     }
84
85     @Test
86     public void sendAlarmMQTopicMsg() throws Exception {
87         Alarm alarm = new Alarm();
88         Connection connection = PowerMock.createMock(Connection.class);
89         Session session = PowerMock.createMock(Session.class);
90         Destination destination = PowerMock.createMock(Topic.class);
91         MessageProducer messageProducer = PowerMock.createMock(MessageProducer.class);
92         ObjectMessage objMessage = PowerMock.createMock(ObjectMessage.class);
93
94         expect(connectionFactory.createConnection()).andReturn(connection);
95         connection.start();
96         expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session);
97         expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination);
98         expect(session.createProducer(anyObject(Destination.class))).andReturn(messageProducer);
99
100         expect(session.createObjectMessage(anyObject(Alarm.class))).andReturn(objMessage);
101         messageProducer.send(objMessage);
102         session.commit();
103         connection.close();
104
105         PowerMock.replayAll();
106
107         mqProducer.sendAlarmMQTopicMsg(alarm);
108
109         PowerMock.verifyAll();
110
111     }
112
113     @Test
114     public void sendAlarmMQTopicMsg_exception() throws Exception {
115         thrown.expect(JMSException.class);
116         Alarm alarm = new Alarm();
117
118         expect(connectionFactory.createConnection()).andThrow(new JMSException(""));
119
120         PowerMock.replayAll();
121
122         mqProducer.sendAlarmMQTopicMsg(alarm);
123
124         PowerMock.verifyAll();
125     }
126
127     @Test
128     public void sendCorrelationMQTopicMsg() throws Exception {
129
130         Connection connection = PowerMock.createMock(Connection.class);
131         Session session = PowerMock.createMock(Session.class);
132         Destination destination = PowerMock.createMock(Topic.class);
133         MessageProducer messageProducer = PowerMock.createMock(MessageProducer.class);
134         ObjectMessage objMessage = PowerMock.createMock(ObjectMessage.class);
135
136         expect(connectionFactory.createConnection()).andReturn(connection);
137         connection.start();
138         expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session);
139         expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination);
140         expect(session.createProducer(anyObject(Destination.class))).andReturn(messageProducer);
141
142         expect(session.createObjectMessage(anyObject(CorrelationResult.class)))
143                 .andReturn(objMessage);
144         messageProducer.send(objMessage);
145         session.commit();
146         connection.close();
147
148         PowerMock.replayAll();
149
150         Alarm parentAlarm = new Alarm();
151         Alarm childAlarm = new Alarm();
152         mqProducer.sendCorrelationMQTopicMsg("ruleId", 123L, parentAlarm, childAlarm);
153
154         PowerMock.verifyAll();
155
156     }
157
158     @Test
159     public void sendCorrelationMQTopicMsg_exception() throws Exception {
160         thrown.expect(JMSException.class);
161
162         expect(connectionFactory.createConnection()).andThrow(new JMSException(""));
163
164         PowerMock.replayAll();
165
166         Alarm parentAlarm = new Alarm();
167         Alarm childAlarm = new Alarm();
168         mqProducer.sendCorrelationMQTopicMsg("ruleId", 123L, parentAlarm, childAlarm);
169
170         PowerMock.verifyAll();
171
172     }
173