ROT: Zur Summe soll die Zahl ausgegeben werden - Logik + Test angepasst
authorKai Moritz <kai@juplo.de>
Mon, 15 Aug 2022 17:23:56 +0000 (19:23 +0200)
committerKai Moritz <kai@juplo.de>
Mon, 15 Aug 2022 17:47:12 +0000 (19:47 +0200)
* `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.

src/main/java/de/juplo/kafka/AdderBusinessLogic.java
src/main/java/de/juplo/kafka/AdderResult.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/ApplicationRecordHandler.java
src/test/java/de/juplo/kafka/AdderBusinessLogicTest.java

index cbd500d..64fdb8c 100644 (file)
@@ -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<String, Long> 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 (file)
index 0000000..44b7da8
--- /dev/null
@@ -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;
+  }
+}
index ce340a7..828dbc2 100644 (file)
@@ -22,7 +22,7 @@ public class ApplicationRecordHandler implements RecordHandler<String, String>
 
     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;
     }
index 35eed73..8e49263 100644 (file)
@@ -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<Arguments> numbersProvider() {