modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/HTTPNetServiceComponentConfiguration.java
| 5
modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/PortNetServiceComponent.java
| 151 ++++++++++
modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/PortNetServiceComponentConfiguration.java
| 51 +++
modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/PortNetServiceDiscoveryComponent.java
| 63 ++++
modules/plugins/netservices/src/main/resources/META-INF/rhq-plugin.xml
| 18 +
modules/plugins/netservices/src/test/java/org/rhq/plugins/netservices/PortNetServiceDiscoveryComponentTest.java
| 128 ++++++++
modules/plugins/netservices/src/test/java/org/rhq/plugins/netservices/itest/PortNetServiceComponentTest.java
| 151 ++++++++++
7 files changed, 562 insertions(+), 5 deletions(-)
New commits:
commit 157e81481a43db6991e544f5fd9f3f3fb4d1e33a
Author: Thomas Segismont <tsegismo(a)redhat.com>
Date: Mon Mar 4 16:26:17 2013 +0100
New PortService in netservices plugin
diff --git
a/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/HTTPNetServiceComponentConfiguration.java
b/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/HTTPNetServiceComponentConfiguration.java
index b28d03f..0072004 100644
---
a/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/HTTPNetServiceComponentConfiguration.java
+++
b/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/HTTPNetServiceComponentConfiguration.java
@@ -35,8 +35,7 @@ class HTTPNetServiceComponentConfiguration {
private Pattern responseValidationPattern;
- public HTTPNetServiceComponentConfiguration(URL endPointUrl, HttpMethod httpMethod,
- Pattern responseValidationPattern) {
+ HTTPNetServiceComponentConfiguration(URL endPointUrl, HttpMethod httpMethod, Pattern
responseValidationPattern) {
this.endPointUrl = endPointUrl;
this.httpMethod = httpMethod;
this.responseValidationPattern = responseValidationPattern;
@@ -54,4 +53,4 @@ class HTTPNetServiceComponentConfiguration {
return responseValidationPattern;
}
-}
\ No newline at end of file
+}
diff --git
a/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/PortNetServiceComponent.java
b/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/PortNetServiceComponent.java
new file mode 100644
index 0000000..618fb5a
--- /dev/null
+++
b/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/PortNetServiceComponent.java
@@ -0,0 +1,151 @@
+/*
+ * RHQ Management Platform
+ * Copyright 2013, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This program 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 version 2 of the License.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+package org.rhq.plugins.netservices;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.rhq.core.domain.configuration.Configuration;
+import org.rhq.core.domain.measurement.AvailabilityType;
+import org.rhq.core.domain.measurement.MeasurementDataNumeric;
+import org.rhq.core.domain.measurement.MeasurementDataTrait;
+import org.rhq.core.domain.measurement.MeasurementReport;
+import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
+import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
+import org.rhq.core.pluginapi.inventory.ResourceComponent;
+import org.rhq.core.pluginapi.inventory.ResourceContext;
+import org.rhq.core.pluginapi.measurement.MeasurementFacet;
+import org.rhq.core.util.StringUtil;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.Set;
+
+/**
+ * @author Thomas Segismont
+ */
+public class PortNetServiceComponent implements ResourceComponent, MeasurementFacet {
+
+ public static final class ConfigKeys {
+
+ private ConfigKeys() {
+ // Defensive
+ }
+
+ public static final String ADDRESS = "address";
+ public static final String PORT = "port";
+
+ }
+
+ private static final Log LOG = LogFactory.getLog(PortNetServiceComponent.class);
+
+ private static final int CONNECTION_TIMEOUT = 2000;
+
+ private PortNetServiceComponentConfiguration componentConfiguration;
+
+ @Override
+ public void start(ResourceContext resourceContext) throws
InvalidPluginConfigurationException {
+ componentConfiguration =
createComponentConfiguration(resourceContext.getPluginConfiguration());
+ }
+
+ static PortNetServiceComponentConfiguration
createComponentConfiguration(Configuration pluginConfig)
+ throws InvalidPluginConfigurationException {
+ String addressString = pluginConfig.getSimpleValue(ConfigKeys.ADDRESS,
StringUtil.EMPTY_STRING);
+ if (StringUtil.isBlank(addressString)) {
+ throw new InvalidPluginConfigurationException("Address is not
defined");
+ }
+ String portString = pluginConfig.getSimpleValue(ConfigKeys.PORT,
StringUtil.EMPTY_STRING);
+ if (StringUtil.isBlank(portString)) {
+ throw new InvalidPluginConfigurationException("Port is not
defined");
+ }
+ int port;
+ try {
+ port = Integer.parseInt(portString);
+ } catch (NumberFormatException e) {
+ throw new InvalidPluginConfigurationException("Invalid port number:
" + portString);
+ }
+ InetAddress address;
+ try {
+ address = InetAddress.getByName(addressString);
+ } catch (UnknownHostException uhe) {
+ throw new InvalidPluginConfigurationException(uhe);
+ }
+ return new PortNetServiceComponentConfiguration(address, port);
+ }
+
+ @Override
+ public void stop() {
+ componentConfiguration = null;
+ }
+
+ @Override
+ public AvailabilityType getAvailability() {
+ return portReachable() ? AvailabilityType.UP : AvailabilityType.DOWN;
+ }
+
+ @Override
+ public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest>
metrics) throws Exception {
+ for (MeasurementScheduleRequest request : metrics) {
+ if (request.getName().equals("ipAddress")) {
+ report.addData(new MeasurementDataTrait(request,
componentConfiguration.getAddress().getHostAddress()));
+ } else if (request.getName().equals("hostName")) {
+ report.addData(new MeasurementDataTrait(request,
componentConfiguration.getAddress()
+ .getCanonicalHostName()));
+ } else if (request.getName().equals("connectTime")) {
+ long start = System.currentTimeMillis();
+ if (portReachable()) {
+ report.addData(new MeasurementDataNumeric(request, (double)
(System.currentTimeMillis() - start)));
+ }
+ }
+ }
+ }
+
+ private boolean portReachable() {
+ Socket socket = null;
+ try {
+ socket = new Socket();
+ socket.connect(
+ new InetSocketAddress(componentConfiguration.getAddress(),
componentConfiguration.getPort()),
+ CONNECTION_TIMEOUT);
+ return true;
+ } catch (Exception e) {
+ if (LOG.isWarnEnabled()) {
+ LOG.warn("Unable to reach remote port: " +
componentConfiguration.toString(), e);
+ }
+ } finally {
+ if (socket != null) {
+ closeQuietly(socket);
+ }
+ }
+ return false;
+ }
+
+ private void closeQuietly(Closeable closeable) {
+ try {
+ closeable.close();
+ } catch (IOException ignore) {
+ }
+ }
+
+}
diff --git
a/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/PortNetServiceComponentConfiguration.java
b/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/PortNetServiceComponentConfiguration.java
new file mode 100644
index 0000000..1419434
--- /dev/null
+++
b/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/PortNetServiceComponentConfiguration.java
@@ -0,0 +1,51 @@
+/*
+ * RHQ Management Platform
+ * Copyright 2013, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This program 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 version 2 of the License.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+package org.rhq.plugins.netservices;
+
+import java.net.InetAddress;
+
+/**
+ * @author Thomas Segismont
+ */
+class PortNetServiceComponentConfiguration {
+
+ private InetAddress address;
+
+ private int port;
+
+ PortNetServiceComponentConfiguration(InetAddress address, int port) {
+ this.address = address;
+ this.port = port;
+ }
+
+ public InetAddress getAddress() {
+ return address;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+ @Override
+ public String toString() {
+ return "PortNetServiceComponentConfiguration[" + "address=" +
address + ", port=" + port + ']';
+ }
+}
diff --git
a/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/PortNetServiceDiscoveryComponent.java
b/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/PortNetServiceDiscoveryComponent.java
new file mode 100644
index 0000000..227d941
--- /dev/null
+++
b/modules/plugins/netservices/src/main/java/org/rhq/plugins/netservices/PortNetServiceDiscoveryComponent.java
@@ -0,0 +1,63 @@
+/*
+ * RHQ Management Platform
+ * Copyright 2013, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This program 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 version 2 of the License.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+package org.rhq.plugins.netservices;
+
+import org.rhq.core.domain.configuration.Configuration;
+import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
+import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
+import org.rhq.core.pluginapi.inventory.ManualAddFacet;
+import org.rhq.core.pluginapi.inventory.ResourceDiscoveryComponent;
+import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * @author Thomas Segismont
+ */
+public class PortNetServiceDiscoveryComponent implements ResourceDiscoveryComponent,
ManualAddFacet {
+
+ private static final String RESOURCE_PREFIX = "Port ";
+
+ private static final String SEPARATOR = ":";
+
+ @Override
+ public Set<DiscoveredResourceDetails>
discoverResources(ResourceDiscoveryContext context)
+ throws InvalidPluginConfigurationException, Exception {
+ // No automatic discovery
+ return Collections.EMPTY_SET;
+ }
+
+ @Override
+ public DiscoveredResourceDetails discoverResource(Configuration pluginConfiguration,
+ ResourceDiscoveryContext context) throws InvalidPluginConfigurationException {
+ PortNetServiceComponentConfiguration componentConfiguration =
PortNetServiceComponent
+ .createComponentConfiguration(pluginConfiguration);
+ String resourceKey = createResourceKey(componentConfiguration);
+ return new DiscoveredResourceDetails(context.getResourceType(), resourceKey,
resourceKey, null, null,
+ pluginConfiguration, null);
+ }
+
+ private String createResourceKey(PortNetServiceComponentConfiguration
componentConfiguration) {
+ return RESOURCE_PREFIX + componentConfiguration.getAddress().getHostAddress() +
SEPARATOR
+ + componentConfiguration.getPort();
+ }
+}
diff --git a/modules/plugins/netservices/src/main/resources/META-INF/rhq-plugin.xml
b/modules/plugins/netservices/src/main/resources/META-INF/rhq-plugin.xml
index 16a48d5..c4aee62 100644
--- a/modules/plugins/netservices/src/main/resources/META-INF/rhq-plugin.xml
+++ b/modules/plugins/netservices/src/main/resources/META-INF/rhq-plugin.xml
@@ -64,7 +64,21 @@
</service>
+ <service name="PortService"
+ discovery="PortNetServiceDiscoveryComponent"
+ class="PortNetServiceComponent"
+ supportsManualAdd="true"
+ description="Remote Port monitoring service">
-</plugin>
+ <plugin-configuration>
+ <c:simple-property name="address" description="Remote IP
address" />
+ <c:simple-property name="port" description="Remote
port" type="integer" />
+ </plugin-configuration>
+
+ <metric property="ipAddress" displayType="summary"
dataType="trait" description="The ip address of the host"/>
+ <metric property="hostName" displayType="summary"
dataType="trait" description="The hostname of the host"/>
+ <metric property="connectTime" units="milliseconds"
displayType="summary" description="Time to establish connection"/>
-
\ No newline at end of file
+ </service>
+
+</plugin>
diff --git
a/modules/plugins/netservices/src/test/java/org/rhq/plugins/netservices/PortNetServiceDiscoveryComponentTest.java
b/modules/plugins/netservices/src/test/java/org/rhq/plugins/netservices/PortNetServiceDiscoveryComponentTest.java
new file mode 100644
index 0000000..ec50f91
--- /dev/null
+++
b/modules/plugins/netservices/src/test/java/org/rhq/plugins/netservices/PortNetServiceDiscoveryComponentTest.java
@@ -0,0 +1,128 @@
+/*
+ * RHQ Management Platform
+ * Copyright 2013, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This program 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 version 2 of the License.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+package org.rhq.plugins.netservices;
+
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.rhq.core.domain.configuration.Configuration;
+import org.rhq.core.domain.resource.ResourceType;
+import org.rhq.core.pluginapi.inventory.DiscoveredResourceDetails;
+import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
+import org.rhq.core.pluginapi.inventory.ResourceDiscoveryContext;
+import org.rhq.plugins.netservices.PortNetServiceComponent.ConfigKeys;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import java.net.UnknownHostException;
+
+import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.fail;
+
+/**
+ * @author Thomas Segismont
+ */
+public class PortNetServiceDiscoveryComponentTest {
+
+ private PortNetServiceDiscoveryComponent portNetServiceDiscoveryComponent;
+
+ private Configuration configuration;
+
+ @Mock
+ private ResourceDiscoveryContext<?> resourceDiscoveryContext;
+
+ @Mock
+ private ResourceType resourceType;
+
+ @BeforeMethod(alwaysRun = true)
+ protected void setUp() throws Exception {
+ portNetServiceDiscoveryComponent = new PortNetServiceDiscoveryComponent();
+ configuration = new Configuration();
+ configuration.setSimpleValue(ConfigKeys.ADDRESS, "127.0.0.1");
+ configuration.setSimpleValue(ConfigKeys.PORT, "8080");
+ MockitoAnnotations.initMocks(this);
+ when(resourceDiscoveryContext.getResourceType()).thenReturn(resourceType);
+ }
+
+ @Test
+ public void testDiscoverResources() throws Exception {
+ // Manual add only, should always return empty set
+ assertEquals(0,
portNetServiceDiscoveryComponent.discoverResources(null).size());
+ }
+
+ @Test
+ public void testValidComponentConfiguration() {
+ try {
+ DiscoveredResourceDetails resourceDetails =
portNetServiceDiscoveryComponent.discoverResource(
+ configuration, resourceDiscoveryContext);
+ assertEquals(resourceDetails.getResourceType(), resourceType);
+ } catch (InvalidPluginConfigurationException e) {
+ fail("Component configuration should be valid", e);
+ }
+ }
+
+ @Test
+ public void testMissingAddress() {
+ configuration.remove(ConfigKeys.ADDRESS);
+ try {
+ portNetServiceDiscoveryComponent.discoverResource(configuration,
resourceDiscoveryContext);
+ fail("Component configuration should be invalid");
+ } catch (InvalidPluginConfigurationException e) {
+ assertEquals(e.getMessage(), "Address is not defined");
+ }
+ }
+
+ @Test
+ public void testMissingPort() {
+ configuration.remove(ConfigKeys.PORT);
+ try {
+ portNetServiceDiscoveryComponent.discoverResource(configuration,
resourceDiscoveryContext);
+ fail("Component configuration should be invalid");
+ } catch (InvalidPluginConfigurationException e) {
+ assertEquals(e.getMessage(), "Port is not defined");
+ }
+ }
+
+ @Test
+ public void testInvalidPortNumberFormat() {
+ String configPort = "NaN";
+ configuration.setSimpleValue(ConfigKeys.PORT, configPort);
+ try {
+ portNetServiceDiscoveryComponent.discoverResource(configuration,
resourceDiscoveryContext);
+ fail("Component configuration should be invalid");
+ } catch (InvalidPluginConfigurationException e) {
+ assertEquals(e.getMessage(), "Invalid port number: " +
configPort);
+ }
+ }
+
+ @Test
+ public void testMalformedAddress() {
+ String configAddress = "pipomolo";
+ configuration.setSimpleValue(ConfigKeys.ADDRESS, configAddress);
+ try {
+ portNetServiceDiscoveryComponent.discoverResource(configuration,
resourceDiscoveryContext);
+ fail("Component configuration should be invalid");
+ } catch (InvalidPluginConfigurationException e) {
+ assertEquals(e.getCause().getClass(), UnknownHostException.class);
+ }
+ }
+
+}
diff --git
a/modules/plugins/netservices/src/test/java/org/rhq/plugins/netservices/itest/PortNetServiceComponentTest.java
b/modules/plugins/netservices/src/test/java/org/rhq/plugins/netservices/itest/PortNetServiceComponentTest.java
new file mode 100644
index 0000000..f97b222
--- /dev/null
+++
b/modules/plugins/netservices/src/test/java/org/rhq/plugins/netservices/itest/PortNetServiceComponentTest.java
@@ -0,0 +1,151 @@
+/*
+ * RHQ Management Platform
+ * Copyright 2013, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This program 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 version 2 of the License.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+package org.rhq.plugins.netservices.itest;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.testng.annotations.AfterClass;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+import org.rhq.core.domain.configuration.Configuration;
+import org.rhq.core.domain.discovery.MergeResourceResponse;
+import org.rhq.core.domain.measurement.AvailabilityType;
+import org.rhq.core.domain.measurement.DataType;
+import org.rhq.core.domain.measurement.MeasurementData;
+import org.rhq.core.domain.measurement.MeasurementReport;
+import org.rhq.core.domain.measurement.MeasurementScheduleRequest;
+import org.rhq.core.pluginapi.inventory.ResourceComponent;
+import org.rhq.core.util.StringUtil;
+import org.rhq.plugins.netservices.PortNetServiceComponent;
+import org.rhq.plugins.netservices.PortNetServiceComponent.ConfigKeys;
+
+/**
+ * @author Thomas Segismont
+ */
+public class PortNetServiceComponentTest extends NetServiceComponentTest {
+
+ private static final Log LOG = LogFactory.getLog(PortNetServiceComponentTest.class);
+
+ private static final String SERVICE_NAME = "PortService";
+
+ private static final String LOOPBACK = "127.0.0.1";
+
+ private PortNetServiceComponent portNetServiceComponent;
+
+ private ServerSocket serverSocket;
+
+ private int serverSocketLocalPort;
+
+ private Thread acceptorThread;
+
+ private volatile boolean stopListening = false;
+
+ @BeforeClass
+ public void startSocketServer() throws Exception {
+ LOG.info("Setting up a socket server");
+ // Let's bind a server socket to any available port
+ serverSocket = new ServerSocket(0, 50, InetAddress.getLocalHost());
+ // Do not block indefinitely on call to #accept
+ serverSocket.setSoTimeout(1000);
+ serverSocketLocalPort = serverSocket.getLocalPort();
+ acceptorThread = new Thread(new Runnable() {
+ @Override
+ public void run() {
+ while (!stopListening) {
+ try {
+ Socket socket = serverSocket.accept();
+ socket.close();
+ } catch (IOException ignore) {
+ }
+ }
+ }
+ });
+ acceptorThread.setDaemon(true);
+ acceptorThread.start();
+ }
+
+ @AfterClass
+ public void stopSocketServer() {
+ LOG.info("Shutting down socket server");
+ stopListening = true;
+ try {
+ serverSocket.close();
+ } catch (IOException ignore) {
+ }
+ }
+
+ @Test(dependsOnMethods = "testPluginLoad")
+ public void testManualAdd() throws Exception {
+ Configuration configuration = new Configuration();
+ configuration.setSimpleValue(ConfigKeys.ADDRESS, LOOPBACK);
+ configuration.setSimpleValue(ConfigKeys.PORT,
String.valueOf(serverSocketLocalPort));
+ MergeResourceResponse response = getInventoryManager().manuallyAddResource(
+ getPluginManager().getMetadataManager().getType(SERVICE_NAME, PLUGIN_NAME),
getPlatform().getId(),
+ configuration, -1);
+ assertNotNull(response, "Manual add response is null");
+ @SuppressWarnings("rawtypes")
+ ResourceComponent resourceComponent =
getInventoryManager().getResourceContainer(response.getResourceId())
+ .getResourceComponent();
+ assertEquals(resourceComponent.getClass(), PortNetServiceComponent.class);
+ portNetServiceComponent = (PortNetServiceComponent) resourceComponent;
+ }
+
+ @Test(dependsOnMethods = "testManualAdd")
+ public void testAvailability() throws Exception {
+ assertEquals(portNetServiceComponent.getAvailability(), AvailabilityType.UP);
+ }
+
+ @Test(dependsOnMethods = "testAvailability")
+ public void testMeasurement() throws Exception {
+ MeasurementReport report = new MeasurementReport();
+ Set<MeasurementScheduleRequest> metrics = new
HashSet<MeasurementScheduleRequest>();
+ int scheduleId = 1;
+ metrics.add(new MeasurementScheduleRequest(scheduleId++, "ipAddress",
1000, true, DataType.TRAIT));
+ metrics.add(new MeasurementScheduleRequest(scheduleId++, "hostName",
1000, true, DataType.TRAIT));
+ metrics.add(new MeasurementScheduleRequest(scheduleId++, "connectTime",
1000, true, DataType.MEASUREMENT));
+ portNetServiceComponent.getValues(report, metrics);
+ Map<String, Object> datas = new HashMap<String, Object>();
+ for (MeasurementData data : report.getNumericData()) {
+ datas.put(data.getName(), data.getValue());
+ }
+ for (MeasurementData data : report.getTraitData()) {
+ datas.put(data.getName(), data.getValue());
+ }
+ assertEquals(getTrait(datas, "ipAddress"), LOOPBACK);
+ assertTrue(StringUtil.isNotBlank(getTrait(datas, "hostName")));
+ assertTrue(getMetric(datas, "connectTime") >= 0);
+ }
+
+}