Search in sources :

Example 76 with JUnitTemporaryDatabase

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

the class CategoryRestServiceIT method testUpdateCategory.

@Test
@JUnitTemporaryDatabase
public void testUpdateCategory() throws Exception {
    // create
    OnmsCategory createMe = new OnmsCategory();
    createMe.setDescription("This is a description");
    createMe.setName("myName");
    createCategory(createMe);
    // verify creation
    String url = "/categories";
    String xml = sendRequest(GET, url, 200);
    assertTrue(xml.contains("<description>This is a description</description>"));
    assertTrue(xml.contains("name=\"myName\""));
    // change
    url += "/myName";
    sendPut(url, "description=My Equipment&name=NewCategory", 204);
    xml = sendRequest(GET, url, 200);
    assertTrue(xml.contains("<description>My Equipment</description>"));
    assertTrue(xml.contains("name=\"NewCategory\""));
    xml = sendRequest(DELETE, url, 204);
    assertFalse(xml.contains("<description>My Equipment</description>"));
    assertFalse(xml.contains("name=\"NewCategory\""));
    assertFalse(xml.contains("name=\"myName\""));
    assertFalse(xml.contains("<description>This is a description</description>"));
    sendRequest(GET, url, 200);
}
Also used : OnmsCategory(org.opennms.netmgt.model.OnmsCategory) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 77 with JUnitTemporaryDatabase

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

the class SnmpAssetProvisioningAdapterIT method testAdd.

@Test
// Relies on records created in @Before so we need a fresh database
@JUnitTemporaryDatabase
@JUnitSnmpAgent(resource = "snmpAssetTestData.properties")
public void testAdd() throws Exception {
    AdapterOperationChecker verifyOperations = new AdapterOperationChecker(1);
    m_adapter.getOperationQueue().addListener(verifyOperations);
    OnmsNode n = m_nodeDao.findByForeignId("rancid", "1");
    assertNotNull(n);
    m_adapter.addNode(n.getId());
    assertTrue(verifyOperations.enqueueLatch.await(4, TimeUnit.SECONDS));
    assertTrue(verifyOperations.dequeueLatch.await(4, TimeUnit.SECONDS));
    assertTrue(verifyOperations.executeLatch.await(4, TimeUnit.SECONDS));
    assertEquals(0, m_adapter.getOperationQueue().getOperationQueueForNode(n.getId()).size());
// TODO: Add assertions to check that the addNode() adapter call updated the asset record
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase) JUnitSnmpAgent(org.opennms.core.test.snmp.annotations.JUnitSnmpAgent)

Example 78 with JUnitTemporaryDatabase

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

the class SnmpAssetProvisioningAdapterFullIT method testAddSameOperationTwice.

