counter: 1.1.7 - Fixed a bug in the assertion-logic for the expected state counter-1.1.7
authorKai Moritz <kai@juplo.de>
Tue, 14 Feb 2023 19:03:08 +0000 (20:03 +0100)
committerKai Moritz <kai@juplo.de>
Fri, 17 Feb 2023 16:31:55 +0000 (17:31 +0100)
- Each combination of a user and a word is a uniq key.
- Since the ordering is only guaranteed for messages, with the same key,
  no expectations can be made according the ordering of messages with
  different keys!
- This also simplifies the test-logic, because no deserialization is needed
  any more to pick the username out of the JSON-data.

pom.xml
src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java
src/test/java/de/juplo/kafka/wordcount/counter/TestData.java

diff --git a/pom.xml b/pom.xml
index 09e7aec..87f1f77 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -10,7 +10,7 @@
        </parent>
        <groupId>de.juplo.kafka.wordcount</groupId>
        <artifactId>counter</artifactId>
-       <version>1.1.6</version>
+       <version>1.1.7</version>
        <name>Wordcount-Counter</name>
        <description>Word-counting stream-processor of the multi-user wordcount-example</description>
        <properties>
index 75f0988..c6eb0a6 100644 (file)
@@ -1,6 +1,5 @@
 package de.juplo.kafka.wordcount.counter;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.kafka.clients.consumer.ConsumerRecord;
@@ -44,8 +43,6 @@ public class CounterApplicationIT
        @Autowired
        KafkaTemplate<String, String> kafkaTemplate;
        @Autowired
-       ObjectMapper mapper;
-       @Autowired
        Consumer consumer;
 
 
@@ -63,7 +60,7 @@ public class CounterApplicationIT
 
                await("Expexted converted data")
                                .atMost(Duration.ofSeconds(10))
-                               .untilAsserted(() -> TestData.assertExpectedResult(consumer.received, mapper));
+                               .untilAsserted(() -> TestData.assertExpectedResult(consumer.received));
        }
 
 
index 4b5482b..dc3bcec 100644 (file)
@@ -1,10 +1,5 @@
 package de.juplo.kafka.wordcount.counter;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import org.springframework.util.LinkedMultiValueMap;
-import org.springframework.util.MultiValueMap;
-
 import java.util.List;
 import java.util.function.BiConsumer;
 
@@ -50,60 +45,61 @@ class TestData
                                "s");
        }
 
-       static void assertExpectedResult(List<Message> receivedMessages, ObjectMapper mapper)
+       static void assertExpectedResult(List<Message> receivedMessages)
        {
-               MultiValueMap<String, Message> received = new LinkedMultiValueMap<>();
-
-               receivedMessages.forEach(message ->
-               {
-                       try
-                       {
-                               Key key = mapper.readValue(message.getKey(), Key.class);
-                               received.add(key.getUsername(), message);
-                       }
-                       catch (JsonProcessingException e)
-                       {
-                               throw new RuntimeException(e);
-                       }
-               });
-
-               assertThat(received).hasSize(2);
-               assertThat(received.get("klaus")).containsExactly(klaus1, klaus2, klaus3, klaus4, klaus5);
-               assertThat(received.get("peter")).containsExactly(peter1, peter2, peter3, peter4, peter5, peter6);
+               assertThat(receivedMessages).hasSize(11);
+               assertThat(receivedMessages).containsSubsequence(
+                               expectedMessages[0]); // Hallo
+               assertThat(receivedMessages).containsSubsequence(
+                               expectedMessages[1],
+                               expectedMessages[3]); // Müsch
+               assertThat(receivedMessages).containsSubsequence(
+                               expectedMessages[2],
+                               expectedMessages[6]);
+               assertThat(receivedMessages).containsSubsequence(
+                               expectedMessages[4],
+                               expectedMessages[8],
+                               expectedMessages[10]); // s
+               assertThat(receivedMessages).containsSubsequence(
+                               expectedMessages[5],
+                               expectedMessages[7],
+                               expectedMessages[9]); // Boäh
        }
 
-       static Message peter1 = Message.of(
-                       "{\"username\":\"peter\",\"word\":\"Hallo\"}",
-                       "1");
-       static Message peter2 = Message.of(
-                       "{\"username\":\"peter\",\"word\":\"Welt\"}",
-                       "1");
-       static Message peter3 = Message.of(
-                       "{\"username\":\"peter\",\"word\":\"Boäh\"}",
-                       "1");
-       static Message peter4 = Message.of(
-                       "{\"username\":\"peter\",\"word\":\"Welt\"}",
-                       "2");
-       static Message peter5 = Message.of(
-                       "{\"username\":\"peter\",\"word\":\"Boäh\"}",
-                       "2");
-       static Message peter6 = Message.of(
-                       "{\"username\":\"peter\",\"word\":\"Boäh\"}",
-                       "3");
-
-       static Message klaus1 = Message.of(
-                       "{\"username\":\"klaus\",\"word\":\"Müsch\"}",
-                       "1");
-       static Message klaus2 = Message.of(
-                       "{\"username\":\"klaus\",\"word\":\"Müsch\"}",
-                       "2");
-       static Message klaus3 = Message.of(
-                       "{\"username\":\"klaus\",\"word\":\"s\"}",
-                       "1");
-       static Message klaus4 = Message.of(
-                       "{\"username\":\"klaus\",\"word\":\"s\"}",
-                       "2");
-       static Message klaus5 = Message.of(
-                       "{\"username\":\"klaus\",\"word\":\"s\"}",
-                       "3");
+       static Message[] expectedMessages =
+       {
+                       Message.of(
+                                       "{\"username\":\"peter\",\"word\":\"Hallo\"}",
+                                       "1"),
+                       Message.of(
+                                       "{\"username\":\"klaus\",\"word\":\"Müsch\"}",
+                                       "1"),
+                       Message.of(
+                                       "{\"username\":\"peter\",\"word\":\"Welt\"}",
+                                       "1"),
+                       Message.of(
+                                       "{\"username\":\"klaus\",\"word\":\"Müsch\"}",
+                                       "2"),
+                       Message.of(
+                                       "{\"username\":\"klaus\",\"word\":\"s\"}",
+                                       "1"),
+                       Message.of(
+                                       "{\"username\":\"peter\",\"word\":\"Boäh\"}",
+                                       "1"),
+                       Message.of(
+                                       "{\"username\":\"peter\",\"word\":\"Welt\"}",
+                                       "2"),
+                       Message.of(
+                                       "{\"username\":\"peter\",\"word\":\"Boäh\"}",
+                                       "2"),
+                       Message.of(
+                                       "{\"username\":\"klaus\",\"word\":\"s\"}",
+                                       "2"),
+                       Message.of(
+                                       "{\"username\":\"peter\",\"word\":\"Boäh\"}",
+                                       "3"),
+                       Message.of(
+                                       "{\"username\":\"klaus\",\"word\":\"s\"}",
+                                       "3"),
+       };
 }