Skip to content

Commit

Permalink
Tests and fixes for tests, juggle optional parameters better.
Browse files Browse the repository at this point in the history
  • Loading branch information
Miles-Garnsey committed Aug 24, 2023
1 parent 5203111 commit da8b860
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@

import com.datastax.mgmtapi.ManagementApplication;
import com.datastax.mgmtapi.resources.common.BaseResources;
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 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.Collection;
import java.util.List;
import java.util.Optional;
import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -68,6 +73,26 @@ public final Response repair(RepairRequest request) {
.entity("keyspaceName must be specified")
.build();
}
// TODO: we need to think about how to handle unspecified/null values for the new
// arguments,
// so that existing callers can still call v2 endpoint without args like
// repairParallelism etc.
// I have something here but I don't like it.
Optional<List<RingRange>> associatedTokens =
request.associatedTokens == null
? Optional.empty()
: Optional.of(request.associatedTokens);
Optional<RepairParallelism> repairParallelism =
request.repairParallelism == null
? Optional.empty()
: Optional.of(request.repairParallelism);
Optional<Collection<String>> datacenters =
request.datacenters == null ? Optional.empty() : Optional.of(request.datacenters);
Optional<Integer> repairThreadCount =
request.repairThreadCount == null
? Optional.empty()
: Optional.of(request.repairThreadCount);

ResultSet res =
app.cqlService.executePreparedStatement(
app.dbUnixSocketFile,
Expand All @@ -76,11 +101,10 @@ public final Response repair(RepairRequest request) {
request.tables,
request.fullRepair,
request.notifications,
request.repairParallelism,
request.datacenters,
request.associatedTokens,
request.repairThreadCount);

repairParallelism,
datacenters,
associatedTokens,
repairThreadCount);
try {
String repairID = res.one().getString(0);
return Response.accepted(new RepairRequestResponse(repairID)).build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class RepairRequest {
public final Collection<String> datacenters;

@JsonProperty(value = "repair_thread_count")
public final int repairThreadCount;
public final Integer repairThreadCount;

@JsonCreator
public RepairRequest(
Expand All @@ -47,7 +47,7 @@ public RepairRequest(
@JsonProperty(value = "associated_tokens") List<RingRange> associatedTokens,
@JsonProperty(value = "repair_parallelism") RepairParallelism repairParallelism,
@JsonProperty(value = "datacenters") Collection<String> datacenters,
@JsonProperty(value = "repair_thread_count") int repairThreadCount) {
@JsonProperty(value = "repair_thread_count") Integer repairThreadCount) {
this.keyspace = keyspace;
this.tables = tables;
this.fullRepair = fullRepair;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.ws.rs.core.MediaType;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.http.HttpStatus;
Expand All @@ -70,18 +71,6 @@ public class K8OperatorResourcesTest {

static String ROOT_PATH = "/api/v0";

static class Context {

Dispatcher dispatcher;
CqlService cqlService;

MockHttpResponse invoke(HttpRequest request) {
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
return response;
}
}

static Context setup() {
Context context = new Context();
context.dispatcher = createDispatcher();
Expand Down Expand Up @@ -1697,11 +1686,15 @@ public void testRepairAsync() throws Exception {
verify(context.cqlService)
.executePreparedStatement(
any(),
eq("CALL NodeOps.repair(?, ?, ?, ?)"),
eq("CALL NodeOps.repair(?, ?, ?, ?, ?, ?, ?, ?)"),
eq("test_ks"),
eq(null),
eq(true),
eq(true));
eq(Optional.empty()),
eq(Optional.empty()),
eq(Optional.empty()),
eq(Optional.empty()),
eq(Optional.empty()),
eq(Optional.empty()),
eq(Optional.empty()));
}

@Test
Expand Down Expand Up @@ -2111,4 +2104,16 @@ public void testMoveAsync_MissingNewToken() throws Exception {
verify(context.cqlService, never())
.executePreparedStatement(any(), eq("CALL NodeOps.move(?, ?)"), eq("1234"), eq(true));
}

static class Context {

Dispatcher dispatcher;
CqlService cqlService;

MockHttpResponse invoke(HttpRequest request) {
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
return response;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright DataStax, Inc.
*
* Please see the included license file for details.
*/
package com.datastax.mgmtapi.resources.v2;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.datastax.mgmtapi.CqlService;
import com.datastax.mgmtapi.ManagementApplication;
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;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.ws.rs.core.Response;
import org.junit.Assert;
import org.junit.Test;

public class RepairResourcesTest {

@Test
public void testRepairResourcesSuccess() throws Exception {
CqlService mockCqlService = mock(CqlService.class);
ManagementApplication app =
new ManagementApplication(
null, null, new File("/tmp/cassandra.sock"), mockCqlService, null);
ResultSet mockResultSet = mock(ResultSet.class);
Row mockRow = mock(Row.class);
when(mockRow.getString(anyString())).thenReturn("mockrepairID");
when(mockCqlService.executePreparedStatement(
any(), anyString(), any(), any(), any(), any(), any(), any(), any(), any()))
.thenReturn(mockResultSet);
RepairResources unit = new RepairResources(app);
List<String> tables = new ArrayList<>();
tables.add("table1");
tables.add("table2");
RepairRequest req =
new RepairRequest(
"keyspace",
tables,
false,
true,
new ArrayList<RingRange>(),
RepairParallelism.DATACENTER_AWARE,
new ArrayList<String>(),
1);
Response resp = unit.repair(req);
Assert.assertEquals(200, resp.getStatus());
Assert.assertEquals("mockrepairID", ((RepairRequestResponse) resp.getEntity()).repairID);
verify(mockCqlService)
.executePreparedStatement(
any(),
eq("CALL NodeOps.repair(?, ?, ?, ?, ?, ?, ?, ?)"),
eq("test_ks"),
eq(null),
eq(true),
eq(true),
eq(Optional.empty()),
eq(Optional.empty()),
eq(Optional.empty()),
eq(1));
}

@Test
public void testRepairResourcesFail() throws Exception {
CqlService mockCqlService = mock(CqlService.class);
ManagementApplication app =
new ManagementApplication(
null, null, new File("/tmp/cassandra.sock"), mockCqlService, null);
ResultSet mockResultSet = mock(ResultSet.class);
Row mockRow = mock(Row.class);
when(mockRow.getString(anyString())).thenReturn("mockrepairID");
when(mockCqlService.executePreparedStatement(
any(), anyString(), any(), any(), any(), any(), any(), any(), any(), any()))
.thenReturn(mockResultSet);
RepairResources unit = new RepairResources(app);
List<String> tables = new ArrayList<>();
tables.add("table1");
tables.add("table2");
RepairRequest req =
new RepairRequest(
"",
tables,
false,
true,
new ArrayList<RingRange>(),
RepairParallelism.DATACENTER_AWARE,
new ArrayList<String>(),
1);
Response resp = unit.repair(req);
Assert.assertEquals(500, resp.getStatus());
}
}

0 comments on commit da8b860

Please sign in to comment.