Stub camel route bean reference for integration test - apache-camel

I have a camel route which has a bean pointing to a amazon wrapper class. That class is making an amazon call.
Ideally I would like to stub out the amazon call for the integration test.
However all I can think of is to put a split on the route to check for a particular flag. If the flag is positive then the code calls thr stub and if its negative then it calls actual amazon.
I really do not like this option as it is mixing production code with test code. Or is it ok in this scenario.

You can use advices for this scenario..
If you need help with it, please Post an example Route that demonstrates your Problem.

Related

How can I include common behavior in several Apache Camel routes?

I am using Camel 2.19.2 in a Spring Boot 1.5.8 application. If I want, for example, to have several of my routes to be "status aware", how might I achieve this? What I mean by "status aware" is that the route will start, notify a component that the workflow has begun, then it will conduct route-specific logic, and when that is complete, it will notify a component that the workflow has completed. I want this to happen automatically, if possible, and without having to call the specific logic in each of the route builders that I want to use this capability.
Here is a code example like what I mean:
public class FooRouteBuilder extends StatusAwareRouteBuilder {
#Override
public void configure() {
// Here I want to have this route know how to notify something
// that this processing has begun, but I do not want to have
// to explicitly call a processor to make it happen, but it
// should know what to do by virtue of extending a custom
// route builder, if appropriate, or by some other/better
// mechanism
// Now conduct any route-specific logic
from("vm:myAction")
.process("myProcessor");
// Now handle the status notification that this is finished...
// Here I want to have this route know how to notify something
// that this processing has finished
}
}
Conceptually, this is almost like AOP, so I would like to be able to define this behavior in one place and include it in some number of routes that need to use this behavior. Is there a way that I can accomplish this? I saw that there is adviceWith for testing, but I need this for regular operation. Thanks in advance.
I think that RoutePolicy and RoutePolicyFactory can be the answer, i.e. you can have a callback invoked when the route or exchnage start/stop.
For more info see http://camel.apache.org/routepolicy.html
Perhaps Camel interceptors can help you. These are typically small generic routes that are applied to all or most of your routes.
For example to do a security check in every route. With an interceptor you write it once and it is applied to all routes, even new ones that are added.
There are three flavours.
intercept intercepts each and every processing step while routing
an Exchange in the route.
interceptFrom intercepts every incoming
Exchange in the route (start of processing)
interceptSendToEndpoint intercepts when an
Exchange is about to be sent to an endpoint.
The interceptors can be configured to "fire" only for specific endpoint types or by Camel predicate on other specific conditions.
There is also the onCompletion feature to do a similar thing at route completion. Either on successful completion on failure completion or both of them (default).

How to capture error at compile level in Apache Camel

I have route like
.bean(OrderService.class, "doSomething")
Now my question is if for any reason any developer misspell method name in route, we would not be able to identify at compile level or we may realize after going to production.
How to handle these scenario?
At a minimum I would recommend a test which ensures that your Camel components are registered with the Camel context, and if they're not, one would expect an exception to be thrown at application startup time.
In essence, you would want a Spring test suite (since Camel will leverage Spring's context for its own context) to ensure that your bean is wired in correctly here.
This cannot be a compile-time error or check since this is specific to how the application context is built, which is done dynamically at runtime.

How (or should) I test 3rd party module results in angular with jasmine?

I am just starting to get into unit tests but I seem to end up thinking in circles and would really like some guidance. I totally understand typical examples like how to test an adding function but my application isn't as simple.
An overview of my application (I can provide code if it will help):
I have services which basically make XHR calls to my API. Each service I have so far tested it goes to the right URL, mocked responses for success and error responses and I feel pretty happy it is tested enough.
Now my controller basically calls the service, grabs the data and on success calls angular-nvd3 nv.addGraph() with some config options to draw a chart. On error it shows a toast message.
So now I am wondering what exactly do I test in this controller? This is what seems right to me but some seem impossible to test:
service is called
the div has a chart in it on success (is this testable?)
a toast is displayed on failure (is this testable?)
I can check if the functions are called but the results don't seem testable to me. I can mock a response from the service call but that seems redundant to my testing on the service.
Any suggestions would really help, functions that manipulate data seem easily understandable to test but when it involves view elements or XHR requests I start getting confused as the expected success will always be accurate since it is mocked.
Thanks a bunch.
On your controller you could test:
Service is called (I think you're doing that already, good job)
You probably put the data from the Service into some variable, you could check if the variable was assigned properly
Toast.show was called with the message you want to be displayed in case of failure
It seems to me that testing if the div has a chart would be a candidate for an Automated UI test.

Testing a websockets server with protractor

I have a custom WebSockets server that triggers route changes in a AngularJS client. I would like to carry out e2e integration tests using protractor. As the route change events are every 30 seconds and involve multiuser interactions, it seems like creating a mock of the service is the best approach. Is there a recommended way of doing this in protractor?
I am not sure if this is exactly what you are looking for, but you can define mock services in Protractor and then upload them to the browser using Protractors browser.addMockModule function. The uploaded module will replace the real one that the application would normally use. See the following blog post for more details.
http://eitanp461.blogspot.com/2014/01/advanced-protractor-features.html
I recently had a similar problem and solved it using addMockModule as stated above. I wrote a post about it describing my solution.
http://www.functionalimperative.com/2015/04/22/protractor-socket-mocks.html
I found organizing my code a certain way helped when mocking the socket modules.

AngularJS Provider dependency injection - using $http in provider?

tl;dr
I'm really struggling to find the appropriate pattern here. How should I best configure a generalized provider to a specific use-case? I can't use $http as a dependency in .configure(); can I?
longer, boring explanation:
I am trying to create a generalized provider which I may reuse in Angular. I have it working, however it requires configuration.
The intention is to provide a fallback REST service to use in saving data to the server, but with provision to save offline in local-storage. Therefore, I need to provide appropriate $http calls for each instance of this provider.
Is it possible to provide appropriate $http calls with .configure() or else should I try and figure out how to inject $http into the provider from the start and then configure it afterward??
It's frustrating... and may change in AngularJS 2.0... But for now, yes, it is not possible to do this. There is a very high wall between the .configure() and .run() states, so you can't access $http from a .configure() function. The reason is that it hasn't actually been created. At this stage, all that exists is the provider. Once all of the dependencies are configured, then the http provider will be used to make the real $http service.
I'm not sure exactly what you're trying to do, but there are two excellent AngularJS developers that are good to follow who have some advanced patterns in projects they've shared: Pascal Precht and Brian Ford. Here are two projects that make heavy use of provider/service concepts as well as $http-driven services:
https://github.com/angular-translate/angular-translate
https://github.com/btford/angular-modal
Angular Modal, especially, does $http work to load its templates. There might be use cases in there that are similar to what you're trying to do.

Resources