public static GetAllResourceProfilesResponse mergeClusterResourceProfilesResponse(
      Collection<GetAllResourceProfilesResponse> responses) {
    GetAllResourceProfilesResponse profilesResponse =
        Records.newRecord(GetAllResourceProfilesResponse.class);
    Map<String, Resource> profilesMap = new HashMap<>();
    for (GetAllResourceProfilesResponse response : responses) {
      if (response != null && response.getResourceProfiles() != null) {
        for (Map.Entry<String, Resource> entry : response.getResourceProfiles().entrySet()) {
          String key = entry.getKey();
          Resource r1 = profilesMap.getOrDefault(key, null);
          Resource r2 = entry.getValue();
          Resource rAdd = r1 == null ? r2 : Resources.add(r1, r2);
          profilesMap.put(key, rAdd);
        }
      }
    }
    profilesResponse.setResourceProfiles(profilesMap);
    return profilesResponse;
  }
@Test
  public void testMergeResourceProfiles() {
    // normal response1
    Map<String, Resource> profiles = new HashMap<>();
    Resource resource1 = Resource.newInstance(1024, 1);
    GetAllResourceProfilesResponse response1 = GetAllResourceProfilesResponse.newInstance();
    profiles.put("maximum", resource1);
    response1.setResourceProfiles(profiles);

    // normal response2
    profiles = new HashMap<>();
    Resource resource2 = Resource.newInstance(2048, 2);
    GetAllResourceProfilesResponse response2 = GetAllResourceProfilesResponse.newInstance();
    profiles.put("maximum", resource2);
    response2.setResourceProfiles(profiles);

    // empty response
    GetAllResourceProfilesResponse response3 = GetAllResourceProfilesResponse.newInstance();

    // null response
    GetAllResourceProfilesResponse response4 = null;

    List<GetAllResourceProfilesResponse> responses = new ArrayList<>();
    responses.add(response1);
    responses.add(response2);
    responses.add(response3);
    responses.add(response4);

    GetAllResourceProfilesResponse response =
        RouterYarnClientUtils.mergeClusterResourceProfilesResponse(responses);
    Resource resource = response.getResourceProfiles().get("maximum");
    Assert.assertEquals(3, resource.getVirtualCores());
    Assert.assertEquals(3072, resource.getMemorySize());
  }