/** * @param i * @return */ public synchronized Collection<MockNotification> waitForAnticipated(long millis) { long waitTime = millis; long start = System.currentTimeMillis(); long now = start; while (waitTime > 0) { if (m_anticipated.isEmpty()) return new ArrayList<MockNotification>(0); try { wait(waitTime); } catch (InterruptedException e) { } now = System.currentTimeMillis(); waitTime -= (now - start); } return getAnticipatedNotifications(); }
- System.currentTimeMillis()); Collection<MockNotification> missingNotifications = waitForAnticipated(totalWaitTime); problems.append(missingNotifications.size() + " expected notifications still outstanding:\n"); problems.append(listNotifications("\t", missingNotifications)); if (getUnanticipated().size() != 0) { problems.append(getUnanticipated().size() + " unanticipated notifications received:\n"); problems.append(listNotifications("\t", getUnanticipated())); problems.append(listNotifications("\t", m_earlyArrival)); problems.append(listNotifications("\t", m_lateBloomers));
protected void verifyAnticipated(long lastNotifyTime, long waitTime, long sleepTime) { m_anticipator.verifyAnticipated(lastNotifyTime, waitTime, sleepTime); }
@Before @Override public void setUp() throws Exception { super.setUp(); m_anticipator.setExpectedDifference(3000); }
@Test public void testRoleNotification() throws Exception { MockNode node = m_network.getNode(1); Date downDate = new Date(); long finishedDowns = anticipateNotificationsForRole("notification test", "Notification Test", "oncall", downDate, 0); m_eventMgr.sendEventToListeners(MockEventUtil.createNodeEvent("Test", "uei.opennms.org/test/roleTestEvent", node)); verifyAnticipated(finishedDowns, 1000); m_anticipator.reset(); }
@Override public int send(List<Argument> arguments) { MockUtil.println("Message sent with arguments:"+arguments); MockNotification notification = new MockNotification(); Iterator<Argument> it = arguments.iterator(); while (it.hasNext()) { Argument arg = it.next(); if (arg.getSwitch().equals(NotificationManager.PARAM_SUBJECT)) { notification.setSubject(arg.getValue()); } else if (arg.getSwitch().equals(NotificationManager.PARAM_EMAIL)) { notification.setEmail(arg.getValue()); } else if (arg.getSwitch().equals(NotificationManager.PARAM_TEXT_MSG)) { notification.setTextMsg(arg.getValue()); } } notification.setExpectedTime(System.currentTimeMillis()); NotificationAnticipator anticipator = getAnticipator(); if (anticipator != null) { anticipator.notificationReceived(notification); } else { throw new NullPointerException("anticipator is null"); } return 0; }
protected long anticipateNotificationsForUsers(String[] users, String subject, String textMsg, long startTime, long interval) throws IOException { long expectedTime = startTime; for (int i = 0; i < users.length; i++) { User user = m_userManager.getUser(users[i]); Contact[] contacts = user.getContacts().toArray(new Contact[0]); for (int j = 0; j < contacts.length; j++) { Contact contact = contacts[j]; if ("email".equals(contact.getType())) { m_anticipator.anticipateNotification(createMockNotification(expectedTime, subject, textMsg, contact.getInfo().orElse(null))); } } expectedTime += interval; } return expectedTime-interval; }
m_pollOutagesConfigManager = new MockPollerConfig(m_network); m_anticipator = new NotificationAnticipator(); MockNotificationStrategy.setAnticipator(m_anticipator);
@Before @Override public void setUp() throws Exception { super.setUp(); // XXX Bogus.. need to rework these tests m_anticipator.setExpectedDifference(5000); }
@Test public void testMockNotificationBasic() throws Exception { MockNode node = m_network.getNode(1); Date downDate = new Date(); long finishedDowns = anticipateNotificationsForGroup("node 1 down.", "All services are down on node 1.", "InitialGroup", downDate, 0); //bring node down now m_eventMgr.sendEventToListeners(node.createDownEvent(downDate)); verifyAnticipated(finishedDowns, 3000); m_anticipator.reset(); Date upDate = new Date(); anticipateNotificationsForGroup("RESOLVED: node 1 down.", "RESOLVED: All services are down on node 1.", "InitialGroup", upDate, 0); long finishedUps = anticipateNotificationsForGroup("node 1 up.", "The node which was previously down is now up.", "UpGroup", upDate, 0); //bring node back up now m_eventMgr.sendEventToListeners(node.createUpEvent(upDate)); verifyAnticipated(finishedUps, 3000); }
@Test public void testBug1114() throws Exception { // XXX Needing to bump up this number is bogus m_anticipator.setExpectedDifference(5000); MockService svc = m_network.getService(1, "192.168.1.1", "ICMP"); long interval = computeInterval(); Event event = MockEventUtil.createServiceEvent("Test", "uei.opennms.org/tests/nodeTimeTest", svc, null); Date date = event.getTime(); String dateString = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(date); long endTime = anticipateNotificationsForGroup("time " + dateString + ".", "Timestamp: " + dateString + ".", "InitialGroup", date, interval); m_eventMgr.sendEventToListeners(event); // XXX Needing to decrease the end time is bogus verifyAnticipated(endTime - 5000, 1000); }