How to unit test an helper function that use HTTPConnection - java

I have written a helper function that can retrieve content from a url. This function can also parse a Map of parameters and feed a url or a body on request depending on GET or POST method. Let's say this function also do other things (change headers, cookies etc...). How can I test this function against an http(s) server ? How to simulate a fake servlet that will answer to the request made by this function ?
I've seen that we can use mock object, or other library that are specialized in servlet unit testing. But it doesn't seem to fit my needs as I trully need to test the request and its answer without changing the function content.

I would say to start up Jetty in embedded mode within your test.
Configure it to accept a request, validate it, and return an appropriate response.
See docs here: http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty

Related

Do we need to stub the other micro service in Spring cloud contract

#marcin
I am doing a pilot on implementing the spring cloud contract for Micro services which has around 50+ services talking to each other. I have few questions which I haven't found the answer precisely in your document.
The service which I am building has controller which processes and transforms my input payload to the desired output in json format. This json is used to build desired structure that should match the response in groovy (Our contract). However the controller, is sending json to another services with some URL as shown below.
request_url=http://localhost:8090/services/rest/transact/v2/pay/validate/0000118228/new response_body=null
Basically it is expecting the Response back from the other service by making use of this json and now response_body=null
My question is do I need to create a stub or mock the service? to make use of this response as an input to produce expected output from the response. Basically the microservice is expecting a ServiceResponse.
Another question is do we need to load in-memory data while doing the contract testing or do we need to just test the controller itself?
I don't really follow your description... "The service which I am building has controller which transforms my input payload sent from groovy and giving the desired output in json format" . Sent from which groovy? Groovy application? Can you explain that in more depth?
But I guess I can try to answer the question anyways...
My question is do I need to create a stub or mock the service? to make use of this response as input to produce expected output from the response. It is expecting a ServiceResponse.
If I understand correctly - service you mean a class not an application? If that's the case then, yes, in the controller I would inject a stubbed service.
Another question is do we need to load in-memory data while doing the contract testing or do we need to just test the controller itself?
That's connected with the previous answer. Your controller doesn't delegate work to any real implementation of a service, so no access to the DB takes place. If you check out the samples (https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/producer/src/test/java/com/example/BeerRestBase.java) you'll see that the base class has mocks injected to it and no real integration takes place
EDIT:
"The service which I am building has controller which transforms my input payload sent from groovy and giving the desired output in json format" is actually the description of what is done via the Spring Cloud Contract generated test. The next sentence was
However the controller, is sending json to another services with some URL as shown below.
In Contract testing, I don't care what your controller further on does. If it's in the controller where you send the request to some other application then you should wrap it in a service class. Then such a service you would mock out in your contract tests. What we care about in the Contract tests is whether we can communicate. Not whether the whole end to end functionality is working correctly.

How to access vertx HttpClientRequest fields?

I am writing a web server in java using vertx.
I use the server as a proxy to other services, and I'm the the testing stage. I want to know that I have created the request correctly with custom tokens and headers.
But, I cant manage to find a way to receive the properties upon creation.
HttpClientRequest clientRequest = vertx.createHttpClient().request(HttpMethod.GET,80,"host","/path?query=value");
When I try to read the host clientRequest.getHost() I receive a null, but in debug, reading its values, I can see a property named delegate which contains all of its data.
How can I access those values from clientRequest?
What you see in debug is:
((HttpClientRequestImpl) req).host
While getHost() method actually returns you hostHeader
For testing purposes I suggest to cast your HttpClientRequest to HttpClientRequestImpl, as it will expose more data.
If everything else fails, you can also fall back to reflection, of course.

Jersey client get sent data to String

I am building some JUnit tests for a REST client using Jersey, I therefore need to have a copy of the data sent to the server to run some tests in JUnit.
Currently my clients invokes:
Invocation invocation = serviceWebTarget.request(MediaType.APPLICATION_JSON).
buildPut(Entity.json((QARecord) valuesList.get(0)));
Response response = invocation.invoke();
In between the two calls the QARecord object is serialized to JSON and sent to the server but I cannot find a way to access it.
By debugging the code I found no variable in either invocation or response which contains the converted JSON text.
How can get the sent data into a String or a File for my JUnit test to check what has been sent?
As i understood you want to check what exactly client will sent to server as a request, am i right ?
If yes how exactly does your Unit test look like ?
For instance Jersey provides JerseyTest class which is base for testing of client code.
In few words such test will run special testcontainer which is able to execute your handlers inside.
By combining it with Mockito / or creating your own handlers by yourself, you can verify what is "captured" by them as a client request at the end of the test (when response is received by client). Among others it'll give you possibility to check not only what your client code is sending to server but also check behaviour of client by emulating various responses (successful or exceptional).
If you just want to get content of what client is really send to the server you can write jersey client filter and get body of request from there.
Filters and Interceptors

Starting point for converting jersey project to standard servlet app

I am trying to implement the example shown here.
The example seems to be for a jersey setup, which I am not using or familiar. How hard would it be to convert this to a standard java servlet project(idk how to name this)
What steps should I take. It seems most of the # annotations need to be changed to servlets.
This also seems very differnt from the standard appengine upload setup which all takes place in one servlet.
This would be a lot of work to rework the code to standard servlet and remove jersey. Jersey takes away so much boilerplate code. For example the JSON conversion is done by jersey, which otherwise would have to be custom implemented.
And you can for sure deploy more than one servlet to gae, in which way should this be standard?
Just look at the first method:
#GET
#Path("/url")
public Response getCallbackUrl() {
String url = blobstoreService.createUploadUrl("/rest/file");
return Response.ok(new FileUrl(url)).build();
}
When using only standard servlet you would need to do:
Servlet Definition and Mapping in web.xml to /url
Implement a HttpServlet, override doGet() method
Send Response Code 200 OK
Set appropriate HTTP Response Headers
Convert Response to JSON and write it to response

When is doPut() called in a servlet?

Hi I was just curious when is the doPut() method in a servlet called. I know that if the form on a jsp/html page has a "post" method then the doPost() is called otherwise if it has a "GET" then the doGet() is called.When is the doPut() called ??
When an HTTP PUT request is received, naturally.
Can a page do a PUT request by code?
The only valid method attribute values of a <form> are get and post, according to the HTML5 spec. I assume that's what you're asking.
The doPut() method handles requests send by using the HTTP PUT method. The PUT method allows a client to store information on the server. For an example, you can use it to post an image file to the server. As the above answer says, goGet() and doPost() are in use, mostly. In my case, I use only these two, and I am getting only get requests, so I simply transfer the get request to doPost() and do my job easily.
if you want to send some confidential values in url via form you must use the post method, If you will use the get method for the form like login the values parameters like userid and password will be visible in url and anyone can hack that thing. So better to use post method in forms. By default it will call get method.
in get the url is like http://url?method=methodname&userid=123&password=123
so if you use post method the url will be like this http://url/methodname.do

Categories

Resources