/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2009-2011 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2011 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
* OpenNMS(R) is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* OpenNMS(R) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenNMS(R). If not, see:
* http://www.gnu.org/licenses/
*
* For more information contact:
* OpenNMS(R) Licensing <license@opennms.org>
* http://www.opennms.org/
* http://www.opennms.com/
*******************************************************************************/
package org.opennms.web.rest;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opennms.netmgt.dao.db.JUnitConfigurationEnvironment;
import org.opennms.core.test.OpenNMSJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;
@RunWith(OpenNMSJUnit4ClassRunner.class)
@ContextConfiguration(locations={
"classpath:/META-INF/opennms/component-dao.xml"
})
@JUnitConfigurationEnvironment
public class ForeignSourceRestServiceTest extends AbstractSpringJerseyRestTestCase {
@Test
public void testForeignSources() throws Exception {
createForeignSource();
String url = "/foreignSources";
String xml = sendRequest(GET, url, 200);
assertTrue(xml.contains("ICMP"));
url = "/foreignSources/test";
sendPut(url, "scanInterval=1h");
xml = sendRequest(GET, url, 200);
assertTrue(xml.contains("<scan-interval>1h</scan-interval>"));
url = "/foreignSources/test";
sendPut(url, "scanInterval=1h");
sendRequest(DELETE, url, 200);
xml = sendRequest(GET, url, 200);
assertTrue(xml.contains("<scan-interval>1d</scan-interval>"));
sendRequest(DELETE, url, 200);
xml = sendRequest(GET, url, 200);
assertTrue(xml.contains("<scan-interval>1d</scan-interval>"));
}
@Test
public void testDetectors() throws Exception {
createForeignSource();
String url = "/foreignSources/test/detectors";
String xml = sendRequest(GET, url, 200);
assertTrue(xml.contains("<detectors "));
assertTrue(xml.contains("<detector "));
assertTrue(xml.contains("name=\"ICMP\""));
url = "/foreignSources/test/detectors/HTTP";
xml = sendRequest(GET, url, 200);
assertTrue(xml.contains("org.opennms.netmgt.provision.detector.simple.HttpDetector"));
xml = sendRequest(DELETE, url, 200);
xml = sendRequest(GET, url, 204);
}
@Test
public void testPolicies() throws Exception {
createForeignSource();
String url = "/foreignSources/test/policies";
String xml = sendRequest(GET, url, 200);
assertTrue(xml.contains("<policies "));
assertTrue(xml.contains("<policy "));
assertTrue(xml.contains("name=\"lower-case-node\""));
assertTrue(xml.contains("value=\"Lower-Case-Nodes\""));
url = "/foreignSources/test/policies/all-ipinterfaces";
xml = sendRequest(GET, url, 200);
assertTrue(xml.contains("org.opennms.netmgt.provision.persist.policies.InclusiveInterfacePolicy"));
xml = sendRequest(DELETE, url, 200);
xml = sendRequest(GET, url, 204);
}
private void createForeignSource() throws Exception {
String fs =
"<foreign-source xmlns=\"http://xmlns.opennms.org/xsd/config/foreign-source\" name=\"test\">" +
"<scan-interval>1d</scan-interval>" +
"<detectors>" +
"<detector class=\"org.opennms.netmgt.provision.detector.datagram.DnsDetector\" name=\"DNS\"/>" +
"<detector class=\"org.opennms.netmgt.provision.detector.simple.HttpDetector\" name=\"HTTP\"/>" +
"<detector class=\"org.opennms.netmgt.provision.detector.simple.HttpsDetector\" name=\"HTTPS\"/>" +
"<detector class=\"org.opennms.netmgt.provision.detector.icmp.IcmpDetector\" name=\"ICMP\"/>" +
"</detectors>" +
"<policies>" +
"<policy name=\"lower-case-node\" class=\"org.opennms.netmgt.provision.persist.policies.NodeCategoryPolicy\">" +
"<parameter key=\"label\" value=\"~^[a-z]$\" />" +
"<parameter key=\"category\" value=\"Lower-Case-Nodes\" />" +
"</policy>" +
"<policy name=\"all-ipinterfaces\" class=\"org.opennms.netmgt.provision.persist.policies.InclusiveInterfacePolicy\" />" +
"</policies>" +
"</foreign-source>";
sendPost("/foreignSources", fs);
}
}