Search in sources :

Example 91 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class PropertiesGraphDaoIT method testBasicPrefabConfigDirectorySingleReports.

/**
 * Test that individual graph files in an include directory are loaded as expected
 */
@Test
public void testBasicPrefabConfigDirectorySingleReports() throws IOException {
    File rootFile = m_fileAnticipator.tempFile("snmp-graph.properties");
    File graphDirectory = m_fileAnticipator.tempDir("snmp-graph.properties.d");
    File graphBits = m_fileAnticipator.tempFile(graphDirectory, "mib2.bits.properties");
    File graphHCbits = m_fileAnticipator.tempFile(graphDirectory, "mib2.HCbits.properties");
    m_outputStream = new FileOutputStream(rootFile);
    m_writer = new OutputStreamWriter(m_outputStream, StandardCharsets.UTF_8);
    m_writer.write(s_baseIncludePrefab);
    m_writer.close();
    m_outputStream.close();
    graphDirectory.mkdir();
    m_outputStream = new FileOutputStream(graphBits);
    m_writer = new OutputStreamWriter(m_outputStream, StandardCharsets.UTF_8);
    m_writer.write(s_separateBitsGraph);
    m_writer.close();
    m_outputStream.close();
    m_outputStream = new FileOutputStream(graphHCbits);
    m_writer = new OutputStreamWriter(m_outputStream, StandardCharsets.UTF_8);
    m_writer.write(s_separateHCBitsGraph);
    m_writer.close();
    m_outputStream.close();
    HashMap<String, Resource> prefabConfigs = new HashMap<String, Resource>();
    prefabConfigs.put("performance", new FileSystemResource(rootFile));
    PropertiesGraphDao dao = createPropertiesGraphDao(prefabConfigs, s_emptyMap);
    PrefabGraph mib2Bits = dao.getPrefabGraph("mib2.bits");
    assertNotNull(mib2Bits);
    assertEquals("mib2.bits", mib2Bits.getName());
    assertEquals("Bits In/Out", mib2Bits.getTitle());
    String[] columns1 = { "ifInOctets", "ifOutOctets" };
    Assert.assertArrayEquals(columns1, mib2Bits.getColumns());
    PrefabGraph mib2HCBits = dao.getPrefabGraph("mib2.HCbits");
    assertNotNull(mib2HCBits);
    assertEquals("mib2.HCbits", mib2HCBits.getName());
    assertEquals("Bits In/Out", mib2HCBits.getTitle());
    String[] columns2 = { "ifHCInOctets", "ifHCOutOctets" };
    Assert.assertArrayEquals(columns2, mib2HCBits.getColumns());
}
Also used : PrefabGraph(org.opennms.netmgt.model.PrefabGraph) HashMap(java.util.HashMap) FileOutputStream(java.io.FileOutputStream) FileSystemResource(org.springframework.core.io.FileSystemResource) OnmsResource(org.opennms.netmgt.model.OnmsResource) Resource(org.springframework.core.io.Resource) OutputStreamWriter(java.io.OutputStreamWriter) FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) Test(org.junit.Test)

Example 92 with FileSystemResource

use of org.springframework.core.io.FileSystemResource 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 93 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class LatencyThresholdingSetIT method setUp.

@Before
public void setUp() throws Exception {
    BeanUtils.setStaticApplicationContext(m_context);
    // Resets Counters Cache Data
    CollectionResourceWrapper.s_cache.clear();
    MockLogAppender.setupLogging();
    m_fileAnticipator = new FileAnticipator();
    m_resourceStorageDao = new FilesystemResourceStorageDao();
    m_resourceStorageDao.setRrdDirectory(m_fileAnticipator.getTempDir());
    // Use a mock FilterDao that always returns 127.0.0.1 in the active IP list
    FilterDao filterDao = EasyMock.createMock(FilterDao.class);
    EasyMock.expect(filterDao.getActiveIPAddressList((String) EasyMock.anyObject())).andReturn(Collections.singletonList(addr("127.0.0.1"))).anyTimes();
    filterDao.flushActiveIpAddressListCache();
    EasyMock.expectLastCall().anyTimes();
    FilterDaoFactory.setInstance(filterDao);
    EasyMock.replay(filterDao);
    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.setInstance(new PollOutagesConfigFactory(new FileSystemResource(file)));
    PollOutagesConfigFactory.getInstance().afterPropertiesSet();
    initFactories("/threshd-configuration.xml", "/test-thresholds.xml");
    m_anticipatedEvents = new ArrayList<>();
}
Also used : FileWriter(java.io.FileWriter) PollOutagesConfigFactory(org.opennms.netmgt.config.PollOutagesConfigFactory) FilesystemResourceStorageDao(org.opennms.netmgt.dao.support.FilesystemResourceStorageDao) FileSystemResource(org.springframework.core.io.FileSystemResource) Date(java.util.Date) FilterDao(org.opennms.netmgt.filter.api.FilterDao) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) FileAnticipator(org.opennms.test.FileAnticipator) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Before(org.junit.Before)

