diff --git a/management-api-server/src/main/java/com/datastax/mgmtapi/resources/v2/RepairResourcesV2.java b/management-api-server/src/main/java/com/datastax/mgmtapi/resources/v2/RepairResourcesV2.java index 98e2559a..ba29a9ad 100644 --- a/management-api-server/src/main/java/com/datastax/mgmtapi/resources/v2/RepairResourcesV2.java +++ b/management-api-server/src/main/java/com/datastax/mgmtapi/resources/v2/RepairResourcesV2.java @@ -13,12 +13,14 @@ import com.datastax.mgmtapi.resources.v2.models.RingRange; import com.datastax.oss.driver.api.core.cql.ResultSet; import com.datastax.oss.driver.api.core.cql.Row; +import com.google.common.annotations.VisibleForTesting; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.ExampleObject; import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import java.util.List; +import java.util.stream.Collectors; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.PUT; @@ -122,16 +124,12 @@ private String getParallelismName(RepairParallelism parallelism) { return parallelism != null ? parallelism.getName() : null; } - private String getRingRangeString(List associatedTokens) { - if (associatedTokens != null && !associatedTokens.isEmpty()) { - StringBuilder sb = new StringBuilder(); - for (RingRange ringRange : associatedTokens) { - sb.append(toRangeString(ringRange)).append(","); - } - // remove trailing comma - return sb.substring(0, sb.length() - 2); + @VisibleForTesting + String getRingRangeString(List associatedTokens) { + if (associatedTokens == null || associatedTokens.isEmpty()) { + return null; } - return null; + return associatedTokens.stream().map(i -> toRangeString(i)).collect(Collectors.joining(",")); } private String toRangeString(RingRange ringRange) { diff --git a/management-api-server/src/test/java/com/datastax/mgmtapi/resources/v2/RepairResourcesV2Test.java b/management-api-server/src/test/java/com/datastax/mgmtapi/resources/v2/RepairResourcesV2Test.java index 12abe689..0eb7353a 100644 --- a/management-api-server/src/test/java/com/datastax/mgmtapi/resources/v2/RepairResourcesV2Test.java +++ b/management-api-server/src/test/java/com/datastax/mgmtapi/resources/v2/RepairResourcesV2Test.java @@ -19,6 +19,7 @@ import com.datastax.mgmtapi.resources.v2.models.RepairParallelism; import com.datastax.mgmtapi.resources.v2.models.RepairRequest; import com.datastax.mgmtapi.resources.v2.models.RepairRequestResponse; +import com.datastax.mgmtapi.resources.v2.models.RingRange; import com.datastax.oss.driver.api.core.cql.ResultSet; import com.datastax.oss.driver.api.core.cql.Row; import java.io.File; @@ -50,7 +51,7 @@ public void testRepairResourcesSuccess() throws Exception { Collections.singletonList("table1"), false, true, - Collections.EMPTY_LIST, + Collections.singletonList(new RingRange("-100", "100")), RepairParallelism.DATACENTER_AWARE, Collections.EMPTY_LIST, 1); @@ -67,7 +68,7 @@ public void testRepairResourcesSuccess() throws Exception { eq(true), eq(RepairParallelism.DATACENTER_AWARE.getName()), eq(Collections.EMPTY_LIST), - eq(null), + eq("-100:100"), eq(Integer.valueOf(1))); } @@ -112,4 +113,55 @@ public void testCancelAllRepairs() throws Exception { assertEquals(202, resp.getStatus()); verify(mockCqlService).executePreparedStatement(any(), eq("CALL NodeOps.stopAllRepairs()")); } + + @Test + public void testGetRingRangeString() throws Exception { + CqlService mockCqlService = mock(CqlService.class); + ManagementApplication app = + new ManagementApplication( + null, null, new File("/tmp/cassandra.sock"), mockCqlService, null); + RepairResourcesV2 unit = new RepairResourcesV2(app); + List associatedTokens = new ArrayList<>(); + // add some random token ranges + associatedTokens.add(new RingRange(-1506836194468667463l, -633835238802072494l)); + associatedTokens.add(new RingRange(-2976249057732638160l, -1506836194468667463l)); + associatedTokens.add(new RingRange(-6235755542119343496l, -2976249057732638160l)); + associatedTokens.add(new RingRange(-633835238802072494l, 660806372122351317l)); + associatedTokens.add(new RingRange(-7075332291273605506l, -6235755542119343496l)); + associatedTokens.add(new RingRange(2303998418447223636l, 7727458699102386551l)); + associatedTokens.add(new RingRange(660806372122351317l, 2303998418447223636l)); + associatedTokens.add(new RingRange(7727458699102386551l, -7075332291273605506l)); + assertEquals( + "-1506836194468667463:-633835238802072494," + + "-2976249057732638160:-1506836194468667463," + + "-6235755542119343496:-2976249057732638160," + + "-633835238802072494:660806372122351317," + + "-7075332291273605506:-6235755542119343496," + + "2303998418447223636:7727458699102386551," + + "660806372122351317:2303998418447223636," + + "7727458699102386551:-7075332291273605506", + unit.getRingRangeString(associatedTokens)); + } + + @Test + public void testGetRingRangeStringNull() throws Exception { + CqlService mockCqlService = mock(CqlService.class); + ManagementApplication app = + new ManagementApplication( + null, null, new File("/tmp/cassandra.sock"), mockCqlService, null); + RepairResourcesV2 unit = new RepairResourcesV2(app); + // test a null ring range + assertEquals(null, unit.getRingRangeString(null)); + } + + @Test + public void testGetRingRangeStringEmpty() throws Exception { + CqlService mockCqlService = mock(CqlService.class); + ManagementApplication app = + new ManagementApplication( + null, null, new File("/tmp/cassandra.sock"), mockCqlService, null); + RepairResourcesV2 unit = new RepairResourcesV2(app); + // test a empty ring range + assertEquals(null, unit.getRingRangeString(Collections.EMPTY_LIST)); + } }