@Test
public void testInitFilter() {

  // Check PROXY_ADDRESS
  MockFilterContainer con = new MockFilterContainer();
  Configuration conf = new Configuration(false);
  conf.set(YarnConfiguration.PROXY_ADDRESS, "host1:1000");
  AmFilterInitializer afi = new MockAmFilterInitializer();
  assertNull(con.givenParameters);
  afi.initFilter(con, conf);
  assertEquals(2, con.givenParameters.size());
  assertEquals("host1", con.givenParameters.get(AmIpFilter.PROXY_HOSTS));
  assertEquals("http://host1:1000/foo",
      con.givenParameters.get(AmIpFilter.PROXY_URI_BASES));
  // Check a single RM_WEBAPP_ADDRESS
  con = new MockFilterContainer();
  conf = new Configuration(false);
  conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS, "host2:2000");
  afi = new MockAmFilterInitializer();
  assertNull(con.givenParameters);
  afi.initFilter(con, conf);
  assertEquals(2, con.givenParameters.size());
  assertEquals("host2", con.givenParameters.get(AmIpFilter.PROXY_HOSTS));
  assertEquals("http://host2:2000/foo",
      con.givenParameters.get(AmIpFilter.PROXY_URI_BASES));
  // Check multiple RM_WEBAPP_ADDRESSes (RM HA)
  con = new MockFilterContainer();
  conf = new Configuration(false);
  conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);
  conf.set(YarnConfiguration.RM_HA_IDS, "rm1,rm2,rm3");
  conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS + ".rm1", "host2:2000");
  conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS + ".rm2", "host3:3000");
  conf.set(YarnConfiguration.RM_WEBAPP_ADDRESS + ".rm3", "host4:4000");
  afi = new MockAmFilterInitializer();
  assertNull(con.givenParameters);
  afi.initFilter(con, conf);
  assertEquals(2, con.givenParameters.size());
  String[] proxyHosts = con.givenParameters.get(AmIpFilter.PROXY_HOSTS)
      .split(AmIpFilter.PROXY_HOSTS_DELIMITER);
  assertEquals(3, proxyHosts.length);
  Arrays.sort(proxyHosts);
  assertEquals("host2", proxyHosts[0]);
  assertEquals("host3", proxyHosts[1]);
  assertEquals("host4", proxyHosts[2]);
  String[] proxyBases = con.givenParameters.get(AmIpFilter.PROXY_URI_BASES)
      .split(AmIpFilter.PROXY_URI_BASES_DELIMITER);
  assertEquals(3, proxyBases.length);
  Arrays.sort(proxyBases);
  assertEquals("http://host2:2000/foo", proxyBases[0]);
  assertEquals("http://host3:3000/foo", proxyBases[1]);
  assertEquals("http://host4:4000/foo", proxyBases[2]);
  // Check multiple RM_WEBAPP_ADDRESSes (RM HA) with HTTPS
  con = new MockFilterContainer();
  conf = new Configuration(false);
  conf.set(YarnConfiguration.YARN_HTTP_POLICY_KEY,
      HttpConfig.Policy.HTTPS_ONLY.toString());
  conf.setBoolean(YarnConfiguration.RM_HA_ENABLED, true);
  conf.set(YarnConfiguration.RM_HA_IDS, "rm1,rm2");
  conf.set(YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS + ".rm1", "host5:5000");
  conf.set(YarnConfiguration.RM_WEBAPP_HTTPS_ADDRESS + ".rm2", "host6:6000");
  afi = new MockAmFilterInitializer();
  assertNull(con.givenParameters);
  afi.initFilter(con, conf);
  assertEquals(2, con.givenParameters.size());
  proxyHosts = con.givenParameters.get(AmIpFilter.PROXY_HOSTS)
      .split(AmIpFilter.PROXY_HOSTS_DELIMITER);
  assertEquals(2, proxyHosts.length);
  Arrays.sort(proxyHosts);
  assertEquals("host5", proxyHosts[0]);
  assertEquals("host6", proxyHosts[1]);
  proxyBases = con.givenParameters.get(AmIpFilter.PROXY_URI_BASES)
      .split(AmIpFilter.PROXY_URI_BASES_DELIMITER);
  assertEquals(2, proxyBases.length);
  Arrays.sort(proxyBases);
  assertEquals("https://host5:5000/foo", proxyBases[0]);
  assertEquals("https://host6:6000/foo", proxyBases[1]);
}