PortServiceStatusProvider extends ServiceProvider { public Map<String, Boolean> getServiceStatus(List<ComponentService> services) { Map<String, Boolean> processStatus = new HashMap<String, Boolean>(); for (ComponentService service : services) { String processName = service.getName(); String port = service.getParameters().get("port"); Result rs = new Result(); try { String[] cmd = { "/bin/sh", "-c", "netstat -nl | grep ':" + port + " '" }; rs = CommandExecutor.executeCommand(cmd); } catch (Exception e) { LOGGER.error(e.getMessage(), e); processStatus.put(service.getRole(), false); } processStatus.put(service.getRole(), false); if (rs.getExitVal() == 0) { processStatus.put(service.getRole(), true); } LOGGER.info(processName + ":" + port + " status - " + processStatus); } return processStatus; }  Map<String, Boolean> getServiceStatus(List<ComponentService> services);  }
@Test public void testGetServiceStatusEmptyServiceList() throws Exception { PortServiceStatusProvider fixture = new PortServiceStatusProvider(); List<ComponentService> services = new LinkedList(); Map<String, Boolean> result = fixture.getServiceStatus(services); assertNotNull(result); assertEquals(0, result.size()); }
@Test public void testGetSSHDServiceStatus() throws Exception { List<ComponentService> services = new LinkedList(); ComponentService cs = new ComponentService(PROCESS_NAME, SSHD, PORT); List<Parameter> parameters = new ArrayList<Parameter>(); parameters.add(new Parameter(PORT, PORT_22)); parameters.add(new Parameter(PROCESS_NAME, SSHD)); cs.setParams(parameters); services.add(cs); Map<String, Boolean> result = fixture.getServiceStatus(services); assertNotNull(result); assertEquals(1, result.size()); assertNotNull(result.get(SSHD)); }