You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2019/08/19 20:20:47 UTC

[camel-quarkus] branch master updated: chore(test): add test to validate camel registry hooks in ArC

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

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-quarkus.git


The following commit(s) were added to refs/heads/master by this push:
     new 9470371  chore(test): add test to validate camel registry hooks in ArC
9470371 is described below

commit 94703710d0e951fdd3b863f663229228de728abe
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Mon Aug 19 17:07:07 2019 +0200

    chore(test): add test to validate camel registry hooks in ArC
---
 build-parent/pom.xml                               |   1 +
 extensions/core/deployment/pom.xml                 |  15 +++
 .../quarkus/core/runtime/CamelProducersTest.java   | 104 +++++++++++++++++++++
 .../quarkus/core/runtime/CamelRegistryTest.java    |  72 ++++++++++++++
 .../core/runtime/support/BeanManagerHelper.java    |  34 ++++---
 .../core/runtime/support/RuntimeRegistry.java      |   1 -
 .../camel/quarkus/core/CamelApplication.java       |  24 -----
 .../apache/camel/quarkus/core/CamelServlet.java    |  28 +++++-
 .../org/apache/camel/quarkus/core/CamelTest.java   |  14 ++-
 9 files changed, 251 insertions(+), 42 deletions(-)

diff --git a/build-parent/pom.xml b/build-parent/pom.xml
index 5f7f01f..3585b8c 100644
--- a/build-parent/pom.xml
+++ b/build-parent/pom.xml
@@ -34,6 +34,7 @@
     <properties>
 
         <xstream.version>1.4.11</xstream.version>
+        <assertj.version>3.11.1</assertj.version>
 
         <!-- maven-surefire-plugin -->
         <failIfNoTests>false</failIfNoTests>
diff --git a/extensions/core/deployment/pom.xml b/extensions/core/deployment/pom.xml
index e1a6dc2..c52249c 100644
--- a/extensions/core/deployment/pom.xml
+++ b/extensions/core/deployment/pom.xml
@@ -45,6 +45,21 @@
             <groupId>org.apache.camel.quarkus</groupId>
             <artifactId>camel-quarkus-core</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>io.quarkus</groupId>
