NodeGroupUtils { public static NodeGroupBroadcastResult toBroadcastResult(NodeGroupBroadcastResponse response) { NodeGroupBroadcastResult result = new NodeGroupBroadcastResult(); result.totalNodeCount = response.nodeCount; result.availableNodeCount = response.availableNodeCount; result.unavailableNodeCount = response.nodeCount - response.availableNodeCount; result.membershipQuorum = response.membershipQuorum; Map<URI, String> hostIdByUrl = response.selectedNodes.entrySet().stream() .collect(toMap(entry -> { URI uri = entry.getValue(); return URI.create(uri.toString().replace(uri.getPath(), "")); }, Map.Entry::getKey)); for (Entry<URI, String> entry : response.jsonResponses.entrySet()) { PeerNodeResult singleResult = new PeerNodeResult(); URI requestUri = entry.getKey(); String json = entry.getValue(); URI hostUri = URI.create(requestUri.toString().replace(requestUri.getPath(), "")); String hostId = hostIdByUrl.get(hostUri); URI nodeGroupUri = response.selectedNodes.get(hostId); singleResult.requestUri = requestUri; singleResult.hostId = hostId; singleResult.nodeGroupUri = nodeGroupUri; singleResult.json = json; singleResult.errorResponse = null; result.allResponses.add(singleResult); result.successResponses.add(singleResult); } for (Entry<URI, ServiceErrorResponse> entry : response.failures.entrySet()) { PeerNodeResult singleResult = new PeerNodeResult(); URI requestUri = entry.getKey(); ServiceErrorResponse errorResponse = entry.getValue(); URI hostUri = URI.create(requestUri.toString().replace(requestUri.getPath(), "")); String hostId = hostIdByUrl.get(hostUri); URI nodeGroupUri = response.selectedNodes.get(hostId); singleResult.requestUri = requestUri; singleResult.hostId = hostId; singleResult.nodeGroupUri = nodeGroupUri; singleResult.json = null; singleResult.errorResponse = errorResponse; result.allResponses.add(singleResult); result.failureResponses.add(singleResult); result.failureErrorResponses.add(errorResponse); } return result; } private  NodeGroupUtils(); static void checkServiceAvailability(CompletionHandler ch, Service s); static void checkServiceAvailability(CompletionHandler ch, ServiceHost host,
            String link, String selectorPath); static void checkServiceAvailability(CompletionHandler ch, ServiceHost host,
            URI service,
            String selectorPath); static void checkConvergenceFromAnyHost(ServiceHost host, NodeGroupState ngs,
            Operation parentOp); static void checkConvergence(ServiceHost host, NodeGroupState ngs, Operation parentOp); static void checkConvergence(ServiceHost host, URI nodegroupReference, Operation parentOp); static boolean isMembershipSettled(ServiceHost host, long maintIntervalMicros,
            NodeGroupState localState); static boolean hasMembershipQuorum(ServiceHost host, NodeGroupState ngs); static boolean isNodeGroupAvailable(ServiceHost host, NodeGroupState localState); static void registerForReplicatedServiceAvailability(ServiceHost host, Operation op,
            String servicePath, String nodeSelectorPath); static NodeGroupBroadcastResult toBroadcastResult(NodeGroupBroadcastResponse response);  }
@Test public void toBroadcastResult() { String hostSuccess1 = "host-success1"; String hostSuccess2 = "host-success2"; String hostSuccess3 = "host-success3"; String hostFailure = "host-fail"; URI successUri1 = URI.create("http: URI successUri2 = URI.create("http: URI successUri3 = URI.create("http: URI failureUri = URI.create("http: URI successNodeGroup1 = URI.create("http: URI successNodeGroup2 = URI.create("http: URI successNodeGroup3 = URI.create("http: URI failureNodeGroup = URI.create("http: ServiceDocument document = new ServiceDocument(); document.documentOwner = hostSuccess1; String jsonForHostSuccess1 = Utils.toJson(document); document.documentOwner = hostSuccess2; String jsonForHostSuccess2 = Utils.toJson(document); document.documentOwner = hostSuccess3; String jsonForHostSuccess3 = Utils.toJson(document); ServiceErrorResponse errorResponse = new ServiceErrorResponse(); NodeGroupBroadcastResponse seed = new NodeGroupBroadcastResponse(); seed.nodeCount = 5; seed.availableNodeCount = 4; seed.membershipQuorum = 2; seed.receivers.add(successUri1); seed.receivers.add(successUri2); seed.receivers.add(successUri3); seed.receivers.add(failureUri); seed.selectedNodes.put(hostSuccess1, successNodeGroup1); seed.selectedNodes.put(hostSuccess2, successNodeGroup2); seed.selectedNodes.put(hostSuccess3, successNodeGroup3); seed.selectedNodes.put(hostFailure, failureNodeGroup); seed.jsonResponses.put(successUri1, jsonForHostSuccess1); seed.jsonResponses.put(successUri2, jsonForHostSuccess2); seed.jsonResponses.put(successUri3, jsonForHostSuccess3); seed.failures.put(failureUri, errorResponse); NodeGroupBroadcastResult result = NodeGroupUtils.toBroadcastResult(seed); assertEquals(5, result.totalNodeCount); assertEquals(4, result.availableNodeCount); assertEquals(1, result.unavailableNodeCount); assertEquals(2, result.membershipQuorum); assertTrue(result.hasSuccess()); assertTrue(result.hasFailure()); assertTrue(result.isMajoritySuccess()); assertFalse(result.isAllSuccess()); assertFalse(result.isAllFailure()); assertFalse(result.isMajorityFailure()); List<PeerNodeResult> successes = result.successResponses; assertEquals(3, successes.size()); List<PeerNodeResult> sorted = successes.stream() .sorted(Comparator.comparing(peerNodeResult -> peerNodeResult.hostId)) .collect(toList()); PeerNodeResult singleResponse = sorted.get(0); assertTrue(singleResponse.isSuccess()); assertFalse(singleResponse.isFailure()); assertEquals(hostSuccess1, singleResponse.hostId); assertEquals(successUri1, singleResponse.requestUri); assertEquals(successNodeGroup1, singleResponse.nodeGroupUri); assertEquals(jsonForHostSuccess1, singleResponse.json); assertNull(singleResponse.errorResponse); singleResponse = sorted.get(1); assertTrue(singleResponse.isSuccess()); assertFalse(singleResponse.isFailure()); assertEquals(hostSuccess2, singleResponse.hostId); assertEquals(successUri2, singleResponse.requestUri); assertEquals(successNodeGroup2, singleResponse.nodeGroupUri); assertEquals(jsonForHostSuccess2, singleResponse.json); assertNull(singleResponse.errorResponse); singleResponse = sorted.get(2); assertTrue(singleResponse.isSuccess()); assertFalse(singleResponse.isFailure()); assertEquals(hostSuccess3, singleResponse.hostId); assertEquals(successUri3, singleResponse.requestUri); assertEquals(successNodeGroup3, singleResponse.nodeGroupUri); assertEquals(jsonForHostSuccess3, singleResponse.json); assertNull(singleResponse.errorResponse); List<PeerNodeResult> failures = result.failureResponses; assertEquals(1, failures.size()); PeerNodeResult failure = failures.iterator().next(); assertTrue(failure.isFailure()); assertEquals(hostFailure, failure.hostId); assertEquals(failureUri, failure.requestUri); assertEquals(failureNodeGroup, failure.nodeGroupUri); assertNull(failure.json); assertSame(errorResponse, failure.errorResponse); }