I'm trying to write a MUNIT test case for mule service and want to ignore a field having timestamp. Currently, i'm using below code to perform the compare.
org.skyscreamer.jsonassert.JSONAssert.assertEquals(getResource('json/item-locations.json').asString(), payload, false);
I want to ignore field "creationDateTime" which is available in my json message.
Just delete the field from the message before comparing it. If you want to make sure it's actually there, then replace it with a constant value, but only if it exists.
Related
I'm trying to test my REST application which generates some JSON and uses it to contact another API. I want to ensure the correct JSON is being generated, but the problem is that two of the fields are unique for each run, specifically an ID and a timestamp.
How can I verify this JSON is correct using Wiremock given that the fields are unique each time? Is there a way I can leave those fields to "any" or something in Wiremock?
obj.verfyObjContaining("{\"id\": 123 ,\"timestamp\": 11:11:11}");
Timestamp and ID are unique so this doesn't match.
Setting them to any isn't your only option. You can also mock the timestamp and id. Setting them to a static value during the test run. The advantage of that would be that you at least test that the value is being pushed out with the api call.
Unfortunately without any code posted I can not give more details than that.
Sorted it! Found a "matching()" verification function in Wiremock which takes a regex.
I'm writing a simple webapp to show my coding skills to potential employers. It connects with an API and receives a JSON file which is then deserialized using Jackson and displayed in a table form in the browser. I want to enable the user to persist the Java object in a Postgres database using Hibernate. I got it to work and it does the job nicely but I want to make it more efficient.
Whenever there is no data in the JSON response to put in the object's field (right now all the possible JSON attributes are present in the Java class/Hibernate entity in the form of String fields) I put an empty String ('') and then, with all fields having something and no null objects, it is stored in the database.
Should I only store what I have and put no empty strings in the DB (using nulls instead) or is what I'm doing now the right way?
Null is an absence of a value. An empty string is a value. But that don't impact much to memory. If you want to display data repeatedly and don't want conversion from null to empty string while retrieval you can go for empty string ''.
But if you want unique constraint for values other than empty string '' then use null.
Sometimes null and empty '' can be used to differentiate either data was known or not. for known but not available data use empty and for unknown data null can be used.
Use NULLwhen there isn't a known value.
Never use the empty string.
For example, if you have a customer which didn't supply his address don't say his address is '', say it is NULL. NULL unambiguously states "no value".
For database columns that must have a value for your web application to work, create the backing table with NOT NULL data constraints on those columns.
In your unit tests, call NULL, ..._address_is_null_ and test for success or failure (depending on if the test should trigger no errors or trigger an exception).
The use of '' in databases as a sentinel, a special value that means something other that '', is discouraged. That's because we won't know what you meant it to mean. Also, there might be more than one special case, and if you use '' first, then it makes restructuring more difficult to add others (unless you fall into the really bad practice of using even more special strings to enumerate other special cases, like "deleted" and so on).
I have a class with 2 values: val1 and val2. I am sending val1 to register (create) API and val2 is auto filled by API itself. I do not want to send val2 while calling create API and that API is not designed for handling unwanted values.
In short I want to ignore val2 while I call create API but I want it while I call get API.
The code that I have right now creates JSON including both the values assigning null to val2. This causes that API to throw an exception.
Is there any easy way of doing it (java /groovy)?
Is there any easy way of doing it (java /groovy)?
Not 100% sure I'm understanding your need. I believe it depends on what json de/serializer you are using. For example, using Jackson we do:
#JsonIgnoreProperties(ignoreUnknown = true)
#JsonTypeName("account")
public class Account {
I believe this allows us to load in objects with a ton of extra json fields into objects without corresponding java fields. To quote from the javadocs:
Property that defines whether it is ok to just ignore any unrecognized properties during deserialization. If true, all properties that are unrecognized -- that is, there are no setters or creators that accept them -- are ignored without warnings (although handlers for unknown properties, if any, will still be called) without exception.
I follow this to create test for testing my endpoint. Problem is that in my response I am returning current time which I dont want to test. Is there some way how to ignore some value like: ${IGNORE}
The default ResponseMatchers class supplied with Spring-WS uses the PayloadDiffMatcher in order to compare the given and expected payload. The compare is done using an XMLUnit Diff. You could extend the PayloadDiffMatcher (or implement your own custom ResponseMatcher) so that it allows specifying a filter for your current time element as illustrated here.
Another option is to use the xpath() ResponseMatcher and only check parts of the returned response, ignoring the time element.
I am currently using the objectMapper of Jackson to serialise values in a JSON and put them in a POJO. I need to validate those values so I use validation annotation such as #Regex, #Max and others.
So what happens for now is that I call objectMapper method to read JSON
publicEnquiry = objectMapper.readValue(jsonNode, Enquiry.class);
Then I retrieve all the validation messages in a list and I return it to the user.
payload = publicEnquiry.getPayload();
Set<ConstraintViolation<Enquiry<payload>>> constraintViolations =
publicEnquiry.getConstraintViolations();
Everything works fine if I send a too big integer, a bad formatted string, or anything that will not create a problem for the serialisation, but if I send a date formatted in an unexpected format, or simply bad formatted like "2010-02" instead of "2010-02-03", then I get a JsonMappingException. It is of course expected because the mapper can't understand a bad-formatted date.
However, I need to manage those exception and to be able to add a validation message each time it happens, in a way that will seem transparent for the user. I need a message like "Validation failed: the expected format is yyyy-MM-dd". And I need to perform the normal validation on the other properties opf the POJO like in a normal case.
Unfortunately, Jackson doesn't offer a method in the objectMapper that would skip the exception-generator fields and give back a list of those troublesome fields, or anything like that. It simply fails if there is a problem.
Does someone would have a solution or at least a suggestion on how to proceed ?