Service Virtualization

Expand all | Collapse all

Scripted Assertion

Jump to Best Answer
  • 1.  Scripted Assertion

    Posted 05-30-2017 05:33 PM

    I was trying to add a Scripted Assertion  on VSM using Java in Devtest WS.But,I didn't find Java in the language available.Do I need additional JARs for it.If Yes,how do I get those?



  • 2.  Re: Scripted Assertion
    Best Answer

    Posted 05-30-2017 06:53 PM

    Are you familiar with Beanshell, Groovy, Velocity, or Javascript. DevTest supports these OOTB.  

    Beanshell follows the same basic syntax as Java but is more loosely typed and it is compiled at runtime.  Groovy is similar to Beanshell but is compiled so it is a bit more efficient.  Refer to: www.beanshell.org 

     

    If you need to perform more complex logic, you can always roll your own Java in a JAR, place it in hotdeploy, and invoke your class(es) from an assertion coded with Beanshell, Groovy or Javascript.  

     

    If you can provide more detail about what you are trying to do, we might be able to give you some example approaches.

    A couple of examples are here: Using BeanShell in DevTest - DevTest Solutions - 10.0 - CA Technologies Documentation  



  • 3.  Re: Scripted Assertion

    Posted 06-01-2017 09:33 AM

    Thanks for the information Joel.I was just trying to take two parameter's values from response meta  and sum their values an populate in the third parameter

    In my case,I would just take two vales from my respnonse: i.e

    <ns1:yodaOrderNumber>900216205</ns1:yodaOrderNumber>
    <ns1:sourceSystem>35775788</ns1:sourceSystem>

     

    and add them and placethe added value under  a different tag dynamically i.e 
    <ns1:sourceLineNumber>00000000</ns1:sourceLineNumber>

     

    Please find my response meta:

     

    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="http://cio.att.com/commonheader/v3" xmlns:urn1="http://cpe.att.com/cpeservice">
    <soapenv:Header>
    <urn:WSResponseHeader/>
    </soapenv:Header>
    <soapenv:Body>
    <ns1:createCPEOrderResponse xmlns:ns1="http://cpe.att.com/cpeservice" xmlns:ns5="http://yoda.att.com/cpecommondata" xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <ns1:yodaOrderNumber>900216205</ns1:yodaOrderNumber>
    <ns1:sourceSystem>{{=request_sourceSystem;/*MAGIC*/}}</ns1:sourceSystem>
    <ns1:sourceSystemOrderNumber>default</ns1:sourceSystemOrderNumber>
    <ns1:lineResponse>
    <ns1:yodaLineNumber>{{= generateRandom(int min, int max) { int range = (max - min) + 1; return (int)(Math.random() * range) + min; }"999"+ generateRandom(10, 999)+"888"; }}</ns1:yodaLineNumber>
    <ns1:sourceLineNumber>GLB.300000012</ns1:sourceLineNumber>
    <ns1:lineStatus>PENDING PROCESSING</ns1:lineStatus>
    <ns1:orderItem>{{=new Date() }}</ns1:orderItem>
    </ns1:lineResponse>
    </ns1:createCPEOrderResponse>
    </soapenv:Body>
    </soapenv:Envelope>



  • 4.  Re: Scripted Assertion

    Posted 06-01-2017 09:26 AM

    Thanks for the information Joel.I was just trying to take two parameter's values from response meta  and sum their values an populate in the third parameter

    In my case,I would just take two values from my respnonse: i.e

    <ns1:yodaOrderNumber>900216205</ns1:yodaOrderNumber>
    <ns1:sourceSystem>{{=request_sourceSystem;/*MAGIC*/}}</ns1:sourceSystem>

     

    and add them and placethe added value under  a different tag i.e 
    <ns1:sourceLineNumber>GLB.300000012</ns1:sourceLineNumber>

     

    Please find my response meta:

     

    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="http://cio.att.com/commonheader/v3" xmlns:urn1="http://cpe.att.com/cpeservice">
    <soapenv:Header>
    <urn:WSResponseHeader/>
    </soapenv:Header>
    <soapenv:Body>
    <ns1:createCPEOrderResponse xmlns:ns1="http://cpe.att.com/cpeservice" xmlns:ns5="http://yoda.att.com/cpecommondata" xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <ns1:yodaOrderNumber>900216205</ns1:yodaOrderNumber>
    <ns1:sourceSystem>{{=request_sourceSystem;/*MAGIC*/}}</ns1:sourceSystem>
    <ns1:sourceSystemOrderNumber>default</ns1:sourceSystemOrderNumber>
    <ns1:lineResponse>
    <ns1:yodaLineNumber>{{= generateRandom(int min, int max) { int range = (max - min) + 1; return (int)(Math.random() * range) + min; }"999"+ generateRandom(10, 999)+"888"; }}</ns1:yodaLineNumber>
    <ns1:sourceLineNumber>GLB.300000012</ns1:sourceLineNumber>
    <ns1:lineStatus>PENDING PROCESSING</ns1:lineStatus>
    <ns1:orderItem>{{=new Date() }}</ns1:orderItem>
    </ns1:lineResponse>
    </ns1:createCPEOrderResponse>
    </soapenv:Body>
    </soapenv:Envelope>



  • 5.  Re: Scripted Assertion

    Posted 06-01-2017 11:34 AM

    In order to do this, let's make some assumptions and then work through an example:

    1) request_sourceSystem is a property that is coming in on the inbound request. So, when the DPHs process the request, this value will contain the value of the source system that applies to this specific request.  We can use that to our advantage by directly referencing it.

     

    2) In the VSI, we know we need to substitute a combination of fields in order to create a response value that will be placed in the <sourceLineNumber> XML. And, the value needs to be dynamic such that it contains a combination of the request_sourceSystem and the yodaOrderNumber.

     

    In the VSI, replace the sourceLineNumber value with a property name that DevTest can construct and substitute at runtime.

    <ns1:yodaOrderNumber>900216205</ns1:yodaOrderNumber>
    <ns1:sourceSystem>{{=request_sourceSystem;/*MAGIC*/}}</ns1:sourceSystem>
    <ns1:sourceSystemOrderNumber>default</ns1:sourceSystemOrderNumber>


    <ns1:lineResponse>

    <ns1:yodaLineNumber>{{... your logic here ... }}</ns1:yodaLineNumber>
    <ns1:sourceLineNumber>{{=fl_generatedLineVal;}}</ns1:sourceLineNumber>
    <ns1:lineStatus>PENDING PROCESSING</ns1:lineStatus>

     

    We now have something that DevTest can replace at runtime.  But, we need to populate the property with a value so DevTest can make the substitution.

     

    Once the VSI Selection Step has executed, the lisa_vse_response contains a response.

     

    Add a JSR-223 step after the VSI selection step (you could use Scripted Assertion but your exit logic will change slightly) In the JSR step, add some code to extract data and "build" a property. (note: code below is not tested so it may contain errors or typos)

     

    // get the response body created in the VSI Selection Step from the lisa_vse_response object
    String theBody = lisa_vse_response.get(0).getBodyAsString();

    int sPos = 0;
    int ePos = 0;

    // find the start of the order number in the XML response
    sPos = theBody.indexOf("yodaOrderNumber>");

     

    // If you don't find an order number XML element, get out, nothing to do
    if ( sPos == -1 )
        return;

     

    // find the end position of the Order Number in the XML response
    ePos = theBody.indexOf( "</", sPos + 16 );
    if ( ePos == -1 )
        return;

     

    // Now use the values to create the property that will be replaced at runtime

    // first extract the order number from the XML response since you did not indicate

    // that the order number is on the input request

    String orderNum = theBody.substring( sPos + 16, ePos);

     

    // create a source line number by combining the incoming source system with the order number

    String sourceLineNumber = testExec.getStateString("request_sourceSystem", "GLB") + "." + orderNum;

    // place the concatenated value into the property that is defined in the VSI's XML response

    testExec.setStateValue("fl_generatedLineVal", sourceLineNumber );

    // when the responder step fires, DevTest will substitute the properties with values so no

    // need to put the value in the actual response payload and set it back into lisa_vse_response

     

    Notes: The logic above only finds the first yodaOrderNumber.  If your payload has more than one yodaOrderNumber AND you need to increment the source line number in the response XML for each occurrence of <lineResponse>, you will need more code.



  • 6.  Re: Scripted Assertion

    Posted 06-01-2017 03:26 PM

    Thanks.Its working perfect.But I just wanted the sum of integers in sourceLineNumber and no "GLB".At present its concating both  request_sourceSystem value and orderNum i.e 

    String sourceLineNumber = testExec.getStateString("request_sourceSystem", "GLB") + "." + orderNum;

     

    I tried coverting it to integer by using to-string method and then adding the numbers.But it fails there for me.

     

    for ex:request_sourceSystem= 123456

    orderNum=111111

    sourceLineNumber=234567



  • 7.  Re: Scripted Assertion

    Posted 06-01-2017 05:42 PM

    Try replacing a few lines. Sorry, I thought the source was alphanumeric rather than numeric.  

    // get source system, if prop does not exist, return a value '0' rather than null | empty

    int srcSys = Integer.parseInt( testExec.getStateString("request_sourceSystem", "0") );

    // put the string orderNum into an int

    int oNum = Integer.parseInt( orderNum );

    // Add them together and return a string value

    String sourceLineNumber = Integer.toString( srcSys + oNum );



  • 8.  Re: Scripted Assertion

    Posted 06-02-2017 03:47 PM

    Thank you so much Joel.Its working as expected. It will give us good kick start to customize several other  flows as well