PageRenderTime 21ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/opennms-alarms/api/src/test/java/org/opennms/netmgt/alarmd/api/support/AbstractNorthbounderTest.java

https://github.com/ajakubo1/opennms
Java | 182 lines | 97 code | 52 blank | 33 comment | 0 complexity | 9248949d63d8bce9b40262aa8c7c4f10 MD5 | raw file
  1/*******************************************************************************
  2 * This file is part of OpenNMS(R).
  3 *
  4 * Copyright (C) 2011-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.alarmd.api.support;
 30
 31import static org.junit.Assert.*;
 32
 33import java.util.List;
 34import java.util.concurrent.CountDownLatch;
 35import java.util.concurrent.TimeUnit;
 36
 37import org.junit.Test;
 38import org.opennms.netmgt.alarmd.api.NorthboundAlarm;
 39import org.opennms.netmgt.alarmd.api.NorthbounderException;
 40import org.opennms.netmgt.alarmd.api.support.AbstractNorthbounder;
 41import org.opennms.netmgt.model.OnmsAlarm;
 42
 43/**
 44 * Tests NBI Supporting abstract class
 45 * 
 46 * @author <a mailto:brozow@opennms.org>Matt Brozowski</a>
 47 * @author <a mailto:david@opennms.org>David Hustace</a>
 48 */
 49public class AbstractNorthbounderTest {
 50    
 51    public static class TestNorthbounder extends AbstractNorthbounder {
 52
 53        private List<NorthboundAlarm> m_alarms;
 54        private boolean m_accepting;
 55        
 56        private CountDownLatch m_forwardAlarmsCalled = new CountDownLatch(1);
 57        
 58        private CountDownLatch m_acceptsCalled = new CountDownLatch(1);
 59
 60        public TestNorthbounder() {
 61            super("TestNorthbounder");
 62        }
 63
 64        @Override
 65        protected boolean accepts(NorthboundAlarm alarm) {
 66            m_acceptsCalled.countDown();
 67            return m_accepting;
 68        }
 69
 70        @Override
 71        public void forwardAlarms(List<NorthboundAlarm> alarms)
 72                throws NorthbounderException {
 73            
 74            m_alarms = alarms;
 75            m_forwardAlarmsCalled.countDown();
 76            
 77        }
 78        
 79        public void waitForForwardToBeCalled(long waitTime) throws InterruptedException {
 80            m_forwardAlarmsCalled.await(waitTime, TimeUnit.MILLISECONDS);
 81        }
 82
 83        public void waitForAcceptsToBeCalled(long waitTime) throws InterruptedException {
 84            m_acceptsCalled.await(waitTime, TimeUnit.MILLISECONDS);
 85        }
 86
 87        
 88        public List<NorthboundAlarm> getAlarms() {
 89            return m_alarms;
 90        }
 91
 92        public boolean isAccepting() {
 93            return m_accepting;
 94        }
 95
 96        public void setAccepting(boolean accepting) {
 97            m_accepting = accepting;
 98        }
 99        
100    }
101
102    @Test
103    public void testAlarmForwarding() throws InterruptedException {
104        
105        TestNorthbounder tnb = new TestNorthbounder();
106        tnb.setAccepting(true);
107        
108        tnb.start();
109        
110        NorthboundAlarm a = createNorthboundAlarm(1);
111        
112        tnb.onAlarm(a);
113
114        tnb.waitForAcceptsToBeCalled(2000);
115        tnb.waitForForwardToBeCalled(2000);
116
117        assertNotNull(tnb.getAlarms());
118        assertTrue(tnb.getAlarms().contains(a));
119
120    }
121    
122    @Test
123    public void testAlarmNotAccepted() throws InterruptedException {
124        
125        TestNorthbounder tnb = new TestNorthbounder();
126        tnb.setAccepting(false);
127        
128        tnb.start();
129        
130        tnb.onAlarm(createNorthboundAlarm(1));
131
132        tnb.waitForAcceptsToBeCalled(2000);
133        
134        Thread.sleep(100);
135
136        assertNull(tnb.getAlarms());
137
138    }
139
140    @Test
141    public void testAlarmForwardingWithNagles() throws InterruptedException {
142        
143        TestNorthbounder tnb = new TestNorthbounder();
144        tnb.setAccepting(true);
145        
146        tnb.setNaglesDelay(500);
147        tnb.start();
148        
149        NorthboundAlarm a1 = createNorthboundAlarm(1);
150        NorthboundAlarm a2 = createNorthboundAlarm(2);
151        NorthboundAlarm a3 = createNorthboundAlarm(3);
152        
153        tnb.onAlarm(a1);
154        Thread.sleep(100);
155        tnb.onAlarm(a2);
156        Thread.sleep(100);
157        tnb.onAlarm(a3);
158
159        tnb.waitForAcceptsToBeCalled(2000);
160        tnb.waitForForwardToBeCalled(2000);
161
162        assertNotNull(tnb.getAlarms());
163        
164        assertEquals(3, tnb.getAlarms().size());
165        
166        assertTrue(tnb.getAlarms().contains(a1));
167        assertTrue(tnb.getAlarms().contains(a2));
168        assertTrue(tnb.getAlarms().contains(a3));
169
170    }
171    
172    private NorthboundAlarm createNorthboundAlarm(int alarmid) {
173        OnmsAlarm alarm = new OnmsAlarm();
174        alarm.setId(alarmid);
175        alarm.setUei("uei.opennms.org/test/httpNorthBounder");
176        
177        return new NorthboundAlarm(alarm);
178    }
179    
180
181
182}