Dpk376 commented on code in PR #22573:
URL: https://github.com/apache/kafka/pull/22573#discussion_r3449543048
##########
tools/src/test/java/org/apache/kafka/tools/DeleteRecordsCommandTest.java:
##########
@@ -162,6 +171,77 @@ public void testParse() throws Exception {
assertEquals(List.of(1L), res.get(new TopicPartition("t", 1)));
}
+ @Test
+ public void testExecuteSuccessOutputWithMockAdmin() throws Exception {
+ TopicPartition tp = new TopicPartition("t", 0);
+
+ KafkaFuture<DeletedRecords> future = KafkaFuture.completedFuture(new
DeletedRecords(5L));
+
+ DeleteRecordsResult deleteResult = mock(DeleteRecordsResult.class);
+ when(deleteResult.lowWatermarks()).thenReturn(Map.of(tp, future));
+
+ Admin mockAdmin = mock(Admin.class);
+ when(mockAdmin.deleteRecords(any())).thenReturn(deleteResult);
+
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ DeleteRecordsCommand.execute(
+ mockAdmin,
+
"{\"partitions\":[{\"topic\":\"t\",\"partition\":0,\"offset\":5}]}",
+ new PrintStream(bout)
+ );
+
+ String output = bout.toString();
+ assertTrue(output.contains("Executing records delete operation"));
+ assertTrue(output.contains("Records delete operation completed:"));
+ assertTrue(output.contains("partition: t-0\tlow_watermark: 5"));
+ }
+
+ @Test
+ public void testExecutePartitionErrorWithMockAdmin() throws Exception {
+ TopicPartition tp = new TopicPartition("t", 0);
+
+ KafkaFutureImpl<DeletedRecords> future = new KafkaFutureImpl<>();
+ future.completeExceptionally(new RuntimeException("Partition not
found"));
+
+ DeleteRecordsResult deleteResult = mock(DeleteRecordsResult.class);
+ when(deleteResult.lowWatermarks()).thenReturn(Map.of(tp, future));
+
+ Admin mockAdmin = mock(Admin.class);
+ when(mockAdmin.deleteRecords(any())).thenReturn(deleteResult);
+
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ DeleteRecordsCommand.execute(
+ mockAdmin,
+
"{\"partitions\":[{\"topic\":\"t\",\"partition\":0,\"offset\":1}]}",
+ new PrintStream(bout)
+ );
+
+ String output = bout.toString();
+ assertTrue(output.contains("partition: t-0\terror:"));
+ }
+
+ @Test
+ public void testExecuteDuplicatePartitionsWithoutCluster() {
+ Admin mockAdmin = mock(Admin.class);
+
+ assertThrows(
+ AdminCommandFailedException.class,
Review Comment:
Good catch — the duplicate-partition test now asserts the full
`AdminCommandFailedException` message, and I also tightened the per-partition
error test to check the printed error text (`Partition not found`). Thanks for
the review, @chia7712!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]