This is an automated email from the ASF dual-hosted git repository.

fmariani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git


The following commit(s) were added to refs/heads/main by this push:
     new 4137e0bf7e5 Add probes test
4137e0bf7e5 is described below

commit 4137e0bf7e582d9479fa4426ab4e3bd46dd64b81
Author: Croway <[email protected]>
AuthorDate: Tue Nov 21 12:03:21 2023 +0100

    Add probes test
---
 core/camel-spring-boot-xml/pom.xml                 |  6 ++
 core/camel-spring-boot/pom.xml                     |  5 ++
 .../boot/actuate/health/CamelProbesTest.java       | 98 ++++++++++++++++++++++
 .../spring/boot/actuate/health/ProbesRoute.java    | 34 ++++++++
 4 files changed, 143 insertions(+)

diff --git a/core/camel-spring-boot-xml/pom.xml 
b/core/camel-spring-boot-xml/pom.xml
index 7f01eb3ec16..2d3789d295f 100644
--- a/core/camel-spring-boot-xml/pom.xml
+++ b/core/camel-spring-boot-xml/pom.xml
@@ -88,6 +88,12 @@
             <version>${spring-boot-version}</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-actuator</artifactId>
+            <version>${spring-boot-version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/core/camel-spring-boot/pom.xml b/core/camel-spring-boot/pom.xml
index d0d06f98bf6..e5c95f8c65f 100644
--- a/core/camel-spring-boot/pom.xml
+++ b/core/camel-spring-boot/pom.xml
@@ -158,6 +158,11 @@
             <artifactId>camel-http</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-kafka</artifactId>
+            <scope>test</scope>
+        </dependency>
 
     </dependencies>
 
diff --git 
a/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/health/CamelProbesTest.java
 
b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/health/CamelProbesTest.java
new file mode 100644
index 00000000000..6b443f31ada
--- /dev/null
+++ 
b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/health/CamelProbesTest.java
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.spring.boot.actuate.health;
+
+import org.apache.camel.spring.boot.CamelAutoConfiguration;
+import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
+import org.assertj.core.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.web.server.LocalManagementPort;
+import org.springframework.boot.web.client.RestTemplateBuilder;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatusCode;
+import org.springframework.http.ResponseEntity;
+import org.springframework.http.client.ClientHttpResponse;
+import org.springframework.web.client.ResponseErrorHandler;
+
+import java.io.IOException;
+
+@CamelSpringBootTest
+@EnableAutoConfiguration
+@SpringBootApplication
+@SpringBootTest(
+               webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
+               classes = {
+                               CamelAutoConfiguration.class,
+                               CamelHealthCheckAutoConfiguration.class,
+                               CamelAvailabilityCheckAutoConfiguration.class,
+                               ProbesRoute.class},
+               properties = {
+                               
"camel.springboot.java-routes-include-pattern=**/ProbesRoute*",
+                               
"management.endpoint.health.probes.enabled=true",
+                               
"management.endpoint.health.group.readiness.include=readinessState,camelReadinessState",
+                               
"management.endpoint.health.group.liveness.include=livenessState,camelLivenessState"})
+public class CamelProbesTest {
+
+       @Autowired
+       RestTemplateBuilder restTemplateBuilder;
+
+       @LocalManagementPort
+       int managementPort;
+
+       @Test
+       public void testMetrics(){
+               ResponseEntity<String> livenessResponse = restTemplateBuilder
+                               .rootUri("http://localhost:"; + managementPort + 
"/actuator")
+                               .build().exchange("/health/liveness", 
HttpMethod.GET, new HttpEntity<>(null), String.class);
+
+               ResponseEntity<String> readinessResponse = restTemplateBuilder
+                               .errorHandler(new NoOpErrorHandler())
+                               .rootUri("http://localhost:"; + managementPort + 
"/actuator")
+                               .build().exchange("/health/readiness", 
HttpMethod.GET, new HttpEntity<>(null), String.class);
+
+               ResponseEntity<String> healthResponse = restTemplateBuilder
+                               .errorHandler(new NoOpErrorHandler())
+                               .rootUri("http://localhost:"; + managementPort + 
"/actuator")
+                               .build().exchange("/health", HttpMethod.GET, 
new HttpEntity<>(null), String.class);
+
+               
Assertions.assertThat(livenessResponse.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(200));
+               
Assertions.assertThat(livenessResponse.getBody()).isEqualTo("{\"status\":\"UP\"}");
+
+               
Assertions.assertThat(readinessResponse.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(503));
+               
Assertions.assertThat(readinessResponse.getBody()).isEqualTo("{\"status\":\"OUT_OF_SERVICE\"}");
+
+               
Assertions.assertThat(healthResponse.getStatusCode()).isEqualTo(HttpStatusCode.valueOf(503));
+       }
+}
+
+class NoOpErrorHandler implements ResponseErrorHandler {
+
+       @Override
+       public boolean hasError(ClientHttpResponse response) throws IOException 
{
+               return false;
+       }
+
+       @Override
+       public void handleError(ClientHttpResponse response) throws IOException 
{
+               // no-op
+       }
+}
diff --git 
a/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/health/ProbesRoute.java
 
b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/health/ProbesRoute.java
new file mode 100644
index 00000000000..2390ef9ccd0
--- /dev/null
+++ 
b/core/camel-spring-boot/src/test/java/org/apache/camel/spring/boot/actuate/health/ProbesRoute.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.spring.boot.actuate.health;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.AvailablePortFinder;
+import org.springframework.stereotype.Component;
+
+@Component
+public class ProbesRoute extends RouteBuilder {
+
+       private final int port = AvailablePortFinder.getNextRandomAvailable();
+
+       @Override
+       public void configure() throws Exception {
+               from("kafka:topic?brokers=localhost:" + port)
+                               .routeId("kafka-route")
+                               .to("log:foo");
+       }
+}

Reply via email to