0

I am using rest-assured to test api.

one of the api expected result format is like "Store <b>"+StoreName+"</b> created successfully".

but the actual result is coming with some encoding, i am not sure what encoding they are using.

example: Store name is " abc&*()_-+=~/?'abc

then i am expecting result as Store <b>abc&*()_-+=~/?'abc</b> created successfully

but it is coming as Store \u003cb\u003eabc\u0026*()_-+\u003d~/?\u0027abc\u003c/b\u003e created successfully"

How to encode my expected result, so that i compare the expected with actual.

I want to convert the expected result only, because the conversion will be at single place.

to Convert actual result to expected result, i have solution as follows:

org.apache.commons.lang3.StringEscapeUtils.unescapeJava("Store \u003cb\u003eabc\u0026*()_-+\u003d~/?\u0027abc\u003c/b\u003e created successfully")

result will come as Store <b>abc&*()_-+=~/?'abc</b> created successfully

but i want to convert my expected result to actual result so that code will be at single place.

Sarada Akurathi
  • 1,164
  • 7
  • 28
  • 56
  • What you get is called unicode. See http://stackoverflow.com/q/11145681/5207900 who had the same problem. – Marteng May 22 '17 at 11:13
  • Does this help: http://stackoverflow.com/questions/15929686/how-to-decode-unicode-html-by-javascript – GhostCat May 22 '17 at 11:13
  • Thanks for reply, actually the complete string is not converted into unicode, only few characters are getting converted. actual result `Store \u003cb\u003eabc\u0026*()_-+\u003d~/?\u0027abc\u003c/b\u003e created successfully"` is converted to normal by using `org.apache.commons.lang3.StringEscapeUtils.unescapeJava("Asset \\u003cb\\u003eNNN\\u0026\\u003d\\u0027NN(Blue Star)\\u003c/b\\u003e was successfully created")` but i want to make is generic, expected result will be get using only of the method, so i want to convert expected message to the actual message format. but i am not sure how to do – Sarada Akurathi May 24 '17 at 06:40

1 Answers1

1

Actually, I wrote an Open Source library that contains some utilities. One of them is converting a Unicode sequence to String and vise-versa. I found it very useful. Here is the quote from the article about this library about Unicode converter:

Class StringUnicodeEncoderDecoder has methods that can convert a String (in any language) into a sequence of Unicode characters and vise-versa. For example a String "Hello World" will be converted into

"\u0048\u0065\u006c\u006c\u006f\u0020 \u0057\u006f\u0072\u006c\u0064"

and may be restored back.

Here is the link to entire article that explains what Utilities the library has and how to get the library to use it. It is available as Maven artifact or as source from Github. It is very easy to use. Open Source Java library with stack trace filtering, Silent String parsing Unicode converter and Version comparison

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • Thanks for reply, actually the complete string is not converted into unicode, only few characters are getting converted. actual result `Store \u003cb\u003eabc\u0026*()_-+\u003d~/?\u0027abc\u003c/b\u003e created successfully"` is converted to normal by using `org.apache.commons.lang3.StringEscapeUtils.unescapeJava("Asset \\u003cb\\u003eNNN\\u0026\\u003d\\u0027NN(Blue Star)\\u003c/b\\u003e was successfully created")` but i want to make is generic, expected result will be get using only of the method, so i want to convert expected message to the actual message format. but i am not sure how to do – Sarada Akurathi May 24 '17 at 06:41