@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_getServersStatistics_all() 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.getDsoAttributes(any(String[].class)))
        .thenReturn(
            new HashMap<String, Object>() {
              {
                put("stat1", "val1");
                put("stat2", "val2");
                put("stat3", "val3");
              }
            });

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

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

    assertThat(response.size(), is(1));
    StatisticsEntity entity = response.iterator().next();
    assertThat(entity.getSourceId(), equalTo("s1"));
    assertThat(
        entity.getStatistics(),
        CoreMatchers.<Map<String, Object>>equalTo(
            new HashMap<String, Object>() {
              {
                put("stat1", "val1");
                put("stat2", "val2");
                put("stat3", "val3");
              }
            }));

    verify(remoteManagementSource)
        .getFromRemoteL2(
            eq("s2"),
            eq(new URI("tc-management-api/agents/statistics/servers;names=s2")),
            eq(Collection.class),
            eq(StatisticsEntity.class));
    verify(remoteManagementSource)
        .getFromRemoteL2(
            eq("s3"),
            eq(new URI("tc-management-api/agents/statistics/servers;names=s3")),
            eq(Collection.class),
            eq(StatisticsEntity.class));
  }
  @Test
  public void test_getBackupsStatus() 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.getBackupStatuses())
        .thenReturn(
            new HashMap<String, String>() {
              {
                put("backup1", "OK");
              }
            });

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

    Collection<BackupEntity> backupsStatus =
        serverManagementService.getBackupsStatus(
            new HashSet<String>(Arrays.asList("s1", "s2", "s3")));
    assertThat(backupsStatus.size(), is(1));
    BackupEntity entity = backupsStatus.iterator().next();
    assertThat(entity.getName(), equalTo("backup1"));
    assertThat(entity.getStatus(), equalTo("OK"));
    assertThat(entity.getSourceId(), equalTo("s1"));
    assertThat(entity.getError(), is((Object) null));

    verify(remoteManagementSource)
        .getFromRemoteL2(
            eq("s2"),
            eq(new URI("tc-management-api/agents/backups;serverNames=s2")),
            eq(Collection.class),
            eq(BackupEntity.class));
    verify(remoteManagementSource)
        .getFromRemoteL2(
            eq("s3"),
            eq(new URI("tc-management-api/agents/backups;serverNames=s3")),
            eq(Collection.class),
            eq(BackupEntity.class));
  }
  @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"));
    }
  }
  @Test
  public void test_serversThreadDump_all() 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.serverThreadDump()).thenReturn("thread dump");

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

    Collection<ThreadDumpEntity> response =
        serverManagementService.serversThreadDump(
            new HashSet<String>(Arrays.asList("s1", "s2", "s3")));
    assertThat(response.size(), is(1));
    ThreadDumpEntity entity = response.iterator().next();
    assertThat(entity.getSourceId(), equalTo("s1"));
    assertThat(entity.getDump(), equalTo("thread dump"));
    assertThat(entity.getNodeType(), equalTo(ThreadDumpEntity.NodeType.SERVER));

    verify(remoteManagementSource)
        .getFromRemoteL2(
            eq("s2"),
            eq(new URI("tc-management-api/agents/diagnostics/threadDump/servers;names=s2")),
            eq(Collection.class),
            eq(ThreadDumpEntity.class));
    verify(remoteManagementSource)
        .getFromRemoteL2(
            eq("s3"),
            eq(new URI("tc-management-api/agents/diagnostics/threadDump/servers;names=s3")),
            eq(Collection.class),
            eq(ThreadDumpEntity.class));
  }