davsclaus commented on code in PR #29049:
URL: https://github.com/apache/camel/pull/29049#discussion_r4451683244


##########
components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/CounterRouteTest.java:
##########
@@ -136,4 +163,66 @@ public void testUsingScriptEvaluation() throws Exception {
         assertEquals(Integer.toString(message.length()), 
counter.getId().getTag("a"));
         endpoint.assertIsSatisfied();
     }
+
+    @Test
+    public void testIncrementFromBody() throws Exception {
+        endpoint.expectedMessageCount(1);
+        producer5.sendBody("42");
+        assertEquals(42.0D, registry.find("E").counter().count(), 0.01D);
+        endpoint.assertIsSatisfied();
+    }
+
+    @Test
+    public void testIncrementFromBodyNotNumber() throws Exception {
+        endpoint.expectedMessageCount(0);
+
+        assertIncrementFails(Map.of("count", 5));
+        assertIncrementFails(List.of(new Object()));
+        assertIncrementFails(new Object());
+        assertIncrementFails(null);
+
+        // the counter is not updated (it was incremented by 1 for each of 
them)
+        assertEquals(0.0D, registry.find("E").counter().count(), 0.01D);

Review Comment:
   Fixed in ad78c4154ad0: the comment now says the counter is not updated 
because the exchange fails with `IllegalArgumentException`.
   
   _Claude Code on behalf of Claus Ibsen (davsclaus)_



##########
components/camel-micrometer/src/test/java/org/apache/camel/component/micrometer/CounterRouteTest.java:
##########
@@ -136,4 +163,66 @@ public void testUsingScriptEvaluation() throws Exception {
         assertEquals(Integer.toString(message.length()), 
counter.getId().getTag("a"));
         endpoint.assertIsSatisfied();
     }
+
+    @Test
+    public void testIncrementFromBody() throws Exception {
+        endpoint.expectedMessageCount(1);
+        producer5.sendBody("42");
+        assertEquals(42.0D, registry.find("E").counter().count(), 0.01D);
+        endpoint.assertIsSatisfied();
+    }
+
+    @Test
+    public void testIncrementFromBodyNotNumber() throws Exception {
+        endpoint.expectedMessageCount(0);
+
+        assertIncrementFails(Map.of("count", 5));
+        assertIncrementFails(List.of(new Object()));
+        assertIncrementFails(new Object());
+        assertIncrementFails(null);
+
+        // the counter is not updated (it was incremented by 1 for each of 
them)
+        assertEquals(0.0D, registry.find("E").counter().count(), 0.01D);
+        endpoint.assertIsSatisfied();
+    }
+
+    @Test
+    public void testIncrementOrDecrementFromHeader() throws Exception {
+        endpoint.expectedMessageCount(2);
+        producer6.sendBodyAndHeader(null, "inc", 10.0D);
+        producer6.sendBodyAndHeader(null, "dec", 3.0D);
+        assertEquals(7.0D, registry.find("F").counter().count(), 0.01D);
+        endpoint.assertIsSatisfied();
+    }
+
+    @Test
+    public void testIncrementOrDecrementFromHeaderMissing() throws Exception {
+        endpoint.expectedMessageCount(0);
+
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> producer6.sendBody("Hello"));
+        assertInstanceOf(IllegalArgumentException.class, e.getCause());
+        assertEquals("The increment (${header.inc}) or decrement 
(${header.dec}) of counter F did not evaluate to a number",
+                e.getCause().getMessage());
+
+        assertEquals(0.0D, registry.find("F").counter().count(), 0.01D);
+        endpoint.assertIsSatisfied();
+    }
+
+    @Test
+    public void testNoIncrementOrDecrement() throws Exception {
+        endpoint.expectedMessageCount(2);
+        producer7.sendBody(new Object());
+        producer7.sendBody(null);
+        assertEquals(2.0D, registry.find("G").counter().count(), 0.01D);
+        endpoint.assertIsSatisfied();
+    }
+
+    private void assertIncrementFails(Object body) {
+        CamelExecutionException e = assertThrows(CamelExecutionException.class,
+                () -> producer5.sendBody(body));
+        assertInstanceOf(IllegalArgumentException.class, e.getCause());
+        assertEquals("The increment (${body}) or decrement (null) of counter E 
did not evaluate to a number",
+                e.getCause().getMessage());

Review Comment:
   Fixed in ad78c4154ad0: the message now names only the configured option(s): 
`The increment (${body}) of counter E did not evaluate to a number`, `The 
decrement (...) of counter X ...`, or with both set `The increment (...) and 
decrement (...) of counter X ...`. Added a decrement-only test. #29019 
(camel-opentelemetry-metrics) gets the same message.
   
   _Claude Code on behalf of Claus Ibsen (davsclaus)_



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to