@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://success1.localhost/core/examples");
    URI successUri2 = URI.create("http://success2.localhost/core/examples");
    URI successUri3 = URI.create("http://success3.localhost/core/examples");
    URI failureUri = URI.create("http://fail.localhost/core/examples");
    URI successNodeGroup1 = URI.create("http://success1.localhost/core/node-groups/default");
    URI successNodeGroup2 = URI.create("http://success2.localhost/core/node-groups/default");
    URI successNodeGroup3 = URI.create("http://success3.localhost/core/node-groups/default");
    URI failureNodeGroup = URI.create("http://fail.localhost/core/node-groups/default");
    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());
    // success1
    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);
    // success2
    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);
    // success3
    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);
    // failure
    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);
}