Validate Text Response using restAssured - rest-assured

How to validate Text Response using restAssured?
Basically I have downloaded the file In CSV format, now the response is coming in text format any suggestion how can we validate the column headers in the text?

I have got the answer.
try {
CsvSchema bootstrapSchema = CsvSchema.emptySchema().withHeader();
File file = new File(fileName) ;
MappingIterator<T> readValues = mapper.readerFor(type).with(bootstrapSchema).readValues(file);
return readValues.readAll();
} catch (Exception e) {
log.error("Error occurred while loading object list from file :{} with }
using Jackson csv formatter dependency

Related

Apache Tika - Getting Metadata Without Downloading File

I have been trying to implement an application to determine content type of any file. I use Apache Tika for determination.
Here is a basic code implementation for that:
InputStream fileStream = ContentTypeController.class.getClassLoader().getResourceAsStream(fileName);
Tika tika = new Tika();
String contentType = null;
try {
contentType = tika.detect(fileStream);
} catch (IOException e) {
e.printStackTrace();
}
Instead of code above I have to download files from Openstack to determine file content type. Some files are more than 100GB and downloading all file is heavy.
I can not figure out how to overcome this necessity of downloading all file, I hope you have any idea/solution without downloading all file
Tika has ability to determine content type of file without downloading all if you pass a URL parameter to detect() function.
Tika tika = new Tika();
String contentType = null;
try {
contentType = tika.detect(new URL("a url"));
} catch (IOException e) {
e.printStackTrace();
}

Rest Assured Json Path returns Exception

I´m reading json from a file in order to compare it to an http request body´s json.
I´m doing
JsonPath expectedJson = new JsonPath(new File("response.json"));
// some more code
assertThat().body("", equalTo(expectedJson.getMap("")));
which results in JsonPathException: Failed to parse the JSON document
This is the response.json file, which I copied from the response in Postman:
{
"screenDefinition":{
"taskId":"account-type",
"parameters":null
},
"prospect":{
"initializationType":"FIRST_HOLDER",
"jointAccount":{
"jointAccountId":655
},
"emailConfirmed":false,
"addressConfirmed":false,
"emailValidated":false,
"smsCodeAttemptsLeft":0,
"mobilePhoneValidated":false,
"paragraphsAccepted":false,
"termsConditionsAccepted":false,
"changedToAutonomousMethod":false,
"changedToIdentificationMethod":false,
"contractAccepted":false,
"prospectOnboardContactType":"NONE",
"secondAccountHolder":false,
"evidencesUploaded":false,
"uploadEvidencesLater":false
}
}
Any ideas?
That JsonPathException you got is probably caused by java.io.FileNotFoundException which means that path to your file is incorrect.
Try checking if file exists first:
File file = new File("response.json");
System.out.println("File exists: " + file.exists());
JsonPath jsonPath = new JsonPath(file);

Highcharts-Export Server for Java JSON parsing issue

I configured the highchart export server for Java from the current master in GitHub. When I tried to export an image using the demo page a corrupted file was returned. I have debugged to code and found out the following message is returned as the result in validate method in ServerObjectFactory.java
String result = server.request("{\"status\":\"isok\"}");
The value of the result String is "Failed rendering:SyntaxError: Unable to parse JSON string"
What can be the cause for this issue.
Thank You
That's very strange. The syntax of the JSON string is syntactically correct. I can point out where from the error is generated.
In the highcharts-convert.js file in highcharts-ecport-convert/src/main/resources/phantomjs the incoming request is expected to be a JSON string and is parsed, see Line 469
function (request, response) {
var jsonStr = request.post,
params,
msg;
try {
params = JSON.parse(jsonStr); // L469
if (params.status) {
// for server health validation
response.statusCode = 200;
response.write('OK');
response.close();
} else {
....
Set in a console.log(jsonStr) before JSON.parse(jsonStr) . I hope this give you a clue of why it's throwing a error.

URL encoding in java and decoding in javascript

I have an URL to encode on my java serveur and then to decode with javascript.
I try to retrieve a String I send in param with java. It is an error message from a form validation function.
I do it like that (server side. Worker.doValidateForm() return a String) :
response.sendRedirect(URLEncoder.encode("form.html?" + Worker.doValidateForm(), "ISO-8859-1"));
Then, in my javascript, I do that :
function retrieveParam() {
var error = window.location.search;
decodeURIComponent(error);
if(error)
alert(error);
}
Of course it doesn't work. Not the same encoding I guess.
So my question is : which method can I use in Java to be able to decode my URL with javascript ?
It's ok ! I have found a solution.
Server side with Java :
URI uri = null;
try {
uri = new URI("http", "localhost:8080", "/PrizeWheel/form.html", Worker.doValidateForm(), null);
} catch (URISyntaxException e) {
this.log.error("class Worker / method doPost:", e); // Just writing the error in my log file
}
String url = uri.toASCIIString();
response.sendRedirect(url);
And in the Javascript (function called in the onload of the redirected page) :
function retrieveParam() {
var error = decodeURI(window.location.search).substring(1);
if(error)
alert(error);
}
You don't use URLEncoder to encode URLs, it us used to encode form data to application/x-www-form-urlencoded MIME format. You use URIEncoder instead, see http://contextroot.blogspot.fi/2012/04/encoding-urls-in-java-is-quite-trivial.html

ASP.NET MVC: how to return a file from a stream

I'm trying to return a zipped file
public FileResult Download()
{
MemoryStream outputStream = new MemoryStream();
using (ZipFile zip = new ZipFile())
{
zip.AddEntry("asdasd.html", "<html>fgdfg</html>");
zip.AddEntry("asdassssd.html", "<html>asddsaf</html>");
zip.Save(outputStream);
}
return File(outputStream, "application/zip", "file.zip");
}
but in response it returns an error (in XML):
XML Parsing Error: no element found Location:
moz-nullprincipal:{122aa411-1418-43f5-b950-4347af7c7217} Line Number
1, Column 1:
What is wrong with my response (to zip files i use DotNetZip)?
You probably need to reset the MemoryStream to the beginning of its buffer before you return it to the client as a File.
outputStream.Seek(0, SeekOrigin.Begin);
I also suggest you use a proxy like Fiddler to inspect the Http response to get a better handle on what exactly your request is sending.

Resources