Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

XML Assertor / Method Assertion

LegacyForum
LegacyForum Posts: 1,664 ✭✭
edited December 2016 in SOAtest
Is there any documentation on the XML Assertor / Method Assertion?

I have an environment variable that contains a string like "M8015S12345". The XML returned from my web service returns two elements <MODEL>8015</MODEL> and <SERIAL>12345</SERIAL>. I need to find a way to compare the first numerical part of my variable to the element <MODEL> and the second numerical value to the element <SERIAL>. I can write a jython script to split the variable to the component I want and return the value, but I am not sure how to make the assertor work correctly. How do I compare my new value to the element chosen in the Assert? Is the Method Assertion the best way to do this, or is there a better way to solve this problem?

I am using SOATest 5.5.2 SP3.

Thanks,
Eyal
Tagged:

Comments

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    The easiest way to go about asserting this case is to use two Method Assertions linked together using a Compound AND Assertion. This way, you can easily pass the <MODEL> and <SERIAL> elements into each method respectively and reference them via the "input" argument in your scripts. Please refer to the attached .tst file for an example test setup and script.

    from com.parasoft.api import *
    from java.lang import String

    def model(input, context):
    myVar = String(context.getEnvironmentVariableValue("ITEM01"))
    modelStr = myVar.substring(1, 5)
    # next two lines meant to show output
    Application.showMessage("modelStr = " + modelStr)
    Application.showMessage("Minput = " + input)
    return modelStr == input
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    This looks close to something I could use.

    Delving deeper into my test scenario for generating numerical keys. The first call generates 100 keys. The second call also generates 100 keys, but the numbering starts at a higher value by adding 2000 to the start of the first value (if it is on a different server). I check "next number" at the end of each set of 100. Here are expected values for the first four calls:

    (These calls go to alternate servers)

    Call 1: 101
    Call 2: 2101
    Call 3: 201
    Call 4: 2201

    This is "best case". Sometimes, however, the keys might start somewhere other than 0, so the call returns could be:

    Call 1: 1215
    Call 2: 3109
    Call 3: 1315
    Call 4: 3209

    I think I would use a method to add 100 or 1000 to the initial values, which are for the most part, unknown...? The value assertions do not seem to allow me to use explicit values (or do they???!) I was hoping to find that Numeric Assertion would allow me to compare it to (parameterized value (+ 100)). Ah well.

    Am I working in the right direction to consider a solution such as the one in this thread?

    Thanks!

    Brian
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Almost there. I came up with this as a script:

    from com.parasoft.api import *
    from java.io import *
    from java.util import *

    def compareValues(x, context):
    firstKey = context.getValue("Generated DataSource", "Test 1: nextNumber 2")
    secondKey = context.getValue("Generated DataSource", "Test 2: nextNumber 2")
    if secondKey != (firstKey + 2000):
    return 1
    else:
    return 0


    But I still need to add some kind of string to int conversion since I am getting this error message:

    Traceback (innermost last): File "<string>", line 8, in compareValues
    TypeError: __add__ nor __radd__ defined for these operands

    Close..........
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Hello Brian,

    I was out of the office on Friday, and I apologize for not getting back to this post sooner. To be direct, I am somewhat fuzzy on the specifics of your testing scenario. Can you attach a copy of the .tst file here so that I can see for myself what your setup looks like? If not, I can try to offer some suggestions, but I cannot guarantee its applicability or accuracy.

    If you are using four SOAP clients (to perform each "call" individually), you can extract the "next number" value from the response, pass it into a Method tool, add 1000 (or whatever number) to it, and then store the result in an XML Databank to parameterize the next test's assertion. This is somewhat roundabout, but will allow you the flexibility in assertion that you are looking for.

    If you are using a single SOAP client that is running through multiple iterations, then the implementation for the assertion you are looking for is much more complex. I'd like to look at your .tst file before offering any suggestions in this case, as the testing structure will heavily affect the assertion setup needed.

    If your scenario is not like either of the above, then I will need either more clarification or a copy of your .tst file. Hopefully, this will not be too inconvenient for you.

    Regards,
    Joseph
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Python provides easy string-to-int conversions. In your case, simply replace line 8 with

    if int(str(secondKey)) != (int(str(firstKey)) + 2000):

    This is because firstKey and secondKey are not Python strings yet, and must be converted to such by using the str() method. After that, converting the Python string to integers is trivial with the int() method.

    Hope this helps,
    Joseph
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    >>I was out of the office on Friday, and I apologize for not getting back to this post sooner.

    No worries! Hope you had a great four-day weekend (w00t!)

    Let me work with the suggested string/int conversions. That might do the trick. If not, I'll pare down the .tst file and attach the most important parts for you.

    Thanks!

  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Nicely done, Joseph. Here is my working script:

    from com.parasoft.api import *
    from java.io import *
    from java.util import *

    def compareValues(x, context):
    firstKey = str(context.getValue("Generated DataSource", "Test 1: nextNumber 2"))
    secondKey = str(context.getValue("Generated DataSource", "Test 2: nextNumber 2"))

    if int(str(secondKey)) != (int(str(firstKey)) + 2000):
    return 1
    else:
    return 0


    Small and tidy, no? The final piece I need is to get the test step to fail when 1 (one) is returned. Right now SOAtest says, "Hey, a 1! Nice job!" and happily goes on it's way. I have verified that the method will return a 1 if the values are equal. Returning a 1 should be a fail! Do I need to convert this back to xml and test it, or is there a more elegant way of causing the step to fail if a 1 is returned? Could I return something else?

    Many thanks!
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Afternoon Brian,

    I assume you are using the XML Assertor's Method Assertion. If you are, then returning 1 means that the test passed, and returning 0 indicates that the test failed. If you are using a regular method tool, you can indicate a passing/failing test by using the same 1/0 return values, except that you need to check the "Return value indicates success" checkbox near the top of the Method tool window. In Python, 1 correlates to 'true' and 0 correlates to 'false'.

    Hope this helps,
    Joseph
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭
    Upon re-reading your previous post, I realized that I had misunderstood your question. Usually, SOAtest treats 1 as 'pass' and 0 as 'fail', and it would be easiest if you code your script as such. However, if you wish to enforce that 1 correlates to 'fail', then you can use a regular Method tool to return that value to a chained XML Asserter, which can then perform a value assertion on the returned value. However, this is rather circuitous and unintuitive, and is unnecessary given the default operation settings stated before.

    In any case, I apologize for my mistake, and hope that this is what you were looking for.
    Joseph
  • LegacyForum
    LegacyForum Posts: 1,664 ✭✭

    In Python, 1 correlates to 'true' and 0 correlates to 'false'

    oops...heh...

    Eureka, that's it! You, my friend, are a life saver. Thank you so much for your help, Joseph!

Tagged