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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 1b742901e91 Fix camel-knative endpoint for events (#7818)
1b742901e91 is described below

commit 1b742901e9116d7ce373c99eca9b88998f9c9e1a
Author: Claudio Miranda <[email protected]>
AuthorDate: Sat Jun 18 02:57:58 2022 -0300

    Fix camel-knative endpoint for events (#7818)
    
    * Only use the configuration name when endpoint.kind=sink
    * Log a debug message when using a "default" knative resource name
    * Set a "default" name when there is no name
---
 .../camel/component/knative/KnativeEndpoint.java   | 19 +++++++++++--
 .../component/knative/KnativeComponentTest.java    | 28 +++++++++++++++++-
 .../src/test/resources/environment.json            | 33 ++++++++++++++++++++++
 .../src/test/resources/environment_classic.json    | 33 ++++++++++++++++++++++
 4 files changed, 109 insertions(+), 4 deletions(-)

diff --git 
a/components/camel-knative/camel-knative-component/src/main/java/org/apache/camel/component/knative/KnativeEndpoint.java
 
b/components/camel-knative/camel-knative-component/src/main/java/org/apache/camel/component/knative/KnativeEndpoint.java
index 5413075840c..f0f49a275be 100644
--- 
a/components/camel-knative/camel-knative-component/src/main/java/org/apache/camel/component/knative/KnativeEndpoint.java
+++ 
b/components/camel-knative/camel-knative-component/src/main/java/org/apache/camel/component/knative/KnativeEndpoint.java
@@ -42,6 +42,8 @@ import org.apache.camel.spi.UriPath;
 import org.apache.camel.support.DefaultEndpoint;
 import org.apache.camel.support.PropertyBindingSupport;
 import org.apache.camel.util.ObjectHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Send and receive events from Knative.
@@ -53,13 +55,17 @@ import org.apache.camel.util.ObjectHelper;
              category = Category.CLOUD)
 public class KnativeEndpoint extends DefaultEndpoint {
 
+    private static final Logger LOG = 
LoggerFactory.getLogger(KnativeEndpoint.class);
+
     private final CloudEvent cloudEvent;
     private final CloudEventProcessor cloudEventProcessor;
 
     @UriPath(description = "The Knative resource type")
     private final Knative.Type type;
+
     @UriPath(description = "The identifier of the Knative resource")
     private final String typeId;
+
     @UriParam
     private KnativeConfiguration configuration;
 
@@ -162,14 +168,21 @@ public class KnativeEndpoint extends DefaultEndpoint {
 
     KnativeResource lookupServiceDefinition(Knative.EndpointKind endpointKind) 
{
         final String resourceName;
-        if (type == Knative.Type.event && configuration.getName() != null) {
+        if (type == Knative.Type.event && configuration.getName() != null && 
endpointKind.equals(Knative.EndpointKind.sink)) {
             resourceName = configuration.getName();
-        } else {
+        } else if (configuration.getTypeId() != null) {
             resourceName = configuration.getTypeId();
+        } else {
+            // in case there is no name in the configuration or type
+            resourceName = "default";
         }
 
         KnativeResource resource = lookupServiceDefinition(resourceName, 
endpointKind)
-                .or(() -> lookupServiceDefinition("default", endpointKind))
+                .or(() -> {
+                    LOG.debug("Knative resource \"{}\" of type \"{}\" not 
found, trying the default named: \"default\"",
+                            resourceName, type);
+                    return lookupServiceDefinition("default", endpointKind);
+                })
                 .orElseThrow(() -> new IllegalArgumentException(
                         String.format("Unable to find a resource definition 
for %s/%s/%s", type, endpointKind, resourceName)));
 
diff --git 
a/components/camel-knative/camel-knative-component/src/test/java/org/apache/camel/component/knative/KnativeComponentTest.java
 
b/components/camel-knative/camel-knative-component/src/test/java/org/apache/camel/component/knative/KnativeComponentTest.java
index 0d5892f551a..a1074ad2788 100644
--- 
a/components/camel-knative/camel-knative-component/src/test/java/org/apache/camel/component/knative/KnativeComponentTest.java
+++ 
b/components/camel-knative/camel-knative-component/src/test/java/org/apache/camel/component/knative/KnativeComponentTest.java
@@ -61,9 +61,10 @@ public class KnativeComponentTest {
     void testLoadEnvironment(String resource) throws Exception {
         KnativeEnvironment env = mandatoryLoadFromResource(context, resource);
 
-        assertThat(env.stream()).hasSize(3);
+        assertThat(env.stream()).hasSize(6);
         assertThat(env.stream()).anyMatch(s -> s.getType() == 
Knative.Type.channel);
         assertThat(env.stream()).anyMatch(s -> s.getType() == 
Knative.Type.endpoint);
+        assertThat(env.stream()).anyMatch(s -> s.getType() == 
Knative.Type.event);
 
         KnativeComponent component = new KnativeComponent();
         component.setEnvironment(env);
@@ -72,6 +73,8 @@ public class KnativeComponentTest {
 
         context.getRegistry().bind("ereg", 
KnativeEnvironmentSupport.endpoint(Knative.EndpointKind.source, "ereg", null));
         context.getRegistry().bind("creg", 
KnativeEnvironmentSupport.channel(Knative.EndpointKind.source, "creg", null));
+        context.getRegistry().bind("evsinkreg", 
KnativeEnvironmentSupport.event(Knative.EndpointKind.sink, "evsinkreg", null));
+        context.getRegistry().bind("evsourcereg", 
KnativeEnvironmentSupport.event(Knative.EndpointKind.source, "evsourcereg", 
null));
         context.addComponent("knative", component);
 
         //
@@ -103,5 +106,28 @@ public class KnativeComponentTest {
             KnativeEndpoint endpoint = 
context.getEndpoint("knative:endpoint/ereg", KnativeEndpoint.class);
             assertThat(endpoint.lookupServiceDefinition("ereg", 
Knative.EndpointKind.source)).isPresent();
         }
+
+        //
+        // Events
+        //
+        {
+            KnativeEndpoint endpoint = 
context.getEndpoint("knative:event/event", KnativeEndpoint.class);
+            assertThat(endpoint.lookupServiceDefinition("example-broker", 
Knative.EndpointKind.sink)).isPresent();
+            assertThat(endpoint.lookupServiceDefinition("c1", 
Knative.EndpointKind.source)).isNotPresent();
+            assertThat(endpoint.lookupServiceDefinition("example-broker", 
Knative.EndpointKind.sink)).isPresent().get()
+                    .hasFieldOrPropertyWithValue("url", 
"http://broker-example/default/example-broker";);
+        }
+        {
+            KnativeEndpoint endpoint = 
context.getEndpoint("knative:event/evt1", KnativeEndpoint.class);
+            assertThat(endpoint.lookupServiceDefinition("evt1", 
Knative.EndpointKind.source)).isPresent();
+            assertThat(endpoint.lookupServiceDefinition("evt1", 
Knative.EndpointKind.source)).isPresent().get()
+                    .hasFieldOrPropertyWithValue("path", "/events/evt1");
+        }
+        {
+            KnativeEndpoint endpoint = context.getEndpoint("knative:event", 
KnativeEndpoint.class);
+            assertThat(endpoint.lookupServiceDefinition("default", 
Knative.EndpointKind.source)).isPresent();
+            assertThat(endpoint.lookupServiceDefinition("default", 
Knative.EndpointKind.source)).isPresent().get()
+                    .hasFieldOrPropertyWithValue("path", "/events/");
+        }
     }
 }
diff --git 
a/components/camel-knative/camel-knative-component/src/test/resources/environment.json
 
b/components/camel-knative/camel-knative-component/src/test/resources/environment.json
index 2c634e4f36c..eeb55b66bdf 100644
--- 
a/components/camel-knative/camel-knative-component/src/test/resources/environment.json
+++ 
b/components/camel-knative/camel-knative-component/src/test/resources/environment.json
@@ -29,6 +29,39 @@
         "knative.event.type": "",
         "camel.endpoint.kind": "source"
       }
+    },
+    {
+      "type": "event",
+      "name": "example-broker",
+      "url": "http://broker-example/default/example-broker";,
+      "metadata": {
+        "camel.endpoint.kind": "sink",
+        "knative.apiVersion": "eventing.knative.dev/v1",
+        "knative.kind": "Broker",
+        "knative.name": "example-broker"
+      }
+    },
+    {
+      "type": "event",
+      "name": "evt1",
+      "path": "/events/evt1",
+      "metadata": {
+        "camel.endpoint.kind": "source",
+        "knative.apiVersion": "eventing.knative.dev/v1",
+        "knative.kind": "Broker",
+        "knative.name": "example-broker"
+      }
+    },
+    {
+      "type": "event",
+      "name": "default",
+      "path": "/events/",
+      "metadata": {
+        "camel.endpoint.kind": "source",
+        "knative.apiVersion": "eventing.knative.dev/v1",
+        "knative.kind": "Broker",
+        "knative.name": "example-broker"
+      }
     }
   ]
 }
diff --git 
a/components/camel-knative/camel-knative-component/src/test/resources/environment_classic.json
 
b/components/camel-knative/camel-knative-component/src/test/resources/environment_classic.json
index 683df584314..1be2d047569 100644
--- 
a/components/camel-knative/camel-knative-component/src/test/resources/environment_classic.json
+++ 
b/components/camel-knative/camel-knative-component/src/test/resources/environment_classic.json
@@ -29,6 +29,39 @@
         "knative.event.type": "",
         "camel.endpoint.kind": "source"
       }
+    },
+    {
+      "type": "event",
+      "name": "example-broker",
+      "url": "http://broker-example/default/example-broker";,
+      "metadata": {
+        "camel.endpoint.kind": "sink",
+        "knative.apiVersion": "eventing.knative.dev/v1",
+        "knative.kind": "Broker",
+        "knative.name": "example-broker"
+      }
+    },
+    {
+      "type": "event",
+      "name": "evt1",
+      "path": "/events/evt1",
+      "metadata": {
+        "camel.endpoint.kind": "source",
+        "knative.apiVersion": "eventing.knative.dev/v1",
+        "knative.kind": "Broker",
+        "knative.name": "example-broker"
+      }
+    },
+    {
+      "type": "event",
+      "name": "default",
+      "path": "/events/",
+      "metadata": {
+        "camel.endpoint.kind": "source",
+        "knative.apiVersion": "eventing.knative.dev/v1",
+        "knative.kind": "Broker",
+        "knative.name": "example-broker"
+      }
     }
   ]
 }

Reply via email to