58ea684330a797577892897d501892180adaa224
[holmes/common.git] / holmes-actions / src / test / java / org / openo / 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.openo.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.openo.holmes.common.api.entity.CorrelationResult;
38 import org.openo.holmes.common.api.stat.Alarm;
39 import org.openo.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 sendAlarmMQTopicMsg() throws Exception {
71         Alarm alarm = new Alarm();
72         Connection connection = PowerMock.createMock(Connection.class);
73         Session session = PowerMock.createMock(Session.class);
74         Destination destination = PowerMock.createMock(Topic.class);
75         MessageProducer messageProducer = PowerMock.createMock(MessageProducer.class);
76         ObjectMessage objMessage = PowerMock.createMock(ObjectMessage.class);
77
78         expect(connectionFactory.createConnection()).andReturn(connection);
79         connection.start();
80         expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session);
81         expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination);
82         expect(session.createProducer(anyObject(Destination.class))).andReturn(messageProducer);
83
84         expect(session.createObjectMessage(anyObject(Alarm.class))).andReturn(objMessage);
85         messageProducer.send(objMessage);
86         session.commit();
87         connection.close();
88
89         PowerMock.replayAll();
90
91         mqProducer.sendAlarmMQTopicMsg(alarm);
92
93         PowerMock.verifyAll();
94
95     }
96
97     @Test
98     public void sendAlarmMQTopicMsg_exception() throws Exception {
99         thrown.expect(JMSException.class);
100         Alarm alarm = new Alarm();
101
102         expect(connectionFactory.createConnection()).andThrow(new JMSException(""));
103
104         PowerMock.replayAll();
105
106         mqProducer.sendAlarmMQTopicMsg(alarm);
107
108         PowerMock.verifyAll();
109     }
110
111     @Test
112     public void sendCorrelationMQTopicMsg() throws Exception {
113
114         Connection connection = PowerMock.createMock(Connection.class);
115         Session session = PowerMock.createMock(Session.class);
116         Destination destination = PowerMock.createMock(Topic.class);
117         MessageProducer messageProducer = PowerMock.createMock(MessageProducer.class);
118         ObjectMessage objMessage = PowerMock.createMock(ObjectMessage.class);
119
120         expect(connectionFactory.createConnection()).andReturn(connection);
121         connection.start();
122         expect(connection.createSession(anyBoolean(), anyInt())).andReturn(session);
123         expect(session.createTopic(anyObject(String.class))).andReturn((Topic) destination);
124         expect(session.createProducer(anyObject(Destination.class))).andReturn(messageProducer);
125
126         expect(session.createObjectMessage(anyObject(CorrelationResult.class)))
127                 .andReturn(objMessage);
128         messageProducer.send(objMessage);
129         session.commit();
130         connection.close();
131
132         PowerMock.replayAll();
133
134         Alarm parentAlarm = new Alarm();
135         Alarm childAlarm = new Alarm();
136         mqProducer.sendCorrelationMQTopicMsg("ruleId", 123L, parentAlarm, childAlarm);
137
138         PowerMock.verifyAll();
139
140     }
141
142     @Test
143     public void sendCorrelationMQTopicMsg_exception() throws Exception {
144         thrown.expect(JMSException.class);
145
146         expect(connectionFactory.createConnection()).andThrow(new JMSException(""));
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