private QueryFederationQueuePoliciesResponse filterPoliciesConfigurationsByQueues(
      List<String> queues, Map<String, SubClusterPolicyConfiguration> policiesConfigurations,
      int pageSize, int currentPage) throws YarnException {

    // Step1. Check the parameters, if the policy list is empty, return empty directly.
    if (MapUtils.isEmpty(policiesConfigurations)) {
      return null;
    }

    // Step2. Filtering for Queue Policies.
    List<FederationQueueWeight> federationQueueWeights = new ArrayList<>();
    for (String queue : queues) {
      SubClusterPolicyConfiguration policyConf = policiesConfigurations.getOrDefault(queue, null);
      if(policyConf == null) {
        continue;
      }
      FederationQueueWeight federationQueueWeight = parseFederationQueueWeight(queue, policyConf);
      if (federationQueueWeight != null) {
        federationQueueWeights.add(federationQueueWeight);
      }
    }

    // Step3. To paginate the returned results.
    return queryFederationQueuePoliciesPagination(federationQueueWeights, pageSize, currentPage);
  }
@Test
  public void testFilterPoliciesConfigurationsByQueues() throws Exception {
    // SubClusters : SC-1,SC-2
    List<String> subClusterLists = new ArrayList<>();
    subClusterLists.add("SC-1");
    subClusterLists.add("SC-2");

    // We initialize 26 queues, queue root.a~root.z
    List<FederationQueueWeight> federationQueueWeights = new ArrayList<>();
    for (char letter = 'a'; letter <= 'z'; letter++) {
      FederationQueueWeight leaf =
          generateFederationQueueWeight("root." + letter, subClusterLists);
      federationQueueWeights.add(leaf);
    }

    // Save Queue Policies in Batches
    BatchSaveFederationQueuePoliciesRequest request =
        BatchSaveFederationQueuePoliciesRequest.newInstance(federationQueueWeights);

    BatchSaveFederationQueuePoliciesResponse policiesResponse =
        interceptor.batchSaveFederationQueuePolicies(request);

    assertNotNull(policiesResponse);
    assertNotNull(policiesResponse.getMessage());
    assertEquals("batch save policies success.", policiesResponse.getMessage());

    // We query 12 queues, root.a ~ root.l
    List<String> queues = new ArrayList<>();
    for (char letter = 'a'; letter <= 'l'; letter++) {
      queues.add("root." + letter);
    }

    // Queue1: We query page 1, 10 items per page, and the returned result should be 10 items.
    // TotalPage should be 2, TotalSize should be 12.
    QueryFederationQueuePoliciesRequest request1 =
        QueryFederationQueuePoliciesRequest.newInstance(10, 1, "", queues);
    QueryFederationQueuePoliciesResponse response1 =
        interceptor.listFederationQueuePolicies(request1);
    assertNotNull(response1);
    assertEquals(1, response1.getCurrentPage());
    assertEquals(10, response1.getPageSize());
    assertEquals(2, response1.getTotalPage());
    assertEquals(12, response1.getTotalSize());
    List<FederationQueueWeight> federationQueueWeights1 = response1.getFederationQueueWeights();
    assertNotNull(federationQueueWeights1);
    assertEquals(10, federationQueueWeights1.size());

    // Queue2: We query page 1, 12 items per page, and the returned result should be 12 items.
    // TotalPage should be 1, TotalSize should be 12.
    QueryFederationQueuePoliciesRequest request2 =
        QueryFederationQueuePoliciesRequest.newInstance(12, 1, "", queues);
    QueryFederationQueuePoliciesResponse response2 =
        interceptor.listFederationQueuePolicies(request2);
    assertNotNull(response2);
    assertEquals(1, response2.getCurrentPage());
    assertEquals(12, response2.getPageSize());
    assertEquals(1, response2.getTotalPage());
    assertEquals(12, response2.getTotalSize());
    List<FederationQueueWeight> federationQueueWeights2 = response2.getFederationQueueWeights();
    assertNotNull(federationQueueWeights2);
    assertEquals(12, federationQueueWeights2.size());

    // Queue3: Boundary limit exceeded
    // We filter 12 queues, should return 12 records, 12 per page,
    // should return 1 page, but we are going to return page 2.
    QueryFederationQueuePoliciesRequest request3 =
        QueryFederationQueuePoliciesRequest.newInstance(12, 2, "", queues);
    QueryFederationQueuePoliciesResponse response3 =
        interceptor.listFederationQueuePolicies(request3);
    assertNotNull(response3);
    assertEquals(2, response3.getCurrentPage());
    assertEquals(12, response3.getPageSize());
    assertEquals(1, response2.getTotalPage());
    assertEquals(12, response3.getTotalSize());
    List<FederationQueueWeight> federationQueueWeights3 = response3.getFederationQueueWeights();
    assertNotNull(federationQueueWeights3);
    assertEquals(0, federationQueueWeights3.size());

    // Queue4: Boundary limit exceeded
    // We pass in some negative parameters and we will get some exceptions
    QueryFederationQueuePoliciesRequest request4 =
        QueryFederationQueuePoliciesRequest.newInstance(-1, 2, "", queues);
    LambdaTestUtils.intercept(YarnException.class, "PageSize cannot be negative or zero.",
        () -> interceptor.listFederationQueuePolicies(request4));

    // Queue5: Boundary limit exceeded
    // We pass in some negative parameters and we will get some exceptions
    QueryFederationQueuePoliciesRequest request5 =
        QueryFederationQueuePoliciesRequest.newInstance(10, -1, "", queues);
    LambdaTestUtils.intercept(YarnException.class, "CurrentPage cannot be negative or zero.",
        () -> interceptor.listFederationQueuePolicies(request5));

    // Queue6: We use Queue as the condition,
    // at this time we will only get the only one return value.
    QueryFederationQueuePoliciesRequest request6 =
        QueryFederationQueuePoliciesRequest.newInstance(10, 1, "root.a", null);
    QueryFederationQueuePoliciesResponse response6 =
        interceptor.listFederationQueuePolicies(request6);
    assertNotNull(response6);
    assertEquals(1, response6.getCurrentPage());
    assertEquals(10, response6.getPageSize());
    assertEquals(1, response6.getTotalPage());
    assertEquals(1, response6.getTotalSize());
    List<FederationQueueWeight> federationQueueWeights6 = response6.getFederationQueueWeights();
    assertNotNull(federationQueueWeights6);
    assertEquals(1, federationQueueWeights6.size());

    // Queue7: We design such a test case, we do not set any filter conditions,
    // but we need to get the return results
    QueryFederationQueuePoliciesRequest request7 =
        QueryFederationQueuePoliciesRequest.newInstance(10, 1, null, null);
    QueryFederationQueuePoliciesResponse response7 =
        interceptor.listFederationQueuePolicies(request7);
    assertNotNull(response7);
    assertEquals(1, response7.getCurrentPage());
    assertEquals(10, response7.getPageSize());
    assertEquals(3, response7.getTotalPage());
    assertEquals(26, response7.getTotalSize());
    List<FederationQueueWeight> federationQueueWeights7 = response7.getFederationQueueWeights();
    assertNotNull(federationQueueWeights7);
    assertEquals(10, federationQueueWeights7.size());

    // Queue8: We are designing a unit test where the number of records
    // we need to retrieve exceeds the maximum number of Policies available.
    QueryFederationQueuePoliciesRequest request8 =
        QueryFederationQueuePoliciesRequest.newInstance(10, 10, null, null);
    LambdaTestUtils.intercept(YarnException.class,
        "The index of the records to be retrieved has exceeded the maximum index.",
        () -> interceptor.listFederationQueuePolicies(request8));
  }