ClusterResource { @GET public Response getClusterList(@QueryParam("seedHost") Optional<String> seedHost) { LOG.debug("get cluster list called"); Collection<String> clusters = context.storage.getClusters() .stream() .filter(c -> !seedHost.isPresent() || c.getSeedHosts().contains(seedHost.get())) .sorted() .map(c -> c.getName()) .collect(Collectors.toList()); return Response.ok().entity(clusters).build(); } ClusterResource(AppContext context, Cryptograph cryptograph, ExecutorService executor); @GET Response getClusterList(@QueryParam("seedHost") Optional<String> seedHost); @GET @Path("/{cluster_name}") Response getCluster(
      @PathParam("cluster_name") String clusterName,
      @QueryParam("limit") Optional<Integer> limit); @GET @Path("/{cluster_name}/tables") Response getClusterTables(@PathParam("cluster_name") String clusterName); @POST Response addOrUpdateCluster(
      @Context UriInfo uriInfo,
      @QueryParam("seedHost") Optional<String> seedHost,
      @QueryParam("jmxPort") Optional<Integer> jmxPort); @POST @Path("/auth") Response addOrUpdateCluster(
      @Context UriInfo uriInfo,
      @FormParam("seedHost") Optional<String> seedHost,
      @FormParam("jmxPort") Optional<Integer> jmxPort,
      @FormParam("jmxUsername") Optional<String> jmxUsername,
      @FormParam("jmxPassword") Optional<String> jmxPassword); @PUT @Path("/{cluster_name}") Response addOrUpdateCluster(
      @Context UriInfo uriInfo,
      @PathParam("cluster_name") String clusterName,
      @QueryParam("seedHost") Optional<String> seedHost,
      @QueryParam("jmxPort") Optional<Integer> jmxPort); @PUT @Path("/auth/{cluster_name}") Response addOrUpdateCluster(
      @Context UriInfo uriInfo,
      @PathParam("cluster_name") String clusterName,
      @FormParam("seedHost") Optional<String> seedHost,
      @FormParam("jmxPort") Optional<Integer> jmxPort,
      @FormParam("jmxUsername") Optional<String> jmxUsername,
      @FormParam("jmxPassword") Optional<String> jmxPassword); Optional<Cluster> findClusterWithSeedHost(String seedHost,
                                                   Optional<Integer> jmxPort,
                                                   Optional<JmxCredentials> jmxCredentials); @DELETE @Path("/{cluster_name}") Response deleteCluster(
      @PathParam("cluster_name") String clusterName,
      @QueryParam("force") Optional<Boolean> force);  }
