Search in sources :

Example 66 with JUnitTemporaryDatabase

use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.

the class LatencyStoringServiceMonitorAdaptorIT method testThresholdsWithScheduledOutage.

// TODO: This test will fail if you have a default locale with >3 characters for month, e.g. Locale.FRENCH
@Test
@JUnitTemporaryDatabase(tempDbClass = MockDatabase.class)
public void testThresholdsWithScheduledOutage() throws Exception {
    DateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss");
    final StringBuilder sb = new StringBuilder("<?xml version=\"1.0\"?>");
    sb.append("<outages>");
    sb.append("<outage name=\"junit outage\" type=\"specific\">");
    sb.append("<time begins=\"");
    sb.append(formatter.format(new Date(System.currentTimeMillis() - 3600000)));
    sb.append("\" ends=\"");
    sb.append(formatter.format(new Date(System.currentTimeMillis() + 3600000)));
    sb.append("\"/>");
    sb.append("<interface address=\"match-any\"/>");
    sb.append("</outage>");
    sb.append("</outages>");
    File file = new File("target/poll-outages.xml");
    FileWriter writer = new FileWriter(file);
    writer.write(sb.toString());
    writer.close();
    PollOutagesConfigFactory oldFactory = PollOutagesConfigFactory.getInstance();
    PollOutagesConfigFactory.setInstance(new PollOutagesConfigFactory(new FileSystemResource(file)));
    PollOutagesConfigFactory.getInstance().afterPropertiesSet();
    executeThresholdTest(new Double[] { 100.0 });
    m_eventIpcManager.getEventAnticipator().verifyAnticipated();
    // Reset the state of the PollOutagesConfigFactory for any subsequent tests
    PollOutagesConfigFactory.setInstance(oldFactory);
    file.delete();
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) FileWriter(java.io.FileWriter) PollOutagesConfigFactory(org.opennms.netmgt.config.PollOutagesConfigFactory) FileSystemResource(org.springframework.core.io.FileSystemResource) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Date(java.util.Date) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 67 with JUnitTemporaryDatabase

use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.

the class AssetSuggestionsRestServiceIT method testSuggestionsJson.

/**
 * Test suggestions JSON.
 *
 * @throws Exception the exception
 */
@Test
@JUnitTemporaryDatabase
public void testSuggestionsJson() throws Exception {
    MockHttpServletRequest jsonRequest = createRequest(m_servletContext, GET, "/assets/suggestions", "admin", Arrays.asList(new String[] { Authentication.ROLE_ADMIN }));
    jsonRequest.addHeader("Accept", MediaType.APPLICATION_JSON);
    String json = sendRequest(jsonRequest, 200);
    JSONArray cities = new JSONObject(json).getJSONObject("city").getJSONArray("suggestion");
    Assert.assertEquals(5, cities.length());
    Assert.assertEquals("Boston", cities.get(0));
}
Also used : JSONObject(org.json.JSONObject) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) JSONArray(org.json.JSONArray) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 68 with JUnitTemporaryDatabase

use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.

the class CategoryRestServiceIT method testCategories.

@Test
@JUnitTemporaryDatabase
public void testCategories() throws Exception {
    // get initial categories
    String xml = sendRequest("GET", "/categories", 200);
    assertNotNull(xml);
    OnmsCategoryCollection categories = JaxbUtils.unmarshal(OnmsCategoryCollection.class, xml);
    int initialSize = categories.size();
    assertNotNull(categories);
    assertEquals(initialSize, categories.size());
    // add category
    createCategory("testCategory");
    xml = sendRequest("GET", "/categories", 200);
    categories = JaxbUtils.unmarshal(OnmsCategoryCollection.class, xml);
    assertEquals(initialSize + 1, categories.size());
    assertTrue(xml.contains("name=\"testCategory\""));
    // add again (should fail)
    sendData("POST", MediaType.APPLICATION_XML, "/categories", JaxbUtils.marshal(new OnmsCategory("testCategory")), 400);
    xml = sendRequest("GET", "/categories", 200);
    categories = JaxbUtils.unmarshal(OnmsCategoryCollection.class, xml);
    assertEquals(initialSize + 1, categories.size());
    assertTrue(xml.contains("name=\"testCategory\""));
    // delete
    sendRequest("DELETE", "/categories/testCategory", 204);
    xml = sendRequest("GET", "/categories", 200);
    categories = JaxbUtils.unmarshal(OnmsCategoryCollection.class, xml);
    assertEquals(initialSize, categories.size());
    assertFalse(xml.contains("name=\"testCategory\""));
}
Also used : OnmsCategory(org.opennms.netmgt.model.OnmsCategory) OnmsCategoryCollection(org.opennms.netmgt.model.OnmsCategoryCollection) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 69 with JUnitTemporaryDatabase

