Search in sources :

Example 1 with NodeCategorySettingPolicy

use of org.opennms.netmgt.provision.persist.policies.NodeCategorySettingPolicy in project opennms by OpenNMS.

the class ProvisionerIT method testSaveCategoriesOnUpdateNodeAttributes.

@Test(timeout = 300000)
public void testSaveCategoriesOnUpdateNodeAttributes() throws Exception {
    final EventAnticipator eventAnticipator = m_mockEventIpcManager.getEventAnticipator();
    final String TEST_CATEGORY = "TEST_CATEGORY";
    final String OLD_LABEL = "apknd";
    final String NEW_LABEL = "apknd-new";
    importFromResource("classpath:/tec_dump.xml.smalltest", Boolean.TRUE.toString());
    getScanExecutor().pause();
    m_provisioner.scheduleRescanForExistingNodes();
    final Collection<OnmsNode> nodes = m_nodeDao.findByLabel(OLD_LABEL);
    assertNotNull(nodes);
    assertEquals(1, nodes.size());
    OnmsNode node = nodes.iterator().next();
    assertNotNull(node);
    OnmsNode nodeCopy = new OnmsNode(m_locationDao.getDefaultLocation(), OLD_LABEL);
    nodeCopy.setId(node.getId());
    nodeCopy.setLabelSource(NodeLabelSource.USER);
    assertNotSame(node, nodeCopy);
    assertEquals(OLD_LABEL, node.getLabel());
    assertFalse(node.hasCategory(TEST_CATEGORY));
    // Create a policy that will apply the category to the node
    final NodeCategorySettingPolicy policy = new NodeCategorySettingPolicy();
    policy.setCategory(TEST_CATEGORY);
    policy.setLabel(OLD_LABEL);
    // Apply the policy
    nodeCopy = policy.apply(nodeCopy);
    assertTrue(nodeCopy.getRequisitionedCategories().contains(TEST_CATEGORY));
    final EventBuilder eb = new EventBuilder(EventConstants.NODE_LABEL_CHANGED_EVENT_UEI, "OnmsNode.mergeNodeAttributes");
    eb.setNodeid(node.getId());
    eb.addParam("oldNodeLabel", OLD_LABEL);
    eb.addParam("oldNodeLabelSource", "U");
    eb.addParam("newNodeLabel", NEW_LABEL);
    eb.addParam("newNodeLabelSource", "U");
    eventAnticipator.anticipateEvent(eb.getEvent());
    // Change the label of the node so that we can trigger a NODE_LABEL_CHANGED_EVENT_UEI event
    nodeCopy.setLabel(NEW_LABEL);
    nodeCopy.setLabelSource(NodeLabelSource.USER);
    assertFalse(node.getLabel().equals(nodeCopy.getLabel()));
    m_provisionService.updateNodeAttributes(nodeCopy);
    // Flush here to force a write so we are sure that the OnmsCategoryCollection are correctly created
    m_nodeDao.flush();
    // Query by the new node label
    final OnmsNode node2 = m_nodeDao.findByLabel(NEW_LABEL).iterator().next();
    assertTrue(node2.hasCategory(TEST_CATEGORY));
    eventAnticipator.resetUnanticipated();
    eventAnticipator.verifyAnticipated();
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) EventBuilder(org.opennms.netmgt.model.events.EventBuilder) NodeCategorySettingPolicy(org.opennms.netmgt.provision.persist.policies.NodeCategorySettingPolicy) EventAnticipator(org.opennms.netmgt.dao.mock.EventAnticipator) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 EventAnticipator (org.opennms.netmgt.dao.mock.EventAnticipator)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)1 NodeCategorySettingPolicy (org.opennms.netmgt.provision.persist.policies.NodeCategorySettingPolicy)1