@Test public void testGetClusters_all() throws ReaperException { final MockObjects mocks = initMocks(); Cluster cluster = Cluster.builder() .withName(CLUSTER_NAME) .withSeedHosts(ImmutableSet.of(SEED_HOST)) .withState(Cluster.State.ACTIVE) .build(); mocks.context.storage.addCluster(cluster); ClusterResource clusterResource = new ClusterResource(mocks.context, mocks.cryptograph, EXECUTOR); Response response = clusterResource.getClusterList(Optional.empty()); Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); List<String> clusterNames = (List<String>) response.getEntity(); Assertions.assertThat(clusterNames).hasSize(1); Assertions.assertThat(clusterNames).contains(CLUSTER_NAME); }
@Test public void testGetClusters_specified() throws ReaperException { final MockObjects mocks = initMocks(); Cluster cluster = Cluster.builder() .withName(CLUSTER_NAME) .withSeedHosts(ImmutableSet.of(SEED_HOST)) .withState(Cluster.State.ACTIVE) .build(); mocks.context.storage.addCluster(cluster); ClusterResource clusterResource = new ClusterResource(mocks.context, mocks.cryptograph, EXECUTOR); Response response = clusterResource.getClusterList(Optional.of(SEED_HOST)); Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); List<String> clusterNames = (List<String>) response.getEntity(); Assertions.assertThat(clusterNames).hasSize(1); Assertions.assertThat(clusterNames).contains(CLUSTER_NAME); }
@Test public void testGetClusters_only_specified_first() throws ReaperException { final MockObjects mocks = initMocks(); Cluster cluster = Cluster.builder() .withName(CLUSTER_NAME) .withSeedHosts(ImmutableSet.of(SEED_HOST)) .withState(Cluster.State.ACTIVE) .build(); mocks.context.storage.addCluster(cluster); cluster = Cluster.builder() .withName("cluster2") .withSeedHosts(ImmutableSet.of("host2")) .withState(Cluster.State.ACTIVE) .build(); mocks.context.storage.addCluster(cluster); ClusterResource clusterResource = new ClusterResource(mocks.context, mocks.cryptograph, EXECUTOR); Response response = clusterResource.getClusterList(Optional.of(SEED_HOST)); Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); List<String> clusterNames = (List<String>) response.getEntity(); Assertions.assertThat(clusterNames).hasSize(1); Assertions.assertThat(clusterNames).contains(CLUSTER_NAME); }
@Test public void testGetClusters_only_specified_second() throws ReaperException { final MockObjects mocks = initMocks(); Cluster cluster = Cluster.builder() .withName(CLUSTER_NAME) .withSeedHosts(ImmutableSet.of(SEED_HOST)) .withState(Cluster.State.ACTIVE) .build(); mocks.context.storage.addCluster(cluster); cluster = Cluster.builder() .withName("cluster2") .withSeedHosts(ImmutableSet.of("host2")) .withState(Cluster.State.ACTIVE) .build(); mocks.context.storage.addCluster(cluster); ClusterResource clusterResource = new ClusterResource(mocks.context, mocks.cryptograph, EXECUTOR); Response response = clusterResource.getClusterList(Optional.of("host2")); Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); List<String> clusterNames = (List<String>) response.getEntity(); Assertions.assertThat(clusterNames).hasSize(1); Assertions.assertThat(clusterNames).contains("cluster2"); }
@Test public void testGetClusters_multiple() throws ReaperException { final MockObjects mocks = initMocks(); Cluster cluster = Cluster.builder() .withName(CLUSTER_NAME) .withSeedHosts(ImmutableSet.of(SEED_HOST)) .withState(Cluster.State.ACTIVE) .build(); mocks.context.storage.addCluster(cluster); cluster = Cluster.builder() .withName("cluster2") .withSeedHosts(ImmutableSet.of("host2")) .withState(Cluster.State.ACTIVE) .build(); mocks.context.storage.addCluster(cluster); ClusterResource clusterResource = new ClusterResource(mocks.context, mocks.cryptograph, EXECUTOR); Response response = clusterResource.getClusterList(Optional.empty()); Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); List<String> clusterNames = (List<String>) response.getEntity(); Assertions.assertThat(clusterNames).hasSize(2); Assertions.assertThat(clusterNames).contains(CLUSTER_NAME, "cluster2"); }
@Test public void testGetClusters_multiple_ordered_by_name() throws ReaperException { final MockObjects mocks = initMocks(); Cluster cluster = Cluster.builder() .withName(CLUSTER_NAME) .withSeedHosts(ImmutableSet.of(SEED_HOST)) .withState(Cluster.State.ACTIVE) .build(); mocks.context.storage.addCluster(cluster); cluster = Cluster.builder() .withName("abc") .withSeedHosts(ImmutableSet.of("host2")) .withState(Cluster.State.ACTIVE) .build(); mocks.context.storage.addCluster(cluster); ClusterResource clusterResource = new ClusterResource(mocks.context, mocks.cryptograph, EXECUTOR); Response response = clusterResource.getClusterList(Optional.empty()); Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); List<String> clusterNames = (List<String>) response.getEntity(); Assertions.assertThat(clusterNames).hasSize(2); Assertions.assertThat(clusterNames).containsExactly("abc", CLUSTER_NAME); }
@Test public void testGetClusters_multiple_ordered_by_state() throws ReaperException { final MockObjects mocks = initMocks(); Cluster cluster = Cluster.builder() .withName(CLUSTER_NAME) .withSeedHosts(ImmutableSet.of(SEED_HOST)) .withState(Cluster.State.UNREACHABLE) .build(); mocks.context.storage.addCluster(cluster); cluster = Cluster.builder() .withName("cluster2") .withSeedHosts(ImmutableSet.of("host2")) .withState(Cluster.State.ACTIVE) .build(); mocks.context.storage.addCluster(cluster); ClusterResource clusterResource = new ClusterResource(mocks.context, mocks.cryptograph, EXECUTOR); Response response = clusterResource.getClusterList(Optional.empty()); Assertions.assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200); List<String> clusterNames = (List<String>) response.getEntity(); Assertions.assertThat(clusterNames).hasSize(2); Assertions.assertThat(clusterNames).containsExactly("cluster2", CLUSTER_NAME); }