Skip to content

Commit

Permalink
Fix bug in getRingRangeString (#392)
Browse files Browse the repository at this point in the history
* Fix bug in getRingRangeString(), add unit test, add integration test.
  • Loading branch information
Miles-Garnsey authored Sep 26, 2023
1 parent 40cef22 commit adb5d0c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,16 +124,12 @@ private String getParallelismName(RepairParallelism parallelism) {
return parallelism != null ? parallelism.getName() : null;
}

private String getRingRangeString(List<RingRange> 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<RingRange> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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)));
}

Expand Down Expand Up @@ -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<RingRange> 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));
}
}

0 comments on commit adb5d0c

Please sign in to comment.