ServerStatisticsAPIImpl implements ServerStatisticsAPI { @Override public Collection<ServerBriefInfo> getAllServersBriefInfo() { Map<String, String> serverHostMap = new HashMap<>(); Collection<String> aliveServers = new ArrayList<>(); Collection<String> crashedServers = new ArrayList<>(); List<String> jobs = registryCenter.getChildrenKeys("/"); for (String jobName : jobs) { JobNodePath jobNodePath = new JobNodePath(jobName); List<String> servers = registryCenter.getChildrenKeys(jobNodePath.getServerNodePath()); for (String server : servers) { serverHostMap.put(server, registryCenter.get(jobNodePath.getServerNodePath(server, "hostName"))); if (!registryCenter.isExisted(jobNodePath.getServerNodePath(server, "shutdown")) && registryCenter.isExisted(jobNodePath.getServerNodePath(server, "status"))) { aliveServers.add(server); } else { crashedServers.add(server); } } } List<ServerBriefInfo> result = new ArrayList<>(serverHostMap.size()); for (Map.Entry<String, String> entry : serverHostMap.entrySet()) { result.add(getServerBriefInfo(aliveServers, crashedServers, entry.getKey(), entry.getValue())); } Collections.sort(result); return result; }  @Override Collection<ServerBriefInfo> getAllServersBriefInfo(); @Override Collection<ServerInfo> getJobs(final String serverIp);  }
@Test public void assertGetAllServersBriefInfo() { when(registryCenter.getChildrenKeys("/")).thenReturn(Arrays.asList("testJob1", "testJob2")); when(registryCenter.getChildrenKeys("/testJob1/servers")).thenReturn(Arrays.asList("ip1", "ip2")); when(registryCenter.getChildrenKeys("/testJob2/servers")).thenReturn(Arrays.asList("ip3", "ip4")); when(registryCenter.get("/testJob1/servers/ip1/hostName")).thenReturn("host1"); when(registryCenter.get("/testJob1/servers/ip2/hostName")).thenReturn("host2"); when(registryCenter.get("/testJob2/servers/ip3/hostName")).thenReturn("host3"); when(registryCenter.get("/testJob2/servers/ip4/hostName")).thenReturn("host4"); when(registryCenter.isExisted("/testJob1/servers/ip1/shutdown")).thenReturn(false); when(registryCenter.isExisted("/testJob1/servers/ip1/status")).thenReturn(true); when(registryCenter.isExisted("/testJob1/servers/ip2/shutdown")).thenReturn(true); when(registryCenter.isExisted("/testJob2/servers/ip3/shutdown")).thenReturn(false); when(registryCenter.isExisted("/testJob2/servers/ip3/status")).thenReturn(false); when(registryCenter.isExisted("/testJob2/servers/ip4/shutdown")).thenReturn(false); when(registryCenter.isExisted("/testJob2/servers/ip4/status")).thenReturn(true); int i = 0; for (ServerBriefInfo each : serverStatisticsAPI.getAllServersBriefInfo()) { i++; assertThat(each.getServerIp(), is("ip" + i)); assertThat(each.getServerHostName(), is("host" + i)); switch (i) { case 1: assertThat(each.getStatus(), is(ServerBriefInfo.ServerBriefStatus.OK)); break; case 2: assertThat(each.getStatus(), is(ServerBriefInfo.ServerBriefStatus.ALL_CRASHED)); break; case 3: assertThat(each.getStatus(), is(ServerBriefInfo.ServerBriefStatus.ALL_CRASHED)); break; case 4: assertThat(each.getStatus(), is(ServerBriefInfo.ServerBriefStatus.OK)); break; default: fail(); } } }