[
https://issues.apache.org/jira/browse/CAMEL-16567?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Dietrich Schulten updated CAMEL-16567:
--------------------------------------
Description:
It is not possible to mock consul producers, at least I have tried with the
catalog and agent producers. Test:
{code:java}
public class MockAgentTest extends CamelTestSupport {
@Test
public void testMockAgent() throws Exception {
MockEndpoint mockConsulAgent = getMockEndpoint("mock:consul:agent");
AdviceWith.adviceWith(context, "servicesRoute", a -> {
a.mockEndpointsAndSkip("consul:agent*");
});
mockConsulAgent.returnReplyBody(constant(ImmutableMap.of("foo-1",
ImmutableService.builder()
.id("foo-1")
.service("foo")
.address("localhost")
.port(80)
.build())));
@SuppressWarnings("unchecked")
Map<String, Service> result =
fluentTemplate.to("direct:start").request(Map.class);
}
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").to("consul:agent?action=" +
ConsulAgentActions.SERVICES);
}
};
}
}
{code}
Error:
{code:java}
org.apache.camel.FailedToStartRouteException: Failed to start route
servicesRoute because of null
at org.apache.camel.impl.engine.RouteService.warmUp(RouteService.java:123)
...
Caused by: java.lang.NullPointerException at
org.apache.camel.support.HeaderSelectorProducer.doBuild(HeaderSelectorProducer.java:150)
at org.apache.camel.support.service.BaseService.build(BaseService.java:63) at
org.apache.camel.support.service.ServiceHelper.buildService(ServiceHelper.java:55)
at
org.apache.camel.support.service.ServiceHelper.buildService(ServiceHelper.java:72)
at
org.apache.camel.processor.InterceptSendToEndpointProcessor.doBuild(InterceptSendToEndpointProcessor.java:151)
at org.apache.camel.support.service.BaseService.build(BaseService.java:63) at
org.apache.camel.support.service.BaseService.init(BaseService.java:79) at
org.apache.camel.support.service.BaseService.start(BaseService.java:111) at
org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
at
org.apache.camel.impl.engine.AbstractCamelContext.internalAddService(AbstractCamelContext.java:1465)
at
org.apache.camel.impl.engine.AbstractCamelContext.addService(AbstractCamelContext.java:1383)
at org.apache.camel.processor.SendProcessor.doStart(SendProcessor.java:247) at
org.apache.camel.support.service.BaseService.start(BaseService.java:119) at
org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
at
org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130)
at
org.apache.camel.processor.errorhandler.RedeliveryErrorHandler.doStart(RedeliveryErrorHandler.java:1638)
at
org.apache.camel.support.ChildServiceSupport.start(ChildServiceSupport.java:60)
... 92 more{code}
The reason is that {{camelContext}} is {{null}} in {{HeaderSelectorProducer}},
but I am still unsure why that is:
{code:java}
@Override
protected void doBuild() throws Exception {
super.doBuild();
String key = this.getClass().getName();
String fqn = RESOURCE_PATH + key;
// -------- camelContext is null here:
strategy =
camelContext.adapt(ExtendedCamelContext.class).getBootstrapFactoryFinder(RESOURCE_PATH)
.newInstance(key, InvokeOnHeaderStrategy.class)
.orElseThrow(() -> new IllegalArgumentException("Cannot find " +
fqn + " in classpath."));
...{code}
Same with catalog:
{code:java}
public class MockCatalogTest extends CamelTestSupport {
@Test
public void testMockCatalog() throws Exception {
MockEndpoint mockConsulAgent = getMockEndpoint("mock:consul:catalog");
AdviceWith.adviceWith(context, "servicesRoute", a -> {
a.mockEndpointsAndSkip("consul:catalog*");
});
mockConsulAgent.returnReplyBody(constant(singletonList(ImmutableNode.builder().node("node-1").build())));
@SuppressWarnings("unchecked")
Map<String, Service> result =
fluentTemplate.to("direct:start").request(Map.class);
}
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").to("consul:catalog?action=" +
ConsulCatalogActions.LIST_NODES);
}
};
}
}
{code}
Why this fails is not clear to me. At runtime the method {{setCamelContext}} of
the failing instance never gets called, although it is a {{CamelContextAware}}.
I suspect that it is the mock or the real object behind the mock which is not a
{{CamelContextAware}} so that it receives no {{camelContext}}. My assumption is
that a mock is a kind of wrapper around the real thing, but I wasn't able to
understand the relationship between the two enough to fix this.
Anyone who understands the mocking mechanics better than me: what might go
wrong here? Any hints how it *should* work? Is the mock a proxy or what exactly
is the relationship between mock and mocked?
was:
It is not possible to mock consul producers, at least I have tried with the
catalog and agent producers. Test:
{code:java}
public class MockAgentTest extends CamelTestSupport {
@Test
public void testMockAgent() throws Exception {
MockEndpoint mockConsulAgent = getMockEndpoint("mock:consul:agent");
AdviceWith.adviceWith(context, "servicesRoute", a -> {
a.mockEndpointsAndSkip("consul:agent*");
});
mockConsulAgent.returnReplyBody(constant(ImmutableMap.of("foo-1",
ImmutableService.builder()
.id("foo-1")
.service("foo")
.address("localhost")
.port(80)
.build())));
@SuppressWarnings("unchecked")
Map<String, Service> result =
fluentTemplate.to("direct:start").request(Map.class);
}
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").to("consul:agent?action=" +
ConsulAgentActions.SERVICES);
}
};
}
}
{code}
Error:
{code:java}
org.apache.camel.FailedToStartRouteException: Failed to start route
servicesRoute because of null
at org.apache.camel.impl.engine.RouteService.warmUp(RouteService.java:123)
...
Caused by: java.lang.NullPointerException at
org.apache.camel.support.HeaderSelectorProducer.doBuild(HeaderSelectorProducer.java:150)
at org.apache.camel.support.service.BaseService.build(BaseService.java:63) at
org.apache.camel.support.service.ServiceHelper.buildService(ServiceHelper.java:55)
at
org.apache.camel.support.service.ServiceHelper.buildService(ServiceHelper.java:72)
at
org.apache.camel.processor.InterceptSendToEndpointProcessor.doBuild(InterceptSendToEndpointProcessor.java:151)
at org.apache.camel.support.service.BaseService.build(BaseService.java:63) at
org.apache.camel.support.service.BaseService.init(BaseService.java:79) at
org.apache.camel.support.service.BaseService.start(BaseService.java:111) at
org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
at
org.apache.camel.impl.engine.AbstractCamelContext.internalAddService(AbstractCamelContext.java:1465)
at
org.apache.camel.impl.engine.AbstractCamelContext.addService(AbstractCamelContext.java:1383)
at org.apache.camel.processor.SendProcessor.doStart(SendProcessor.java:247) at
org.apache.camel.support.service.BaseService.start(BaseService.java:119) at
org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
at
org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130)
at
org.apache.camel.processor.errorhandler.RedeliveryErrorHandler.doStart(RedeliveryErrorHandler.java:1638)
at
org.apache.camel.support.ChildServiceSupport.start(ChildServiceSupport.java:60)
... 92 more{code}
The reason is that {{camelContext}} is {{null}} in {{HeaderSelectorProducer}},
but I am still unsure why that is:
{code:java}
@Override
protected void doBuild() throws Exception {
super.doBuild();
String key = this.getClass().getName();
String fqn = RESOURCE_PATH + key;
// -------- camelContext is null here:
strategy =
camelContext.adapt(ExtendedCamelContext.class).getBootstrapFactoryFinder(RESOURCE_PATH)
.newInstance(key, InvokeOnHeaderStrategy.class)
.orElseThrow(() -> new IllegalArgumentException("Cannot find " +
fqn + " in classpath."));
...{code}
Same with catalog:
{code:java}
public class MockCatalogTest extends CamelTestSupport {
@Test
public void testMockCatalog() throws Exception {
MockEndpoint mockConsulAgent = getMockEndpoint("mock:consul:catalog");
AdviceWith.adviceWith(context, "servicesRoute", a -> {
a.mockEndpointsAndSkip("consul:catalog*");
});
mockConsulAgent.returnReplyBody(constant(singletonList(ImmutableNode.builder().node("node-1").build())));
@SuppressWarnings("unchecked")
Map<String, Service> result =
fluentTemplate.to("direct:start").request(Map.class);
}
@Override
protected RoutesBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").to("consul:catalog?action=" +
ConsulCatalogActions.LIST_NODES);
}
};
}
}
{code}
Why this fails is not clear to me. At runtime the method {{setCamelContext}} of
the failing instance never gets called, although it is a {{CamelContextAware}}.
I suspect that it is the mock or the real object behind the mock which is not a
{{CamelContextAware}} so that it receives no {{camelContext}}. My assumption is
that a mock is a kind of wrapper around the real thing, but I wasn't able to
understand the relationship between the two enough to fix this.
Anyone who understands the mocking mechanics better than me: would might go
wrong here? Any hints how it *should* work? Is the mock a proxy or what exactly
is the relationship between mock and mocked?
> Mocking of consul producers fails
> ---------------------------------
>
> Key: CAMEL-16567
> URL: https://issues.apache.org/jira/browse/CAMEL-16567
> Project: Camel
> Issue Type: Bug
> Components: camel-consul
> Affects Versions: 3.9.0
> Reporter: Dietrich Schulten
> Priority: Major
>
> It is not possible to mock consul producers, at least I have tried with the
> catalog and agent producers. Test:
> {code:java}
> public class MockAgentTest extends CamelTestSupport {
> @Test
> public void testMockAgent() throws Exception {
> MockEndpoint mockConsulAgent = getMockEndpoint("mock:consul:agent");
> AdviceWith.adviceWith(context, "servicesRoute", a -> {
> a.mockEndpointsAndSkip("consul:agent*");
> });
> mockConsulAgent.returnReplyBody(constant(ImmutableMap.of("foo-1",
> ImmutableService.builder()
> .id("foo-1")
> .service("foo")
> .address("localhost")
> .port(80)
> .build())));
> @SuppressWarnings("unchecked")
> Map<String, Service> result =
> fluentTemplate.to("direct:start").request(Map.class);
> }
> @Override
> protected RoutesBuilder createRouteBuilder() throws Exception {
> return new RouteBuilder() {
> @Override
> public void configure() throws Exception {
> from("direct:start").to("consul:agent?action=" +
> ConsulAgentActions.SERVICES);
> }
> };
> }
> }
> {code}
> Error:
> {code:java}
> org.apache.camel.FailedToStartRouteException: Failed to start route
> servicesRoute because of null
> at org.apache.camel.impl.engine.RouteService.warmUp(RouteService.java:123)
> ...
> Caused by: java.lang.NullPointerException at
> org.apache.camel.support.HeaderSelectorProducer.doBuild(HeaderSelectorProducer.java:150)
> at org.apache.camel.support.service.BaseService.build(BaseService.java:63)
> at
> org.apache.camel.support.service.ServiceHelper.buildService(ServiceHelper.java:55)
> at
> org.apache.camel.support.service.ServiceHelper.buildService(ServiceHelper.java:72)
> at
> org.apache.camel.processor.InterceptSendToEndpointProcessor.doBuild(InterceptSendToEndpointProcessor.java:151)
> at org.apache.camel.support.service.BaseService.build(BaseService.java:63)
> at org.apache.camel.support.service.BaseService.init(BaseService.java:79) at
> org.apache.camel.support.service.BaseService.start(BaseService.java:111) at
> org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
> at
> org.apache.camel.impl.engine.AbstractCamelContext.internalAddService(AbstractCamelContext.java:1465)
> at
> org.apache.camel.impl.engine.AbstractCamelContext.addService(AbstractCamelContext.java:1383)
> at org.apache.camel.processor.SendProcessor.doStart(SendProcessor.java:247)
> at org.apache.camel.support.service.BaseService.start(BaseService.java:119)
> at
> org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:113)
> at
> org.apache.camel.support.service.ServiceHelper.startService(ServiceHelper.java:130)
> at
> org.apache.camel.processor.errorhandler.RedeliveryErrorHandler.doStart(RedeliveryErrorHandler.java:1638)
> at
> org.apache.camel.support.ChildServiceSupport.start(ChildServiceSupport.java:60)
> ... 92 more{code}
> The reason is that {{camelContext}} is {{null}} in
> {{HeaderSelectorProducer}}, but I am still unsure why that is:
> {code:java}
> @Override
> protected void doBuild() throws Exception {
> super.doBuild();
> String key = this.getClass().getName();
> String fqn = RESOURCE_PATH + key;
> // -------- camelContext is null here:
> strategy =
> camelContext.adapt(ExtendedCamelContext.class).getBootstrapFactoryFinder(RESOURCE_PATH)
> .newInstance(key, InvokeOnHeaderStrategy.class)
> .orElseThrow(() -> new IllegalArgumentException("Cannot find " +
> fqn + " in classpath."));
> ...{code}
> Same with catalog:
> {code:java}
> public class MockCatalogTest extends CamelTestSupport {
> @Test
> public void testMockCatalog() throws Exception {
> MockEndpoint mockConsulAgent = getMockEndpoint("mock:consul:catalog");
> AdviceWith.adviceWith(context, "servicesRoute", a -> {
> a.mockEndpointsAndSkip("consul:catalog*");
> });
>
> mockConsulAgent.returnReplyBody(constant(singletonList(ImmutableNode.builder().node("node-1").build())));
> @SuppressWarnings("unchecked")
> Map<String, Service> result =
> fluentTemplate.to("direct:start").request(Map.class);
> }
> @Override
> protected RoutesBuilder createRouteBuilder() throws Exception {
> return new RouteBuilder() {
> @Override
> public void configure() throws Exception {
> from("direct:start").to("consul:catalog?action=" +
> ConsulCatalogActions.LIST_NODES);
> }
> };
> }
> }
> {code}
> Why this fails is not clear to me. At runtime the method {{setCamelContext}}
> of the failing instance never gets called, although it is a
> {{CamelContextAware}}. I suspect that it is the mock or the real object
> behind the mock which is not a {{CamelContextAware}} so that it receives no
> {{camelContext}}. My assumption is that a mock is a kind of wrapper around
> the real thing, but I wasn't able to understand the relationship between the
> two enough to fix this.
> Anyone who understands the mocking mechanics better than me: what might go
> wrong here? Any hints how it *should* work? Is the mock a proxy or what
> exactly is the relationship between mock and mocked?
--
This message was sent by Atlassian Jira
(v8.3.4#803005)