From: Kai Moritz Date: Mon, 15 Aug 2022 17:02:58 +0000 (+0200) Subject: ROT: Signatur für `AdderBusinessLogic` und neue Erwartungen formuliert X-Git-Tag: sumup-adder---lvm-2-tage~17 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=d576eea9bc9208d9e5003bd8c8c132bed96b5c40;p=demos%2Fkafka%2Ftraining ROT: Signatur für `AdderBusinessLogic` und neue Erwartungen formuliert * Anwendung so überarbeitet, dass sie weniger motzig ist, dafür aber einfach Rechenfehler produziert - weil diese bei Experimenten leichter nachvollziehbar sind. * Dafür eine neue Signatur für `AdderBusinessLogic` entwickelt, die Implementierung aber noch nicht angepasst. * Die neuen Erwartungen an `AdderBusinessLogic` formuliert. --- diff --git a/src/main/java/de/juplo/kafka/AdderBusinessLogic.java b/src/main/java/de/juplo/kafka/AdderBusinessLogic.java index 1f3d9aa..ecd9590 100644 --- a/src/main/java/de/juplo/kafka/AdderBusinessLogic.java +++ b/src/main/java/de/juplo/kafka/AdderBusinessLogic.java @@ -22,14 +22,6 @@ public class AdderBusinessLogic } - public synchronized void startSum(String user) - { - if (state.containsKey(user)) - throw new IllegalStateException("Sumation for " + user + " already in progress, state: " + state.get(user)); - - state.put(user, 0l); - } - public synchronized Optional getSum(String user) { return Optional.ofNullable(state.get(user)); @@ -46,7 +38,7 @@ public class AdderBusinessLogic state.put(user, result); } - public synchronized Long endSum(String user) + public synchronized Long calculate(String user) { if (!state.containsKey(user)) throw new IllegalStateException("No sumation for " + user + " in progress"); diff --git a/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java b/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java index d0d385c..ce340a7 100644 --- a/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java +++ b/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java @@ -19,21 +19,15 @@ public class ApplicationRecordHandler implements RecordHandler Integer partition = record.partition(); String user = record.key(); String message = record.value(); - switch (message) + + if (message.equals("CALCULATE")) { - case "START": - state.get(partition).startSum(user); - break; - - case "END": - Long result = state.get(partition).endSum(user); - log.info("New result for {}: {}", user, result); - break; - - default: - state.get(partition).addToSum(user, Integer.parseInt(message)); - break; + Long result = state.get(partition).calculate(user); + log.info("New result for {}: {}", user, result); + return; } + + state.get(partition).addToSum(user, Integer.parseInt(message)); } protected void addPartition(Integer partition, Map state) diff --git a/src/test/java/de/juplo/kafka/AdderBusinessLogicTest.java b/src/test/java/de/juplo/kafka/AdderBusinessLogicTest.java index 63447e3..35eed73 100644 --- a/src/test/java/de/juplo/kafka/AdderBusinessLogicTest.java +++ b/src/test/java/de/juplo/kafka/AdderBusinessLogicTest.java @@ -16,23 +16,6 @@ import static org.assertj.core.api.Assertions.*; public class AdderBusinessLogicTest { - @Test - @DisplayName("A new sum can be started, if it does not exist") - public void testStartSumPossibleIfNotExists() - { - AdderBusinessLogic adder = new AdderBusinessLogic(); - assertThatNoException().isThrownBy(() -> adder.startSum("foo")); - } - - @Test - @DisplayName("Starting an already existing sum again, causes an IllegalStateException") - public void testStartSumCausesExceptionIfExists() - { - AdderBusinessLogic adder = new AdderBusinessLogic(); - adder.startSum("foo"); - assertThatIllegalStateException().isThrownBy(() -> adder.startSum("foo")); - } - @Test @DisplayName("An empty Optional should be returned, for a non-existing sum") public void testGetSumReturnsEmptyOptionalForNonExistingSum() @@ -46,102 +29,81 @@ public class AdderBusinessLogicTest public void testGetSumReturnsNonEmptyOptionalForExistingSum() { AdderBusinessLogic adder = new AdderBusinessLogic(); - adder.startSum("foo"); + adder.addToSum("foo", 6); assertThat(adder.getSum("foo")).isNotEmpty(); } @Test - @DisplayName("A sum can be ended, if it does exist") - public void testEndSumPossibleIfSumExists() + @DisplayName("A sum can be calculated, if it does exist") + public void testCalculatePossibleIfSumExists() { AdderBusinessLogic adder = new AdderBusinessLogic(); - adder.startSum("foo"); - assertThatNoException().isThrownBy(() -> adder.endSum("foo")); + adder.addToSum("foo", 6); + assertThatNoException().isThrownBy(() -> adder.calculate("foo")); } @Test @DisplayName("An existing sum is removed, if ended") - public void testEndSumRemovesSumIfSumExists() + public void testCalculateRemovesSumIfSumExists() { AdderBusinessLogic adder = new AdderBusinessLogic(); - adder.startSum("foo"); - adder.endSum("foo"); + adder.addToSum("foo", 6); + adder.calculate("foo"); assertThat(adder.getSum("foo")).isEmpty(); } @Test - @DisplayName("An existing Sum returns a non-null value, if ended") - public void testEndSumReturnsNonNullValueIfSumExists() + @DisplayName("An existing sum returns a non-null value, if calculated") + public void testCalculateReturnsNonNullValueIfSumExists() { AdderBusinessLogic adder = new AdderBusinessLogic(); - adder.startSum("foo"); - assertThat(adder.endSum("foo")).isNotNull(); - } - - @Test - @DisplayName("An existing Sum returns a non-negative value, if ended") - public void testEndSumReturnsNonNegativeValueIfSumExists() - { - AdderBusinessLogic adder = new AdderBusinessLogic(); - adder.startSum("foo"); - assertThat(adder.endSum("foo")).isNotNegative(); + adder.addToSum("foo", 6); + assertThat(adder.calculate("foo")).isNotNull(); } @Test @DisplayName("Ending a non-existing sum, causes an IllegalStateException") - public void testEndSumCausesExceptionIfNotExists() - { - AdderBusinessLogic adder = new AdderBusinessLogic(); - assertThatIllegalStateException().isThrownBy(() -> adder.endSum("foo")); - } - - @Test - @DisplayName("Adding to a non-existent sum causes an IllegalStateException") - public void testAddToSumCausesExceptionIfNotExists() + public void testCalculateCausesExceptionIfNotExists() { AdderBusinessLogic adder = new AdderBusinessLogic(); - assertThatIllegalStateException().isThrownBy(() -> adder.addToSum("foo", 1)); + assertThatIllegalStateException().isThrownBy(() -> adder.calculate("foo")); } @Test - @DisplayName("Adding a null-value to an existing sum causes an IllegalArgumentException") - public void testAddSumWithNullValueToExistingSumCausesException() + @DisplayName("Adding a null-value to a sum causes an IllegalArgumentException") + public void testAddToSumWithNullValueCausesException() { AdderBusinessLogic adder = new AdderBusinessLogic(); - adder.startSum("foo"); assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", null)); } @ParameterizedTest(name = "{index}: Adding {0}") - @DisplayName("Adding a non-positive value to an existing sum causes an IllegalArgumentException") + @DisplayName("Adding a non-positive value to a sum causes an IllegalArgumentException") @ValueSource(ints = { 0, -1, -6, -66, Integer.MIN_VALUE }) - public void testAddSumWithNonPositiveValueToExistingSumCausesException(int value) + public void testAddToSumWithNonPositiveValueCausesException(int value) { AdderBusinessLogic adder = new AdderBusinessLogic(); - adder.startSum("foo"); assertThatIllegalArgumentException().isThrownBy(() -> adder.addToSum("foo", value)); } @ParameterizedTest(name = "{index}: Adding {0}") - @DisplayName("Can add a positive value to an existing sum") + @DisplayName("Can add a positive value to a sum") @ValueSource(ints = { 1, 3, 6, 66, 7, 9 }) - public void testAddSumWithPositiveValuePossibleIfSumExists(int value) + public void testAddToSumWithPositiveValuePossible(int value) { AdderBusinessLogic adder = new AdderBusinessLogic(); - adder.startSum("foo"); assertThatNoException().isThrownBy(() -> adder.addToSum("foo", value)); } @ParameterizedTest(name = "{index}: Summing up {0}") @DisplayName("Adds up numbers correctly") @MethodSource("numbersProvider") - public void testAddSumAddsUpNumbersCorrectlyIfSumExists(int... numbers) + public void testAddToSumAddsUpNumbersCorrectlyIfSumExists(int... numbers) { long expectedResult = Arrays.stream(numbers).sum(); AdderBusinessLogic adder = new AdderBusinessLogic(); - adder.startSum("foo"); Arrays.stream(numbers).forEach(number -> adder.addToSum("foo", number)); - assertThat(adder.endSum("foo")).isEqualTo(expectedResult); + assertThat(adder.calculate("foo")).isEqualTo(expectedResult); } static Stream numbersProvider() { diff --git a/src/test/java/de/juplo/kafka/ApplicationTests.java b/src/test/java/de/juplo/kafka/ApplicationTests.java index 4ddf8a9..9dda079 100644 --- a/src/test/java/de/juplo/kafka/ApplicationTests.java +++ b/src/test/java/de/juplo/kafka/ApplicationTests.java @@ -22,8 +22,7 @@ public class ApplicationTests extends GenericApplicationTests .mapToObj(i -> "seeräuber-" + i) .toArray(i -> new String[i]); final StringSerializer stringSerializer = new StringSerializer(); - final Bytes startMessage = new Bytes(stringSerializer.serialize(TOPIC, "START")); - final Bytes endMessage = new Bytes(stringSerializer.serialize(TOPIC, "END")); + final Bytes calculateMessage = new Bytes(stringSerializer.serialize(TOPIC, "CALCULATE")); int counter = 0; @@ -43,13 +42,12 @@ public class ApplicationTests extends GenericApplicationTests Bytes key = new Bytes(stringSerializer.serialize(TOPIC, seeräuber)); - send(key, startMessage, logicErrors, messageSender); for (int message = 1; message <= number; message++) { Bytes value = new Bytes(stringSerializer.serialize(TOPIC, Integer.toString(message))); send(key, value, logicErrors, messageSender); } - send(key, endMessage, logicErrors, messageSender); + send(key, calculateMessage, logicErrors, messageSender); } return counter; @@ -67,7 +65,7 @@ public class ApplicationTests extends GenericApplicationTests { if (logicErrors) { - value = value.equals(startMessage) ? endMessage : startMessage; + value = new Bytes(stringSerializer.serialize(TOPIC, Integer.toString(-1))); } }