Hi Claus,
Yep - I've come around to thinking exactly as you state above, and in fact I
did get everything working fine with 'adviceWith()'.
But as I stated originally, I still think for newbies (like me!), the
following code is far more intuitive than using 'adviceWith()':
Processor emulator = new ServiceEmulator();
MockEndpoint mock = getMockEndpoint("mock:Service*");
mock.whenAnyExchangeReceived(emulator);
But you're absolutely right about not overloading MockEndpoint to handle
multiple endpoints, and defining a MockEndpoints or MockWildcardEndpoints or
MockMultipleEndpoints etc. makes more sense. But really this highlights my
initial confusion, because I thought 'mock:Service?a=b' and
'mock:Service?x=y' should actually be considered the *same* endpoint (i.e.
'mock:Service') and that the query parameters won't affect that (in pure URI
terms they are the same endpoint (I think!), and Camel is supposed to fully
supports URIs, so is this an example Camel 'breaking' the URI spec...??).
Maybe just a note in the documentation would be helpful to make the
distinction clear.
Anyway, I think the idea of a MockEndpoints (plural) was pretty much the
approach suggested by Willem.
So Willem, I tried your code example, and got it all working, although there
were a couple of typos i nyour original code and I had to extend it a little
bit too!
Basically when I fixed the typos (a couple of 'else' statements missing,
marked in bold below), my ServiceEmulator was invoked correctly for each
endpoint listed in my recipient list, which was great.
But the first line of my 'process()' method in the ServiceEmulator is:
String fullUri = exchange.getProperty(Exchange.TO_ENDPOINT,
String.class);
...since my emulator needs access to all the URI query parameters. And the
value I saw was not the actual URI I specified in the recipient list (e.g.
'myMock:Service?param1=a¶m2=b'), but was in fact the URI used to create
the initial mock instance (i.e. 'myMock:Service*').
So to fix this, I need to be able to overwrite the URI of the endpoint that
you retrieve from the Map of endpoints. Unfortunately, the
'setEndpointUri()' method is marked as 'protected', so I had to derive my
own 'MockEndpointAllowingUriToBeOverwritten' class to allow me call that
method to overwrite the URI.
With that, everything works perfectly!!
So guys, thanks so much for all your input on this question - it's really
helped me gain a better, deeper appreciation for Camel. I'm not sure if it
makes any sense to tidy up this 'MyMockComponent' class and contribute it to
the Camel codebase (for instance, it's certainly not thread-safe!) - I'll
leave that to you guys to decide...
Thanks again!
public class MyMockComponent extends DefaultComponent {
private Map<String, MockEndpointAllowingUriToBeOverwritten> endpoints =
new HashMap<String, MockEndpointAllowingUriToBeOverwritten>();
@Override
protected Endpoint createEndpoint(String uri, String remaining,
Map<String, Object> parameters) throws Exception {
MockEndpointAllowingUriToBeOverwritten endpoint = null;
// deal with the uri which is end with *
if (remaining.endsWith("*")) {
String key = remaining.substring(0, remaining.length() - 1);
if (endpoints.containsKey(key)) {
endpoint = endpoints.get(key);
* } else {*
endpoint = new MockEndpointAllowingUriToBeOverwritten(uri,
this);
endpoints.put(key, endpoint);
}
* } else {*
// check if the remaining is used here
if (endpoints.containsKey(remaining)) {
endpoint = endpoints.get(remaining);
endpoint.overwriteEndpointUri(uri);
} else {
endpoint = new MockEndpointAllowingUriToBeOverwritten(uri,
this);
}
}
return endpoint;
}
class MockEndpointAllowingUriToBeOverwritten extends MockEndpoint {
public MockEndpointAllowingUriToBeOverwritten(String uri, Component
component) {
super(uri, component);
}
public void overwriteEndpointUri(String uri) {
setEndpointUri(uri);
}
}
}
--
View this message in context:
http://camel.465427.n5.nabble.com/How-to-mock-endpoints-taking-lots-of-different-query-parameters-values-tp5719629p5719756.html
Sent from the Camel - Users mailing list archive at Nabble.com.