You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ff...@apache.org on 2022/03/03 19:55:51 UTC
[camel-spring-boot] branch main updated: add tests in camel-platform-http-start (#452)
This is an automated email from the ASF dual-hosted git repository.
ffang 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 cd9e6d0 add tests in camel-platform-http-start (#452)
cd9e6d0 is described below
commit cd9e6d075c96706519936f64b71182f734d7313c
Author: Freeman(Yue) Fang <fr...@gmail.com>
AuthorDate: Thu Mar 3 14:51:31 2022 -0500
add tests in camel-platform-http-start (#452)
---
.../camel-platform-http-starter/pom.xml | 18 +++
.../JettyCustomPlatformHttpConsumer.java | 130 +++++++++++++++++++
.../springboot/JettyCustomPlatformHttpEngine.java | 31 +++++
.../platform/http/springboot/JettyServerTest.java | 53 ++++++++
.../platform/http/springboot/PlatformHttpTest.java | 144 +++++++++++++++++++++
...stPlatformHttpContextPathConfigurationTest.java | 130 +++++++++++++++++++
6 files changed, 506 insertions(+)
diff --git a/components-starter/camel-platform-http-starter/pom.xml b/components-starter/camel-platform-http-starter/pom.xml
index 7d40ea1..89a421a 100644
--- a/components-starter/camel-platform-http-starter/pom.xml
+++ b/components-starter/camel-platform-http-starter/pom.xml
@@ -39,6 +39,24 @@
<artifactId>camel-platform-http</artifactId>
<version>${camel-version}</version>
</dependency>
+ <dependency>
+ <groupId>io.rest-assured</groupId>
+ <artifactId>rest-assured</artifactId>
+ <version>${rest-assured-version}</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.eclipse.jetty</groupId>
+ <artifactId>jetty-server</artifactId>
+ <version>${jetty9-version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.eclipse.jetty.orbit</groupId>
+ <artifactId>javax.servlet</artifactId>
+ </exclusion>
+ </exclusions>
+ <scope>test</scope>
+ </dependency>
<!--START OF GENERATED CODE-->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
diff --git a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/JettyCustomPlatformHttpConsumer.java b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/JettyCustomPlatformHttpConsumer.java
new file mode 100644
index 0000000..a7cdb13
--- /dev/null
+++ b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/JettyCustomPlatformHttpConsumer.java
@@ -0,0 +1,130 @@
+/*
+ * 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.component.platform.http.springboot;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.util.regex.Pattern;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.component.platform.http.PlatformHttpEndpoint;
+import org.apache.camel.support.CamelContextHelper;
+import org.apache.camel.support.DefaultConsumer;
+import org.apache.camel.support.DefaultMessage;
+import org.eclipse.jetty.server.Request;
+import org.eclipse.jetty.server.handler.AbstractHandler;
+import org.eclipse.jetty.server.handler.ContextHandler;
+
+public class JettyCustomPlatformHttpConsumer extends DefaultConsumer {
+ private static final Pattern PATH_PARAMETER_PATTERN = Pattern.compile("\\{([^/}]+)\\}");
+
+ public JettyCustomPlatformHttpConsumer(PlatformHttpEndpoint endpoint, Processor processor) {
+ super(endpoint, processor);
+ }
+
+ @Override
+ protected void doStart() throws Exception {
+ super.doStart();
+ final PlatformHttpEndpoint endpoint = getEndpoint();
+ final String path = configureEndpointPath(endpoint);
+
+ JettyServerTest jettyServerTest = CamelContextHelper.mandatoryLookup(
+ getEndpoint().getCamelContext(),
+ JettyServerTest.JETTY_SERVER_NAME,
+ JettyServerTest.class);
+
+ ContextHandler contextHandler = createHandler(endpoint, path);
+ // add handler after starting server.
+ jettyServerTest.addHandler(contextHandler);
+
+ }
+
+ private ContextHandler createHandler(PlatformHttpEndpoint endpoint, String path) {
+ ContextHandler contextHandler = new ContextHandler();
+ contextHandler.setContextPath(path);
+ contextHandler.setResourceBase(".");
+ contextHandler.setClassLoader(Thread.currentThread().getContextClassLoader());
+ contextHandler.setAllowNullPathInfo(true);
+ contextHandler.setHandler(new AbstractHandler() {
+ @Override
+ public void handle(
+ String s, Request request, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
+ throws IOException, ServletException {
+ Exchange exchg = null;
+ try {
+ BufferedReader reader = httpServletRequest.getReader();
+ String bodyRequest = "";
+ String strCurrentLine = "";
+ while ((strCurrentLine = reader.readLine()) != null) {
+ bodyRequest += strCurrentLine;
+ }
+ final Exchange exchange = exchg = toExchange(request, bodyRequest);
+ createUoW(exchange);
+ getProcessor().process(
+ exchange);
+ httpServletResponse.setStatus(HttpServletResponse.SC_OK);
+ request.setHandled(true);
+ httpServletResponse.getWriter().println(exchange.getMessage().getBody());
+ } catch (Exception e) {
+ getExceptionHandler().handleException("Failed handling platform-http endpoint " + endpoint.getPath(), exchg,
+ e);
+ } finally {
+ if (exchg != null) {
+ doneUoW(exchg);
+ }
+ }
+ }
+ });
+ return contextHandler;
+ }
+
+ private Exchange toExchange(Request request, String body) {
+ final Exchange exchange = getEndpoint().createExchange();
+ final Message message = new DefaultMessage(exchange);
+
+ final String charset = request.getHeader("charset");
+ if (charset != null) {
+ exchange.setProperty(Exchange.CHARSET_NAME, charset);
+ message.setHeader(Exchange.HTTP_CHARACTER_ENCODING, charset);
+ }
+
+ message.setBody(body.length() != 0 ? body : null);
+ exchange.setMessage(message);
+ return exchange;
+ }
+
+ @Override
+ public PlatformHttpEndpoint getEndpoint() {
+ return (PlatformHttpEndpoint) super.getEndpoint();
+ }
+
+ private String configureEndpointPath(PlatformHttpEndpoint endpoint) {
+ String path = endpoint.getPath();
+ if (endpoint.isMatchOnUriPrefix()) {
+ path += "*";
+ }
+ // Transform from the Camel path param syntax /path/{key} to vert.x web's /path/:key
+ return PATH_PARAMETER_PATTERN.matcher(path).replaceAll(":$1");
+ }
+
+}
diff --git a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/JettyCustomPlatformHttpEngine.java b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/JettyCustomPlatformHttpEngine.java
new file mode 100644
index 0000000..fcf83de
--- /dev/null
+++ b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/JettyCustomPlatformHttpEngine.java
@@ -0,0 +1,31 @@
+/*
+ * 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.component.platform.http.springboot;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.component.platform.http.PlatformHttpEndpoint;
+import org.apache.camel.component.platform.http.spi.PlatformHttpEngine;
+
+
+public class JettyCustomPlatformHttpEngine implements PlatformHttpEngine {
+
+ @Override
+ public Consumer createConsumer(PlatformHttpEndpoint platformHttpEndpoint, Processor processor) {
+ return new JettyCustomPlatformHttpConsumer(platformHttpEndpoint, processor);
+ }
+}
diff --git a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/JettyServerTest.java b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/JettyServerTest.java
new file mode 100644
index 0000000..3b0c7ab
--- /dev/null
+++ b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/JettyServerTest.java
@@ -0,0 +1,53 @@
+/*
+ * 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.component.platform.http.springboot;
+
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.handler.ContextHandler;
+import org.eclipse.jetty.server.handler.HandlerCollection;
+
+public class JettyServerTest {
+ public static final String JETTY_SERVER_NAME = "JettyServerTest";
+
+ private Server server;
+ private HandlerCollection contextHandlerCollection;
+
+ public JettyServerTest(int port) {
+ server = new Server(port);
+ contextHandlerCollection = new HandlerCollection(true);
+ server.setHandler(contextHandlerCollection);
+ }
+
+ public void start() throws Exception {
+ server.start();
+ }
+
+ public void stop() throws Exception {
+ server.stop();
+ }
+
+ /**
+ * adds a context handler and starts it
+ *
+ * @param contextHandler
+ * @throws Exception
+ */
+ public void addHandler(ContextHandler contextHandler) throws Exception {
+ contextHandlerCollection.addHandler(contextHandler);
+ contextHandler.start();
+ }
+}
diff --git a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/PlatformHttpTest.java b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/PlatformHttpTest.java
new file mode 100644
index 0000000..8009c87
--- /dev/null
+++ b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/PlatformHttpTest.java
@@ -0,0 +1,144 @@
+/*
+ * 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.component.platform.http.springboot;
+
+
+import static io.restassured.RestAssured.given;
+
+import java.util.Iterator;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.platform.http.PlatformHttpComponent;
+import org.apache.camel.component.platform.http.PlatformHttpConstants;
+import org.apache.camel.spring.boot.CamelAutoConfiguration;
+import org.apache.camel.spring.boot.CamelContextConfiguration;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import io.restassured.RestAssured;
+import io.restassured.response.Response;
+import io.restassured.specification.RequestSpecification;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.test.annotation.DirtiesContext;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
+
+
+@DirtiesContext
+@CamelSpringBootTest
+@SpringBootTest(
+ classes = {
+ CamelAutoConfiguration.class,
+ PlatformHttpTest.class,
+ PlatformHttpTest.TestConfiguration.class
+ }
+)
+public class PlatformHttpTest {
+
+ static int port = AvailablePortFinder.getNextAvailable();
+
+ @Autowired
+ private CamelContext context;
+
+
+
+
+ @Bean
+ CamelContextConfiguration contextConfiguration() {
+ return new CamelContextConfiguration() {
+ @Override
+ public void beforeApplicationStart(CamelContext context) {
+ context.getRegistry().bind(PlatformHttpConstants.PLATFORM_HTTP_ENGINE_FACTORY, new JettyCustomPlatformHttpEngine());
+
+
+ JettyServerTest server = new JettyServerTest(port);
+
+ context.getRegistry().bind(JettyServerTest.JETTY_SERVER_NAME, server);
+ try {
+ server.start();
+ } catch (Exception e) {
+
+ e.printStackTrace();
+ }
+
+ }
+ @Override
+ public void afterApplicationStart(CamelContext camelContext) {
+ //do nothing here
+ }
+
+ };
+ }
+
+ @Test
+ public void testGet() throws Exception {
+ given()
+ .header("Accept", "application/json")
+ .port(port)
+ .expect()
+ .statusCode(200)
+ .when()
+ .get("/get");
+ }
+
+ @Test
+ public void testPost() {
+ RequestSpecification request = RestAssured.given();
+ request.port(port);
+ request.body("test");
+ Response response = request.get("/post");
+
+ int statusCode = response.getStatusCode();
+ assertEquals(200, statusCode);
+ assertEquals("TEST", response.body().asString().trim());
+
+ PlatformHttpComponent phc = context.getComponent("platform-http", PlatformHttpComponent.class);
+ assertEquals(2, phc.getHttpEndpoints().size());
+ Iterator<String> it = phc.getHttpEndpoints().iterator();
+ assertEquals("/get", it.next());
+ assertEquals("/post", it.next());
+ }
+
+
+ // *************************************
+ // Config
+ // *************************************
+
+ @Configuration
+ public static class TestConfiguration {
+
+ @Bean
+ public RouteBuilder routeBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("platform-http:/get")
+ .setBody().constant("get");
+ from("platform-http:/post")
+ .transform().body(String.class, b -> b.toUpperCase());
+ }
+ };
+ }
+ }
+}
diff --git a/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/RestPlatformHttpContextPathConfigurationTest.java b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/RestPlatformHttpContextPathConfigurationTest.java
new file mode 100644
index 0000000..aa717a5
--- /dev/null
+++ b/components-starter/camel-platform-http-starter/src/test/java/org/apache/camel/component/platform/http/springboot/RestPlatformHttpContextPathConfigurationTest.java
@@ -0,0 +1,130 @@
+/*
+ * 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.component.platform.http.springboot;
+
+
+import static org.hamcrest.CoreMatchers.containsString;
+
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.platform.http.PlatformHttpConstants;
+import org.apache.camel.spring.boot.CamelAutoConfiguration;
+import org.apache.camel.spring.boot.CamelContextConfiguration;
+
+import org.junit.jupiter.api.Test;
+
+
+import io.restassured.RestAssured;
+
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.test.annotation.DirtiesContext;
+import org.apache.camel.test.AvailablePortFinder;
+import org.apache.camel.test.spring.junit5.CamelSpringBootTest;
+
+
+@DirtiesContext
+@CamelSpringBootTest
+@SpringBootTest(
+ classes = {
+ CamelAutoConfiguration.class,
+ RestPlatformHttpContextPathConfigurationTest.class,
+ RestPlatformHttpContextPathConfigurationTest.TestConfiguration.class
+ }
+)
+public class RestPlatformHttpContextPathConfigurationTest {
+
+ static int port = AvailablePortFinder.getNextAvailable();
+
+
+ @Bean
+ CamelContextConfiguration contextConfiguration() {
+ return new CamelContextConfiguration() {
+ @Override
+ public void beforeApplicationStart(CamelContext context) {
+ context.getRegistry().bind(PlatformHttpConstants.PLATFORM_HTTP_ENGINE_FACTORY, new JettyCustomPlatformHttpEngine());
+
+
+ JettyServerTest server = new JettyServerTest(port);
+
+ context.getRegistry().bind(JettyServerTest.JETTY_SERVER_NAME, server);
+ try {
+ server.start();
+ } catch (Exception e) {
+
+ e.printStackTrace();
+ }
+
+ }
+ @Override
+ public void afterApplicationStart(CamelContext camelContext) {
+ //do nothing here
+ }
+
+ };
+ }
+
+ @Test
+ public void contextPath() {
+ RestAssured.given()
+ .port(port)
+ .get("/rest/get")
+ .then()
+ .body(containsString("GET: /get"));
+
+ RestAssured.given()
+ .port(port)
+ .contentType("text/plain")
+ .post("/rest/post")
+ .then()
+ .body(containsString("POST: /post"));
+ }
+
+
+
+ // *************************************
+ // Config
+ // *************************************
+
+ @Configuration
+ public static class TestConfiguration {
+
+ @Bean
+ public RouteBuilder routeBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ restConfiguration()
+ .component("platform-http")
+ .contextPath("/rest");
+
+ rest()
+ .get("/get").to("direct:get")
+ .post("/post").consumes("text/plain").produces("text/plain").to("direct:post");
+
+ from("direct:get")
+ .setBody(constant("GET: /get"));
+ from("direct:post")
+ .setBody(constant("POST: /post"));
+
+ }
+ };
+ }
+ }
+}