From 4a8642d0c36413cbc283d35dda8977b0e9320372 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Mon, 15 Aug 2022 19:23:56 +0200 Subject: [PATCH] ROT: Zur Summe soll die Zahl ausgegeben werden - Logik + Test angepasst MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * `AdderBusinessLogic` gibt jetzt ein `AdderResult` zurück, das die Summe zusammen mit der zugehörigen Zahl ausgibt. * Anwendung (insbesondere die Signatur von `AdderBusinessLogic`!) entsprechend angepasst. * Erwartungen an `AdderBusinessLogic` entsprechend überarbeitet. --- .../de/juplo/kafka/AdderBusinessLogic.java | 4 ++-- src/main/java/de/juplo/kafka/AdderResult.java | 21 +++++++++++++++++++ .../juplo/kafka/ApplicationRecordHandler.java | 2 +- .../juplo/kafka/AdderBusinessLogicTest.java | 4 +++- 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 src/main/java/de/juplo/kafka/AdderResult.java diff --git a/src/main/java/de/juplo/kafka/AdderBusinessLogic.java b/src/main/java/de/juplo/kafka/AdderBusinessLogic.java index cbd500d..64fdb8c 100644 --- a/src/main/java/de/juplo/kafka/AdderBusinessLogic.java +++ b/src/main/java/de/juplo/kafka/AdderBusinessLogic.java @@ -39,12 +39,12 @@ public class AdderBusinessLogic state.put(user, sum + value); } - public synchronized Long calculate(String user) + public synchronized AdderResult calculate(String user) { if (!state.containsKey(user)) throw new IllegalStateException("No sumation for " + user + " in progress"); - return state.remove(user); + return new AdderResult(66, state.remove(user)); } protected Map getState() diff --git a/src/main/java/de/juplo/kafka/AdderResult.java b/src/main/java/de/juplo/kafka/AdderResult.java new file mode 100644 index 0000000..44b7da8 --- /dev/null +++ b/src/main/java/de/juplo/kafka/AdderResult.java @@ -0,0 +1,21 @@ +package de.juplo.kafka; + +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + + +@RequiredArgsConstructor +@Getter +@EqualsAndHashCode +public class AdderResult +{ + final int number; + final long sum; + + @Override + public String toString() + { + return "sum(" + number + ") = " + sum; + } +} diff --git a/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java b/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java index ce340a7..828dbc2 100644 --- a/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java +++ b/src/main/java/de/juplo/kafka/ApplicationRecordHandler.java @@ -22,7 +22,7 @@ public class ApplicationRecordHandler implements RecordHandler if (message.equals("CALCULATE")) { - Long result = state.get(partition).calculate(user); + AdderResult result = state.get(partition).calculate(user); log.info("New result for {}: {}", user, result); return; } diff --git a/src/test/java/de/juplo/kafka/AdderBusinessLogicTest.java b/src/test/java/de/juplo/kafka/AdderBusinessLogicTest.java index 35eed73..8e49263 100644 --- a/src/test/java/de/juplo/kafka/AdderBusinessLogicTest.java +++ b/src/test/java/de/juplo/kafka/AdderBusinessLogicTest.java @@ -103,7 +103,9 @@ public class AdderBusinessLogicTest long expectedResult = Arrays.stream(numbers).sum(); AdderBusinessLogic adder = new AdderBusinessLogic(); Arrays.stream(numbers).forEach(number -> adder.addToSum("foo", number)); - assertThat(adder.calculate("foo")).isEqualTo(expectedResult); + AdderResult result = adder.calculate("foo"); + assertThat(result.number).isEqualTo(numbers[numbers.length-1]); + assertThat(result.sum).isEqualTo(expectedResult); } static Stream numbersProvider() { -- 2.20.1