Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

need help with Serenity Rest API Scenario #244

Open
varmavg opened this issue Jul 29, 2020 · 10 comments
Open

need help with Serenity Rest API Scenario #244

varmavg opened this issue Jul 29, 2020 · 10 comments

Comments

@varmavg
Copy link

varmavg commented Jul 29, 2020

Good afternoon,
I am using cucumber serenity (serenity-rest-assured) for testing my Rest API and I ran in to an issue with a specific scenario. Hope I get some help here.

I need to drill in to 2 levels via Reset API and validate the response (scenario as mentioned below).

In serenity RestAPI --> executing first API (Providing 1st Header, Body and Post URL) --> from response body getting some values --> trying to update a Json file with those values via “JSONParser jsonParser “ --> write to json and close the file (not sure if this is right thing to do in the first place but I see that the new value is reflected in Json when opened that file) --> now trying to execute the 2nd API ((Providing 2nd Header, “updated Body.Json” and Post URL)) --> but I see that code is picking up the old JSON file values (rather than updated values). not too sure if all the Json files and property files are cached during the execution and my updates are not propagated.

if i run the 2 API's separately it works fine (run 1st API --> update the JSON, run 2nd API --> using previously updated File )

Basically I have a body json with some parameters (coming from UI/feature file) and some parameters coming from 1st API so trying to construct the json to be used as a body to the 2nd API call.

apiBody = MergeFrom.template("RestBody/BodyJSONPath"))
.withDefaultValuesFrom(FieldValues.in("RestBody/BodyPropertiesPath")))
.withFieldsFrom(apiDetails);

Here is the versions that I am using

net.serenity-bdd serenity-rest-assured 2.0.91

Can you please let me know or point me to a direction on how we can achieve this scenario in serenity.

Thanks in advance
Vinay

@varmavg
Copy link
Author

varmavg commented Jul 29, 2020

After the post i did some more testing and here is what i found. Looks like when i was writing data to Json i was using as below file .write which is updating the file as expected but it looks like its not the same as "open file --> write something --> save the file."

File writer code:

try (FileWriter file = new FileWriter("pathtofile/test.json")) {

            file.write(jsonObject.toJSONString());
            file.flush();
            file.close();
 
        } catch (IOException e) {
            e.printStackTrace();
        }

the way i tested this is added a thread.sleep after the write and manually opened the file and close it --> then below code is working as expected.

apiBody = MergeFrom.template("RestBody/BodyJSONPath"))
.withDefaultValuesFrom(FieldValues.in("RestBody/BodyPropertiesPath")))
.withFieldsFrom(apiDetails);

@varmavg
Copy link
Author

varmavg commented Jul 30, 2020

As far as i checked the Serenity restassured internal code looks like serenity is copying all the files to cache first and then processing --> looks like any updates to the json files during run time it is not picking up in my case (still picking up from Cache rather than picking up from file system).

Any Alternative workaround solution would be appreciated.

@wakaleo
Copy link
Member

wakaleo commented Jul 30, 2020

Maybe use RestAssured directly for that code?

@varmavg
Copy link
Author

varmavg commented Jul 31, 2020

thanks John, is there a sample code that you can point me to ?

the reason why i am asking is in serenity its very easy to pass the parameters from Jenkins/Feature file to a property file and it updates the Json file using mergeFrom template (MergeFrom.template("RestBody/BodyJSONPath"))). looks like now i may have to write a bit of code to do this.

@varmavg
Copy link
Author

varmavg commented Aug 2, 2020

hi John,
For now i wrote custom code to merge the the information which is coming from UI/Feature file, property file and Json file accordingly. I got it working but code is not pretty.

is it possible to get a code fix for this issue in next release? thanks in advance.

thanks
vinay

@wakaleo
Copy link
Member

wakaleo commented Aug 2, 2020

I’m not really clear on where a fix would go. Could you propose a PR?

@varmavg
Copy link
Author

varmavg commented Aug 4, 2020

I am afraid I will not be able to propose a PR (as I am not an expert in Java). But I can give you an example/scenario where this issue is occurring (as per my analysis).

One thing I like about this simple serenity RestAPI framework (than restassured) is we don’t have to write too much code and worry about updating nested json object on the fly (as its done internally).

Problem :

Execute a RestAPI (provide headers, body, endpoint) --> you will get a response--> we want to take few parameters from the response --> update the 2nd API body with those parameters (1st API parameters)--> now execute the 2nd RestAPI (provide headers, “updated body from 1st response”, endpoint) --> here you will see that when you are executing the 2nd API internal code is still picking up the json body from cache (instead of updating the new updated body json file from file system)

Issue:

As far as I see Serenity is using “FreeMarker templates” which copies all the files to cache at the start --> now if any file is update during the execution --> when you try to access the updated file --> its trying to get the files from cache rather than reloading the updated file to cache.

As far as I read the freemarker template documentation. Looks like it’s a known issue (as listed below).

https://freemarker.apache.org/docs/pgui_config_templateloading.html#autoid_41

“Note that some template loaders won't see that a template was changed because of the underlying storage mechanism doesn't support that; for example, class-loader based template loaders may have this problem.”

@wakaleo
Copy link
Member

wakaleo commented Aug 4, 2020

I'd need to spend some time on this. Are you able to get your company to raise a support ticket for this?

@varmavg
Copy link
Author

varmavg commented Aug 4, 2020

i dont know the process here but will find out from my peers and see.

@varmavg
Copy link
Author

varmavg commented Aug 5, 2020

FYI i checked with my peers and looks like we don't have any support contract with Serenity. i guess my company will not be able to raise any request.

again thanks for your time. for now i wrote my own code to read the updated json and send the request.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants