Service Virtualization

  • 1.  How do I validate date format in the request arguments?

    Posted Mar 31, 2017 07:40 AM

    I have created a virtual service image where I need to validate whether the date in the incoming xml request is in the valid format (DDMMYYHHMMSS).

    If the date is not in the specified format I have to set an error code in the response. How do I achieve this?



  • 2.  Re: How do I validate date format in the request arguments?
    Best Answer

    Posted Mar 31, 2017 11:45 AM

    There could be many approaches for addressing this. Keep in mind that we do not know all of the details of your service and its requirements so the generic approaches may or may not achieve the requirements.  

     

    Before sharing ideas, I want to caution that one should be careful about implementing too much business logic in a service.  When done, the service begins to require additional maintenance to keep the business rules consistent with the live application.  I recognize that this is unavoidable in many scenarios. Moving on....

     

    Approach 1: Scriptable DPH (Beanshell, Javascript, Groovy, etc.)

    I might consider implementing a Scriptable DPH to check the date's validity.  If the date is invalid, the Scriptable could insert an additional argument onto the incoming request argument list and make that argument visible in the VSI. Or, maybe the scriptable changes the value of the incoming date to "error". The Scriptable could be used both at recording and playback time.  

     

    In your example, the VSI exists, so if the DPH adds a field like the code below does, you would copy/paste the transaction in the VSI, CLICK on the new transaction's META, and manually add the additional argument exactly as you define it in the Scriptable.  At this point, requests having an "invalid date" have a unique transaction in the VSI. (i.e., they are separate from transactions with valid date times.)  The META (i.e., signature matches) could respond with a generic error response and/or specific transactions (i.e., exact matches) could identify more specific error responses. An example VSI might look like this.

    Basic, untested, DPH code snippet is:

    // check operation, if OP is not one with a date, get out

    // replace 'theOpNameWithDates' with the real operation name

    // as it appears in the VSI
    if ( ! "theOpNameWithDates".equals( lisa_vse_request.getOperation() )
       return;

    com.itko.util.ParameterList pl = lisa_vse_request.getArguments();

    // get the argument that contains the date

    // replace 'someDate' with the exact name of the arg holding the date
    if ( pl.containsKey( "someDate" ) {

       // date format based on the date time you identified in your post 
        java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("MMddyyHHmmSS");
        df.setLenient( false );
        String dt = pl.get( "someDate" );
        try {
            df.parse( dt );
        } catch (java.text.ParseException pe ) {

            // I guess the date is invalid so add a special arg to the list

            // add lisa_dateIsInvalid in the arglist in your VSI
            pl.addParameters( "lisa_dateIsInvalid=y");
            lisa_vse_request.setArguments( pl );
        }
    }
    return;

     

    Approach 2: Scripted Assertion or JSR-223 Step

    In the VSI Image Selection (Scripted Assertion) or in a JSR-223 step following, you could check the date and manually insert the ERROR information into the lisa_vse_response payload.  I do not know the response payload's content, so I cannot offer a recommendation as to the logic required. Basically, the same check as above occurs to determine if the date is invalid.  If invalid, build some logic to update the response with the Error Message data.

     

    Approach 3: Regex Expression in VSI

    Perhaps, it is possible to construct a REGEX expression for the date argument in the VSI.  The regex would be pretty beefy to cater for valid months, days, hours and minutes, but perhaps the first specific transaction contains a comparison of only the date argument's value against the REGEX (all other arguments compare on ANYTHING).  If the REGEX does not evaluate true, this error response transaction is sent.    

     

    Approach 4: Custom DPH (Java)

    You could create a custom DPH to handle the implementation.

     

    Perhaps, there are other options as well such as checking against an XSD.  Again, the above are just ideas and may or may not achieve your requirements.



  • 3.  Re: How do I validate date format in the request arguments?

    Posted Apr 03, 2017 08:40 AM

    Approach 1 worked perfectly for me. Thanks!!!