From: Kai Moritz Date: Mon, 15 Aug 2022 17:23:56 +0000 (+0200) Subject: ROT: Zur Summe soll die Zahl ausgegeben werden - Logik + Test angepasst X-Git-Tag: sumup-adder---lvm-2-tage~15 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=4a8642d0c36413cbc283d35dda8977b0e9320372;p=demos%2Fkafka%2Ftraining ROT: Zur Summe soll die Zahl ausgegeben werden - Logik + Test angepasst * `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. --- 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() {