@Test
  public void testProxyClientRequest_noActiveInCoordinatorGroupThrowsHttp400() throws Exception {
    LocalManagementSource localManagementSource = mock(LocalManagementSource.class);
    TimeoutServiceImpl timeoutService = new TimeoutServiceImpl(1000L);
    DfltSecurityContextService securityContextService = new DfltSecurityContextService();
    RemoteManagementSource remoteManagementSource =
        spy(new RemoteManagementSource(localManagementSource, timeoutService));

    when(localManagementSource.getServerGroupInfos())
        .thenReturn(
            new ServerGroupInfo[] {
              new ServerGroupInfo(new L2Info[] {L2_INFOS[0], L2_INFOS[1]}, "group0", 0, true),
              new ServerGroupInfo(new L2Info[] {L2_INFOS[2]}, "group1", 1, false),
            });

    ServerManagementService serverManagementService =
        new ServerManagementService(
            executorService,
            timeoutService,
            localManagementSource,
            remoteManagementSource,
            securityContextService);

    try {
      serverManagementService.proxyClientRequest();
      fail("expected WebApplicationException");
    } catch (WebApplicationException wae) {
      assertThat(wae.getResponse().getStatus(), is(400));
    }
  }
  @Test
  public void test_getServerGroups() throws Exception {
    LocalManagementSource localManagementSource = mock(LocalManagementSource.class);
    TimeoutServiceImpl timeoutService = new TimeoutServiceImpl(1000L);
    DfltSecurityContextService securityContextService = new DfltSecurityContextService();
    RemoteManagementSource remoteManagementSource =
        spy(new RemoteManagementSource(localManagementSource, timeoutService));

    when(localManagementSource.getL2Infos()).thenReturn(L2_INFOS);
    when(localManagementSource.getLocalServerName()).thenReturn("s1");
    when(localManagementSource.isActiveCoordinator()).thenReturn(true);
    when(localManagementSource.getServerGroupInfos())
        .thenReturn(
            new ServerGroupInfo[] {
              new ServerGroupInfo(new L2Info[] {L2_INFOS[0], L2_INFOS[1]}, "group0", 0, true),
              new ServerGroupInfo(new L2Info[] {L2_INFOS[2]}, "group1", 1, false),
            });

    ServerManagementService serverManagementService =
        new ServerManagementService(
            executorService,
            timeoutService,
            localManagementSource,
            remoteManagementSource,
            securityContextService);

    Collection<ServerGroupEntity> response =
        serverManagementService.getServerGroups(
            new HashSet<String>(Arrays.asList("s1", "s2", "s3")));

    assertThat(response.size(), is(1));
    ServerGroupEntity entity = response.iterator().next();
    assertThat(entity.getName(), equalTo("group0"));
    assertThat(entity.getId(), equalTo(0));
    assertThat(entity.isCoordinator(), equalTo(true));

    verify(remoteManagementSource)
        .getFromRemoteL2(
            eq("s2"),
            eq(new URI("tc-management-api/agents/topologies/servers;names=s2")),
            eq(Collection.class),
            eq(TopologyEntity.class));
    verify(remoteManagementSource)
        .getFromRemoteL2(
            eq("s3"),
            eq(new URI("tc-management-api/agents/topologies/servers;names=s3")),
            eq(Collection.class),
            eq(TopologyEntity.class));
  }
  @Test
  public void testProxyClientRequest_throwsProxyException() throws Exception {
    LocalManagementSource localManagementSource = mock(LocalManagementSource.class);
    TimeoutServiceImpl timeoutService = new TimeoutServiceImpl(1000L);
    DfltSecurityContextService securityContextService = new DfltSecurityContextService();
    RemoteManagementSource remoteManagementSource =
        spy(new RemoteManagementSource(localManagementSource, timeoutService));

    when(localManagementSource.getLocalServerName()).thenReturn("s2");
    when(localManagementSource.getServerGroupInfos())
        .thenReturn(
            new ServerGroupInfo[] {
              new ServerGroupInfo(new L2Info[] {L2_INFOS[0], L2_INFOS[1]}, "group0", 0, true),
              new ServerGroupInfo(new L2Info[] {L2_INFOS[2]}, "group1", 1, false),
            });

    when(localManagementSource.getServerUrls())
        .thenReturn(
            new HashMap<String, String>() {
              {
                put("s1", "http://s1");
                put("s3", "http://s3");
              }
            });

    doReturn(createTopologySingleServerResponse(true, "s1", "ACTIVE-COORDINATOR"))
        .when(remoteManagementSource)
        .getFromRemoteL2(eq("s1"), any(URI.class), eq(Collection.class), eq(TopologyEntity.class));
    doReturn(createTopologySingleServerResponse(true, "s3", "PASSIVE_STANDBY"))
        .when(remoteManagementSource)
        .getFromRemoteL2(eq("s3"), any(URI.class), eq(Collection.class), eq(TopologyEntity.class));

    ServerManagementService serverManagementService =
        new ServerManagementService(
            executorService,
            timeoutService,
            localManagementSource,
            remoteManagementSource,
            securityContextService);

    try {
      serverManagementService.proxyClientRequest();
      fail("expected ProxyException");
    } catch (ProxyException pe) {
      assertThat(pe.getActiveL2WithMBeansUrl(), equalTo("http://s1"));
    }
  }