@Test
  public void getTopologyLogClusterWhileAuthenticated() {
    final String requestTopologyId = "topology-test";
    final TopologyLogCriteria requestLogCriteria =
        RandomGenerator.randomObject(TopologyLogCriteria.class);
    final TopologyLogPage mockedLogPage = RandomGenerator.randomObject(TopologyLogPage.class);

    doReturn(mockedLogPage)
        .when(topologyServiceMock)
        .getTopologyLogCluster(requestTopologyId, TEST_SUBJECT_ID, requestLogCriteria);

    mockAuthenticatedSubject();

    TopologyLogPage responseLogPage =
        resource()
            .path("/api/topologies/" + requestTopologyId + "/log")
            .accept(MediaType.APPLICATION_JSON)
            .type(MediaType.APPLICATION_JSON)
            .post(TopologyLogPage.class, requestLogCriteria);

    assertEquals(
        "Response topology log should match the request topology log",
        mockedLogPage,
        responseLogPage);

    verify(topologyServiceMock)
        .getTopologyLogCluster(requestTopologyId, TEST_SUBJECT_ID, requestLogCriteria);
  }
  @Test
  public void updateTopologyConfigWhileAuthenticated() {
    final Topology mockedTopology = RandomGenerator.randomObject(Topology.class);
    final TopologyConfig requestTopologyConfig = RandomGenerator.randomObject(TopologyConfig.class);

    doReturn(mockedTopology)
        .when(topologyServiceMock)
        .getTopology(mockedTopology.getId(), TEST_SUBJECT_ID);
    doNothing()
        .when(topologyServiceMock)
        .updateTopology(mockedTopology.getId(), mockedTopology, TEST_SUBJECT_ID);

    mockAuthenticatedSubject();

    ClientResponse clientResponse =
        resource()
            .path("/api/topologies/" + mockedTopology.getId() + "/config")
            .type(MediaType.APPLICATION_JSON)
            .put(ClientResponse.class, requestTopologyConfig);

    assertEquals("Response HTTP status code should be 200 (OK)", clientResponse.getStatus(), 200);

    verify(topologyServiceMock).getTopology(mockedTopology.getId(), TEST_SUBJECT_ID);
    verify(topologyServiceMock)
        .updateTopology(mockedTopology.getId(), mockedTopology, TEST_SUBJECT_ID);
  }
  @Test
  public void listTopologiesWhileAuthenticated() {
    Topology topology1 = RandomGenerator.randomObject(Topology.class);
    topology1.setName("Topology First");

    Topology topology2 = RandomGenerator.randomObject(Topology.class);
    topology2.setName("Topology Second");

    Topology topology3 = RandomGenerator.randomObject(Topology.class);
    topology3.setName("Topology Third");

    List<Topology> mockedTopologies = new ArrayList<Topology>();
    mockedTopologies.add(topology1);
    mockedTopologies.add(topology2);
    mockedTopologies.add(topology3);

    doReturn(mockedTopologies).when(topologyServiceMock).listTopologies(TEST_SUBJECT_ID);

    mockAuthenticatedSubject();

    List<Topology> responseUsers =
        resource()
            .path("/api/topologies")
            .accept(MediaType.APPLICATION_JSON)
            .get(new GenericType<List<Topology>>() {});

    assertEquals(
        "Response topologies should be equal to the mocked topologies",
        mockedTopologies,
        responseUsers);

    verify(topologyServiceMock).listTopologies(TEST_SUBJECT_ID);
  }
  @Test
  public void getTopologyLogLocalWhileAuthenticated() {
    final String requestTopologyId = "topology-test";
    final TopologyLog mockedLog = RandomGenerator.randomObject(TopologyLog.class);

    doReturn(mockedLog)
        .when(topologyServiceMock)
        .getTopologyLogLocal(
            requestTopologyId, TEST_SUBJECT_ID, mockedLog.getOffset(), mockedLog.getCount());

    mockAuthenticatedSubject();

    TopologyLog responseLog =
        resource()
            .path("/api/topologies/" + requestTopologyId + "/log")
            .queryParam("offset", Long.toString(mockedLog.getOffset()))
            .queryParam("limit", Long.toString(mockedLog.getCount()))
            .accept(MediaType.APPLICATION_JSON)
            .get(TopologyLog.class);

    assertEquals(
        "Response topology log should match the request topology log", mockedLog, responseLog);

    verify(topologyServiceMock)
        .getTopologyLogLocal(
            requestTopologyId, TEST_SUBJECT_ID, mockedLog.getOffset(), mockedLog.getCount());
  }
  @Test
  public void getTopologyWhileAuthenticated() {
    final Topology mockedTopology = RandomGenerator.randomObject(Topology.class);

    doReturn(mockedTopology)
        .when(topologyServiceMock)
        .getTopology(mockedTopology.getId(), TEST_SUBJECT_ID);

    mockAuthenticatedSubject();

    Topology responseTopology =
        resource()
            .path("/api/topologies/" + mockedTopology.getId())
            .accept(MediaType.APPLICATION_JSON)
            .get(Topology.class);

    assertEquals(
        "Response topology should match the mocked topology", mockedTopology, responseTopology);

    verify(topologyServiceMock).getTopology(anyString(), anyString());
  }
  @Test
  public void createTopologyWhileAuthenticated() {
    final Topology requestTopology = RandomGenerator.randomObject(Topology.class);

    doReturn(requestTopology)
        .when(topologyServiceMock)
        .createTopology(requestTopology, TEST_SUBJECT_ID);

    mockAuthenticatedSubject();

    Topology responseTopology =
        resource()
            .path("/api/topologies")
            .accept(MediaType.APPLICATION_JSON)
            .type(MediaType.APPLICATION_JSON)
            .post(Topology.class, requestTopology);

    assertEquals(
        "Response topology should match the request topology", requestTopology, responseTopology);

    verify(topologyServiceMock).createTopology(requestTopology, TEST_SUBJECT_ID);
  }
  @Test
  public void submitTopologyWhileAuthenticated() {
    final String requestClusterId = "LOCAL";
    final String requestLogLevel = "INFO";
    final String requestClassLoaderPolicy = "FRAMEWORK_FIRST";
    final Topology mockedTopology = RandomGenerator.randomObject(Topology.class);

    doReturn(mockedTopology)
        .when(topologyServiceMock)
        .submitTopology(
            mockedTopology.getId(),
            TEST_SUBJECT_ID,
            requestClusterId,
            requestLogLevel,
            requestClassLoaderPolicy);

    mockAuthenticatedSubject();

    Topology responseTopology =
        resource()
            .path("/api/topologies/" + mockedTopology.getId() + "/submit")
            .queryParam("clusterId", requestClusterId)
            .accept(MediaType.APPLICATION_JSON)
            .get(Topology.class);

    assertEquals(
        "Response topology should match the mocked topology", mockedTopology, responseTopology);

    verify(topologyServiceMock)
        .submitTopology(
            mockedTopology.getId(),
            TEST_SUBJECT_ID,
            requestClusterId,
            requestLogLevel,
            requestClassLoaderPolicy);
  }
  @Test
  public void getTopologyInfoWhileAuthenticated() {
    final String requestTopologyId = "topology-test";
    final TopologyInfo mockedTopologyInfo = RandomGenerator.randomObject(TopologyInfo.class);

    doReturn(mockedTopologyInfo)
        .when(topologyServiceMock)
        .getTopologyInfo(requestTopologyId, TEST_SUBJECT_ID);

    mockAuthenticatedSubject();

    TopologyInfo responseTopologyInfo =
        resource()
            .path("/api/topologies/" + requestTopologyId + "/info")
            .accept(MediaType.APPLICATION_JSON)
            .get(TopologyInfo.class);

    assertEquals(
        "Response topology info should match the mocked topology info",
        mockedTopologyInfo,
        responseTopologyInfo);

    verify(topologyServiceMock).getTopologyInfo(requestTopologyId, TEST_SUBJECT_ID);
  }