Serenity + Rest services - java

I am trying to demo serenity with Restassured at my workplace here and show them how awesome and easy it is to use in comparison to using jasmine.js
How ever I am stuck with few things in the basic test I am trying to do
My test says
Given we have valid credentials for the client using this test
When we try to serach for a medicine '<medicine>'
Then we get a valid '<perfLabel>' response with search results
|medicine|perflabel|
|Salbutamol|perflabel1|
|Panadol|perflabel2|
|Salbutamol (GA)|perflabel3|
When I go into the next step
#When("we try to serach for a medicine '(.*)' ")
public void tryToSearchUsingEquals(String medicine)
{
tsApiActions.requestServiceSearchWhichEquals(medicine);
}
In my Step method
#Step
public void requestServiceSearchWhichEquals(String medicine)
{
host = "http://www.int.abc.com.au/api/cs/v1/terminology-service/trade-product/search-summary?offset=0&limit=20&prefLabel=eq "+medicine+"&sort=prefLabel DESC&cache=false";
requestSend(host);
}
The questions I have are
How do i inject the variables(Salbutamol, Panadol) into the uri?
How do I put this URI into a seperate properties file and call it in the Step method?
Any help is really appreciated
Thanks

RestAssured requests follow the same code structure which should be added into your sendRequest method:
given().
param("prefLabel", medicine).
when().
get(URL).
then().
body(containsString(medicine));
URL can come from property file, but you need to create a method to upload it before test run and then you have to create a getPropety() method to get the current value you need.
I suggest to read the official documentation here:
https://github.com/rest-assured/rest-assured

Related

How to test a HTTP request

I have set up this simple http request, which simply returns a "hello world" response to my IDE terminal. I have been looking into testing and I am not quite sure how i would test what this method is returning.
Currently i have done my own research into JUnit, but again i am not even sure if this would be the correct tool to use for this problem. I only researched this as it is a Java tool.
public static void newRequest() throws IOException {
URL helloServer = new URL("http://localhost:5050/");
HttpRequestFactory requestFactory = new NetHttpTransport().createRequestFactory();
HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(helloServer));
HttpResponse rawResponse = request.execute();
String responseString = rawResponse.parseAsString();
logger.debug(responseString);
}
Any guidance on this would be greatly appreciated.
Does the function even need to be tested?
Does the function even need to be tested? Well, that is entirely up to you. Does this function contain code that is critical to your application? If so then yes. If the impact of a bug in this function is minimal then probably not.
Assuming that you want to test this, then:
The method in question is not returning anything void before the function name says this. You will need to look at testing the logic of the function. In this case you need to check that the correct response is received. There are two ways that I can think of to do this:
Modify the code to return the response.
You could change the function to return a String and then return rawResponse.parseAsString(); (which is the same thing you are logging.
Then you can call the function from the test and check the String that is returned.
Get the log message from your logger.
Depending on the logging that you are using, you could get the log message that was written by the function. Assuming log4j then there are some posts on how to do this:
log4j: how to get the last inserted log message?
Personally, I prefer the first option as it is less effort. I would also consider returning the body of the response rather than the raw response.

Java Jersey-client: witing test a GET method

I want to write a unit test for GET method.I have a GET request that returns a json file of when I request path localhost:9090/application/a/b:
{
name: a,
Age:b
}
I want to write unit test using jersey client. I tried writing as such:
Response response = target("a/b").request(MediaType.APPLICATION_JSON).get();
The objective is to retrieve from the GET response and assert the returned json with an expected value:
assertEqual(expected value:{name: a, Age:b},returned json from response)
However, I am not quite sure how I can write this. Can someone guide me on this? I have been searching through many code samples but mostly it is for POST request so I am not sure how this is implemented for GET request.
Edit: I am not required to post anything (I can just grab most code sample for POST) What I am trying to do is just to invoke a call using the path without sending any json document or object and have it returned to me a response in json. After that, I am supposed to grab this json response, and then assert the object with an expected document. The part that I need guidance on is writing the right Response line of code. Also, since my test class has to inherit a jersey client parent, I am expected to use the Response class. I couldnt find any example like this online. That is why I am here to ask.
Thanks in advance

Get Test Instance Names from test set folder in Java using API

I am trying to get the test instance names from the test set. I am able to get the test id by using the following URL:
"rest/domains/"+domain+"/projects/"+project+"/test-instances?query={cycle-id["+testSetId+"]}" ;
Is there any way I can get the test names also?
I guess you're talking about the ALM Rest API.
If you do, you can try to add in final of your URL the following:
&fields=test-config.name
Then your URL will look like this:
rest/domains/{domain}/projects/{project}/test-instances?query={cycle-id[{testSetId}]}&fields=test-config.name
The name of your Test Instance will be shown into the RelatedEntities tag from your XML.
For more information about ALM REST API you can click here.

Get Dropbox accessToken using DbxWebAuth.finish method

I'm trying to complete the oAuth2 trip to get the AccessToken.
I followed this official guide to understand how Java API works, and I'm using the documentation to understand how class work together, but I'm not able to understand how com.dropbox.core.DbxWebAuth#finish(Map<String, String[]> queryParams).
I don't understand which values give to queryParams.
Do someone explain me?
PS: This is some code that I write to retrive the access token.
String accessToken(String code, String state, DbxWebAuth webAuth) {
DbxAuthFinish authFinish = webAuth.finish(????);
return authFinish.accessToken;
}
The Dropbox Java Core SDK tutorial does use DbxWebAuthNoRedirect which has a different finish method than DbxWebAuth:
DbxWebAuthNoRedirect.finish
DbxWebAuth.finish
The DbxWebAuth.finish documentation has the following for queryParams:
queryParams - The query parameters on the GET request to your redirectUri.
For a sample of how to use it, the web-file-browser example app included with the SDK uses DbxWebAuth.finish as such:
DbxAuthFinish authFinish;
try {
authFinish = getWebAuth(request).finish(request.getParameterMap());
}

simple example of native app passing string to website?

Can somebody give a simple example for java code of a native app passing a string to a website?
For example: when a string has the value Hello everybody, the text Hello everybody should get pasted into the Google search field.
For the most simple use, you can try:
public static void browseURL(Activity activity, String url)
{
try
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
activity.startActivity(intent);
}
catch (Exception e)
{
message(activity, "Sorry, failed to view the desired page.");
}
}
and then call:
browseURL("http://www.google.com/search?q=Hello+World")
Do you want to fill the fields and submit them? If so, just do the request with the request parameters filled, and parse the response given by the server. Look into Apache HttpClient.
You don't actually have to add text to the Google search field explicitly. You can send a URL with a query string.
Depending on the website the query string will always be different. For Google it is http://www.google.ca/search?q=something . Anything after a ? is considered a query string which any good web developer will include in a webpage. That query string takes custom commands in the form of ?command=query for command&command2=query for command 2.
Since this is tagged blackberry, I assume you want to implement a blackberry app, and you don't explicitly explain what you want to do, so you have two options,
Invoke the browser
On that page it describes how to open a browser session. So within the
browserSession.displayPage("http://http://www.google.ca/search?q=searching%20for%20something");
If you need a class for URL encoding, let me know and I'll send one your way.
Http Request to pull the html of the webpage into the code. To do that, you'll have to look at my blog this week as I'll be posting a full in code network class either tomorrow or tuesday, which I'll edit this post to contain a link to.
OR you can send me a message if you need it NOW and I can email the non-cleaned up code to you.

Categories

Resources