Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): add test to validate camel registry hooks in ArC #146

Merged
merged 1 commit into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions build-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
15 changes: 15 additions & 0 deletions extensions/core/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>



Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this too many empty lines?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to fix it

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -50,24 +50,36 @@ static <T> Map<String, T> getReferencesByTypeWithName(Class<T> type, Annotation.
}

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) {
return type.cast(manager.getReference(bean, type, manager.createCreationalContext(bean)));
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,4 @@ public <T> Set<T> findByType(Class<T> type) {
result.addAll(BeanManagerHelper.getReferencesByType(type));
return result;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,9 +29,10 @@

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
Expand Down Expand Up @@ -59,4 +61,28 @@ public String getProperty(@PathParam("name") String name) throws Exception {
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -43,4 +42,9 @@ public void timerPropertyPropagated() {
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"));
}
}