Skip to content

Commit

Permalink
Add tests for the async endpoint in v1 also
Browse files Browse the repository at this point in the history
  • Loading branch information
burmanm committed Aug 10, 2023
1 parent 7b18329 commit 20ff27e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ public Response repair(RepairRequest repairRequest) {
}
cqlService.executePreparedStatement(
app.dbUnixSocketFile,
"CALL NodeOps.repair(?, ?, ?)",
"CALL NodeOps.repair(?, ?, ?, ?)",
repairRequest.keyspaceName,
repairRequest.tables,
repairRequest.full,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public Response repair(RepairRequest repairRequest) {
ResponseTools.getSingleRowStringResponse(
app.dbUnixSocketFile,
cqlService,
"CALL NodeOps.repair(?, ?, ?)",
"CALL NodeOps.repair(?, ?, ?, ?)",
repairRequest.keyspaceName,
repairRequest.tables,
repairRequest.full,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,40 @@ public void testRepair() throws Exception {
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
verify(context.cqlService)
.executePreparedStatement(
any(), eq("CALL NodeOps.repair(?, ?, ?)"), eq("test_ks"), eq(null), eq(true));
any(), eq("CALL NodeOps.repair(?, ?, ?, ?)"), eq("test_ks"), eq(null), eq(true), eq(false));
}

@Test
public void testRepairAsync() throws Exception {
Context context = setup();
when(context.cqlService.executePreparedStatement(any(), anyString())).thenReturn(null);

RepairRequest repairRequest = new RepairRequest("test_ks", null, Boolean.TRUE);
String repairRequestAsJSON = WriterUtility.asString(repairRequest, MediaType.APPLICATION_JSON);

ResultSet mockResultSet = mock(ResultSet.class);
Row mockRow = mock(Row.class);

when(context.cqlService.executePreparedStatement(any(), any(), any()))
.thenReturn(mockResultSet);

when(mockResultSet.one()).thenReturn(mockRow);

when(mockRow.getString(0)).thenReturn("0fe65b47-98c2-47d8-9c3c-5810c9988e10");

MockHttpRequest request = MockHttpRequest.post("/api/v1/ops/node/repair")
.content(repairRequestAsJSON.getBytes())
.accept(MediaType.TEXT_PLAIN)
.contentType(MediaType.APPLICATION_JSON_TYPE);

MockHttpResponse response = context.invoke(request);

Assert.assertEquals(HttpStatus.SC_ACCEPTED, response.getStatus());
Assert.assertEquals("0fe65b47-98c2-47d8-9c3c-5810c9988e10", response.getContentAsString());

verify(context.cqlService)
.executePreparedStatement(
any(), eq("CALL NodeOps.repair(?, ?, ?, ?)"), eq("test_ks"), eq(null), eq(true), eq(true));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,54 @@ public void testRepair() throws IOException, URISyntaxException, InterruptedExce
assertTrue("Repair request was not successful", repairSuccessful);
}

@Test
public void testAsyncRepair() throws IOException, URISyntaxException, InterruptedException {
assumeTrue(IntegrationTestUtils.shouldRun());
ensureStarted();

NettyHttpClient client = new NettyHttpClient(BASE_URL);

URIBuilder uriBuilder = new URIBuilder("http://localhost:8080/api/v1/ops/node/repair");
URI repairUri = uriBuilder.build();

// execute repair
RepairRequest repairRequest = new RepairRequest("system_auth", null, Boolean.TRUE);
String requestAsJSON = WriterUtility.asString(repairRequest, MediaType.APPLICATION_JSON);

Pair<Integer, String> repairResponse =
client.post(repairUri.toURL(), null).thenApply(this::responseAsCodeAndBody).join();
assertThat(repairResponse.getLeft()).isEqualTo(HttpStatus.SC_ACCEPTED);
String jobId = repairResponse.getRight();
assertThat(jobId).isNotEmpty();

URI getJobDetailsUri =
new URIBuilder(BASE_PATH + "/ops/executor/job").addParameter("job_id", jobId).build();

await()
.atMost(Duration.ofMinutes(5))
.untilAsserted(
() -> {
Pair<Integer, String> getJobDetailsResponse =
client
.get(getJobDetailsUri.toURL())
.thenApply(this::responseAsCodeAndBody)
.join();
assertThat(getJobDetailsResponse.getLeft()).isEqualTo(HttpStatus.SC_OK);
Map<String, String> jobDetails =
new JsonMapper()
.readValue(
getJobDetailsResponse.getRight(),
new TypeReference<Map<String, String>>() {});
assertThat(jobDetails)
.hasEntrySatisfying("id", value -> assertThat(value).isEqualTo(jobId))
.hasEntrySatisfying("type", value -> assertThat(value).isEqualTo("repair"))
// if the server has only one token, the job will be completed, otherwise it will
// end up in error
.hasEntrySatisfying(
"status", value -> assertThat(value).isIn("COMPLETED", "ERROR"));
});
}

@Test
public void testGetReplication() throws IOException, URISyntaxException {
assumeTrue(IntegrationTestUtils.shouldRun());
Expand Down

0 comments on commit 20ff27e

Please sign in to comment.