How to verify optional parameters using Wiremock Request Matching Stubs(JAVA Usage)? - java

How to match a wiremock POST request with some optional JSON parameters & any values?
Being new to Stack community, I have raised the below query(afraid that it would be marked as duplicate) in the post (link mentioned above) but it has been deleted stating its the different question and need to raise new question.
Below is my query on top of it:
If the optional parameter is not present in the request than the solution provided in the above post suffice the purpose.
But If the optional parameter is present and we need to check that it contains specific values only (say not null) then how to do that using request matching in wiremock.
Kindly provide your inputs as I am new to wiremock
For instance:
{
"optional1"="ValueAlwaysYESIfPresent",
"optional2"="ValueAlwaysNOIfPresent"
}
Thanks in advance.

Have a look at http://wiremock.org/docs/request-matching/
instead of going to other places
basically you can do matching like
withQueryParam("optional2", equalTo("ValueAlwaysNOIfPresent"))
Or
withQueryParam("optional2", matching(".*12345.*"))
and many other things.

Related

micrometer statistics for RestTemplate

I am trying to get metric for rest uri using micrometer. I read this, this, and also bunch of SO posts. After reading all these, I have a some questions about it.
For micrometer to work correctly uri should be parameterized(Reference). I assume this is only path variable not request params. Am i correct?
should we not use UriComponentsBuilder at all because we don't want to expand uri outside resttemplate and let resttemplate do it for us.
lets say if i use this restClient.getForObject( fooSvcUrl, FooBar.class, uriVariables );, does micrometer looks at fooSvcUrl and it should be parametertized at this point ?
I am asking this because if i do following, it will not work. AM I correct ? restClient.getForObject(UriComponentsBuilder.fromUriString(fooSvcUrl).buildAndExpand(uriVariables ).toUri() , FooBar.class);
I recently had to deal with all that stuff, so I can give you the following answers:
1. "Parametrized" means here that you apply placeholders into the URI, regardless of their position. Basically, you can parametrize everything in the URI - the scheme, the host, path variables and even query parameters, as long as you provide values to replace the placeholders.
As such, the following URIs will work when passed to RestTemplate:
https://www.foo.bar/baz/{id} with {"id":1}
https://www.foo.bar/baz?id={id} with {"id":1}
https://{host}/foo/{id}?target={target} with {"host":"www.foo.bar", "id":1, "target":"baz"}
2. You should not, especially if you would expect reserved symbols that would be put into your URI, as UriComponentsBuilder will try to encode them immediately which results in RestTemplate doing a double tap on the encoding, turning a non-fragment # first into a %23 and then into a %2523.
3. You are right, the URI should be parametrized when used as an argument for RestTemplate.getForObject, as the overloaded methods taking parameter value arguments will expand it before actually performing the request.

Adding custom step definitions to Karate framework

I have a need to extract a field parsed from a "complex" response header and use that value later in the test.
It seems that the "header" keyword in Karate is set up for setting request headers, not parsing response headers.
Is there a way to add a custom step definition maintaining access to the scenario variable stores? It appears the variable stores are private in the StepDefs class, and there doesn't seem to be a way to extend it easily.
You can get access to the response headers. Please look at the documentation for responseHeaders.
That said, the match header short-cut is most likely what you are looking for.
Karate's philosophy is that you never need to write custom step-definitions.
edit: some examples, sounds like you just need to do some string manipulation of the Location header ? You can freely mix JS code into Karate expressions.
* def location = responseHeaders['Location'][0]
# assume location = 'foo?bar=baz'
* def bar = location.substring(location.indexOf('bar=') + 4)

How to hit the rest endpoint that has `:.*` as a part of the path param field

