forked from confluentinc/kafka-streams-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordCountLambdaExampleTest.java
124 lines (112 loc) · 4.62 KB
/
WordCountLambdaExampleTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.confluent.examples.streams;
import org.apache.kafka.common.serialization.LongDeserializer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.TestInputTopic;
import org.apache.kafka.streams.TestOutputTopic;
import org.apache.kafka.streams.TopologyTestDriver;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
/**
* Stream processing unit test of {@link WordCountLambdaExample}, using TopologyTestDriver.
*
* See {@link WordCountLambdaExample} for further documentation.
*/
public class WordCountLambdaExampleTest {
private TopologyTestDriver testDriver;
private TestInputTopic<String, String> inputTopic;
private TestOutputTopic<String, Long> outputTopic;
private StringSerializer stringSerializer = new StringSerializer();
private StringDeserializer stringDeserializer = new StringDeserializer();
private LongDeserializer longDeserializer = new LongDeserializer();
@Before
public void setup() {
final StreamsBuilder builder = new StreamsBuilder();
//Create Actual Stream Processing pipeline
WordCountLambdaExample.createWordCountStream(builder);
testDriver = new TopologyTestDriver(builder.build(), WordCountLambdaExample.getStreamsConfiguration("localhost:9092"));
inputTopic = testDriver.createInputTopic(WordCountLambdaExample.inputTopic,
stringSerializer,
stringSerializer);
outputTopic = testDriver.createOutputTopic(WordCountLambdaExample.outputTopic,
stringDeserializer,
longDeserializer);
}
@After
public void tearDown() {
try {
testDriver.close();
} catch (final RuntimeException e) {
// https://issues.apache.org/jira/browse/KAFKA-6647 causes exception when executed in Windows, ignoring it
// Logged stacktrace cannot be avoided
System.out.println("Ignoring exception, test failing in Windows due this exception:" + e.getLocalizedMessage());
}
}
/**
* Simple test validating count of one word
*/
@Test
public void testOneWord() {
//Feed word "Hello" to inputTopic and no kafka key, timestamp is irrelevant in this case
inputTopic.pipeInput("Hello", Instant.ofEpochMilli(1L));
//Read and validate output
final KeyValue<String, Long> output = outputTopic.readKeyValue();
assertThat(output, equalTo(KeyValue.pair("hello", 1L)));
//No more output in topic
assertTrue(outputTopic.isEmpty());
}
/**
* Test Word count of sentence list.
*/
@Test
public void shouldCountWords() {
final List<String> inputValues = Arrays.asList(
"Hello Kafka Streams",
"All streams lead to Kafka",
"Join Kafka Summit",
"И теперь пошли русские слова"
);
final Map<String, Long> expectedWordCounts = new HashMap<>();
expectedWordCounts.put("hello", 1L);
expectedWordCounts.put("all", 1L);
expectedWordCounts.put("streams", 2L);
expectedWordCounts.put("lead", 1L);
expectedWordCounts.put("to", 1L);
expectedWordCounts.put("join", 1L);
expectedWordCounts.put("kafka", 3L);
expectedWordCounts.put("summit", 1L);
expectedWordCounts.put("и", 1L);
expectedWordCounts.put("теперь", 1L);
expectedWordCounts.put("пошли", 1L);
expectedWordCounts.put("русские", 1L);
expectedWordCounts.put("слова", 1L);
inputTopic.pipeValueList(inputValues, Instant.ofEpochMilli(1L), Duration.ofMillis(100L));
assertThat(outputTopic.readKeyValuesToMap(), equalTo(expectedWordCounts));
}
}