How we can build "Params" dynamically in rest api automation - java

In api automation, let's consider below code to hit the api and get response.
Response res= given().
formParam("email", "value").
formParam("password", "value").
formParam("action", "login").
header("token","value").
when().post("MyResource").then().assertThat().statusCode(200).extract().response();
Actually in the above code we're just building an api with formParams, header and resource with post request right!
so, there we have created 3 formParm manually and passed the values right? now i want that to added automatically based on no of parameters that we have from an excel sheet.
Thing is these parameters can be deleted or new parameters gets added in future, so that's why i want to add those dynamically from an excel sheet data.
how i can do that? any suggestions?

If i've understood you right, you want to have the parameters on the spreadsheet to use dynamically and on runtime.
To do that, you'll need to:
Read the spreadsheet file and get the parameters contained in a data structure such as a Map<String,String>, in which, the key is the field and the value is... the value :P
Iterate upon the data structure selected and set the formParam generically
final RequestSpecification given = given();
for(final Map.Entry<String,String> entry : map.entrySet()) {
given.formParam(entry.getKey(), entry.getValue());
}
final Response res = given.other_things() ...
I hope it helps, but keep in mind the use of Map is not necessary. Choose the data structure more appropriate to your situation!

Related

How to get custom extension attributes with microsoft graph api