Example 94 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class ThresholdingVisitorIT method setUp.

@Before
public void setUp() throws Exception {
    // Resets Counters Cache Data
    CollectionResourceWrapper.s_cache.clear();
    MockLogAppender.setupLogging();
    m_fileAnticipator = new FileAnticipator();
    m_hrStorageProperties = new HashMap<Integer, File>();
    m_resourceStorageDao = new FilesystemResourceStorageDao();
    m_resourceStorageDao.setRrdDirectory(new File(m_fileAnticipator.getTempDir(), "snmp"));
    m_filterDao = EasyMock.createMock(FilterDao.class);
    EasyMock.expect(m_filterDao.getActiveIPAddressList((String) EasyMock.anyObject())).andReturn(Collections.singletonList(addr("127.0.0.1"))).anyTimes();
    m_filterDao.flushActiveIpAddressListCache();
    EasyMock.expectLastCall().anyTimes();
    FilterDaoFactory.setInstance(m_filterDao);
    EasyMock.replay(m_filterDao);
    m_anticipator = new EventAnticipator();
    MockEventIpcManager eventMgr = new MockEventIpcManager();
    eventMgr.setEventAnticipator(m_anticipator);
    eventMgr.setSynchronous(true);
    EventIpcManager eventdIpcMgr = (EventIpcManager) eventMgr;
    EventIpcManagerFactory.setIpcManager(eventdIpcMgr);
    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.setInstance(new PollOutagesConfigFactory(new FileSystemResource(file)));
    PollOutagesConfigFactory.getInstance().afterPropertiesSet();
    initFactories("/threshd-configuration.xml", "/test-thresholds.xml");
    m_anticipatedEvents = new ArrayList<>();
}
Also used : MockEventIpcManager(org.opennms.netmgt.dao.mock.MockEventIpcManager) FileWriter(java.io.FileWriter) PollOutagesConfigFactory(org.opennms.netmgt.config.PollOutagesConfigFactory) FilesystemResourceStorageDao(org.opennms.netmgt.dao.support.FilesystemResourceStorageDao) FileSystemResource(org.springframework.core.io.FileSystemResource) Date(java.util.Date) EventAnticipator(org.opennms.netmgt.dao.mock.EventAnticipator) BigInteger(java.math.BigInteger) FilterDao(org.opennms.netmgt.filter.api.FilterDao) JdbcFilterDao(org.opennms.netmgt.filter.JdbcFilterDao) EventIpcManager(org.opennms.netmgt.events.api.EventIpcManager) MockEventIpcManager(org.opennms.netmgt.dao.mock.MockEventIpcManager) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) FileAnticipator(org.opennms.test.FileAnticipator) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Before(org.junit.Before)

Example 95 with FileSystemResource

use of org.springframework.core.io.FileSystemResource in project opennms by OpenNMS.

the class Normalizer method unmarshal.

/**
 * Unmarshall.
 *
 * @param source the source
 */
public static void unmarshal(Source source) {
    try {
        System.out.println("Normalizing " + source.configFile);
        final Resource resource = new FileSystemResource(source.configFile);
        Object result = null;
        result = JaxbUtils.unmarshal(source.clazz, resource);
        JaxbUtils.marshal(result, new FileWriter(source.configFile));
        // Double check the newly generated file can be processed correctly.
        result = JaxbUtils.unmarshal(source.clazz, resource);
        if (result == null) {
            throw new IllegalArgumentException("Something went wrong in JAXB.");
        }
    } catch (Exception ex) {
        System.err.println("Cannot normalize file: " + ex);
    }
}
Also used : FileWriter(java.io.FileWriter) Resource(org.springframework.core.io.Resource) FileSystemResource(org.springframework.core.io.FileSystemResource) FileSystemResource(org.springframework.core.io.FileSystemResource) IOException(java.io.IOException)

Aggregations

FileSystemResource (org.springframework.core.io.FileSystemResource)148 File (java.io.File)77 Test (org.junit.Test)44 Resource (org.springframework.core.io.Resource)42 Before (org.junit.Before)27 ClassPathResource (org.springframework.core.io.ClassPathResource)16 IOException (java.io.IOException)12 FileWriter (java.io.FileWriter)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 Properties (java.util.Properties)8 PrefabGraph (org.opennms.netmgt.model.PrefabGraph)8 FileOutputStream (java.io.FileOutputStream)7 URL (java.net.URL)7 InputStreamResource (org.springframework.core.io.InputStreamResource)7 UrlResource (org.springframework.core.io.UrlResource)7 Date (java.util.Date)5 FilesystemResourceStorageDao (org.opennms.netmgt.dao.support.FilesystemResourceStorageDao)5 FileReader (java.io.FileReader)4 OutputStreamWriter (java.io.OutputStreamWriter)4