PageRenderTime 49ms CodeModel.GetById 18ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 1ms

/opennms-ackd/src/test/java/org/opennms/netmgt/ackd/AckdTest.java

https://github.com/ajakubo1/opennms
Java | 386 lines | 268 code | 70 blank | 48 comment | 1 complexity | 5ba26d1a09b064015df5dbd91a23ae95 MD5 | raw file
  1/*******************************************************************************
  2 * This file is part of OpenNMS(R).
  3 *
  4 * Copyright (C) 2009-2012 The OpenNMS Group, Inc.
  5 * OpenNMS(R) is Copyright (C) 1999-2012 The OpenNMS Group, Inc.
  6 *
  7 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
  8 *
  9 * OpenNMS(R) is free software: you can redistribute it and/or modify
 10 * it under the terms of the GNU General Public License as published
 11 * by the Free Software Foundation, either version 3 of the License,
 12 * or (at your option) any later version.
 13 *
 14 * OpenNMS(R) is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with OpenNMS(R).  If not, see:
 21 *      http://www.gnu.org/licenses/
 22 *
 23 * For more information contact:
 24 *     OpenNMS(R) Licensing <license@opennms.org>
 25 *     http://www.opennms.org/
 26 *     http://www.opennms.com/
 27 *******************************************************************************/
 28
 29package org.opennms.netmgt.ackd;
 30
 31import java.util.Date;
 32import java.util.HashSet;
 33import java.util.List;
 34import java.util.Properties;
 35import java.util.Set;
 36
 37import org.junit.Assert;
 38
 39import org.junit.Before;
 40import org.junit.Test;
 41import org.junit.runner.RunWith;
 42import org.opennms.core.spring.BeanUtils;
 43import org.opennms.core.test.MockLogAppender;
 44import org.opennms.core.test.OpenNMSJUnit4ClassRunner;
 45import org.opennms.core.test.db.annotations.JUnitTemporaryDatabase;
 46import org.opennms.netmgt.EventConstants;
 47import org.opennms.netmgt.ackd.AckReader.AckReaderState;
 48import org.opennms.netmgt.config.ackd.Reader;
 49import org.opennms.netmgt.dao.DatabasePopulator;
 50import org.opennms.netmgt.dao.api.AcknowledgmentDao;
 51import org.opennms.netmgt.dao.api.AlarmDao;
 52import org.opennms.netmgt.dao.api.EventDao;
 53import org.opennms.netmgt.dao.api.NodeDao;
 54import org.opennms.netmgt.dao.api.NotificationDao;
 55import org.opennms.netmgt.dao.api.UserNotificationDao;
 56import org.opennms.netmgt.model.AckType;
 57import org.opennms.netmgt.model.OnmsAcknowledgment;
 58import org.opennms.netmgt.model.OnmsAlarm;
 59import org.opennms.netmgt.model.OnmsEvent;
 60import org.opennms.netmgt.model.OnmsNode;
 61import org.opennms.netmgt.model.OnmsNotification;
 62import org.opennms.netmgt.model.OnmsSeverity;
 63import org.opennms.netmgt.model.OnmsUserNotification;
 64import org.opennms.netmgt.model.events.EventBuilder;
 65import org.opennms.test.JUnitConfigurationEnvironment;
 66import org.springframework.beans.factory.InitializingBean;
 67import org.springframework.beans.factory.annotation.Autowired;
 68import org.springframework.test.context.ContextConfiguration;
 69import org.springframework.test.context.transaction.BeforeTransaction;
 70import org.springframework.transaction.annotation.Transactional;
 71
 72/**
 73 * Acknowledgment Daemon tests
 74 * 
 75 * @author <a href="mailto:david@opennms.org">David Hustace</a>
 76 */
 77@RunWith(OpenNMSJUnit4ClassRunner.class)
 78@ContextConfiguration(locations={
 79        "classpath:/META-INF/opennms/applicationContext-soa.xml",
 80        "classpath:/META-INF/opennms/applicationContext-commonConfigs.xml",
 81        "classpath:/META-INF/opennms/applicationContext-dao.xml",
 82        "classpath*:/META-INF/opennms/component-dao.xml",
 83        "classpath:/META-INF/opennms/applicationContext-daemon.xml",
 84        "classpath*:/META-INF/opennms/component-service.xml",
 85        "classpath:/META-INF/opennms/mockEventIpcManager.xml",
 86        "classpath:/META-INF/opennms/applicationContext-setupIpLike-enabled.xml",
 87        "classpath:/META-INF/opennms/applicationContext-ackd.xml",
 88        "classpath:/META-INF/opennms/applicationContext-databasePopulator.xml",
 89        "classpath:/META-INF/opennms/applicationContext-minimal-conf.xml"
 90})
 91@JUnitConfigurationEnvironment
 92@JUnitTemporaryDatabase(dirtiesContext=false)
 93@Transactional
 94public class AckdTest implements InitializingBean {
 95
 96    @Autowired
 97    private AlarmDao m_alarmDao;
 98    
 99    @Autowired
100    private EventDao m_eventDao;
101    
102    @Autowired
103    private Ackd m_daemon;
104
105    @Autowired
106    private AcknowledgmentDao m_ackDao;
107
108    @Autowired
109    private NodeDao m_nodeDao;
110    
111    @Autowired
112    private DatabasePopulator m_populator;
113
114    @Autowired
115    private NotificationDao m_notificationDao;
116    
117    @Autowired
118    private UserNotificationDao m_userNotificationDao;
119    
120    private static boolean m_populated = false;
121    
122    @BeforeTransaction
123    public void populateDatabase() {
124        try {
125            if (!m_populated) {
126                m_populator.populateDatabase();
127            }
128        } catch (Throwable e) {
129            e.printStackTrace(System.err);
130        } finally {
131            m_populated = true;
132        }
133    }
134
135    @Before
136    public void setUp() throws Exception {
137        Properties props = new Properties();
138        props.setProperty("log4j.logger.org.hibernate", "INFO");
139        props.setProperty("log4j.logger.org.springframework", "INFO");
140        props.setProperty("log4j.logger.org.hibernate.SQL", "DEBUG");
141        MockLogAppender.setupLogging(props);
142    }
143    
144    @Override
145    public void afterPropertiesSet() throws Exception {
146        BeanUtils.assertAutowiring(this);
147        Assert.assertSame("dao from populator should refer to same dao from local properties", m_populator.getAcknowledgmentDao(), m_ackDao);
148    }
149    
150    @Test
151    public void testRestartReaders() throws Exception {
152        AckReader reader = m_daemon.getAckReaders().get(0);
153        Reader readerConfig = m_daemon.getConfigDao().getReader("JavaMailReader");
154        readerConfig.setEnabled(true);
155        Assert.assertTrue("Unexpected reader state: "+reader.getState(), AckReaderState.STOPPED.equals(reader.getState()));
156        
157        m_daemon.restartReaders(false);
158        Thread.sleep(30);
159        Assert.assertTrue("Unexpected reader state: "+reader.getState(), AckReaderState.STARTED.equals(reader.getState()));
160        
161        m_daemon.pauseReaders();
162        Thread.sleep(30);
163        Assert.assertTrue("Unexpected reader state: "+reader.getState(), AckReaderState.PAUSED.equals(reader.getState()));
164        
165        m_daemon.resumeReaders();
166        Thread.sleep(30);
167        Assert.assertTrue("Unexpected reader state: "+reader.getState(), AckReaderState.RESUMED.equals(reader.getState()));
168        
169        readerConfig.setEnabled(false);
170        m_daemon.restartReaders(true);
171        Thread.sleep(300);
172        Assert.assertTrue("Unexpected reader state: "+reader.getState(), AckReaderState.STOPPED.equals(reader.getState()));
173        
174        m_daemon.resumeReaders();
175        Thread.sleep(30);
176        Assert.assertTrue("Unexpected reader state: "+reader.getState(), AckReaderState.STOPPED.equals(reader.getState()));
177        
178        readerConfig.setEnabled(true);
179        m_daemon.startReaders();
180        Thread.sleep(300);
181        Assert.assertTrue("Unexpected reader state: "+reader.getState(), AckReaderState.STARTED.equals(reader.getState()));
182                
183        m_daemon.destroy();
184    }
185    
186
187    /**
188     * Make sure the DB is not empty
189     */
190    @Test
191    public void testDbState() {
192        Assert.assertFalse(m_nodeDao.findAll().isEmpty());
193    }
194
195    
196    /**
197     * This tests the acknowledgment of an alarm and any related notifications.
198     */
199    @Test
200    public void testAcknowledgeAlarm() {
201        
202        VerificationObject vo = createAckStructure();
203        Assert.assertTrue(vo.m_nodeId > 0);
204        Assert.assertTrue(vo.m_alarmId > 0);
205        Assert.assertTrue(vo.m_eventID > 0);
206        Assert.assertTrue(vo.m_userNotifId > 0);
207        
208        OnmsAlarm alarm = m_alarmDao.get(vo.m_alarmId);
209
210        OnmsAcknowledgment ack = new OnmsAcknowledgment(m_alarmDao.get(vo.m_alarmId));
211
212        m_ackDao.save(ack);
213        m_ackDao.flush();
214        
215        m_ackDao.processAck(ack);
216        
217        alarm = m_alarmDao.get(ack.getRefId());
218        Assert.assertNotNull(alarm.getAlarmAckUser());
219        Assert.assertEquals("admin", alarm.getAlarmAckUser());
220        
221        OnmsNotification notif = m_notificationDao.get(vo.m_notifId);
222        Assert.assertNotNull(notif);
223        Assert.assertEquals("admin", notif.getAnsweredBy());
224        
225        Assert.assertTrue(alarm.getAlarmAckTime().before(notif.getRespondTime()));
226        
227    }
228    
229    
230    /**
231     * This tests acknowledging a notification and a related alarm.  If events are being deduplicated
232     * they should all have the same alarm ID.
233     * @throws InterruptedException 
234     */
235    @Test
236    public void testAcknowledgeNotification() throws InterruptedException {
237        
238        VerificationObject vo = createAckStructure();
239        Assert.assertTrue(vo.m_nodeId > 0);
240        Assert.assertTrue(vo.m_alarmId > 0);
241        Assert.assertTrue(vo.m_eventID > 0);
242        Assert.assertTrue(vo.m_userNotifId > 0);
243        
244        OnmsAcknowledgment ack = new OnmsAcknowledgment(m_notificationDao.get(vo.m_notifId));
245
246        m_ackDao.save(ack);
247        m_ackDao.flush();
248        
249        Thread.sleep(1);
250        m_ackDao.processAck(ack);
251        
252        OnmsNotification notif = m_notificationDao.get(ack.getRefId());
253        Assert.assertNotNull(notif.getAnsweredBy());
254        Assert.assertEquals("admin", notif.getAnsweredBy());
255        
256        OnmsAlarm alarm = m_alarmDao.get(vo.m_alarmId);
257        Assert.assertNotNull(alarm);
258        Assert.assertEquals("admin", alarm.getAlarmAckUser());
259        
260        long ackTime = ack.getAckTime().getTime();
261        long respondTime = notif.getRespondTime().getTime();
262        
263        //the DAOs now set the acknowledgment time for each Acknowledgable and should
264        //be later (by a few millis in this test) than the time the acknowledgment was created
265        //this will give us an idea about the processing time of an acknowledgment
266        Assert.assertTrue(ackTime < respondTime);
267        
268    }
269    
270    @Test
271    public void testHandelEvent() throws InterruptedException {
272        
273        VerificationObject vo = createAckStructure();
274        EventBuilder bldr = new EventBuilder(EventConstants.ACKNOWLEDGE_EVENT_UEI, "AckdTest");
275        bldr.addParam("ackType", String.valueOf(AckType.ALARM));
276        bldr.addParam("refId", vo.m_alarmId);
277        final String user = "ackd-test-user";
278        bldr.addParam("user", user);
279
280        m_daemon.handleAckEvent(bldr.getEvent());
281        
282        OnmsNotification notif = m_notificationDao.get(vo.m_notifId);
283        Assert.assertEquals(notif.getAckUser(), user);
284//        Assert.assertEquals(notif.getAckTime(), bldr.getEvent().getTime());
285        
286        OnmsAlarm alarm = m_alarmDao.get(vo.m_alarmId);
287        Assert.assertEquals(alarm.getAckUser(), user);
288//        Assert.assertEquals(alarm.getAckTime(), bldr.getEvent().getTime());
289    }
290    
291    
292    class VerificationObject {
293        int m_eventID;
294        int m_alarmId;
295        int m_nodeId;
296        int m_notifId;
297        int m_userNotifId;
298    }
299    
300    private VerificationObject createAckStructure() {
301        
302        final Date time = new Date();
303        VerificationObject vo = new VerificationObject();
304        
305        List<OnmsNode> nodes = m_nodeDao.findAll();
306        Assert.assertTrue("List of nodes should not be empty", nodes.size() > 0);
307        OnmsNode node = m_nodeDao.get(nodes.get(0).getId());
308        vo.m_nodeId = node.getId();
309                
310        OnmsEvent event = new OnmsEvent();
311        event.setDistPoller(node.getDistPoller());
312        event.setNode(node);
313        
314        event.setEventCreateTime(time);
315        event.setEventDescr("Test node down event.");
316        event.setEventSeverity(6);
317        event.setEventSource("AckdTest");
318        event.setEventTime(time);
319        event.setEventUei(EventConstants.NODE_DOWN_EVENT_UEI);
320        event.setIpAddr(node.getPrimaryInterface().getIpAddress());
321        event.setEventLog("Y");
322        event.setEventDisplay("Y");
323        event.setEventLogMsg("Testing node down event from AckdTest.");
324        m_eventDao.save(event);
325        m_eventDao.flush();
326        vo.m_eventID = event.getId();
327        
328        OnmsAlarm alarm = new OnmsAlarm();
329        alarm.setAlarmType(1);
330        alarm.setClearKey(EventConstants.NODE_UP_EVENT_UEI + ":localhost:1");
331        alarm.setCounter(1);
332        alarm.setDescription(event.getEventDescr());
333        alarm.setDistPoller(event.getDistPoller());
334        alarm.setFirstEventTime(event.getEventTime());
335        alarm.setIpAddr(event.getIpAddr());
336        alarm.setLastEvent(event);
337        alarm.setLastEventTime(event.getEventTime());
338        alarm.setLogMsg("Some Log Message");
339        alarm.setNode(event.getNode());
340        alarm.setReductionKey("xyz");
341        alarm.setServiceType(event.getServiceType());
342        alarm.setSeverity(OnmsSeverity.get(event.getEventSeverity()));
343        alarm.setUei(event.getEventUei());
344        m_alarmDao.save(alarm);
345        m_alarmDao.flush();
346        vo.m_alarmId = alarm.getId();
347        event.setAlarm(alarm);
348        
349        OnmsNotification notif = new OnmsNotification();
350        notif.setEvent(event);
351        notif.setEventUei(event.getEventUei());
352        notif.setIpAddress(event.getIpAddr());
353        notif.setNode(event.getNode());
354        notif.setNotifConfigName("abc");
355        notif.setNumericMsg(event.getEventLogMsg());
356        notif.setPageTime(event.getEventTime());
357        notif.setServiceType(event.getServiceType());
358        notif.setSubject("notifyid: 1, node down");
359        notif.setTextMsg(event.getEventLogMsg());
360        m_notificationDao.save(notif);
361        vo.m_notifId = notif.getNotifyId();
362        
363        OnmsUserNotification userNotif = new OnmsUserNotification();
364        userNotif.setAutoNotify("Y");
365        userNotif.setContactInfo("me@yourock.com");
366        userNotif.setMedia("page");
367        userNotif.setNotification(notif);
368        userNotif.setNotifyTime(event.getEventTime());
369        userNotif.setUserId("me");
370        
371        Set<OnmsUserNotification> usersnotifieds = new HashSet<OnmsUserNotification>();
372        usersnotifieds.add(userNotif);
373        m_userNotificationDao.save(userNotif);
374        vo.m_userNotifId = userNotif.getId();
375        
376        notif.setUsersNotified(usersnotifieds);
377        m_notificationDao.update(notif);
378        
379        m_eventDao.update(event);
380        m_eventDao.flush();
381        
382        return vo;
383    }
384    
385    
386}