/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. package org.opennms.netmgt.alarmd.api.support;
  29. import static org.junit.Assert.*;
  30. import java.util.List;
  31. import java.util.concurrent.CountDownLatch;
  32. import java.util.concurrent.TimeUnit;
  33. import org.junit.Test;
  34. import org.opennms.netmgt.alarmd.api.NorthboundAlarm;
  35. import org.opennms.netmgt.alarmd.api.NorthbounderException;
  36. import org.opennms.netmgt.alarmd.api.support.AbstractNorthbounder;
  37. import org.opennms.netmgt.model.OnmsAlarm;
  38. /**
  39. * Tests NBI Supporting abstract class
  40. *
  41. * @author <a mailto:brozow@opennms.org>Matt Brozowski</a>
  42. * @author <a mailto:david@opennms.org>David Hustace</a>
  43. */
  44. public class AbstractNorthbounderTest {
  45. public static class TestNorthbounder extends AbstractNorthbounder {
  46. private List<NorthboundAlarm> m_alarms;
  47. private boolean m_accepting;
  48. private CountDownLatch m_forwardAlarmsCalled = new CountDownLatch(1);
  49. private CountDownLatch m_acceptsCalled = new CountDownLatch(1);
  50. public TestNorthbounder() {
  51. super("TestNorthbounder");
  52. }
  53. @Override
  54. protected boolean accepts(NorthboundAlarm alarm) {
  55. m_acceptsCalled.countDown();
  56. return m_accepting;
  57. }
  58. @Override
  59. public void forwardAlarms(List<NorthboundAlarm> alarms)
  60. throws NorthbounderException {
  61. m_alarms = alarms;
  62. m_forwardAlarmsCalled.countDown();
  63. }
  64. public void waitForForwardToBeCalled(long waitTime) throws InterruptedException {
  65. m_forwardAlarmsCalled.await(waitTime, TimeUnit.MILLISECONDS);
  66. }
  67. public void waitForAcceptsToBeCalled(long waitTime) throws InterruptedException {
  68. m_acceptsCalled.await(waitTime, TimeUnit.MILLISECONDS);
  69. }
  70. public List<NorthboundAlarm> getAlarms() {
  71. return m_alarms;
  72. }
  73. public boolean isAccepting() {
  74. return m_accepting;
  75. }
  76. public void setAccepting(boolean accepting) {
  77. m_accepting = accepting;
  78. }
  79. }
  80. @Test
  81. public void testAlarmForwarding() throws InterruptedException {
  82. TestNorthbounder tnb = new TestNorthbounder();
  83. tnb.setAccepting(true);
  84. tnb.start();
  85. NorthboundAlarm a = createNorthboundAlarm(1);
  86. tnb.onAlarm(a);
  87. tnb.waitForAcceptsToBeCalled(2000);
  88. tnb.waitForForwardToBeCalled(2000);
  89. assertNotNull(tnb.getAlarms());
  90. assertTrue(tnb.getAlarms().contains(a));
  91. }
  92. @Test
  93. public void testAlarmNotAccepted() throws InterruptedException {
  94. TestNorthbounder tnb = new TestNorthbounder();
  95. tnb.setAccepting(false);
  96. tnb.start();
  97. tnb.onAlarm(createNorthboundAlarm(1));
  98. tnb.waitForAcceptsToBeCalled(2000);
  99. Thread.sleep(100);
  100. assertNull(tnb.getAlarms());
  101. }
  102. @Test
  103. public void testAlarmForwardingWithNagles() throws InterruptedException {
  104. TestNorthbounder tnb = new TestNorthbounder();
  105. tnb.setAccepting(true);
  106. tnb.setNaglesDelay(500);
  107. tnb.start();
  108. NorthboundAlarm a1 = createNorthboundAlarm(1);
  109. NorthboundAlarm a2 = createNorthboundAlarm(2);
  110. NorthboundAlarm a3 = createNorthboundAlarm(3);
  111. tnb.onAlarm(a1);
  112. Thread.sleep(100);
  113. tnb.onAlarm(a2);
  114. Thread.sleep(100);
  115. tnb.onAlarm(a3);
  116. tnb.waitForAcceptsToBeCalled(2000);
  117. tnb.waitForForwardToBeCalled(2000);
  118. assertNotNull(tnb.getAlarms());
  119. assertEquals(3, tnb.getAlarms().size());
  120. assertTrue(tnb.getAlarms().contains(a1));
  121. assertTrue(tnb.getAlarms().contains(a2));
  122. assertTrue(tnb.getAlarms().contains(a3));
  123. }
  124. private NorthboundAlarm createNorthboundAlarm(int alarmid) {
  125. OnmsAlarm alarm = new OnmsAlarm();
  126. alarm.setId(alarmid);
  127. alarm.setUei("uei.opennms.org/test/httpNorthBounder");
  128. return new NorthboundAlarm(alarm);
  129. }
  130. }