Check if JSON Array is sorted by 2 elements

PeterPeter Posts: 3

Using SOAtest 2020.2

Need to test if the a JSON Array is sorted.

Assuming JSON array is like:

[
{"id": 1, "folder": "fb\f2", "name": "n1"},
{"id": 2, "folder": "fa\f2", "name": "n2"},
{"id": 3, "folder": "fc\f2", "name": "n3"},
{"id": 4, "folder": "fb\f2", "name": "n4"}
]

the sort should use only folder + \ + name

So the output of the API being tested should be:

[
{"id": 2, "folder": "fa\f2", "name": "n2"},
{"id": 1, "folder": "fb\f2", "name": "n1"},
{"id": 4, "folder": "fb\f2", "name": "n4"},
{"id": 3, "folder": "fc\f2", "name": "n3"}
]

I want to test in SOAtest the sort is correct.

So I am guessing I would need to use a JSON Assertor then use a Custom Assertion? Unclear since I would be needing access to the next array element if I should not use a JSON Data Bank. but unclear how to test against this.

for a Language would be using JavaScript (legacy)

How do I reference the incoming JSON?

I guess in the text box use something like:

function myFunction(complete_response) {
// do stuff with it
}

Tagged:

Comments

  • benken_parasoftbenken_parasoft Posts: 1,025 ✭✭✭
    edited October 14

    I want to test in SOAtest the sort is correct.

    Do you only want to test the sort order or the content as well? If you want to check the content, including the order of array items, then you can just use the Diff tool.

    Otherwise, you could sort the message the way you expect then see if it is the same as the original. You could potentially do this by storing the original JSON in a JSON Data Bank, using the XML Converter to convert the JSON to XML, use the XML Sort tool to perform the desired sort, use the XML Converter to convert the sorted XML back to JSON again, then use a Diff tool to compare the sorted JSON against the original JSON from the JSON Data Bank.

    for a Language would be using JavaScript (legacy)

    If you decide to script things instead then you can use whatever language you like. I just would not recommend using anything that says "legacy" as that would be an old script engine that is obsolete, only there for backwards compatibility. If you prefer JavaScript then use the other one instead.

    How do I reference the incoming JSON?

    If you are just trying to write a script that accepts the entire JSON as input then I recommend using the Extension Tool instead. The Custom Assertion only receives the value of a specific node you select in your message. In either case, the first argument to your function is the input. It will typically be a string.

  • jakubiakjakubiak Posts: 746 admin

    A slightly simpler way to do it is to attach an XML Converter to the Response Traffic of your REST Client or whatever tool has access to the JSON. Then attach a XML Sort tool to the XML Converter, and after that a Diff Tool to the the XML Sort tool. The default sort in the XML Sort tool is sorting the way you want with the JSON payload you showed us. The only caveat is that the XML Converter is treating the folder names in a strange way - it interprets the "\f" as a special XML character that XML Sort doesn't like. So I had to remove the "\" to get the sort to work. If those are indeed the names of your folders, then additional processing might be needed to strip out the backslashes before sorting.

  • PeterPeter Posts: 3

    Going to give the XML Converter a shot. Having a little bit of question around the Diff tool. .. Seems like you are saying take the result of the test. convert to XML, sort that .. but then Diff it. Seems like the Diff tool would have 2 inputs. The original Response Traffic, and the SOAtest sorted. The input from the XML sort is obvious however not seeing where to connect the original to the Diff tool. Where / How to I connect the original? It at least seems to me I would need to do something like take the original Response Traffic convert to XML then have it as the second input to the Diff tool then finally if the original and the SOAtest sort generate are different treat as a failure of the test.

  • benken_parasoftbenken_parasoft Posts: 1,025 ✭✭✭
    edited October 15

    The input from the XML sort is obvious however not seeing where to connect the original to the Diff tool

    As I wrote earlier, you should store the original JSON response in a JSON Data Bank. You can also use a JSON Data Bank to store the sorted version, depending on your preference. The Diff tool can reference data bank columns for the control source under "Regression Control > Regression Control Source > Data Source". If you were to add a Diff tool as a separate test, then you can also reference data bank columns on the Input tab if you select the "Data source" radio button.

Sign In or Register to comment.