28dfe712b97a208ffc121b641417b04b5f65c10e
[demos/kafka/wordcount] / src / test / java / de / juplo / kafka / wordcount / stats / TestData.java
1 package de.juplo.kafka.wordcount.stats;
2
3 import de.juplo.kafka.wordcount.top10.TestEntry;
4 import de.juplo.kafka.wordcount.top10.TestRanking;
5 import de.juplo.kafka.wordcount.top10.TestUser;
6 import de.juplo.kafka.wordcount.users.TestUserData;
7 import org.apache.kafka.streams.KeyValue;
8
9 import java.util.Arrays;
10 import java.util.function.Function;
11 import java.util.stream.Stream;
12
13 import static org.assertj.core.api.Assertions.assertThat;
14
15
16 class TestData
17 {
18         static final TestUser PETER = TestUser.of(StatisticsType.POPULAR.name(), "peter");
19         static final TestUser KLAUS = TestUser.of(StatisticsType.POPULAR.name(), "klaus");
20         static final TestUser OTHER_CHANNEL = TestUser.of("COUNTER", "klaus");
21
22         static final Stream<KeyValue<TestUser, TestRanking>> getTop10Messages()
23         {
24                 return Stream.of(TOP10_MESSAGES);
25         }
26
27         static final Stream<KeyValue<String, TestUserData>> getUsersMessages()
28         {
29                 return Stream.of(USERS_MESSAGES);
30         }
31
32         static void assertExpectedState(Function<String, UserRanking> function)
33         {
34                 assertRankingEqualsRankingFromLastMessage(PETER.getChannel(), function.apply(PETER.getChannel()));
35                 assertRankingEqualsRankingFromLastMessage(KLAUS.getChannel(), function.apply(KLAUS.getChannel()));
36         }
37
38         private static void assertRankingEqualsRankingFromLastMessage(String user, UserRanking rankingJson)
39         {
40                 assertThat(rankingJson).isEqualTo(getLastMessageFor(user));
41         }
42
43         private static UserRanking getLastMessageFor(String user)
44         {
45                 return getTop10Messages()
46                                 .filter(kv -> kv.key.getChannel().equals(user))
47                                 .map(kv -> kv.value)
48                                 .map(testRanking -> userRankingFor(user, testRanking))
49                                 .reduce(null, (left, right) -> right);
50         }
51
52         private static UserRanking userRankingFor(String user, TestRanking testRanking)
53         {
54                 TestUserData testUserData = getUsersMessages()
55                                 .filter(kv -> kv.key.equals(user))
56                                 .map(kv -> kv.value)
57                                 .reduce(null, (left, right) -> right);
58
59                 Entry[] entries = Arrays
60                                 .stream(testRanking.getEntries())
61                                 .map(testEntry -> entryOf(testEntry))
62                                 .toArray(size -> new Entry[size]);
63
64                 return UserRanking.of(
65                                 testUserData.getFirstName(),
66                                 testUserData.getLastName(),
67                                 entries);
68         }
69
70         private static Entry entryOf(TestEntry testEntry)
71         {
72                 Entry entry = new Entry();
73                 entry.setKey(testEntry.getKey());
74                 entry.setCounter(testEntry.getCounter());
75                 return entry;
76         }
77         private static KeyValue<TestUser, TestRanking>[] TOP10_MESSAGES = new KeyValue[]
78         {
79                         KeyValue.pair( // 0
80                                         PETER,
81                                         TestRanking.of(
82                                                         TestEntry.of("Hallo", 1l))),
83                         KeyValue.pair( // BOOM!
84                                         OTHER_CHANNEL,
85                                         TestRanking.of(
86                                                         TestEntry.of("Müsch", 1l))),
87                         KeyValue.pair( // 1
88                                         KLAUS,
89                                         TestRanking.of(
90                                                         TestEntry.of("Müsch", 1l))),
91                         KeyValue.pair( // 2
92                                         PETER,
93                                         TestRanking.of(
94                                                         TestEntry.of("Hallo", 1l),
95                                                         TestEntry.of("Welt", 1l))),
96                         KeyValue.pair( // 3
97                                         KLAUS,
98                                         TestRanking.of(
99                                                         TestEntry.of("Müsch", 2l))),
100                         KeyValue.pair( // 4
101                                         KLAUS,
102                                         TestRanking.of(
103                                                         TestEntry.of("Müsch", 2l),
104                                                         TestEntry.of("s", 1l))),
105                         KeyValue.pair( // 5
106                                         PETER,
107                                         TestRanking.of(
108                                                         TestEntry.of("Hallo", 1l),
109                                                         TestEntry.of("Welt", 1l),
110                                                         TestEntry.of("Boäh", 1l))),
111                         KeyValue.pair( // 6
112                                         PETER,
113                                         TestRanking.of(
114                                                         TestEntry.of("Welt", 2l),
115                                                         TestEntry.of("Hallo", 1l),
116                                                         TestEntry.of("Boäh", 1l))),
117                         KeyValue.pair( // 7
118                                         PETER,
119                                         TestRanking.of(
120                                                         TestEntry.of("Welt", 2l),
121                                                         TestEntry.of("Boäh", 2l),
122                                                         TestEntry.of("Hallo", 1l))),
123                         KeyValue.pair( // 8
124                                         KLAUS,
125                                         TestRanking.of(
126                                                         TestEntry.of("Müsch", 2l),
127                                                         TestEntry.of("s", 2l))),
128                         KeyValue.pair( // BOOM!
129                                         KLAUS,
130                                         TestRanking.of(
131                                                         TestEntry.of("Müsch", 2l),
132                                                         TestEntry.of("s", 2l))),
133                         KeyValue.pair( // 9
134                                         PETER,
135                                         TestRanking.of(
136                                                         TestEntry.of("Boäh", 3l),
137                                                         TestEntry.of("Welt", 2l),
138                                                         TestEntry.of("Hallo", 1l))),
139                         KeyValue.pair( // 10
140                                         KLAUS,
141                                         TestRanking.of(
142                                                         TestEntry.of("s", 3l),
143                                                         TestEntry.of("Müsch", 2l))),
144         };
145
146         private static KeyValue<String, TestUserData>[] USERS_MESSAGES = new KeyValue[]
147         {
148                         KeyValue.pair(
149                                         PETER.getChannel(),
150                                         TestUserData.of(PETER.getChannel(), "Peter", "Pan", TestUserData.Sex.MALE)),
151                         KeyValue.pair(
152                                         KLAUS.getChannel(),
153                                         TestUserData.of(KLAUS.getChannel(), "Klaus", "Klüse", TestUserData.Sex.OTHER)),
154         };
155 }