I am using the Stash's REST API in my project. My task is to get the tag details for a specific tag. After checking the Stash's REST API documentation, I found the correct endpoint that I should be using. It is
/rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/tags/{name:.*}
Please see this link for the Stash's REST API documentation.
There is one more endpoint /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/tags
With this endpoint I am able to retrieve all the tags. The StashTag object looks something like this.
{
"id": "refs/tags/v4.0.0",
"displayId": "v4.0.0",
"latestChangeset": "234dadf41742cfc2a10cadc7c2364438bd8891c5",
"latestCommit": "234dadf41742cfc2a10cadc7c2278658bd8891c5"
"hash" : "null"
}
My first problem is, I don't know which field to use as the parameter for {name:.*}. Should it be the displayId or Id or anything else.
The second problem is, I don't understand what it means to have : (colon) followed by a . (dot) in the endpoint /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/tags/{name:.*}.
Can someone explain me what is the purpose of :. in the path param and how to hit this kind of an endpoint. Also an example of the complete endpoint would be nice.
So far I have tried hitting
https://stashtest.abc.com/rest/api/1.0/projects/KARTIK/repos/kartiks-test-repository/tags/v4.0.0
https://stashtest.abc.com/rest/api/1.0/projects/KARTIK/repos/kartiks-test-repository/tags/refs/tags/v4.0.0
None of these endpoints work.
Any help is appreciated.
The {name:.*} is really just saying that the field name can be anything. Chalk this one up to poor documentation on their part. Think of it like Regex field, because that's exactly what it is. I'm sure at one point they had something like ^[0-9] then went back and changed it when they realized using only tag numbers would omit anyone using their lightweight tag features.
Remove the v from your tag version and see if that helps. If it does not, I would also recommend creating a lightweight tag (something like mytag) and seeing if you can hit it that way (i.e., /kartiks-test-repository/tags/mytag).
But looking at that documentation is telling me that your v in your tag name is throwing off the REST call.

box-api search parameters not working?

I'm using the Java SDK to connect to Box. I find the root folder (this is a small dev instance so I don't mind searching from there.) I execute the search query and I get results. My problem is that the search parameters do not seem to work consistently or at all.
For example, this query
Iterator resultSet = rootFolder.search("query=NR_chewy_chic_swt_pot_app&file_extensions=jpg&content_type=name&type=file").iterator();
returns three entries.
NR_chewy_chic_swt_pot_app.jpg
NR Chewy Chicken AD02.xls
PreInvoice_M197301-3644756_NR Chewy Treats SURP.pdf
I remove the substring "&file_extensions=jpg" because it doesn't seem to do anything and save/recompile/run and I get the same three results.
I change "&type=file" to "&type=folder" and I get the same three results.
I change "query=NR_chewy_chic_swt_pot_app" to "query=NR" and I get NO results. Even though SO user Peter (who appears to work for Box) says that partial strings should match1.
I've tried rearranging the order of the search parameters to no avail. What am I missing?
Thanks,
Eric B.
Advanced search has yet to be implemented in the SDK (since it's still in beta), but it will be added in the coming weeks.
The reason why your call doesn't work is because the query method parameter is sent as the "query" URL parameter in the API call. Therefore, the ampersands in your query string are being escaped.
If you need an immediate workaround, you can use the BoxAPIRequest and BoxAPIResponse classes to make a custom search request. For example:
BoxAPIConnection api = new BoxAPIConnection("token");
URL url = new URL("https://api.box.com/2.0/search?query=NR_chewy_chic_swt_pot_app&file_extensions=jpg&content_type=name&type=file")
BoxAPIRequest request = new BoxAPIRequest(api, url, "GET");
BoxJSONResponse response = (BoxJSONResponse) request.send();
String json = response.getJSON();
Sorry that this wasn't clear. We'll update the documentation to make it more explicit that query represents the query field and not the URL query string.

Sending custom object as a parameter for a web-service

How can one specify a custom object as a parameter for the web-service's method when invoking through SOAP message?
Say I have this code:
SOAPElement operation = body.addChildElement("MyMethod", "", trgNamespace);
SOAPElement value = operation.addChildElement("arg0");
value.addTextNode("i need to send here a custom object not a string")
request.saveChanges();
The addTextNode sends a string whereas I need to send my own object as a parameter for invocation.
You have to serialize your object to transfer it over the line. Serialization is often done using XML or JSON, see the following link for details: http://en.wikipedia.org/wiki/Serialization
That should get you on the right path.
Maybe try higher level and use wsdl-based stubs generator for java? It's Axis wsdl to java
I could think of another approach
You can send that custom object as a binary data (I assume your object is serialize-able). Then encode that data in say Base64 encoding.
There is similar problem asked earlier. Plz check out this link. This seems most relevant to your problem.
Another link mentioned in the above posting gives nice overview of handling these type of problems in general.

Categories

Resources