Simple camel routing from to - apache-camel

I have a fundamental question regarding routing in Camel. Assuming I have this following route:
from(amq:MyQueue).process("jmsToHttp4").to("http4://dummyhost:8080").to("file://out/MyFolder");
Assuming that the http4-Response is just a String "Your Response". As far as I understood the documentation, "Your Response" can be retrieved through:
exchange.getOut().getBody()
Lets say that I only want to write "file://out/MyFolder", only if the reponse contains the word "Response". How can I achive this?
One more question:
If I want to write a test in a spring environment, how can I mock the response with "Your Response positive test" and "Your negative test"? Somehow I need to be able to write the response strings in the exchange.out.body right?
Thank you,
Hadi

One option would be to declare a .filter(body().contains("Response")) right after the HTTP call.
As for the test, you might use the mock component that offers you ways of processing the exchange and also asserting whatever you need when the message hits your mock endpoint.
There is actually some alternatives to test... I'm used to declaring the endpoints in the properties file and using the key in the class, e.g. .to("{{my.http.target}}"). Thus, in this case, in the test environment (dedicated properties file) you'd replace your http4 with mock.
In my opinion, it is cleaner and requires less control of the context when writing tests, mainly in big/complex applications. On the other hand, this might affect code readability.
But if you prefer to keep declaration in your Java class, you'll have to intercept the http4 call in the test env, then divert it to your mock endpoint.
I hope it's helpful.

Related

App engine - raw request body - stripe webhooks

I'm trying to run my node.js app app engine and I am having trouble with stripe webhooks - with the constructEvent, that I need to give a request raw body. Worked on virtual machine but not on app engine.
event = stripe.webhooks.constructEvent(req.rawBody, sig, stripeKeys.webhookPaymentIntent);
Says:
No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? https://github.com/stripe/stripe-node#webhook-signing
Just looking at the code that you posted here, I wonder if the last parameter is indeed the value that you wanted to pass to the constructEvent function. it reads webHookPaymentIntent. I wonder if this should really be the webhook signature secret? It may be that it really is the webhook secret value, but just named a bit misleadingly.
Maybe this is something though you can verify? A simple test would really to be to pass the string literal here to see if that would work first. Of course make sure not to commit that to any source control.
The stripe-node method params are listed here for reference: https://github.com/stripe/stripe-node/blob/1d6207e34f978d8709d42d8a05d7d7e8be6599c7/lib/Webhooks.js#L11

The reason of usage axios-mock-adapter

I'm a beginner to test axios calls and started using axios-mock-adapter but I don't get why we use axios-mock-adapter.
mock.onPost('/api').reply(200, userData, headers);
In this code snippet, does the request really go to the server or is this just a simulation?
Because if I give wrong credentials, it responses with 200 status as I identify it on 'reply' to return 200.
So if I identify the response status, what is the reason to use it?
If it doesn't really go to server, it seems like this is useless.
Maybe I miss something I don't know because I'm new but someone should put the light on this issue on my mind.
Answers to your questions
In this code snippet, does the request really go to the server or is this just a simulation?
It is just a simulation. No request is made, just the "reply" is returned. This is called mocking and is hugely popular and useful for writing tests.
If you are new to different kinds of testing in general, this answer is well worth reading.
If it doesn't really go to server, it seems like this is useless.
If you want to test the system as a whole: i.e. your website + backend logic (like auth, data retrieval etc.) then yes, this is useless. But you would not use a mock for that.
Integration tests typically are executed against a running system, they are valuable, but very hard to maintain and are generally slower to execute. You have to take care of not just your tests, but even the data.
When would you use a mock
Mocks are essential for unit testing your code. Mocks help isolate your front end code logic from the dynamic behavior. This makes it easier for you to simulate many scenarios without the overhead of maintaining data.
Example Scenario
Use Case
In your application, you have to authenticate a user against a REST end point. It is expected that:
When user is logged in successfully, Logout button is shown in the header.
When users password has expired, a "change your password" screen is shown.
When user has entered wrong credentials, a warning is shown
When backend is not responding user is shown a screen to try again later
Without mocks, you need to ensure that you have the exact data configured in your authentication system. From experience I can tell you it is hard, especially #2 & #4.
But with mocks, you can just configure the mock to return the response you expect for each scenarios, in/before each it() block.
This is also easier to maintain as expectation (assert/expect statements) is set near the test data (mock.reply()).

How to implement a generic HTTP request/response logging with web reactive

With the "traditional" web framework, one could use e.g. AbstractRequestLoggingFilter for implementing a generic logging filter. With web-reactive the filter isn't called anymore (what makes sense, since it operates on HttpServletRequest).
Can anyone point me into the right direction for implementing a request filter with web-reactive, which logs the HTTP request, including its body, before and after the request like in AbstractRequestLoggingFilter?
You can implement a WebFilter and declare it as a bean, it will be picked up automatically.
Note that the WebFilter contract is based on ServerWebExchange, which holds a ServerHttpRequest. The body is not accessible directly as byte[], but rather as a Flux<DataBuffer>; this is not meant to be buffered in memory or consumed by the filter, so logging the whole request body is more complex than in MVC scenarios. Also, you should avoid blocking operations during request processing.

