diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java index 9885e1a579cd9..5d53aab0a46d8 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelJBangMain.java @@ -21,7 +21,6 @@ import org.apache.camel.catalog.CamelCatalog; import org.apache.camel.catalog.DefaultCamelCatalog; import org.apache.camel.dsl.jbang.core.commands.action.*; -import org.apache.camel.dsl.jbang.core.commands.bind.Bind; import org.apache.camel.dsl.jbang.core.commands.catalog.CatalogCommand; import org.apache.camel.dsl.jbang.core.commands.catalog.CatalogComponent; import org.apache.camel.dsl.jbang.core.commands.catalog.CatalogDataFormat; @@ -162,7 +161,6 @@ public static void run(CamelJBangMain main, String... args) { .addSubcommand("other", new CommandLine(new CatalogOther(main))) .addSubcommand("kamelet", new CommandLine(new CatalogKamelet(main)))) .addSubcommand("doc", new CommandLine(new CatalogDoc(main))) - .addSubcommand("bind", new CommandLine(new Bind(main))) .addSubcommand("script", new CommandLine(new Script(main))) .addSubcommand("jolokia", new CommandLine(new Jolokia(main))) .addSubcommand("hawtio", new CommandLine(new Hawtio(main))) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/Bind.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/Bind.java deleted file mode 100644 index 28ec264787854..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/Bind.java +++ /dev/null @@ -1,304 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import java.io.FileOutputStream; -import java.io.InputStream; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Stack; -import java.util.stream.Collectors; - -import org.apache.camel.CamelException; -import org.apache.camel.dsl.jbang.core.commands.CamelCommand; -import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain; -import org.apache.camel.dsl.jbang.core.common.JSonHelper; -import org.apache.camel.dsl.jbang.core.common.YamlHelper; -import org.apache.camel.util.FileUtil; -import org.apache.camel.util.IOHelper; -import org.apache.camel.util.json.Jsoner; -import picocli.CommandLine; -import picocli.CommandLine.Command; - -@Command(name = "bind", description = "Bind source and sink Kamelets as a new Camel integration", - sortOptions = false, showDefaultValues = true) -public class Bind extends CamelCommand { - - @CommandLine.Parameters(description = "Name of binding file to be saved", arity = "1", - paramLabel = "", parameterConsumer = FileConsumer.class) - Path filePath; // Defined only for file path completion; the field never used - String file; - - @CommandLine.Option(names = { "--source" }, description = "Source (from) such as a Kamelet or Camel endpoint uri", - required = true) - String source; - - @CommandLine.Option(names = { "--step" }, description = "Optional steps such as a Kamelet or Camel endpoint uri") - String[] steps; - - @CommandLine.Option(names = { "--sink" }, description = "Sink (to) such as a Kamelet or Camel endpoint uri", - required = true) - String sink; - - @CommandLine.Option(names = { "--error-handler" }, - description = "Add error handler (none|log|sink:). Sink endpoints are expected in the format \"[[apigroup/]version:]kind:[namespace/]name\", plain Camel URIs or Kamelet name.") - String errorHandler; - - @CommandLine.Option(names = { "--property" }, - description = "Adds a pipe property in the form of [source|sink|error-handler|step-].= where is the step number starting from 1", - arity = "0") - String[] properties; - - @CommandLine.Option(names = { "--output" }, - defaultValue = "file", - description = "Output format generated by this command (supports: file, yaml or json).") - String output; - - private final TemplateProvider templateProvider; - - // Available binding providers, order in array is important! - private final BindingProvider[] bindingProviders = new BindingProvider[] { - new PipeProvider(), - new KnativeBrokerBindingProvider(), - new KnativeChannelBindingProvider(), - new StrimziKafkaTopicBindingProvider(), - new ObjectReferenceBindingProvider(), - new UriBindingProvider() - }; - - public Bind(CamelJBangMain main) { - this(main, new TemplateProvider() { - }); - } - - public Bind(CamelJBangMain main, TemplateProvider templateProvider) { - super(main); - this.templateProvider = templateProvider; - } - - @Override - public Integer doCall() throws Exception { - String pipe = constructPipe(); - - if (pipe.isEmpty()) { - printer().printErr("Failed to construct Pipe resource"); - return -1; - } - - return dumpPipe(pipe); - } - - public String constructPipe() throws Exception { - try { - String sourceEndpoint = resolveEndpoint(BindingProvider.EndpointType.SOURCE, source, getProperties("source")); - String sinkEndpoint = resolveEndpoint(BindingProvider.EndpointType.SINK, sink, getProperties("sink")); - - InputStream is = templateProvider.getPipeTemplate(); - String context = IOHelper.loadText(is); - IOHelper.close(is); - - String stepsContext = ""; - if (steps != null) { - StringBuilder sb = new StringBuilder("\n steps:\n"); - for (int i = 0; i < steps.length; i++) { - sb.append(resolveEndpoint(BindingProvider.EndpointType.STEP, steps[i], - getProperties("step-%d".formatted(i + 1)))); - - if (i < steps.length - 1) { - sb.append("\n"); - } - } - stepsContext = sb.toString(); - } - - String errorHandlerContext = ""; - if (errorHandler != null) { - StringBuilder sb = new StringBuilder("\n errorHandler:\n"); - - Map errorHandlerParameters = getProperties("error-handler"); - - String[] errorHandlerTokens = errorHandler.split(":", 2); - String errorHandlerType = errorHandlerTokens[0]; - - String errorHandlerSpec; - switch (errorHandlerType) { - case "sink": - if (errorHandlerTokens.length != 2) { - printer().printErr( - "Invalid error handler syntax. Type 'sink' needs an endpoint configuration (ie sink:endpointUri)"); - // Error abort Pipe construction - return ""; - } - String endpointUri = errorHandlerTokens[1]; - Map errorHandlerSinkProperties = getProperties("error-handler.sink"); - - // remove sink properties from error handler parameters - errorHandlerSinkProperties.keySet().stream() - .map(key -> "sink." + key) - .filter(errorHandlerParameters::containsKey) - .forEach(errorHandlerParameters::remove); - - String endpoint = resolveEndpoint(BindingProvider.EndpointType.ERROR_HANDLER, endpointUri, - errorHandlerSinkProperties); - - is = templateProvider.getErrorHandlerTemplate("sink"); - errorHandlerSpec = IOHelper.loadText(is); - IOHelper.close(is); - errorHandlerSpec = errorHandlerSpec.replaceFirst("\\{\\{ \\.Endpoint }}", endpoint); - errorHandlerSpec = errorHandlerSpec.replaceFirst("\\{\\{ \\.ErrorHandlerParameter }}", - templateProvider.asErrorHandlerParameters(errorHandlerParameters)); - break; - case "log": - is = templateProvider.getErrorHandlerTemplate("log"); - errorHandlerSpec = IOHelper.loadText(is); - IOHelper.close(is); - errorHandlerSpec = errorHandlerSpec.replaceFirst("\\{\\{ \\.ErrorHandlerParameter }}", - templateProvider.asErrorHandlerParameters(errorHandlerParameters)); - break; - default: - errorHandlerSpec = " none: {}"; - } - sb.append(errorHandlerSpec); - errorHandlerContext = sb.toString(); - } - - String name = FileUtil.onlyName(file, false); - context = context.replaceFirst("\\{\\{ \\.Name }}", name); - context = context.replaceFirst("\\{\\{ \\.Source }}\n", sourceEndpoint); - context = context.replaceFirst("\\{\\{ \\.Sink }}\n", sinkEndpoint); - context = context.replaceFirst("\\{\\{ \\.Steps }}", stepsContext); - context = context.replaceFirst("\\{\\{ \\.ErrorHandler }}", errorHandlerContext); - return context; - } catch (Exception e) { - printer().printErr(e); - } - - return ""; - } - - private String resolveEndpoint( - BindingProvider.EndpointType endpointType, String uriExpression, Map endpointProperties) - throws Exception { - for (BindingProvider provider : bindingProviders) { - if (provider.canHandle(uriExpression)) { - String context - = provider.getEndpoint(endpointType, uriExpression, endpointProperties, templateProvider); - - int additionalIndent = templateProvider.getAdditionalIndent(endpointType); - if (additionalIndent > 0) { - context = Arrays.stream(context.split("\n")) - .map(line -> " ".repeat(additionalIndent) + line) - .collect(Collectors.joining("\n")); - } - - return context; - } - } - - throw new CamelException( - "Failed to resolve endpoint URI expression %s - no matching binding provider found" - .formatted(uriExpression)); - } - - public int dumpPipe(String pipe) throws Exception { - switch (output) { - case "file": - if (file.endsWith(".yaml")) { - IOHelper.writeText(pipe, new FileOutputStream(file, false)); - } else if (file.endsWith(".json")) { - IOHelper.writeText(Jsoner.serialize(YamlHelper.yaml().loadAs(pipe, Map.class)), - new FileOutputStream(file, false)); - } else { - IOHelper.writeText(pipe, new FileOutputStream(file + ".yaml", false)); - } - break; - case "yaml": - printer().println(pipe); - break; - case "json": - printer().println(JSonHelper.prettyPrint(Jsoner.serialize(YamlHelper.yaml().loadAs(pipe, Map.class)), 2) - .replaceAll("\\\\/", "/")); - break; - default: - printer().printErr("Unsupported output format '%s' (supported: file, yaml, json)".formatted(output)); - return -1; - } - return 0; - } - - /** - * Extracts properties from given property arguments. Filter properties by given prefix. This way each component in - * pipe (source, sink, errorHandler, step[1-n]) can have its individual properties. - */ - private Map getProperties(String keyPrefix) { - Map props = new HashMap<>(); - if (properties != null) { - for (String propertyExpression : properties) { - if (propertyExpression.startsWith(keyPrefix + ".")) { - String[] keyValue = propertyExpression.split("=", 2); - if (keyValue.length != 2) { - printer().printErr( - "property '%s' does not follow format [source|sink|error-handler|step-].=" - .formatted(propertyExpression)); - continue; - } - - props.put(keyValue[0].substring(keyPrefix.length() + 1), keyValue[1]); - } - } - } - - return props; - } - - static class FileConsumer extends ParameterConsumer { - @Override - protected void doConsumeParameters(Stack args, Bind cmd) { - cmd.file = args.pop(); - } - } - - public void setFile(String file) { - this.file = file; - } - - public void setSource(String source) { - this.source = source; - } - - public void setSink(String sink) { - this.sink = sink; - } - - public void setSteps(String[] steps) { - this.steps = steps; - } - - public void setProperties(String[] properties) { - this.properties = properties; - } - - public void setErrorHandler(String errorHandler) { - this.errorHandler = errorHandler; - } - - public void setOutput(String output) { - this.output = output; - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/BindingProvider.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/BindingProvider.java deleted file mode 100644 index 64df50ced4057..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/BindingProvider.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import java.util.Map; - -/** - * Binding provider able to create an endpoint that can be used as a source/sink/step in a Pipe specification. Endpoints - * may represent a Kamelet, Camel endpoint URI or a Kubernetes object reference such as a reference to a Knative broker - * for instance. Implementations must not hold any state as the binding provider instance is used for multiple calls. - */ -public interface BindingProvider { - - String getEndpoint( - EndpointType type, String uriExpression, Map endpointProperties, TemplateProvider templateProvider) - throws Exception; - - boolean canHandle(String uriExpression); - - enum EndpointType { - SOURCE, - SINK, - STEP, - ERROR_HANDLER - } - -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/KnativeBrokerBindingProvider.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/KnativeBrokerBindingProvider.java deleted file mode 100644 index 8413e1479a1ac..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/KnativeBrokerBindingProvider.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import java.util.Map; - -import org.apache.camel.util.StringHelper; - -/** - * Binding to a Knative broker resource. - */ -public class KnativeBrokerBindingProvider extends ObjectReferenceBindingProvider { - - private static final String prefix = "knative:broker:"; - - public KnativeBrokerBindingProvider() { - super("eventing.knative.dev/v1", "Broker"); - } - - @Override - public String getEndpoint( - EndpointType type, String uriExpression, Map endpointProperties, TemplateProvider templateProvider) - throws Exception { - if (uriExpression.startsWith(prefix)) { - return super.getEndpoint(type, StringHelper.after(uriExpression, prefix), endpointProperties, templateProvider); - } - - return super.getEndpoint(type, uriExpression, endpointProperties, templateProvider); - } - - @Override - protected Map getEndpointUriProperties( - EndpointType type, String objectName, String uriExpression, Map endpointProperties) - throws Exception { - Map props = super.getEndpointUriProperties(type, objectName, uriExpression, endpointProperties); - - if (type == EndpointType.SOURCE && !props.containsKey("type")) { - // When acting as a source the type property is added by default in order to filter the event stream. - props.put("type", "org.apache.camel.event"); - } - - return props; - } - - @Override - public boolean canHandle(String uriExpression) { - return uriExpression.startsWith(prefix); - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/KnativeChannelBindingProvider.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/KnativeChannelBindingProvider.java deleted file mode 100644 index b134633dd7abd..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/KnativeChannelBindingProvider.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import java.util.Map; - -import org.apache.camel.util.StringHelper; - -public class KnativeChannelBindingProvider extends ObjectReferenceBindingProvider { - - private static final String prefix = "knative:channel:"; - - public KnativeChannelBindingProvider() { - super("messaging.knative.dev/v1", "Channel"); - } - - @Override - public String getEndpoint( - EndpointType type, String uriExpression, Map endpointProperties, TemplateProvider templateProvider) - throws Exception { - if (uriExpression.startsWith(prefix)) { - return super.getEndpoint(type, StringHelper.after(uriExpression, prefix), endpointProperties, templateProvider); - } - - return super.getEndpoint(type, uriExpression, endpointProperties, templateProvider); - } - - @Override - public boolean canHandle(String uriExpression) { - return uriExpression.startsWith(prefix); - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/ObjectReferenceBindingProvider.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/ObjectReferenceBindingProvider.java deleted file mode 100644 index f93db17eea858..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/ObjectReferenceBindingProvider.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import java.io.InputStream; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.apache.camel.CamelException; -import org.apache.camel.util.IOHelper; -import org.apache.camel.util.ObjectHelper; -import org.apache.camel.util.StringHelper; -import org.apache.camel.util.URISupport; - -/** - * Binding provider creates an object reference, usually to a Kubernetes resource. An object is identified by its fully - * qualified reference with Kind, ApiVersion, name and optional namespace. In addition to that the reference is able to - * specify resource properties. Subclasses may add logic for a very specific Kubernetes resource such as Kamelets or - * Knative brokers. - */ -public class ObjectReferenceBindingProvider implements BindingProvider { - - private static final Pattern OBJECT_REFERENCE_URI_PATTERN - = Pattern.compile("^([a-z.]+/[alphbetv0-9]+):([A-Z][a-z]+):([a-z][a-z-]*/?[a-z][a-z-]*)\\??[^?]*", Pattern.DOTALL); - - private final String apiVersion; - private final String kind; - - public ObjectReferenceBindingProvider() { - this("", ""); - } - - protected ObjectReferenceBindingProvider(String apiVersion, String kind) { - if (ObjectHelper.isNotEmpty(kind) && ObjectHelper.isEmpty(apiVersion)) { - throw new IllegalArgumentException( - "Object reference provider with static kind '%s' requires apiVersion to be set.".formatted(kind)); - } - - this.apiVersion = apiVersion; - this.kind = kind; - } - - @Override - public String getEndpoint( - EndpointType type, String uriExpression, Map endpointProperties, TemplateProvider templateProvider) - throws Exception { - - String apiVersionValue; - String kindValue; - String namespace; - String objectName; - if (ObjectHelper.isEmpty(kind)) { - Matcher objectRef = OBJECT_REFERENCE_URI_PATTERN.matcher(uriExpression); - if (objectRef.matches()) { - apiVersionValue = objectRef.group(1); - kindValue = objectRef.group(2); - - String namespacedName = objectRef.group(3); - objectName = getObjectName(namespacedName); - namespace = getNamespace(namespacedName); - } else { - throw new CamelException("Unsupported object reference: %s".formatted(uriExpression)); - } - } else { - apiVersionValue = apiVersion; - kindValue = kind; - objectName = getObjectName(uriExpression); - namespace = getNamespace(uriExpression); - } - - Map endpointUriProperties - = getEndpointUriProperties(type, objectName, uriExpression, endpointProperties); - - InputStream is; - if (type == EndpointType.STEP) { - is = templateProvider.getStepTemplate("ref"); - } else { - is = templateProvider.getEndpointTemplate("ref"); - } - - String context = IOHelper.loadText(is); - IOHelper.close(is); - - String namespaceContext = ""; - if (namespace != null) { - namespaceContext = " namespace: " + namespace + "\n"; - } - - context = context.replaceFirst("\\{\\{ \\.ApiVersion }}", apiVersionValue); - context = context.replaceFirst("\\{\\{ \\.Kind }}", kindValue); - context = context.replaceFirst("\\{\\{ \\.Name }}", objectName); - context = context.replaceFirst("\\{\\{ \\.Namespace }}\n", namespaceContext); - context = context.replaceFirst("\\{\\{ \\.EndpointProperties }}\n", - templateProvider.asEndpointProperties(endpointUriProperties)); - - return context; - } - - protected String getObjectName(String uriExpression) { - String namespacedName = uriExpression; - if (uriExpression.contains("?")) { - namespacedName = StringHelper.before(uriExpression, "?"); - } - - if (namespacedName.contains("/")) { - return namespacedName.split("/", 2)[1]; - } - - return namespacedName; - } - - protected String getNamespace(String uriExpression) { - String namespacedName = uriExpression; - if (uriExpression.contains("?")) { - namespacedName = StringHelper.before(uriExpression, "?"); - } - - if (namespacedName.contains("/")) { - return namespacedName.split("/", 2)[0]; - } - - return null; - } - - protected Map getEndpointUriProperties( - EndpointType type, String objectName, String uriExpression, Map endpointProperties) - throws Exception { - Map endpointUriProperties = new HashMap<>(endpointProperties); - if (uriExpression.contains("?")) { - String query = StringHelper.after(uriExpression, "?"); - if (query != null) { - endpointUriProperties = URISupport.parseQuery(query, true); - } - } - - return endpointUriProperties; - } - - @Override - public boolean canHandle(String uriExpression) { - if (ObjectHelper.isNotEmpty(kind)) { - return uriExpression.startsWith(kind.toLowerCase(Locale.US) + ":"); - } - - return OBJECT_REFERENCE_URI_PATTERN.matcher(uriExpression).matches(); - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/PipeProvider.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/PipeProvider.java deleted file mode 100644 index 26cd72bcb9d87..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/PipeProvider.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import java.io.InputStream; -import java.net.URI; -import java.net.URL; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - -import org.apache.camel.github.GitHubResourceResolver; -import org.apache.camel.impl.engine.DefaultResourceResolvers; -import org.apache.camel.spi.Resource; -import org.apache.camel.spi.ResourceResolver; -import org.apache.camel.util.IOHelper; -import org.apache.camel.util.StringHelper; -import org.snakeyaml.engine.v2.api.LoadSettings; -import org.snakeyaml.engine.v2.api.YamlUnicodeReader; -import org.snakeyaml.engine.v2.composer.Composer; -import org.snakeyaml.engine.v2.nodes.Node; -import org.snakeyaml.engine.v2.parser.Parser; -import org.snakeyaml.engine.v2.parser.ParserImpl; -import org.snakeyaml.engine.v2.scanner.StreamReader; - -import static org.apache.camel.dsl.yaml.common.YamlDeserializerSupport.asStringSet; -import static org.apache.camel.dsl.yaml.common.YamlDeserializerSupport.asText; -import static org.apache.camel.dsl.yaml.common.YamlDeserializerSupport.nodeAt; - -/** - * Binding to Kamelets as Kubernetes object references. Automatically resolves Kamelet from catalog and reads required - * properties. Adds required properties as placeholder to the object reference when not set already by the user. - */ -public class PipeProvider extends ObjectReferenceBindingProvider { - - private static final String prefix = "kamelet:"; - - public PipeProvider() { - super("camel.apache.org/v1", "Kamelet"); - } - - @Override - public String getEndpoint( - EndpointType type, String uriExpression, Map endpointProperties, TemplateProvider templateProvider) - throws Exception { - if (uriExpression.startsWith(prefix)) { - return super.getEndpoint(type, StringHelper.after(uriExpression, prefix), endpointProperties, templateProvider); - } - - return super.getEndpoint(type, uriExpression, endpointProperties, templateProvider); - } - - @Override - protected Map getEndpointUriProperties( - EndpointType type, String objectName, String uriExpression, Map endpointProperties) - throws Exception { - return kameletProperties(objectName, - super.getEndpointUriProperties(type, objectName, uriExpression, endpointProperties)); - } - - /** - * Get required properties from Kamelet specification and add those to the given user properties if not already set. - * In case a required property is not present in the provided user properties the value is either set to the example - * coming from the Kamelet specification or to a placeholder value for users to fill in manually. Property values do - * already have quotes when the type is String. - * - * @param kamelet - * @return - * @throws Exception - */ - protected Map kameletProperties(String kamelet, Map userProperties) throws Exception { - Map endpointProperties = new HashMap<>(); - InputStream is; - String loc; - Resource res; - - // try local disk first before GitHub - ResourceResolver resolver = new DefaultResourceResolvers.FileResolver(); - try { - res = resolver.resolve("file:" + kamelet + ".kamelet.yaml"); - } finally { - resolver.close(); - } - if (res.exists()) { - is = res.getInputStream(); - loc = res.getLocation(); - } else { - resolver = new GitHubResourceResolver(); - try { - res = resolver.resolve( - "github:apache:camel-kamelets:main:kamelets/" + kamelet + ".kamelet.yaml"); - } finally { - resolver.close(); - } - loc = res.getLocation(); - URL u = URI.create(loc).toURL(); - is = u.openStream(); - } - if (is != null) { - try { - LoadSettings local = LoadSettings.builder().setLabel(loc).build(); - final StreamReader reader = new StreamReader(local, new YamlUnicodeReader(is)); - final Parser parser = new ParserImpl(local, reader); - final Composer composer = new Composer(local, parser); - Node root = composer.getSingleNode().orElse(null); - if (root != null) { - Set required = asStringSet(nodeAt(root, "/spec/definition/required")); - if (required != null && !required.isEmpty()) { - for (String req : required) { - if (!userProperties.containsKey(req)) { - String type = asText(nodeAt(root, "/spec/definition/properties/" + req + "/type")); - String example = asText(nodeAt(root, "/spec/definition/properties/" + req + "/example")); - StringBuilder vb = new StringBuilder(); - if (example != null) { - if ("string".equals(type)) { - vb.append("\""); - } - vb.append(example); - if ("string".equals(type)) { - vb.append("\""); - } - } else { - vb.append("\"value\""); - } - endpointProperties.put(req, vb.toString()); - } - } - } - } - IOHelper.close(is); - } catch (Exception e) { - System.err.println("Error parsing Kamelet: " + loc + " due to: " + e.getMessage()); - } - } else { - System.err.println("Kamelet not found on github: " + kamelet); - } - - endpointProperties.putAll(userProperties); - - return endpointProperties; - } - - @Override - public boolean canHandle(String uriExpression) { - return uriExpression.startsWith(prefix) || !uriExpression.contains(":"); - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/StrimziKafkaTopicBindingProvider.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/StrimziKafkaTopicBindingProvider.java deleted file mode 100644 index 7482e870dcb58..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/StrimziKafkaTopicBindingProvider.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import java.util.Map; - -import org.apache.camel.util.StringHelper; - -/** - * Binding to a Strimzi Kafka topic resource. - */ -public class StrimziKafkaTopicBindingProvider extends ObjectReferenceBindingProvider { - - private static final String prefix = "kafka:topic:"; - - public StrimziKafkaTopicBindingProvider() { - super("kafka.strimzi.io/v1beta2", "KafkaTopic"); - } - - @Override - public String getEndpoint( - EndpointType type, String uriExpression, Map endpointProperties, TemplateProvider templateProvider) - throws Exception { - if (uriExpression.startsWith(prefix)) { - return super.getEndpoint(type, StringHelper.after(uriExpression, prefix), endpointProperties, templateProvider); - } - - return super.getEndpoint(type, uriExpression, endpointProperties, templateProvider); - } - - @Override - public boolean canHandle(String uriExpression) { - return uriExpression.startsWith(prefix); - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/TemplateProvider.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/TemplateProvider.java deleted file mode 100644 index 75fe1e7a8bc2c..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/TemplateProvider.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import java.io.InputStream; -import java.util.Map; - -import org.apache.camel.util.StringHelper; - -/** - * Helper class provides access to the templates that construct the Pipe resource. Subclasses may overwrite the provider - * to inject their own templates. - */ -public interface TemplateProvider { - default InputStream getPipeTemplate() { - return TemplateProvider.class.getClassLoader().getResourceAsStream("templates/pipe.yaml.tmpl"); - } - - default InputStream getStepTemplate(String stepType) { - return TemplateProvider.class.getClassLoader().getResourceAsStream("templates/step-%s.yaml.tmpl".formatted(stepType)); - } - - default InputStream getEndpointTemplate(String endpointType) { - return TemplateProvider.class.getClassLoader() - .getResourceAsStream("templates/endpoint-%s.yaml.tmpl".formatted(endpointType)); - } - - default InputStream getErrorHandlerTemplate(String type) { - return TemplateProvider.class.getClassLoader() - .getResourceAsStream("templates/error-handler-%s.yaml.tmpl".formatted(type)); - } - - /** - * Creates YAML snippet representing the endpoint properties section. - * - * @param props the properties to set as endpoint properties. - * @return - */ - default String asEndpointProperties(Map props) { - StringBuilder sb = new StringBuilder(); - if (props.isEmpty()) { - // create a dummy placeholder, so it is easier to add new properties manually - return sb.append("#properties:\n ").append("#key: \"value\"").toString(); - } - - sb.append("properties:\n"); - for (Map.Entry propertyEntry : props.entrySet()) { - String propValue = propertyEntry.getValue().toString(); - - // Take care of Strings with colon - need to quote these values to avoid breaking YAML - if (propValue.contains(":") && !StringHelper.isQuoted(propValue)) { - propValue = "\"%s\"".formatted(propValue); - } - - sb.append(" ").append(propertyEntry.getKey()).append(": ") - .append(propValue).append("\n"); - } - return sb.toString().trim(); - } - - /** - * Creates YAML snippet representing the error handler parameters section. - * - * @param props the properties to set as error handler parameters. - */ - default String asErrorHandlerParameters(Map props) { - if (props.isEmpty()) { - return "parameters: {}"; - } - - StringBuilder sb = new StringBuilder(); - sb.append("parameters:\n"); - for (Map.Entry propertyEntry : props.entrySet()) { - sb.append(" ").append(propertyEntry.getKey()).append(": ").append(propertyEntry.getValue()).append("\n"); - } - return sb.toString().trim(); - } - - /** - * Get additional indent that should be applied to endpoint templates. - * - * @param type the endpoint type. - * @return - */ - default int getAdditionalIndent(BindingProvider.EndpointType type) { - if (type == BindingProvider.EndpointType.ERROR_HANDLER) { - return 4; - } - - return 0; - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/UriBindingProvider.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/UriBindingProvider.java deleted file mode 100644 index 746952e6db548..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/bind/UriBindingProvider.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import java.io.InputStream; -import java.util.HashMap; -import java.util.Map; -import java.util.regex.Pattern; - -import org.apache.camel.util.IOHelper; -import org.apache.camel.util.StringHelper; -import org.apache.camel.util.URISupport; - -public class UriBindingProvider implements BindingProvider { - - private static final Pattern CAMEL_ENDPOINT_URI_PATTERN = Pattern.compile("^[a-z0-9+][a-zA-Z0-9-+]*:.*$"); - - @Override - public String getEndpoint( - EndpointType type, String uriExpression, Map endpointProperties, TemplateProvider templateProvider) - throws Exception { - String endpointUri = uriExpression; - Map endpointUriProperties = new HashMap<>(); - if (uriExpression.contains("?")) { - endpointUri = StringHelper.before(uriExpression, "?"); - String query = StringHelper.after(uriExpression, "?"); - if (query != null) { - endpointUriProperties = URISupport.parseQuery(query, true); - } - } - - endpointProperties.putAll(endpointUriProperties); - - InputStream is; - if (type == EndpointType.STEP) { - is = templateProvider.getStepTemplate("uri"); - } else { - is = templateProvider.getEndpointTemplate("uri"); - } - - String context = IOHelper.loadText(is); - IOHelper.close(is); - - context = context.replaceFirst("\\{\\{ \\.URI }}", endpointUri); - context = context.replaceFirst("\\{\\{ \\.EndpointProperties }}\n", - templateProvider.asEndpointProperties(endpointProperties)); - - return context; - } - - @Override - public boolean canHandle(String uriExpression) { - return CAMEL_ENDPOINT_URI_PATTERN.matcher(uriExpression).matches(); - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindKnativeBrokerTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindKnativeBrokerTest.java deleted file mode 100644 index 7ea3c92a61f46..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindKnativeBrokerTest.java +++ /dev/null @@ -1,323 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import org.apache.camel.dsl.jbang.core.commands.CamelCommandBaseTest; -import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain; -import org.apache.camel.util.StringHelper; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class BindKnativeBrokerTest extends CamelCommandBaseTest { - - @Test - public void shouldBindToKnativeBroker() throws Exception { - Bind command = createCommand("timer-source", "knative:broker:default"); - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-broker-default - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Broker - apiVersion: eventing.knative.dev/v1 - name: default - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindToNamespacedKnativeBroker() throws Exception { - Bind command = createCommand("timer-source", "knative:broker:default"); - - command.sink = "knative:broker:my-namespace/default"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-broker-default - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Broker - apiVersion: eventing.knative.dev/v1 - name: default - namespace: my-namespace - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindToKnativeBrokerWithProperties() throws Exception { - Bind command = createCommand("timer-source", "knative:broker:default"); - - command.properties = new String[] { - "source.message=Hello", - "sink.type=my-event", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-broker-default - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: Hello - sink: - ref: - kind: Broker - apiVersion: eventing.knative.dev/v1 - name: default - properties: - type: my-event - """.trim(), output); - } - - @Test - public void shouldBindToKnativeBrokerWithUriProperties() throws Exception { - Bind command = createCommand("timer-source", "knative:broker:default?type=my-event&source=camel"); - - command.properties = new String[] { - "source.message=Hello", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-broker-default - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: Hello - sink: - ref: - kind: Broker - apiVersion: eventing.knative.dev/v1 - name: default - properties: - type: my-event - source: camel - """.trim(), output); - } - - @Test - public void shouldBindKnativeBrokerSource() throws Exception { - Bind command = createCommand("knative:broker:default", "log-sink"); - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: broker-default-to-log - spec: - source: - ref: - kind: Broker - apiVersion: eventing.knative.dev/v1 - name: default - properties: - type: org.apache.camel.event - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindNamespacedKnativeBrokerSource() throws Exception { - Bind command = createCommand("knative:broker:default", "log-sink"); - - command.source = "knative:broker:my-namespace/default"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: broker-default-to-log - spec: - source: - ref: - kind: Broker - apiVersion: eventing.knative.dev/v1 - name: default - namespace: my-namespace - properties: - type: org.apache.camel.event - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindKnativeBrokerSourceWithProperties() throws Exception { - Bind command = createCommand("knative:broker:default", "log-sink"); - - command.properties = new String[] { - "source.type=my-event", - "sink.showHeaders=true", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: broker-default-to-log - spec: - source: - ref: - kind: Broker - apiVersion: eventing.knative.dev/v1 - name: default - properties: - type: my-event - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - properties: - showHeaders: true - """.trim(), output); - } - - @Test - public void shouldBindKnativeBrokerSourceWithUriProperties() throws Exception { - Bind command = createCommand("knative:broker:default?type=my-event&source=camel", "log-sink"); - - command.properties = new String[] { - "sink.showHeaders=true", - - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: broker-default-to-log - spec: - source: - ref: - kind: Broker - apiVersion: eventing.knative.dev/v1 - name: default - properties: - type: my-event - source: camel - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - properties: - showHeaders: true - """.trim(), output); - } - - private Bind createCommand(String source, String sink) { - Bind command = new Bind(new CamelJBangMain().withPrinter(printer)); - - String sourceName; - if (source.startsWith("knative:")) { - sourceName = StringHelper.after(source, "knative:").replaceAll(":", "-"); - if (sourceName.contains("?")) { - sourceName = StringHelper.before(sourceName, "?"); - } - } else { - sourceName = StringHelper.before(source, "-source"); - } - - String sinkName; - if (sink.startsWith("knative:")) { - sinkName = StringHelper.after(sink, "knative:").replaceAll(":", "-"); - if (sinkName.contains("?")) { - sinkName = StringHelper.before(sinkName, "?"); - } - } else { - sinkName = StringHelper.before(sink, "-sink"); - } - - command.file = sourceName + "-to-" + sinkName + ".yaml"; - command.source = source; - command.sink = sink; - command.output = "yaml"; - - return command; - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindKnativeChannelTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindKnativeChannelTest.java deleted file mode 100644 index f88ff8ce2b9a5..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindKnativeChannelTest.java +++ /dev/null @@ -1,257 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import org.apache.camel.dsl.jbang.core.commands.CamelCommandBaseTest; -import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain; -import org.apache.camel.util.StringHelper; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class BindKnativeChannelTest extends CamelCommandBaseTest { - - @Test - public void shouldBindToKnativeChannel() throws Exception { - Bind command = createCommand("timer-source", "knative:channel:my-channel"); - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-channel-my-channel - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Channel - apiVersion: messaging.knative.dev/v1 - name: my-channel - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindToKnativeChannelWithProperties() throws Exception { - Bind command = createCommand("timer-source", "knative:channel:my-channel"); - - command.properties = new String[] { - "source.message=Hello", - "sink.type=my-event", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-channel-my-channel - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: Hello - sink: - ref: - kind: Channel - apiVersion: messaging.knative.dev/v1 - name: my-channel - properties: - type: my-event - """.trim(), output); - } - - @Test - public void shouldBindToKnativeChannelWithUriProperties() throws Exception { - Bind command = createCommand("timer-source", "knative:channel:my-channel?type=my-event&source=camel"); - - command.properties = new String[] { - "source.message=Hello", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-channel-my-channel - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: Hello - sink: - ref: - kind: Channel - apiVersion: messaging.knative.dev/v1 - name: my-channel - properties: - type: my-event - source: camel - """.trim(), output); - } - - @Test - public void shouldBindKnativeChannelSource() throws Exception { - Bind command = createCommand("knative:channel:my-channel", "log-sink"); - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: channel-my-channel-to-log - spec: - source: - ref: - kind: Channel - apiVersion: messaging.knative.dev/v1 - name: my-channel - #properties: - #key: "value" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindKnativeChannelSourceWithProperties() throws Exception { - Bind command = createCommand("knative:channel:my-channel", "log-sink"); - - command.properties = new String[] { - "source.type=my-event", - "sink.showHeaders=true", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: channel-my-channel-to-log - spec: - source: - ref: - kind: Channel - apiVersion: messaging.knative.dev/v1 - name: my-channel - properties: - type: my-event - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - properties: - showHeaders: true - """.trim(), output); - } - - @Test - public void shouldBindKnativeChannelSourceWithUriProperties() throws Exception { - Bind command = createCommand("knative:channel:my-channel?type=my-event&source=camel", "log-sink"); - - command.properties = new String[] { - "sink.showHeaders=true", - - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: channel-my-channel-to-log - spec: - source: - ref: - kind: Channel - apiVersion: messaging.knative.dev/v1 - name: my-channel - properties: - type: my-event - source: camel - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - properties: - showHeaders: true - """.trim(), output); - } - - private Bind createCommand(String source, String sink) { - Bind command = new Bind(new CamelJBangMain().withPrinter(printer)); - - String sourceName; - if (source.startsWith("knative:")) { - sourceName = StringHelper.after(source, "knative:").replaceAll(":", "-"); - if (sourceName.contains("?")) { - sourceName = StringHelper.before(sourceName, "?"); - } - } else { - sourceName = StringHelper.before(source, "-source"); - } - - String sinkName; - if (sink.startsWith("knative:")) { - sinkName = StringHelper.after(sink, "knative:").replaceAll(":", "-"); - if (sinkName.contains("?")) { - sinkName = StringHelper.before(sinkName, "?"); - } - } else { - sinkName = StringHelper.before(sink, "-sink"); - } - - command.file = sourceName + "-to-" + sinkName + ".yaml"; - command.source = source; - command.sink = sink; - command.output = "yaml"; - - return command; - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindObjectReferenceTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindObjectReferenceTest.java deleted file mode 100644 index 4d4162f8bfd0e..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindObjectReferenceTest.java +++ /dev/null @@ -1,216 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import org.apache.camel.dsl.jbang.core.commands.CamelCommandBaseTest; -import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain; -import org.apache.camel.util.StringHelper; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -class BindObjectReferenceTest extends CamelCommandBaseTest { - - @Test - public void shouldBindToObjectReference() throws Exception { - Bind command = createCommand("timer", "foo"); - - command.sink = "sandbox.camel.apache.org/v1:Foo:bar"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-foo - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Foo - apiVersion: sandbox.camel.apache.org/v1 - name: bar - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindToNamespacedObjectReference() throws Exception { - Bind command = createCommand("timer", "foo"); - - command.sink = "sandbox.camel.apache.org/v1alpha1:Foo:my-namespace/bar"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-foo - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Foo - apiVersion: sandbox.camel.apache.org/v1alpha1 - name: bar - namespace: my-namespace - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindToObjectReferenceWithProperties() throws Exception { - Bind command = createCommand("timer", "foo"); - - command.sink = "sandbox.camel.apache.org/v1:Foo:bar"; - command.properties = new String[] { - "source.message=Hello", - "sink.foo=bar", - "sink.bar=baz", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-foo - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: Hello - sink: - ref: - kind: Foo - apiVersion: sandbox.camel.apache.org/v1 - name: bar - properties: - bar: baz - foo: bar - """.trim(), output); - } - - @Test - public void shouldBindToObjectReferenceWithUriProperties() throws Exception { - Bind command = createCommand("timer", "foo"); - - command.sink = "sandbox.camel.apache.org/v1:Foo:bar?bar=baz&foo=bar"; - command.properties = new String[] { - "source.message=Hello", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-foo - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: Hello - sink: - ref: - kind: Foo - apiVersion: sandbox.camel.apache.org/v1 - name: bar - properties: - bar: baz - foo: bar - """.trim(), output); - } - - @Test - public void shouldHandleInvalidObjectReference() throws Exception { - Bind command = createCommand("timer", "foo"); - - command.sink = "sandbox.camel.apache.org:Foo:bar"; // missing api version - - command.doCall(); - - String output = printer.getOutput(); - assertThat(output).isEqualToIgnoringNewLines( - """ - ERROR: Failed to resolve endpoint URI expression sandbox.camel.apache.org:Foo:bar - no matching binding provider found - ERROR: Failed to construct Pipe resource - """ - .trim()); - } - - private Bind createCommand(String source, String sink) { - Bind command = new Bind(new CamelJBangMain().withPrinter(printer)); - - String sourceName; - String sourceUri; - if (source.contains(":")) { - sourceName = StringHelper.before(source, ":"); - sourceUri = source; - } else { - sourceName = source; - sourceUri = source + "-source"; - } - - String sinkName; - String sinkUri; - if (sink.contains(":")) { - sinkName = StringHelper.before(sink, ":"); - sinkUri = sink; - } else { - sinkName = sink; - sinkUri = sink + "-sink"; - } - - command.file = sourceName + "-to-" + sinkName + ".yaml"; - command.source = sourceUri; - command.sink = sinkUri; - command.output = "yaml"; - - return command; - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindStrimziKafkaTopicTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindStrimziKafkaTopicTest.java deleted file mode 100644 index 5c777c93d389d..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindStrimziKafkaTopicTest.java +++ /dev/null @@ -1,320 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import org.apache.camel.dsl.jbang.core.commands.CamelCommandBaseTest; -import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain; -import org.apache.camel.util.StringHelper; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class BindStrimziKafkaTopicTest extends CamelCommandBaseTest { - - @Test - public void shouldBindToKafkaTopic() throws Exception { - Bind command = createCommand("timer-source", "kafka:topic:my-topic"); - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-topic-my-topic - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: KafkaTopic - apiVersion: kafka.strimzi.io/v1beta2 - name: my-topic - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindToNamespacedKafkaTopic() throws Exception { - Bind command = createCommand("timer-source", "kafka:topic:my-topic"); - - command.sink = "kafka:topic:my-namespace/my-topic"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-topic-my-topic - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: KafkaTopic - apiVersion: kafka.strimzi.io/v1beta2 - name: my-topic - namespace: my-namespace - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindToKafkaTopicWithProperties() throws Exception { - Bind command = createCommand("timer-source", "kafka:topic:my-topic"); - - command.properties = new String[] { - "source.message=Hello", - "sink.brokers=my-cluster-kafka-bootstrap:9092", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-topic-my-topic - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: Hello - sink: - ref: - kind: KafkaTopic - apiVersion: kafka.strimzi.io/v1beta2 - name: my-topic - properties: - brokers: "my-cluster-kafka-bootstrap:9092" - """.trim(), output); - } - - @Test - public void shouldBindToKafkaTopicWithUriProperties() throws Exception { - Bind command = createCommand("timer-source", "kafka:topic:my-topic?brokers=my-cluster-kafka-bootstrap:9092"); - - command.properties = new String[] { - "source.message=Hello", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-topic-my-topic - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: Hello - sink: - ref: - kind: KafkaTopic - apiVersion: kafka.strimzi.io/v1beta2 - name: my-topic - properties: - brokers: "my-cluster-kafka-bootstrap:9092" - """.trim(), output); - } - - @Test - public void shouldBindKafkaTopicSource() throws Exception { - Bind command = createCommand("kafka:topic:my-topic", "log-sink"); - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: topic-my-topic-to-log - spec: - source: - ref: - kind: KafkaTopic - apiVersion: kafka.strimzi.io/v1beta2 - name: my-topic - #properties: - #key: "value" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindNamespacedKafkaTopicSource() throws Exception { - Bind command = createCommand("kafka:topic:my-topic", "log-sink"); - - command.source = "kafka:topic:my-namespace/my-topic"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: topic-my-topic-to-log - spec: - source: - ref: - kind: KafkaTopic - apiVersion: kafka.strimzi.io/v1beta2 - name: my-topic - namespace: my-namespace - #properties: - #key: "value" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindKafkaTopicSourceWithProperties() throws Exception { - Bind command = createCommand("kafka:topic:my-topic", "log-sink"); - - command.properties = new String[] { - "source.brokers=my-cluster-kafka-bootstrap:9092", - "sink.showHeaders=true", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: topic-my-topic-to-log - spec: - source: - ref: - kind: KafkaTopic - apiVersion: kafka.strimzi.io/v1beta2 - name: my-topic - properties: - brokers: "my-cluster-kafka-bootstrap:9092" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - properties: - showHeaders: true - """.trim(), output); - } - - @Test - public void shouldBindKafkaTopicSourceWithUriProperties() throws Exception { - Bind command = createCommand("kafka:topic:my-topic?brokers=my-cluster-kafka-bootstrap:9092", "log-sink"); - - command.properties = new String[] { - "sink.showHeaders=true", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: topic-my-topic-to-log - spec: - source: - ref: - kind: KafkaTopic - apiVersion: kafka.strimzi.io/v1beta2 - name: my-topic - properties: - brokers: "my-cluster-kafka-bootstrap:9092" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - properties: - showHeaders: true - """.trim(), output); - } - - private Bind createCommand(String source, String sink) { - Bind command = new Bind(new CamelJBangMain().withPrinter(printer)); - - String sourceName; - if (source.startsWith("kafka:")) { - sourceName = StringHelper.after(source, "kafka:").replaceAll(":", "-"); - if (sourceName.contains("?")) { - sourceName = StringHelper.before(sourceName, "?"); - } - } else { - sourceName = StringHelper.before(source, "-source"); - } - - String sinkName; - if (sink.startsWith("kafka:")) { - sinkName = StringHelper.after(sink, "kafka:").replaceAll(":", "-"); - if (sinkName.contains("?")) { - sinkName = StringHelper.before(sinkName, "?"); - } - } else { - sinkName = StringHelper.before(sink, "-sink"); - } - - command.file = sourceName + "-to-" + sinkName + ".yaml"; - command.source = source; - command.sink = sink; - command.output = "yaml"; - - return command; - } -} diff --git a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindTest.java b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindTest.java deleted file mode 100644 index fccd879465950..0000000000000 --- a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/bind/BindTest.java +++ /dev/null @@ -1,1128 +0,0 @@ -/* - * 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.dsl.jbang.core.commands.bind; - -import org.apache.camel.dsl.jbang.core.commands.CamelCommandBaseTest; -import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain; -import org.apache.camel.util.StringHelper; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -class BindTest extends CamelCommandBaseTest { - - @Test - public void shouldBindKameletSourceToKameletSink() throws Exception { - Bind command = createCommand("timer", "log"); - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindNamespacedKamelets() throws Exception { - Bind command = createCommand("timer", "log"); - command.source = "my-namespace/timer-source"; - command.sink = "my-namespace/log-sink"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - namespace: my-namespace - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - namespace: my-namespace - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindKameletsExplicitPrefix() throws Exception { - Bind command = createCommand("timer", "log"); - command.source = "kamelet:timer-source"; - command.sink = "kamelet:log-sink"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindKameletSourceToKameletSinkWithProperties() throws Exception { - Bind command = createCommand("timer", "log"); - - command.properties = new String[] { - "source.message=Hello", - "source.period=5000", - "sink.showHeaders=true", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: Hello - period: 5000 - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - properties: - showHeaders: true - """.trim(), output); - } - - @Test - public void shouldBindKameletsWithUriProperties() throws Exception { - Bind command = createCommand("timer", "log"); - command.source = "timer-source?message=Hi"; - command.sink = "log-sink?showHeaders=true"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: Hi - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - properties: - showHeaders: true - """.trim(), output); - } - - @Test - public void shouldBindWithSteps() throws Exception { - Bind command = createCommand("timer", "http"); - - command.steps = new String[] { - "set-body-action", - "log-action" - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-http - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - steps: - - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: set-body-action - properties: - value: "value" - - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-action - #properties: - #key: "value" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: http-sink - properties: - url: "https://my-service/path" - """.trim(), output); - } - - @Test - public void shouldBindWithUriSteps() throws Exception { - Bind command = createCommand("timer", "http"); - - command.steps = new String[] { - "set-body-action", - "log:info" - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-http - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - steps: - - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: set-body-action - properties: - value: "value" - - uri: log:info - #properties: - #key: "value" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: http-sink - properties: - url: "https://my-service/path" - """.trim(), output); - } - - @Test - public void shouldBindWithStepsAndProperties() throws Exception { - Bind command = createCommand("timer", "http"); - - command.steps = new String[] { - "set-body-action", - "log-action" - }; - - command.properties = new String[] { - "step-1.value=\"Camel rocks!\"", - "step-2.showHeaders=true", - "step-2.showExchangePattern=false" - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-http - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - steps: - - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: set-body-action - properties: - value: "Camel rocks!" - - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-action - properties: - showHeaders: true - showExchangePattern: false - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: http-sink - properties: - url: "https://my-service/path" - """.trim(), output); - } - - @Test - public void shouldBindWithUriStepsAndProperties() throws Exception { - Bind command = createCommand("timer", "http"); - - command.steps = new String[] { - "set-body-action", - "log:info" - }; - - command.properties = new String[] { - "step-1.value=\"Camel rocks!\"", - "step-2.showHeaders=true" - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-http - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - steps: - - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: set-body-action - properties: - value: "Camel rocks!" - - uri: log:info - properties: - showHeaders: true - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: http-sink - properties: - url: "https://my-service/path" - """.trim(), output); - } - - @Test - public void shouldBindWithUriStepsAndUriProperties() throws Exception { - Bind command = createCommand("timer", "http"); - - command.steps = new String[] { - "set-body-action", - "log:info?showExchangePattern=false&showStreams=true" - }; - - command.properties = new String[] { - "step-1.value=\"Camel rocks!\"", - "step-2.showHeaders=true" - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-http - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - steps: - - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: set-body-action - properties: - value: "Camel rocks!" - - uri: log:info - properties: - showStreams: true - showHeaders: true - showExchangePattern: false - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: http-sink - properties: - url: "https://my-service/path" - """.trim(), output); - } - - @Test - public void shouldBindKameletSourceToUri() throws Exception { - Bind command = createCommand("timer", "log:info"); - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - uri: log:info - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindKameletSourceToUriWithProperties() throws Exception { - Bind command = createCommand("timer", "log:info"); - - command.properties = new String[] { - "source.message=Hello", - "sink.showHeaders=true", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: Hello - sink: - uri: log:info - properties: - showHeaders: true - """.trim(), output); - } - - @Test - public void shouldBindKameletSourceToUriWithUriProperties() throws Exception { - Bind command = createCommand("timer", "log:info?showStreams=false"); - - command.properties = new String[] { - "source.message=Hello", - "sink.showHeaders=true", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: Hello - sink: - uri: log:info - properties: - showStreams: false - showHeaders: true - """.trim(), output); - } - - @Test - public void shouldBindUriToUri() throws Exception { - Bind command = createCommand("timer:tick", "log:info"); - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - uri: timer:tick - #properties: - #key: "value" - sink: - uri: log:info - #properties: - #key: "value" - """.trim(), output); - } - - @Test - public void shouldBindUriToUriWithProperties() throws Exception { - Bind command = createCommand("timer:tick", "log:info"); - - command.properties = new String[] { - "source.message=Hello", - "sink.showHeaders=true", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - uri: timer:tick - properties: - message: Hello - sink: - uri: log:info - properties: - showHeaders: true - """.trim(), output); - } - - @Test - public void shouldBindUriToUriWithUriProperties() throws Exception { - Bind command = createCommand("timer:tick?period=10000", "log:info?showStreams=false"); - - command.properties = new String[] { - "source.message=Hello", - "sink.showHeaders=true", - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - uri: timer:tick - properties: - period: 10000 - message: Hello - sink: - uri: log:info - properties: - showStreams: false - showHeaders: true - """.trim(), output); - } - - @Test - public void shouldBindKameletSinkErrorHandler() throws Exception { - Bind command = createCommand("timer", "log"); - - command.errorHandler = "sink:log-sink"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - errorHandler: - sink: - endpoint: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - parameters: {} - """.trim(), output); - } - - @Test - public void shouldBindKameletSinkErrorHandlerWithParameters() throws Exception { - Bind command = createCommand("timer", "log"); - - command.errorHandler = "sink:log-sink"; - - command.properties = new String[] { - "error-handler.maximumRedeliveries=3", - "error-handler.redeliveryDelay=2000" - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - errorHandler: - sink: - endpoint: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - parameters: - redeliveryDelay: 2000 - maximumRedeliveries: 3 - """.trim(), output); - } - - @Test - public void shouldBindKameletSinkErrorHandlerAndSinkProperties() throws Exception { - Bind command = createCommand("timer", "log"); - - command.errorHandler = "sink:log-sink"; - - command.properties = new String[] { - "error-handler.sink.showHeaders=true", - "error-handler.maximumRedeliveries=3", - "error-handler.redeliveryDelay=2000" - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - errorHandler: - sink: - endpoint: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - properties: - showHeaders: true - parameters: - redeliveryDelay: 2000 - maximumRedeliveries: 3 - """.trim(), output); - } - - @Test - public void shouldBindEndpointUriSinkErrorHandler() throws Exception { - Bind command = createCommand("timer", "log"); - - command.errorHandler = "sink:log:error"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - errorHandler: - sink: - endpoint: - uri: log:error - #properties: - #key: "value" - parameters: {} - """.trim(), output); - } - - @Test - public void shouldBindEndpointUriSinkErrorHandlerWithParameters() throws Exception { - Bind command = createCommand("timer", "log"); - - command.errorHandler = "sink:log:error"; - - command.properties = new String[] { - "error-handler.maximumRedeliveries=3", - "error-handler.redeliveryDelay=2000" - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - errorHandler: - sink: - endpoint: - uri: log:error - #properties: - #key: "value" - parameters: - redeliveryDelay: 2000 - maximumRedeliveries: 3 - """.trim(), output); - } - - @Test - public void shouldBindEndpointUriSinkErrorHandlerAndSinkProperties() throws Exception { - Bind command = createCommand("timer", "log"); - - command.errorHandler = "sink:log:error"; - - command.properties = new String[] { - "error-handler.sink.showHeaders=true", - "error-handler.maximumRedeliveries=3", - "error-handler.redeliveryDelay=2000" - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - errorHandler: - sink: - endpoint: - uri: log:error - properties: - showHeaders: true - parameters: - redeliveryDelay: 2000 - maximumRedeliveries: 3 - """.trim(), output); - } - - @Test - public void shouldBindEndpointUriSinkErrorHandlerAndUriProperties() throws Exception { - Bind command = createCommand("timer", "log"); - - command.errorHandler = "sink:log:error?showStreams=false"; - - command.properties = new String[] { - "error-handler.sink.showHeaders=true", - "error-handler.maximumRedeliveries=3", - "error-handler.redeliveryDelay=2000" - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - errorHandler: - sink: - endpoint: - uri: log:error - properties: - showStreams: false - showHeaders: true - parameters: - redeliveryDelay: 2000 - maximumRedeliveries: 3 - """.trim(), output); - } - - @Test - public void shouldBindWithLogErrorHandler() throws Exception { - Bind command = createCommand("timer", "log"); - - command.errorHandler = "log"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - errorHandler: - log: - parameters: {} - """.trim(), output); - } - - @Test - public void shouldBindWithLogErrorHandlerWithParameters() throws Exception { - Bind command = createCommand("timer", "log"); - - command.errorHandler = "log"; - - command.properties = new String[] { - "error-handler.maximumRedeliveries=3", - "error-handler.redeliveryDelay=2000" - }; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - errorHandler: - log: - parameters: - redeliveryDelay: 2000 - maximumRedeliveries: 3 - """.trim(), output); - } - - @Test - public void shouldBindWithNoErrorHandler() throws Exception { - Bind command = createCommand("timer", "log"); - - command.errorHandler = "none"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - apiVersion: camel.apache.org/v1 - kind: Pipe - metadata: - name: timer-to-log - spec: - source: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: timer-source - properties: - message: "hello world" - sink: - ref: - kind: Kamelet - apiVersion: camel.apache.org/v1 - name: log-sink - #properties: - #key: "value" - errorHandler: - none: {} - """.trim(), output); - } - - @Test - public void shouldSupportJsonOutput() throws Exception { - Bind command = createCommand("timer", "log"); - command.output = "json"; - - command.doCall(); - - String output = printer.getOutput(); - Assertions.assertEquals(""" - { - "apiVersion": "camel.apache.org/v1", - "kind": "Pipe", - "metadata": { - "name": "timer-to-log" - }, - "spec": { - "source": { - "ref": { - "kind": "Kamelet", - "apiVersion": "camel.apache.org/v1", - "name": "timer-source" - }, - "properties": { - "message": "hello world" - } - }, - "sink": { - "ref": { - "kind": "Kamelet", - "apiVersion": "camel.apache.org/v1", - "name": "log-sink" - } - } - } - } - """.trim(), output); - } - - @Test - public void shouldHandleUnsupportedOutputFormat() throws Exception { - Bind command = createCommand("timer", "log"); - command.output = "wrong"; - - Assertions.assertEquals(-1, command.doCall()); - - Assertions.assertEquals("ERROR: Unsupported output format 'wrong' (supported: file, yaml, json)", printer.getOutput()); - } - - private Bind createCommand(String source, String sink) { - Bind command = new Bind(new CamelJBangMain().withPrinter(printer)); - - String sourceName; - String sourceUri; - if (source.contains(":")) { - sourceName = StringHelper.before(source, ":"); - sourceUri = source; - } else { - sourceName = source; - sourceUri = source + "-source"; - } - - String sinkName; - String sinkUri; - if (sink.contains(":")) { - sinkName = StringHelper.before(sink, ":"); - sinkUri = sink; - } else { - sinkName = sink; - sinkUri = sink + "-sink"; - } - - command.file = sourceName + "-to-" + sinkName + ".yaml"; - command.source = sourceUri; - command.sink = sinkUri; - command.output = "yaml"; - - return command; - } -}