use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.

the class CategoryRestServiceIT method testAddCategory.

@Test
@JUnitTemporaryDatabase
public void testAddCategory() throws Exception {
    // add with description
    OnmsCategory createMe = new OnmsCategory();
    createMe.setDescription("This is a description");
    createMe.setName("myName");
    createCategory(createMe);
    // verify
    String xml = sendRequest("GET", "/categories/myName", 200);
    OnmsCategory category = JaxbUtils.unmarshal(OnmsCategory.class, xml);
    assertNotNull(category.getId());
    createMe.setId(category.getId());
    assertTrue(category.getId().equals(createMe.getId()));
    assertEquals(createMe, category);
}
Also used : OnmsCategory(org.opennms.netmgt.model.OnmsCategory) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 70 with JUnitTemporaryDatabase

use of org.opennms.core.test.db.annotations.JUnitTemporaryDatabase in project opennms by OpenNMS.

the class AlarmRepositoryFilterIT method testParmsNotLikeFilter.

@Test
@Transactional
@JUnitTemporaryDatabase
public void testParmsNotLikeFilter() {
    OnmsDistPoller poller = m_dbPopulator.getDistPollerDao().whoami();
    assertNotNull(poller);
    final OnmsEvent event1 = new OnmsEvent();
    event1.setDistPoller(poller);
    event1.setEventCreateTime(new Date(1436881548292L));
    event1.setEventTime(new Date(1436881548292L));
    event1.setEventCreateTime(new Date(1437061537126L));
    event1.setEventDescr("This is the description of a test event.");
    event1.setEventDisplay("Y");
    event1.setEventHost("127.0.0.1");
    event1.setEventLog("Y");
    event1.setEventLogMsg("Test Event Log Message");
    event1.setEventSeverity(1);
    event1.setEventSource("test");
    event1.setEventTime(new Date(1437061537105L));
    event1.setEventUei("uei.opennms.org/test");
    event1.setIpAddr(InetAddressUtils.getInetAddress("192.168.1.1"));
    event1.setNode(m_dbPopulator.getNode1());
    event1.setServiceType(m_dbPopulator.getServiceTypeDao().findByName("ICMP"));
    event1.setEventParameters(Lists.newArrayList(new OnmsEventParameter(event1, "url", "http://localhost:8980/opennms/rtc/post/Network+Interfaces", "string"), new OnmsEventParameter(event1, "user", "rtc", "string"), new OnmsEventParameter(event1, "passwd", "rtc", "string"), new OnmsEventParameter(event1, "catlabel", "Network Interfaces", "string")));
    m_dbPopulator.getEventDao().saveOrUpdate(event1);
    m_dbPopulator.getEventDao().flush();
    final OnmsEvent event2 = new OnmsEvent();
    event2.setDistPoller(poller);
    event2.setEventCreateTime(new Date(1436881548292L));
    event2.setEventTime(new Date(1436881548292L));
    event2.setEventCreateTime(new Date(1437061537126L));
    event2.setEventDescr("This is the description of a test event.");
    event2.setEventDisplay("Y");
    event2.setEventHost("127.0.0.1");
    event2.setEventLog("Y");
    event2.setEventLogMsg("Test Event Log Message");
    event2.setEventSeverity(1);
    event2.setEventSource("test");
    event2.setEventTime(new Date(1437061537105L));
    event2.setEventUei("uei.opennms.org/test");
    event2.setIpAddr(InetAddressUtils.getInetAddress("192.168.1.1"));
    event2.setNode(m_dbPopulator.getNode1());
    event2.setServiceType(m_dbPopulator.getServiceTypeDao().findByName("ICMP"));
    event2.setEventParameters(Lists.newArrayList(new OnmsEventParameter(event2, "componentType", "serviceElement", "string"), new OnmsEventParameter(event2, "url", "http://localhost:8980/opennms/rtc/post/Network+Interfaces", "string"), new OnmsEventParameter(event2, "user", "rtcbomb", "string"), new OnmsEventParameter(event2, "passwd", "rtc", "string"), new OnmsEventParameter(event2, "catlabel", "Network Interfaces", "string")));
    m_dbPopulator.getEventDao().saveOrUpdate(event2);
    m_dbPopulator.getEventDao().flush();
    OnmsAlarm alarm = new OnmsAlarm();
    alarm.setUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnIfUp");
    alarm.setLastEvent(event1);
    alarm.setSeverityId(3);
    alarm.setDistPoller(poller);
    alarm.setCounter(100);
    alarm.setLastEvent(event1);
    AlarmDao alarmDao = m_dbPopulator.getAlarmDao();
    alarmDao.save(alarm);
    alarmDao.flush();
    OnmsAlarm alarm2 = new OnmsAlarm();
    alarm2.setUei("uei.opennms.org/vendor/Juniper/traps/jnxVpnIfUp");
    alarm2.setLastEvent(event2);
    alarm2.setSeverityId(3);
    alarm2.setDistPoller(poller);
    alarm2.setCounter(100);
    alarm2.setLastEvent(event2);
    alarmDao.save(alarm2);
    alarmDao.flush();
    NegativeEventParmLikeFilter parmFilter = new NegativeEventParmLikeFilter("user=rtc");
    assertEquals("user is not \"rtc\"", parmFilter.getTextDescription());
    AlarmCriteria criteria = new AlarmCriteria(parmFilter);
    OnmsAlarm[] alarms = m_daoAlarmRepo.getMatchingAlarms(AlarmUtil.getOnmsCriteria(criteria));
    // alarm2 and the alarm from DatabasePopulator match this criteria
    assertEquals(2, alarms.length);
}
Also used : OnmsEvent(org.opennms.netmgt.model.OnmsEvent) OnmsAlarm(org.opennms.netmgt.model.OnmsAlarm) OnmsEventParameter(org.opennms.netmgt.model.OnmsEventParameter) OnmsDistPoller(org.opennms.netmgt.model.OnmsDistPoller) AlarmDao(org.opennms.netmgt.dao.api.AlarmDao) Date(java.util.Date) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

JUnitTemporaryDatabase (org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)109 Test (org.junit.Test)105 OnmsNode (org.opennms.netmgt.model.OnmsNode)21 Date (java.util.Date)20 JSONObject (org.json.JSONObject)18 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)18 OnmsAlarm (org.opennms.netmgt.model.OnmsAlarm)17 Transactional (org.springframework.transaction.annotation.Transactional)14 FileInputStream (java.io.FileInputStream)10 AlarmCriteria (org.opennms.web.alarm.filter.AlarmCriteria)10 OnmsEvent (org.opennms.netmgt.model.OnmsEvent)9 OnmsOutage (org.opennms.netmgt.model.OnmsOutage)8 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)7 AlarmIdFilter (org.opennms.web.alarm.filter.AlarmIdFilter)7 HashMap (java.util.HashMap)6 OutageCriteria (org.opennms.web.outage.filter.OutageCriteria)6 Matcher (java.util.regex.Matcher)4 Pattern (java.util.regex.Pattern)4 JSONArray (org.json.JSONArray)4 AcknowledgedByFilter (org.opennms.web.alarm.filter.AcknowledgedByFilter)4