From 920e8e0c1b909dbc54b07e0d22e7ea71d043d768 Mon Sep 17 00:00:00 2001 From: Christoph Deppisch Date: Fri, 11 Mar 2022 22:40:41 +0100 Subject: [PATCH] chore: Bind Camel components to Citrus reference resolver --- .../yaks/camel/CamelReferenceResolver.java | 134 ++++++++++++++++++ .../yaks/camel/CamelSteps.java | 10 ++ 2 files changed, 144 insertions(+) create mode 100644 java/steps/yaks-camel/src/main/java/org/citrusframework/yaks/camel/CamelReferenceResolver.java diff --git a/java/steps/yaks-camel/src/main/java/org/citrusframework/yaks/camel/CamelReferenceResolver.java b/java/steps/yaks-camel/src/main/java/org/citrusframework/yaks/camel/CamelReferenceResolver.java new file mode 100644 index 00000000..adb5c720 --- /dev/null +++ b/java/steps/yaks-camel/src/main/java/org/citrusframework/yaks/camel/CamelReferenceResolver.java @@ -0,0 +1,134 @@ +/* + * 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.citrusframework.yaks.camel; + +import java.util.Map; +import java.util.Set; + +import com.consol.citrus.exceptions.CitrusRuntimeException; +import com.consol.citrus.spi.ReferenceResolver; +import com.consol.citrus.spi.SimpleReferenceResolver; +import org.apache.camel.CamelContext; + +/** + * @author Christoph Deppisch + */ +public class CamelReferenceResolver implements ReferenceResolver { + + private CamelContext camelContext; + + private ReferenceResolver fallback = new SimpleReferenceResolver(); + + public CamelReferenceResolver() { + super(); + } + + /** + * Constructor initializes with given Camel context. + * @param camelContext + */ + public CamelReferenceResolver(CamelContext camelContext) { + this.camelContext = camelContext; + } + + @Override + public T resolve(Class type) { + Set components = camelContext.getRegistry().findByType(type); + + if (components.isEmpty()) { + throw new CitrusRuntimeException(String.format("Unable to find bean reference for type '%s'", type)); + } + + return components.iterator().next(); + } + + @Override + public T resolve(String name, Class type) { + T component = camelContext.getRegistry().lookupByNameAndType(name, type); + + if (component != null) { + return component; + } + + if (fallback.isResolvable(name)) { + return fallback.resolve(name, type); + } + throw new CitrusRuntimeException(String.format("Unable to find bean reference for name '%s'", name)); + } + + @Override + public Object resolve(String name) { + Object component = camelContext.getRegistry().lookupByName(name); + + if (component != null) { + return component; + } + + if (fallback.isResolvable(name)) { + return fallback.resolve(name); + } + + throw new CitrusRuntimeException(String.format("Unable to find bean reference for name '%s'", name)); + } + + @Override + public Map resolveAll(Class requiredType) { + return camelContext.getRegistry().findByTypeWithName(requiredType); + } + + @Override + public boolean isResolvable(String name) { + return camelContext.getRegistry().lookupByName(name) != null || fallback.isResolvable(name); + } + + @Override + public boolean isResolvable(Class type) { + return !camelContext.getRegistry().findByType(type).isEmpty() || fallback.isResolvable(type); + } + + @Override + public boolean isResolvable(String name, Class type) { + return camelContext.getRegistry().lookupByNameAndType(name, type) != null || fallback.isResolvable(name, type); + } + + /** + * Specifies the fallback. + * @param fallback + */ + public CamelReferenceResolver withFallback(ReferenceResolver fallback) { + this.fallback = fallback; + return this; + } + + @Override + public void bind(String name, Object value) { + camelContext.getRegistry().bind(name, value); + } + + public void setCamelContext(CamelContext camelContext) { + this.camelContext = camelContext; + } + + /** + * Specifies the fallback. + * @param fallback + */ + public void setFallback(ReferenceResolver fallback) { + this.fallback = fallback; + } +} diff --git a/java/steps/yaks-camel/src/main/java/org/citrusframework/yaks/camel/CamelSteps.java b/java/steps/yaks-camel/src/main/java/org/citrusframework/yaks/camel/CamelSteps.java index 686897af..a2754f31 100644 --- a/java/steps/yaks-camel/src/main/java/org/citrusframework/yaks/camel/CamelSteps.java +++ b/java/steps/yaks-camel/src/main/java/org/citrusframework/yaks/camel/CamelSteps.java @@ -107,6 +107,11 @@ public void before(Scenario scenario) { } } + if (camelContext != null && !(context.getReferenceResolver() instanceof CamelReferenceResolver)) { + context.setReferenceResolver(new CamelReferenceResolver(camelContext) + .withFallback(citrus.getCitrusContext().getReferenceResolver())); + } + headers = new HashMap<>(); body = null; } @@ -177,6 +182,7 @@ public void bindComponent(String name, String configurationScript) { } camelContext().getRegistry().bind(name, component); + citrus.getCitrusContext().bind(name, component); } @Given("^load to Camel registry ([^\"\\s]+)\\.groovy$") @@ -407,6 +413,8 @@ private CamelContext camelContext() { if (camelContext == null) { try { camelContext = new DefaultCamelContext(); + context.setReferenceResolver(new CamelReferenceResolver(camelContext) + .withFallback(citrus.getCitrusContext().getReferenceResolver())); camelContext.start(); } catch (Exception e) { throw new IllegalStateException("Failed to start default Camel context", e); @@ -422,6 +430,8 @@ private CamelContext springCamelContext(String beans) { ApplicationContext ctx = new GenericXmlApplicationContext( new ByteArrayResource(context.replaceDynamicContentInString(beans).getBytes(StandardCharsets.UTF_8))); camelContext = ctx.getBean(SpringCamelContext.class); + context.setReferenceResolver(new CamelReferenceResolver(camelContext) + .withFallback(citrus.getCitrusContext().getReferenceResolver())); camelContext.start(); } catch (Exception e) { throw new IllegalStateException("Failed to start Spring Camel context", e);