This is an automated email from the ASF dual-hosted git repository. lburgazzoli pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
commit 9058f7c4598441e90ced3fd6537a6cb6ad0e58cf Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Thu Jun 20 10:48:02 2019 +0200 Add camel-k-runtime-knative to provide custom steps for the yaml dsl --- .../maven/processors/CatalogProcessor_3_x.java | 18 +++++ ...cessor3Test.java => CatalogProcessor3Test.java} | 17 +++- camel-k-runtime-bom/pom.xml | 17 ++-- camel-k-runtime-knative/pom.xml | 89 +++++++++++++++++++++ .../k/knative/yaml/parser/KnativeStepParser.java | 78 ++++++++++++++++++ .../services/org/apache/camel/k/yaml/knative | 18 +++++ .../knative/yaml/parser/KnativeConverterTest.java | 93 ++++++++++++++++++++++ .../src/test/resources/log4j2-test.xml | 17 ++++ .../src/test/resources/route.yaml | 28 +++++++ 9 files changed, 367 insertions(+), 8 deletions(-) diff --git a/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/processors/CatalogProcessor_3_x.java b/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/processors/CatalogProcessor_3_x.java index 2596740..9285701 100644 --- a/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/processors/CatalogProcessor_3_x.java +++ b/camel-k-maven-plugin/src/main/java/org/apache/camel/k/tooling/maven/processors/CatalogProcessor_3_x.java @@ -180,6 +180,24 @@ public class CatalogProcessor_3_x implements CatalogProcessor { // ************************ // + // camel-k-runtime-knative + // + // ************************ + + { + CamelArtifact artifact = new CamelArtifact(); + artifact.setGroupId("org.apache.camel.k"); + artifact.setArtifactId("camel-k-runtime-knative"); + artifact.addDependency("org.apache.camel.k", "camel-k-runtime-yaml"); + artifact.addDependency("org.apache.camel.k", "camel-knative"); + artifact.addDependency("org.apache.camel.k", "camel-knative-http"); + artifact.addDependency("org.apache.camel", "camel-netty4-http"); + + artifacts.put(artifact.getArtifactId(), artifact); + } + + // ************************ + // // // // ************************ diff --git a/camel-k-maven-plugin/src/test/java/org/apache/camel/k/tooling/maven/processors/CataloProcessor3Test.java b/camel-k-maven-plugin/src/test/java/org/apache/camel/k/tooling/maven/processors/CatalogProcessor3Test.java similarity index 82% rename from camel-k-maven-plugin/src/test/java/org/apache/camel/k/tooling/maven/processors/CataloProcessor3Test.java rename to camel-k-maven-plugin/src/test/java/org/apache/camel/k/tooling/maven/processors/CatalogProcessor3Test.java index cdf55fc..4b3f6a7 100644 --- a/camel-k-maven-plugin/src/test/java/org/apache/camel/k/tooling/maven/processors/CataloProcessor3Test.java +++ b/camel-k-maven-plugin/src/test/java/org/apache/camel/k/tooling/maven/processors/CatalogProcessor3Test.java @@ -28,8 +28,7 @@ import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; -public class CataloProcessor3Test extends AbstractCataloProcessorTest { - +public class CatalogProcessor3Test extends AbstractCataloProcessorTest { @Test public void testAcceptHyphen(){ CatalogProcessor_3_x cp3 = new CatalogProcessor_3_x(); @@ -108,6 +107,20 @@ public class CataloProcessor3Test extends AbstractCataloProcessorTest { d -> d.getGroupId().equals("org.apache.camel") && d.getArtifactId().equals("camel-servlet") ); }); + assertThat(artifactMap.get("camel-k-runtime-knative")).satisfies(a -> { + assertThat(a.getDependencies()).anyMatch( + d -> d.getGroupId().equals("org.apache.camel.k") && d.getArtifactId().equals("camel-knative") + ); + assertThat(a.getDependencies()).anyMatch( + d -> d.getGroupId().equals("org.apache.camel.k") && d.getArtifactId().equals("camel-knative-http") + ); + assertThat(a.getDependencies()).anyMatch( + d -> d.getGroupId().equals("org.apache.camel.k") && d.getArtifactId().equals("camel-k-runtime-yaml") + ); + assertThat(a.getDependencies()).anyMatch( + d -> d.getGroupId().equals("org.apache.camel") && d.getArtifactId().equals("camel-netty4-http") + ); + }); assertThat(artifactMap.get("camel-knative")).satisfies(a -> { assertThat(a.getDependencies()).anyMatch( diff --git a/camel-k-runtime-bom/pom.xml b/camel-k-runtime-bom/pom.xml index 28e5a61..2933a44 100644 --- a/camel-k-runtime-bom/pom.xml +++ b/camel-k-runtime-bom/pom.xml @@ -92,6 +92,16 @@ <!-- camel-k --> <dependency> <groupId>org.apache.camel.k</groupId> + <artifactId>camel-knative</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.camel.k</groupId> + <artifactId>camel-knative-http</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.camel.k</groupId> <artifactId>camel-k-maven-plugin</artifactId> <version>${project.version}</version> </dependency> @@ -132,12 +142,7 @@ </dependency> <dependency> <groupId>org.apache.camel.k</groupId> - <artifactId>camel-knative</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.apache.camel.k</groupId> - <artifactId>camel-knative-http</artifactId> + <artifactId>camel-k-runtime-knative</artifactId> <version>${project.version}</version> </dependency> diff --git a/camel-k-runtime-knative/pom.xml b/camel-k-runtime-knative/pom.xml new file mode 100644 index 0000000..dd411fc --- /dev/null +++ b/camel-k-runtime-knative/pom.xml @@ -0,0 +1,89 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <groupId>org.apache.camel.k</groupId> + <artifactId>camel-k-runtime-parent</artifactId> + <version>1.0.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>camel-k-runtime-knative</artifactId> + + <dependencies> + <dependency> + <groupId>org.apache.camel.k</groupId> + <artifactId>camel-knative</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.k</groupId> + <artifactId>camel-knative-http</artifactId> + </dependency> + <dependency> + <groupId>org.apache.camel.k</groupId> + <artifactId>camel-k-runtime-yaml</artifactId> + <optional>true</optional> + </dependency> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-core</artifactId> + <scope>provided</scope> + </dependency> + + <!-- ****************************** --> + <!-- --> + <!-- TESTS --> + <!-- --> + <!-- ****************************** --> + + <dependency> + <groupId>org.apache.camel</groupId> + <artifactId>camel-properties</artifactId> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-api</artifactId> + <version>${junit-jupiter.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <version>${junit-jupiter.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.assertj</groupId> + <artifactId>assertj-core</artifactId> + <version>${assertj.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j-impl</artifactId> + <version>${log4j2.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + +</project> diff --git a/camel-k-runtime-knative/src/main/java/org/apache/camel/k/knative/yaml/parser/KnativeStepParser.java b/camel-k-runtime-knative/src/main/java/org/apache/camel/k/knative/yaml/parser/KnativeStepParser.java new file mode 100644 index 0000000..2da32bc --- /dev/null +++ b/camel-k-runtime-knative/src/main/java/org/apache/camel/k/knative/yaml/parser/KnativeStepParser.java @@ -0,0 +1,78 @@ +/** + * 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.k.knative.yaml.parser; + +import java.io.UnsupportedEncodingException; +import java.net.URISyntaxException; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import org.apache.camel.component.knative.Knative; +import org.apache.camel.k.yaml.parser.ProcessorStepParser; +import org.apache.camel.k.yaml.parser.StartStepParser; +import org.apache.camel.k.yaml.parser.StepParserSupport; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.model.RouteDefinition; +import org.apache.camel.model.ToDefinition; +import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.URISupport; + +public class KnativeStepParser implements ProcessorStepParser, StartStepParser { + @Override + public ProcessorDefinition<?> toStartProcessor(Context context) { + final Definition definition = context.node(Definition.class); + final String uri = definition.getEndpointUri(); + final RouteDefinition route = new RouteDefinition().from(uri); + + // steps are mandatory + ObjectHelper.notNull(definition.steps, "from steps"); + + return StepParserSupport.convertSteps( + context, + route, + definition.steps + ); + } + + @Override + public ProcessorDefinition<?> toProcessor(Context context) { + return new ToDefinition(context.node(Definition.class).getEndpointUri()); + } + + public static final class Definition { + public Knative.Type type; + public String name; + public Map<String, Object> parameters; + public List<org.apache.camel.k.yaml.model.Step> steps; + + @JsonIgnore + public String getEndpointUri() { + String answer = String.format("knative:%s/%s", type.name(), name); + + if (parameters != null) { + try { + answer = URISupport.appendParametersToURI(answer, parameters); + } catch (URISyntaxException | UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + return answer; + } + } +} diff --git a/camel-k-runtime-knative/src/main/resources/META-INF/services/org/apache/camel/k/yaml/knative b/camel-k-runtime-knative/src/main/resources/META-INF/services/org/apache/camel/k/yaml/knative new file mode 100644 index 0000000..e2e3d21 --- /dev/null +++ b/camel-k-runtime-knative/src/main/resources/META-INF/services/org/apache/camel/k/yaml/knative @@ -0,0 +1,18 @@ +# +# 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. +# + +class=org.apache.camel.k.knative.yaml.parser.KnativeStepParser \ No newline at end of file diff --git a/camel-k-runtime-knative/src/test/java/org/apache/camel/k/knative/yaml/parser/KnativeConverterTest.java b/camel-k-runtime-knative/src/test/java/org/apache/camel/k/knative/yaml/parser/KnativeConverterTest.java new file mode 100644 index 0000000..4bc7133 --- /dev/null +++ b/camel-k-runtime-knative/src/test/java/org/apache/camel/k/knative/yaml/parser/KnativeConverterTest.java @@ -0,0 +1,93 @@ +/** + * 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.k.knative.yaml.parser; + +import java.util.List; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.k.RoutesLoader; +import org.apache.camel.k.Source; +import org.apache.camel.k.support.RuntimeSupport; +import org.apache.camel.k.yaml.YamlRoutesLoader; +import org.apache.camel.model.FromDefinition; +import org.apache.camel.model.ProcessorDefinition; +import org.apache.camel.model.RouteDefinition; +import org.apache.camel.model.ToDefinition; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class KnativeConverterTest { + + + @Test + public void testLoadRoutes() throws Exception { + DefaultCamelContext context = new DefaultCamelContext(); + Source source = Source.create("classpath:route.yaml"); + RoutesLoader loader = RuntimeSupport.loaderFor(context, source); + RouteBuilder builder = loader.load(context, source); + + assertThat(loader).isInstanceOf(YamlRoutesLoader.class); + assertThat(builder).isNotNull(); + + context.addRoutes(builder); + + List<RouteDefinition> routes = context.getRouteDefinitions(); + assertThat(routes).hasSize(1); + + // definition + assertThat(routes) + .first() + .extracting(RouteDefinition::getId) + .isEqualTo("knative"); + assertThat(routes) + .first() + .extracting(RouteDefinition::getGroup) + .isEqualTo("flows"); + + // input + assertThat(routes) + .first() + .extracting(RouteDefinition::getInput) + .isInstanceOfSatisfying(FromDefinition.class, t-> { + assertThat(t.getEndpointUri()).isEqualTo("knative:endpoint/from"); + }); + + // outputs + assertThat(routes) + .first() + .satisfies(KnativeConverterTest::validateSteps); + } + + private static void validateSteps(RouteDefinition definition) { + List<ProcessorDefinition<?>> outputs = definition.getOutputs(); + + assertThat(outputs).hasSize(2); + + assertThat(outputs) + .element(0) + .isInstanceOfSatisfying(ToDefinition.class, t-> { + assertThat(t.getEndpointUri()).isEqualTo("log:info"); + }); + assertThat(outputs) + .element(1) + .isInstanceOfSatisfying(ToDefinition.class, t-> { + assertThat(t.getEndpointUri()).isEqualTo("knative:endpoint/to"); + }); + } +} diff --git a/camel-k-runtime-knative/src/test/resources/log4j2-test.xml b/camel-k-runtime-knative/src/test/resources/log4j2-test.xml new file mode 100644 index 0000000..9af8521 --- /dev/null +++ b/camel-k-runtime-knative/src/test/resources/log4j2-test.xml @@ -0,0 +1,17 @@ +<?xml version="1.0" encoding="UTF-8"?> +<Configuration status="INFO"> + <Appenders> + <Console name="STDOUT" target="SYSTEM_OUT"> + <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS}|%-5level|%t|%c{1} - %msg%n"/> + </Console> + <Null name="NONE"/> + </Appenders> + + <Loggers> + <Root level="INFO"> + <!--<AppenderRef ref="STDOUT"/>--> + <AppenderRef ref="NONE"/> + </Root> + </Loggers> + +</Configuration> \ No newline at end of file diff --git a/camel-k-runtime-knative/src/test/resources/route.yaml b/camel-k-runtime-knative/src/test/resources/route.yaml new file mode 100644 index 0000000..3e57c82 --- /dev/null +++ b/camel-k-runtime-knative/src/test/resources/route.yaml @@ -0,0 +1,28 @@ +# +# 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. +# + +- id: "knative" + group: "flows" + knative: + type: "endpoint" + name: "from" + steps: + - to: + uri: "log:info" + - knative: + type: "endpoint" + name: "to" \ No newline at end of file