Skip to content

Commit 348e64c

Browse files
authored
MINOR: Add unit tests for verifying --formatter-property in console tools. (#20560)
*What* In the implementation of KIP-1147 for console tools - https://github.com/apache/kafka/pull/20479/files#diff-85b87c675a4b933e8e0e05c654d35d60e9cfd36cebe3331af825191b2cc688ee, we missed adding unit tests for verifying the new "`--formatter-property`" option. Thanks to @Yunyung for pointing this out. PR adds unit tests to both `ConsoleConsumerOptionsTest` and `ConsoleShareConsumerOptionsTest` to verify the same. Reviewers: Jhen-Yung Hsu <[email protected]>, Chia-Ping Tsai <[email protected]>
1 parent cbea4f6 commit 348e64c

File tree

2 files changed

+126
-5
lines changed

2 files changed

+126
-5
lines changed

tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleConsumerOptionsTest.java

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void shouldExitIfFromBeginningAndOffset() {
151151
}
152152

153153
@Test
154-
public void shouldParseValidSimpleConsumerValidConfigWithStringOffset() throws Exception {
154+
public void shouldParseValidSimpleConsumerValidConfigWithStringOffsetDeprecated() throws Exception {
155155
String[] args = new String[]{
156156
"--bootstrap-server", "localhost:9092",
157157
"--topic", "test",
@@ -171,6 +171,27 @@ public void shouldParseValidSimpleConsumerValidConfigWithStringOffset() throws E
171171
assertFalse(((DefaultMessageFormatter) config.formatter()).printValue());
172172
}
173173

174+
@Test
175+
public void shouldParseValidSimpleConsumerValidConfigWithStringOffset() throws Exception {
176+
String[] args = new String[]{
177+
"--bootstrap-server", "localhost:9092",
178+
"--topic", "test",
179+
"--partition", "0",
180+
"--offset", "LatEst",
181+
"--formatter-property", "print.value=false"
182+
};
183+
184+
ConsoleConsumerOptions config = new ConsoleConsumerOptions(args);
185+
186+
assertEquals("localhost:9092", config.bootstrapServer());
187+
assertEquals("test", config.topicArg().orElse(""));
188+
assertTrue(config.partitionArg().isPresent());
189+
assertEquals(0, config.partitionArg().getAsInt());
190+
assertEquals(-1, config.offsetArg());
191+
assertFalse(config.fromBeginning());
192+
assertFalse(((DefaultMessageFormatter) config.formatter()).printValue());
193+
}
194+
174195
@Test
175196
public void shouldParseValidConsumerConfigWithAutoOffsetResetLatestDeprecated() throws IOException {
176197
String[] args = new String[]{
@@ -355,7 +376,7 @@ public void groupIdsProvidedInDifferentPlacesMustMatchDeprecated() throws IOExce
355376
}
356377

357378
@Test
358-
public void testCustomPropertyShouldBePassedToConfigureMethod() throws Exception {
379+
public void testCustomPropertyShouldBePassedToConfigureMethodDeprecated() throws Exception {
359380
String[] args = new String[]{
360381
"--bootstrap-server", "localhost:9092",
361382
"--topic", "test",
@@ -378,7 +399,30 @@ public void testCustomPropertyShouldBePassedToConfigureMethod() throws Exception
378399
}
379400

380401
@Test
381-
public void testCustomConfigShouldBePassedToConfigureMethod() throws Exception {
402+
public void testCustomPropertyShouldBePassedToConfigureMethod() throws Exception {
403+
String[] args = new String[]{
404+
"--bootstrap-server", "localhost:9092",
405+
"--topic", "test",
406+
"--formatter-property", "print.key=true",
407+
"--formatter-property", "key.deserializer=org.apache.kafka.test.MockDeserializer",
408+
"--formatter-property", "key.deserializer.my-props=abc"
409+
};
410+
411+
ConsoleConsumerOptions config = new ConsoleConsumerOptions(args);
412+
413+
assertInstanceOf(DefaultMessageFormatter.class, config.formatter());
414+
assertTrue(config.formatterArgs().containsKey("key.deserializer.my-props"));
415+
DefaultMessageFormatter formatter = (DefaultMessageFormatter) config.formatter();
416+
assertTrue(formatter.keyDeserializer().isPresent());
417+
assertInstanceOf(MockDeserializer.class, formatter.keyDeserializer().get());
418+
MockDeserializer keyDeserializer = (MockDeserializer) formatter.keyDeserializer().get();
419+
assertEquals(1, keyDeserializer.configs.size());
420+
assertEquals("abc", keyDeserializer.configs.get("my-props"));
421+
assertTrue(keyDeserializer.isKey);
422+
}
423+
424+
@Test
425+
public void testCustomConfigShouldBePassedToConfigureMethodDeprecated() throws Exception {
382426
Map<String, String> configs = new HashMap<>();
383427
configs.put("key.deserializer.my-props", "abc");
384428
configs.put("print.key", "false");
@@ -404,6 +448,33 @@ public void testCustomConfigShouldBePassedToConfigureMethod() throws Exception {
404448
assertTrue(keyDeserializer.isKey);
405449
}
406450

451+
@Test
452+
public void testCustomConfigShouldBePassedToConfigureMethod() throws Exception {
453+
Map<String, String> configs = new HashMap<>();
454+
configs.put("key.deserializer.my-props", "abc");
455+
configs.put("print.key", "false");
456+
File propsFile = ToolsTestUtils.tempPropertiesFile(configs);
457+
String[] args = new String[]{
458+
"--bootstrap-server", "localhost:9092",
459+
"--topic", "test",
460+
"--formatter-property", "print.key=true",
461+
"--formatter-property", "key.deserializer=org.apache.kafka.test.MockDeserializer",
462+
"--formatter-config", propsFile.getAbsolutePath()
463+
};
464+
465+
ConsoleConsumerOptions config = new ConsoleConsumerOptions(args);
466+
467+
assertInstanceOf(DefaultMessageFormatter.class, config.formatter());
468+
assertTrue(config.formatterArgs().containsKey("key.deserializer.my-props"));
469+
DefaultMessageFormatter formatter = (DefaultMessageFormatter) config.formatter();
470+
assertTrue(formatter.keyDeserializer().isPresent());
471+
assertInstanceOf(MockDeserializer.class, formatter.keyDeserializer().get());
472+
MockDeserializer keyDeserializer = (MockDeserializer) formatter.keyDeserializer().get();
473+
assertEquals(1, keyDeserializer.configs.size());
474+
assertEquals("abc", keyDeserializer.configs.get("my-props"));
475+
assertTrue(keyDeserializer.isKey);
476+
}
477+
407478
@Test
408479
public void shouldParseGroupIdFromBeginningGivenTogether() throws IOException {
409480
// Start from earliest

tools/src/test/java/org/apache/kafka/tools/consumer/ConsoleShareConsumerOptionsTest.java

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ public void testClientIdOverrideDeprecated() throws IOException {
222222
}
223223

224224
@Test
225-
public void testCustomPropertyShouldBePassedToConfigureMethod() throws Exception {
225+
public void testCustomPropertyShouldBePassedToConfigureMethodDeprecated() throws Exception {
226226
String[] args = new String[]{
227227
"--bootstrap-server", "localhost:9092",
228228
"--topic", "test",
@@ -245,7 +245,30 @@ public void testCustomPropertyShouldBePassedToConfigureMethod() throws Exception
245245
}
246246

247247
@Test
248-
public void testCustomConfigShouldBePassedToConfigureMethod() throws Exception {
248+
public void testCustomPropertyShouldBePassedToConfigureMethod() throws Exception {
249+
String[] args = new String[]{
250+
"--bootstrap-server", "localhost:9092",
251+
"--topic", "test",
252+
"--formatter-property", "print.key=true",
253+
"--formatter-property", "key.deserializer=org.apache.kafka.test.MockDeserializer",
254+
"--formatter-property", "key.deserializer.my-props=abc"
255+
};
256+
257+
ConsoleShareConsumerOptions config = new ConsoleShareConsumerOptions(args);
258+
259+
assertInstanceOf(DefaultMessageFormatter.class, config.formatter());
260+
assertTrue(config.formatterArgs().containsKey("key.deserializer.my-props"));
261+
DefaultMessageFormatter formatter = (DefaultMessageFormatter) config.formatter();
262+
assertTrue(formatter.keyDeserializer().isPresent());
263+
assertInstanceOf(MockDeserializer.class, formatter.keyDeserializer().get());
264+
MockDeserializer keyDeserializer = (MockDeserializer) formatter.keyDeserializer().get();
265+
assertEquals(1, keyDeserializer.configs.size());
266+
assertEquals("abc", keyDeserializer.configs.get("my-props"));
267+
assertTrue(keyDeserializer.isKey);
268+
}
269+
270+
@Test
271+
public void testCustomConfigShouldBePassedToConfigureMethodDeprecated() throws Exception {
249272
Map<String, String> configs = new HashMap<>();
250273
configs.put("key.deserializer.my-props", "abc");
251274
configs.put("print.key", "false");
@@ -271,6 +294,33 @@ public void testCustomConfigShouldBePassedToConfigureMethod() throws Exception {
271294
assertTrue(keyDeserializer.isKey);
272295
}
273296

297+
@Test
298+
public void testCustomConfigShouldBePassedToConfigureMethod() throws Exception {
299+
Map<String, String> configs = new HashMap<>();
300+
configs.put("key.deserializer.my-props", "abc");
301+
configs.put("print.key", "false");
302+
File propsFile = ToolsTestUtils.tempPropertiesFile(configs);
303+
String[] args = new String[]{
304+
"--bootstrap-server", "localhost:9092",
305+
"--topic", "test",
306+
"--formatter-property", "print.key=true",
307+
"--formatter-property", "key.deserializer=org.apache.kafka.test.MockDeserializer",
308+
"--formatter-config", propsFile.getAbsolutePath()
309+
};
310+
311+
ConsoleShareConsumerOptions config = new ConsoleShareConsumerOptions(args);
312+
313+
assertInstanceOf(DefaultMessageFormatter.class, config.formatter());
314+
assertTrue(config.formatterArgs().containsKey("key.deserializer.my-props"));
315+
DefaultMessageFormatter formatter = (DefaultMessageFormatter) config.formatter();
316+
assertTrue(formatter.keyDeserializer().isPresent());
317+
assertInstanceOf(MockDeserializer.class, formatter.keyDeserializer().get());
318+
MockDeserializer keyDeserializer = (MockDeserializer) formatter.keyDeserializer().get();
319+
assertEquals(1, keyDeserializer.configs.size());
320+
assertEquals("abc", keyDeserializer.configs.get("my-props"));
321+
assertTrue(keyDeserializer.isKey);
322+
}
323+
274324
@Test
275325
public void testDefaultClientId() throws IOException {
276326
String[] args = new String[]{

0 commit comments

Comments
 (0)