+            <artifactId>quarkus-junit5-internal</artifactId>
+            <scope>test</scope>
+        </dependency>
+
+
+
+        <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <version>${assertj.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
     <build>
         <plugins>
diff --git a/extensions/core/deployment/src/test/java/org/apache/camel/quarkus/core/runtime/CamelProducersTest.java b/extensions/core/deployment/src/test/java/org/apache/camel/quarkus/core/runtime/CamelProducersTest.java
new file mode 100644
index 0000000..88aba5b
--- /dev/null
+++ b/extensions/core/deployment/src/test/java/org/apache/camel/quarkus/core/runtime/CamelProducersTest.java
@@ -0,0 +1,104 @@
+/*
+ * 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.quarkus.core.runtime;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+
+import io.quarkus.test.QuarkusUnitTest;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ConsumerTemplate;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.spi.Registry;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+
+public class CamelProducersTest {
+    @RegisterExtension
+    static final QuarkusUnitTest config = new QuarkusUnitTest()
+        .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
+            .addClasses(BeanUsingProducerTemplate.class)
+            .addClasses(BeanUsingConsumerTemplate.class)
+            .addClasses(BeanUsingCamelContext.class)
+            .addClasses(BeanUsingRegistry.class)
+        );
+
+    @Inject
+    BeanUsingProducerTemplate usingProducerTemplate;
+    @Inject
+    BeanUsingConsumerTemplate usingConsumerTemplate;
+    @Inject
+    BeanUsingCamelContext usingCamelContext;
+    @Inject
+    BeanUsingRegistry usingRegistry;
+
+    @Test
+    public void testInjection() throws Exception {
+        usingProducerTemplate.verify();
+        usingConsumerTemplate.verify();
+        usingCamelContext.verify();
+        usingRegistry.verify();
+    }
+
+    @ApplicationScoped
+    static class BeanUsingProducerTemplate {
+        @Inject
+        ProducerTemplate target;
+
+        public void verify() throws Exception {
+            assertThat(target).isNotNull();
+        }
+    }
+
+    @ApplicationScoped
+    static class BeanUsingConsumerTemplate {
+        @Inject
+        ConsumerTemplate target;
+
+        public void verify() throws Exception {
+            assertThat(target).isNotNull();
+        }
+    }
+
+    @ApplicationScoped
+    static class BeanUsingCamelContext {
+        @Inject
+        CamelContext target;
+
+        public void verify() throws Exception {
+            assertThat(target).isNotNull();
+            assertThat(target.getName()).startsWith("camel-");
+        }
+    }
+
+    @ApplicationScoped
+    static class BeanUsingRegistry {
+        @Inject
+        Registry target;
+
+        public void verify() throws Exception {
+            assertThat(target).isNotNull();
+            assertThat(target.findByType(BeanUsingProducerTemplate.class)).hasSize(1);
+            assertThat(target.findByType(BeanUsingConsumerTemplate.class)).hasSize(1);
+        }
+    }
+}
diff --git a/extensions/core/deployment/src/test/java/org/apache/camel/quarkus/core/runtime/CamelRegistryTest.java b/extensions/core/deployment/src/test/java/org/apache/camel/quarkus/core/runtime/CamelRegistryTest.java
new file mode 100644
index 0000000..28a25ba
--- /dev/null
+++ b/extensions/core/deployment/src/test/java/org/apache/camel/quarkus/core/runtime/CamelRegistryTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.quarkus.core.runtime;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.enterprise.inject.Produces;
+import javax.inject.Inject;
+import javax.inject.Named;
+
+import io.quarkus.test.QuarkusUnitTest;
+import org.apache.camel.spi.Registry;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class CamelRegistryTest {
+    @RegisterExtension
+    static final QuarkusUnitTest config = new QuarkusUnitTest()
+        .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
+            .addClasses(BeanProducer.class)
+        );
+
+    @Inject
+    Registry registry;
+
+    @Test
+    public void testLookupByName() {
+        assertThat(registry.lookupByName("bean-1")).isInstanceOfSatisfying(String.class, s -> assertThat(s).isEqualTo("a"));
+        assertThat(registry.lookupByName("bean-2")).isInstanceOfSatisfying(String.class, s -> assertThat(s).isEqualTo("b"));
+        assertThat(registry.lookupByNameAndType("bean-1", String.class)).isEqualTo("a");
+        assertThat(registry.lookupByNameAndType("bean-2", String.class)).isEqualTo("b");
+    }
+    @Test
+    public void testFindByType() {
+        assertThat(registry.findByType(String.class)).containsOnly("a", "b");
+        assertThat(registry.findByTypeWithName(String.class))
+            .containsEntry("bean-1", "a")
+            .containsEntry("bean-2", "b");
+    }
+
+    @ApplicationScoped
+    public static class BeanProducer {
+        @Named("bean-1")
+        @Produces
+        public String bean1() {
+            return "a";
+        }
+
+        @Named("bean-2")
+        @Produces
+        public String bean2() {
+            return "b";
+        }
+    }
+}
diff --git a/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/BeanManagerHelper.java b/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/BeanManagerHelper.java
index dba065c..541d6a7 100644
--- a/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/BeanManagerHelper.java
+++ b/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/BeanManagerHelper.java
@@ -17,13 +17,13 @@
 package org.apache.camel.quarkus.core.runtime.support;
 
 import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
-import java.util.stream.Collectors;
 import javax.enterprise.inject.Vetoed;
 import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.BeanAttributes;
 import javax.enterprise.inject.spi.BeanManager;
 
 import io.quarkus.arc.Arc;
@@ -50,15 +50,20 @@ final class BeanManagerHelper {
     }
 
     static <T> Set<T> getReferencesByType(BeanManager manager, Class<T> type, Annotation... qualifiers) {
-        return manager.getBeans(type, qualifiers).stream()
-            .map(bean -> getReference(manager, type, bean))
-            .collect(Collectors.toSet());
+        Set<T> answer = new HashSet<>();
+
+        for (Bean<?> bean: manager.getBeans(type, qualifiers)) {
+            T ref = getReference(manager, type, bean);
+            if (ref != null) {
+                answer.add(ref);
+            }
+        }
+
+        return answer;
     }
 
     static <T> Optional<T> getReferenceByName(BeanManager manager, String name, Class<T> type) {
-        return Optional.of(manager.getBeans(name))
-            .<Bean<?>> map(manager::resolve)
-            .map(bean -> getReference(manager, type, bean));
+        return Optional.ofNullable(manager.resolve(manager.getBeans(name))).map(bean -> getReference(manager, type, bean));
     }
 
     static <T> T getReference(BeanManager manager, Class<T> type, Bean<?> bean) {
@@ -66,8 +71,15 @@ final class BeanManagerHelper {
     }
 
     static <T> Map<String, T> getReferencesByTypeWithName(BeanManager manager, Class<T> type, Annotation... qualifiers) {
-        return manager.getBeans(type, qualifiers).stream()
-            .collect(Collectors.toMap(BeanAttributes::getName, b -> getReference(manager, type, b)));
-    }
+        Map<String, T> answer = new HashMap<>();
 
+        for (Bean<?> bean: manager.getBeans(type, qualifiers)) {
+            T ref = getReference(manager, type, bean);
+            if (ref != null) {
+                answer.put(bean.getName(), ref);
+            }
+        }
+
+        return answer;
+    }
 }
diff --git a/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/RuntimeRegistry.java b/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/RuntimeRegistry.java
index 8fd9e05..d796c6d 100644
--- a/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/RuntimeRegistry.java
+++ b/extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/support/RuntimeRegistry.java
@@ -109,5 +109,4 @@ public class RuntimeRegistry extends HashMap<String, Map<Class<?>, Object>> impl
         result.addAll(BeanManagerHelper.getReferencesByType(type));
         return result;
     }
-
 }
diff --git a/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CamelApplication.java b/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CamelApplication.java
deleted file mode 100644
index dd9d167..0000000
--- a/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CamelApplication.java
+++ /dev/null
@@ -1,24 +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.quarkus.core;
-
-import javax.ws.rs.ApplicationPath;
-import javax.ws.rs.core.Application;
-
-@ApplicationPath("/test")
-public class CamelApplication extends Application {
-}
\ No newline at end of file
diff --git a/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CamelServlet.java b/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CamelServlet.java
index 1a2e43a..fe7d1be 100644
--- a/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CamelServlet.java
+++ b/integration-tests/core/src/main/java/org/apache/camel/quarkus/core/CamelServlet.java
@@ -17,6 +17,7 @@
 package org.apache.camel.quarkus.core;
 
 import java.util.List;
+import java.util.Set;
 import java.util.stream.Collectors;
 import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
@@ -28,9 +29,10 @@ import javax.ws.rs.core.MediaType;
 
 import org.apache.camel.Route;
 import org.apache.camel.component.timer.TimerComponent;
+import org.apache.camel.quarkus.core.runtime.CamelConfig;
 import org.apache.camel.quarkus.core.runtime.CamelRuntime;
 
-@Path("/")
+@Path("/test")
 @ApplicationScoped
 public class CamelServlet {
     @Inject
@@ -59,4 +61,28 @@ public class CamelServlet {
     public boolean timerResolvePropertyPlaceholders() throws Exception {
         return runtime.getContext().getComponent("timer", TimerComponent.class).isResolvePropertyPlaceholders();
     }
+
+    @Path("/registry/produces-config-build")
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public boolean producesBuildTimeConfig() {
+        return lookupSingleInstanceFromRegistry(CamelConfig.BuildTime.class) != null;
+    }
+
+    @Path("/registry/produces-config-runtime")
+    @GET
+    @Produces(MediaType.TEXT_PLAIN)
+    public boolean producesRuntimeConfig() {
+        return lookupSingleInstanceFromRegistry(CamelConfig.Runtime.class) != null;
+    }
+
+    private <T> T lookupSingleInstanceFromRegistry(Class<T> type) {
+        final Set<T> answer = runtime.getContext().getRegistry().findByType(type);
+
+        if (answer.size() == 1) {
+            return answer.iterator().next();
+        }
+
+        return null;
+    }
 }
diff --git a/integration-tests/core/src/test/java/org/apache/camel/quarkus/core/CamelTest.java b/integration-tests/core/src/test/java/org/apache/camel/quarkus/core/CamelTest.java
index b259d2a..d3e1325 100644
--- a/integration-tests/core/src/test/java/org/apache/camel/quarkus/core/CamelTest.java
+++ b/integration-tests/core/src/test/java/org/apache/camel/quarkus/core/CamelTest.java
@@ -16,13 +16,12 @@
  */
 package org.apache.camel.quarkus.core;
 
-import static org.hamcrest.Matchers.containsString;
-import static org.hamcrest.Matchers.is;
-
-import org.junit.jupiter.api.Test;
-
 import io.quarkus.test.junit.QuarkusTest;
 import io.restassured.RestAssured;
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.is;
 
 @QuarkusTest
 public class CamelTest {
@@ -43,4 +42,9 @@ public class CamelTest {
         RestAssured.when().get("/test/timer/resolve-property-placeholders").then().body(is("false"));
     }
 
+    @Test
+    public void testRegistry() {
+        RestAssured.when().get("/test/registry/produces-config-build").then().body(is("true"));
+        RestAssured.when().get("/test/registry/produces-config-runtime").then().body(is("true"));
+    }
 }