I'm trying to get user details with microsoft-graph
I'm looking for a custom extension element in my response, such as "extension_3a4189d71ad149c6ab5e65ac45bd6add_MyAttribute1"
when I retrieve the response with String, I can see all the elements.
final ResponseEntity<String> response = restTemplate.exchange("http://graph.windows.net/tenant.com/me?api-version=1.6, HttpMethod.GET, new HttpEntity(headers),String.class);
But when I retrieve the response with com.microsoft.graph.models.extensions.User I can't see the extention anymore.
final ResponseEntity<User> response = restTemplate.exchange("http://graph.windows.net/tenant.com/me?api-version=1.6, HttpMethod.GET, new HttpEntity(headers),User.class);
How can I retrieve the custom extension in more elegant way than getting in String object and look for elements one by one?
because the extension attributes are specific to your tenant, that means its non standard, no out of the box "object class" in the sdk would contain it since it has the app id appended to it. extension_appid_attribname.
so you would have to handle it yourself. you can try to extend the user class and add a method to read or deserialize/map the json return from the graph api similar to what Hury suggested, or something to that effect. there won't likely be an out of the box solution for this.
there are also json libraries out there that may help you deserialize to a dynamic object of some sort, if you really didn't want to map the object manually.
Update:
I dug into this a bit further. I don't think its in extensions.extension however, I did find that in the java sdk you can access it . Here's the documentation: https://github.com/microsoftgraph/msgraph-sdk-java/wiki/Working-with-Open-Types
You would do something like
String ext =
user
.additionalDataManager()
.get("extension_2lkj3l12jl3j2kj3_yourproperty")
.getAsString();
Give that a try
Hopefully that helps.
Please use api
https://graph.microsoft.com/beta/users instead of https://graph.microsoft.com/v1.0/users api https://graph.microsoft.com/beta/users will return all data with custom data of users.

Creating type inside index in elastic search through rest high level client

I am using rest high level client elastic search in my JAVA application. Document can be found here.
In my application at startup I am deleting index named "posts" where Elasticsearch datas are stored and creating again Index "posts" following this link
CreateIndexRequest request = new CreateIndexRequest("posts");
But, Inside index I need to create one type named "doc". Which is not mentioned in the website.
Temporary fix is when I am posting some data following this link it is creating type
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "kimchy");
jsonMap.put("postDate", new Date());
jsonMap.put("message", "trying out Elasticsearch");
IndexRequest indexRequest = new IndexRequest("posts", "doc", "1")
.source(jsonMap);
But, in this process when I am posting only then I can able to create type "doc". If I am not posting and trying to hit controller which calls data frmo index "posts" and type "doc". It gives error as "doc" type is not there.
Anyone hava any idea how to create type using rest high level client ES in java
By type you mean document type?
What about the second section Index Mappings in the link you provided?
Does this not work for you?
I needed to set type to "_doc" to make it working with ES7.6
If you know how to insert documents through API then this way will much more easy for you to do anything similar API (DELETE,POST,PUT...)
First, you will need RestHighLevelClient and all you have to do
String index = "/indexName/_doc"; <- Your path or type here
Request request = new Request("POST", index); <- Your method
request.setJsonEntity(
"{ \"message\": \" example add insert\" }" <- Your request body
);
client.getLowLevelClient().performRequest(request);
    
This will execute like how API does.

Show properties file content and edit it from web page

I have a properties file whose content would be like below
myprop=key1:value1,key2:value2
myprop2=key21:value21,key22:value22
second set
myprop3=key31:value31,key32:value32
How can I show the content on a web page and make it editable, so that, if I change the value of key2 to "value 444", it just changes that and everything else remains intact?
Apache commons configuration looks helpful but not sure how above can be accomplished
I solved it using Apache commons library.
Get Builder object.
Get Configuration object from Builder.
Iterate through all the keys and their values and you can return it as a JSON response to a web page (using controller class).
Make them editable their.
When you update one value of a key; click on save will call another method end point wherein input param would be config key and updated value. which will do config.setProperty and will do builder.save();.
By this, my property file would be updated.

How to read file content values in file array

I am using ng-file-upload AngularJs API to upload multiple files to server.But this is the traditional way to do it.But my requirement is that i dont need to store files in a server as it is.I have a REST end point that responsible for store user input data to DB.Along with the REST request i pass the file Array object with other forms values.When data comes to REST end point it access each attributes and store data.When it tried to read File Array obj i can not read the file content for each file.
Sample File Upload Code
jsfiddle
Note that i just want to pass only $scope.files along with the REST request.Please let me know how can i read file content values from server side reading file Array in Java.If you guys know any better way to do this please share your ideas.
REST Service Code Snippet
#POST
#Path("/manual")
#Produces(MediaType.APPLICATION_JSON)
public boolean insertResults(testVO testResult) {
for(Object o:testVO.getFiles()){
LinkedHashMap<String, String> l=(LinkedHashMap<String, String>) o;
System.out.println(l.get("result"));
}
}
Note: testVO.getFiles() type is Object[] array.
In my preceding code i convert object into LinkedHashMap and access the necessary fields like size,type,etc.But my requirement is that how can i get the content belong to that file.

Jersey Post request - How to perform a file upload with an unknown number of additional parameters?

I asked something like this previously, but upon re-reading my original post, it was not easy to understand what I was really asking. I have the following situation. We have (or at least I'm trying to get working) a custom file upload procedure that will take in the file, a set number of 'known' metadata values (and they will always be there), as well as potentially an unknown number of additional metadata values. The service that exists currently uses the Jersey framework (1.16)
I currently have both client and server code that handles dealing with the file upload portion and the known metadata values (server code below)
#POST
#Path("asset/{obfuscatedValue0}/")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public UUID uploadBlob(#PathParam("obfuscatedValue0") Integer obfuscatedValue0,
#FormDataParam("obfuscatedValue1") String obfuscatedValue1,
#FormDataParam("obfuscatedValue2") String obfuscatedValue2,
#FormDataParam("obfuscatedValue3") String obfuscatedValue3,
#FormDataParam("obfuscatedValue4") String obfuscatedValue4,
#FormDataParam("obfuscatedValue5") String obfuscatedValue5,
#FormDataParam("file") InputStream uploadedInputStream) {
.....
}
...and excerpt of client code:
Builder requestBuilder = _storageService
.path("asset")
.path(obfuscatedValue0.toString())
.type(MediaType.MULTIPART_FORM_DATA)
.accept(MediaType.APPLICATION_JSON);
FormDataMultiPart part = new FormDataMultiPart()
.field("file", is, MediaType.TEXT_PLAIN_TYPE) // 'is' is an inputstream from earlier in code.
.field("obfuscatedValue1", obfuscatedValue1)
.field("obfuscatedValue2", obfuscatedValue2)
.field("obfuscatedValue3", obfuscatedValue3)
.field("obfuscatedValue4", obfuscatedValue4)
.field("obfuscatedValue5", obfuscatedValue5);
storedAsset = requestBuilder.post(UUID.class, part);
However, I need to pass a map of additional parameters that will have an unknown number of values/names. From what I've seen, there is no easy way to do this using the FormDataParam annotation like my previous example.
Based upon various internet searches related to Jersey file uploads, I've attempted to convert it to use MultivaluedMap with the content type set to "application/x-www-form-urlencoded" so it resembles this:
#POST
#Path("asset/{value}/")
#Consumes("application/x-www-form-urlencoded")
public UUID uploadBlob(#PathParam(value), MultivaluedMap<String,String> formParams) {
....
}
It's my understanding that MultivaluedMap is intended to obtain a general map of form parameters (and as such, cannot play nicely together in the same method bearing #FormDataParam annotations.) If I can pass all this information from the Client inside some sort of map, I think I can figure out how to handle parsing the map to grab and 'doMagic()' on the data to get what I want done; I don't think I'll have a problem there.
What I AM fairly confused about is how to format the request client-side code when using this second method within the jersey framework. Can anyone provide some guidance for the situation, or some suggestions on how to proceed? I'm considering trying the solution proposed here and developing a custom xml adapter to deal with this situation, and sending xml instead of multipart-form-data but I'm still confused how this would interact with the InputStream value that will need to be passed. It appears the examples with MultivaluedMap that I've seen only deal with String data.

Categories