APEX Rest API - Swagger

I am a bit new to the whole APEX service plugins but I was wondering if Salesforce has native support for Swagger, or any similar REST description language, for the REST api's that I create in the APEX service platform?
For example:
#RestResource(urlMapping='/v1/users/*')
global with sharing class UserRestService {
...
#HttpGet
global static List<Member__c> doGet(....)
{
...
}
}
I would like the ability to return the swagger json, a WADL document, or something for this REST service (and all other REST services I have in there). Does anyone know of a way I can do this?
Thanks in advance!
There is no built in support at this time. I was interested in seeing what could be done via currently available public APIs. The first thing I ran into is the grammar does not seem to like parameters to HttpGet methods. That right there will make it challenging since the only way to get input parameters appears to be via the Request entity which means you would have to parse the actual code. In other words, there does not appear to be declarative input binding.
Further, in looking at the tooling API which let's me get some amount of "reflective" information about the class, there is not always sufficient information to render a response payload (in your case, it just shows LIST but not what's in the list)
Again, it looks like one would have to rely on a parser (there is at least one Antl grammar floating around).
(this is getting some internal attention but I can't say any more at this time)

Best practices for model validation using a REST API and a javascript front-end such as Angular

I'm transitioning towards more responsive front-end web apps and I have a question about model validation. Here's the set-up: the server has a standard REST API for inserting, updating, retrieving, etc. This could be written in Node or Java Spring, it doesn't matter. The front-end is written with something like Angular (or similar).
What I need is to figure out where to put the validation code. Here's the requirements:
All validation code should be written in one place only. Not both client and server. this implies that it should reside on the server, inside the REST API when persisting.
The front-end should be capable of understanding validation errors from the server and associating them to the particular field that caused the error. So if the field "username" is mandatory, the client can place an error next to that field saying "Username is mandatory".
It should be possible to validate correct variable types. So if we were expecting a number or a date and got a string instead, the error would be something like "'Yo' is not a correct date."
The error messages should be localized to the user's language.
Can anyone help me out? I need something simple and robust.
Thanks
When validating your input and it fails you can return a response in appropriate format (guessing you use JSON) to contain the error messages along with a proper HTTP error code.
Just working on a project with a Symfony backend, using FOSRestBundle to provide proper REST API. Using the form component of Symfony whenever there's a problem with the input a well structured JSON response is generated with error messages mapped to the fields or the top level if for example there's unexpected input.
After much research I found a solution using the Meteor.js platform. Since it's a pure javascript solution running on both the server and the client, you can define scripts once and have them run on both the client and the server.
From the official Meteor documentation:
Files outside the client, server and tests subdirectories are loaded on both the client and the server! That's the place for model definitions and other functions.
Wow. Defining models and validation scripts only once is pretty darn cool if you ask me. Also, there's no need to map between JSON and whatever server-side technology. Plus, no ORM mapping to get it in the DB. Nice!
Again, from the docs:
In Meteor, the client and server share the same database API. The same exact application code — like validators and computed properties — can often run in both places. But while code running on the server has direct access to the database, code running on the client does not. This distinction is the basis for Meteor's data security model.
Sounds good to me. Here's the last little gem:
Input validation: Meteor allows your methods and publish functions to take arguments of any JSON type. (In fact, Meteor's wire protocol supports EJSON, an extension of JSON which also supports other common types like dates and binary buffers.) JavaScript's dynamic typing means you don't need to declare precise types of every variable in your app, but it's usually helpful to ensure that the arguments that clients are passing to your methods and publish functions are of the type that you expect.
Anyway, sounds like I've found the a solution to the problem. If anyone else knows of a way to define validation once and have it run on both client and server please post an answer below, I'd love to hear it.
Thanks all.
To be strict, your last gate keeper of validation for any CRUD operations is of course on server-side. I do not know what is your concern that you should handle your validation on one end only(either server or client), but usually doing on both sides is better for both user experience and performance.
Say your username field is a mandatory field. This field can be easily handled in front-end side; before a user click submit and then been sent to the server and then get returned and shows the error code. You can save that round trip with a one liner code in front-end.
Of course, one may argue that from client-side the bad guys may manipulate the data and thus bypassing the front-end validation. That goes to my first point - your final gate keeper in validation should be on your server-side. That's why, data integrity is still the server's job. Make sure whatever that goes into your database is clean, dry and valid.
To answer you question, (biased opinion though) AngularJS is still a pretty awesome framework to let you do front-end validation, as well as providing a good way to do server-side error handling.

Resources