@Test
// Relies on records created in @Before so we need a fresh database
@JUnitTemporaryDatabase
public void testAddSameOperationTwice() throws InterruptedException {
    AdapterOperationChecker verifyOperations = new AdapterOperationChecker(2);
    m_adapter.getOperationQueue().addListener(verifyOperations);
    try {
        OnmsNode node = m_nodeDao.get(NODE_ID);
        assertNotNull(node);
        int firstNodeId = node.getId();
        m_adapter.addNode(firstNodeId);
        // should get deduplicated
        m_adapter.addNode(firstNodeId);
        m_adapter.updateNode(firstNodeId);
        assertTrue(verifyOperations.enqueueLatch.await(4, TimeUnit.SECONDS));
        assertTrue(verifyOperations.dequeueLatch.await(4, TimeUnit.SECONDS));
        assertTrue(verifyOperations.executeLatch.await(4, TimeUnit.SECONDS));
        assertEquals(0, m_adapter.getOperationQueue().getOperationQueueForNode(firstNodeId).size());
        node = m_nodeDao.get(firstNodeId);
        assertNotNull(node);
        System.out.println("ID: " + node.getAssetRecord().getId());
        System.out.println("Comment: " + node.getAssetRecord().getComment());
        assertNotNull("AssetRecord comment is null", node.getAssetRecord().getComment());
        assertEquals(EXPECTED_COMMENT_FIELD, node.getAssetRecord().getComment());
    } finally {
        m_adapter.getOperationQueue().removeListener(verifyOperations);
    }
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 79 with JUnitTemporaryDatabase

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

the class MigratorIT method testUpdate.

@Test
@JUnitTemporaryDatabase(createSchema = false)
public void testUpdate() throws Exception {
    // Make sure there is no databasechangelog table
    assertFalse(changelogExists());
    Resource aResource = null;
    for (final Resource resource : getTestResources()) {
        URI uri = resource.getURI();
        if (uri.getScheme().equals("file") && uri.toString().contains("test-api/schema/a")) {
            aResource = resource;
        }
        if (uri.getScheme().equals("jar") && uri.toString().contains("test-api.schema.a")) {
            aResource = resource;
        }
    }
    assertNotNull("aResource must not be null", aResource);
    Set<String> tables = getTables();
    assertFalse("must not contain table 'schematest'", tables.contains("schematest"));
    final Migration migration = new Migration();
    migration.setAdminUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setAdminPassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setDatabaseUser(System.getProperty(TemporaryDatabase.ADMIN_USER_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_USER));
    migration.setDatabasePassword(System.getProperty(TemporaryDatabase.ADMIN_PASSWORD_PROPERTY, TemporaryDatabase.DEFAULT_ADMIN_PASSWORD));
    migration.setChangeLog("changelog.xml");
    migration.setAccessor(new ExistingResourceAccessor(aResource));
    LOG.info("Running migration on database: {}", migration);
    final Migrator m = new Migrator();
    m.setDataSource(m_dataSource);
    m.setAdminDataSource(m_dataSource);
    m.setValidateDatabaseVersion(false);
    m.setCreateUser(false);
    m.setCreateDatabase(false);
    m.prepareDatabase(migration);
    m.migrate(migration);
    LOG.info("Migration complete: {}", migration);
    tables = getTables();
    assertTrue("must contain table 'schematest'", tables.contains("schematest"));
}
Also used : Migration(org.opennms.core.schema.Migration) Resource(org.springframework.core.io.Resource) URI(java.net.URI) Migrator(org.opennms.core.schema.Migrator) ExistingResourceAccessor(org.opennms.core.schema.ExistingResourceAccessor) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

Example 80 with JUnitTemporaryDatabase

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

the class NodeRestServiceIT method testLimits.

@Test
@JUnitTemporaryDatabase
public void testLimits() throws Exception {
    JAXBContext context = JAXBContext.newInstance(OnmsNodeList.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    // Testing POST
    for (m_nodeCounter = 0; m_nodeCounter < 20; m_nodeCounter++) {
        createNode();
    }
    String url = "/nodes";
    // Testing GET Collection
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("limit", "10");
    parameters.put("orderBy", "id");
    String xml = sendRequest(GET, url, parameters, 200);
    assertTrue(xml, xml.contains("Darwin TestMachine 9.4.0 Darwin Kernel Version 9.4.0"));
    Pattern p = Pattern.compile("<node [^>]*\\s*id=", Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);
    Matcher m = p.matcher(xml);
    int count = 0;
    while (m.find()) {
        count++;
    }
    assertEquals("should get 10 nodes back", 10, count);
    // Validate object by unmarshalling
    OnmsNodeList list = (OnmsNodeList) unmarshaller.unmarshal(new StringReader(xml));
    assertEquals(Integer.valueOf(10), list.getCount());
    assertEquals(10, list.size());
    assertEquals(Integer.valueOf(20), list.getTotalCount());
    int i = 0;
    Set<OnmsNode> sortedNodes = new TreeSet<OnmsNode>(new Comparator<OnmsNode>() {

        @Override
        public int compare(OnmsNode o1, OnmsNode o2) {
            if (o1 == null && o2 == null) {
                return 0;
            } else if (o1 == null) {
                return 1;
            } else if (o2 == null) {
                return -1;
            } else {
                if (o1.getId() == null) {
                    throw new IllegalStateException("Null ID on node: " + o1.toString());
                }
                return o1.getId().compareTo(o2.getId());
            }
        }
    });
    // Sort the nodes by ID
    sortedNodes.addAll(list.getObjects());
    for (OnmsNode node : sortedNodes) {
        assertEquals(node.toString(), "TestMachine" + i++, node.getLabel());
    }
}
Also used : Pattern(java.util.regex.Pattern) OnmsNode(org.opennms.netmgt.model.OnmsNode) OnmsNodeList(org.opennms.netmgt.model.OnmsNodeList) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) JAXBContext(javax.xml.bind.JAXBContext) TreeSet(java.util.TreeSet) StringReader(java.io.StringReader) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test) JUnitTemporaryDatabase(org.opennms.core.test.db.annotations.JUnitTemporaryDatabase)

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