Forum Discussion

smartTest's avatar
smartTest
Contributor
13 years ago

SetUp Scripts for current and future dates

Hiya, I'm really new to Java/Groovy/SoapUI but have found these boards to be invaluable. I'm therefore asking the community for help with something I'm struggling with.....

I've created a setUp script:

testRunner.testCase.setPropertyValue("TODAY", new Date().format("yyyy-MM-dd"));

to set a property to be Today's date. My question is, how can I amend this script to allow for tomorrow's date (i.e. Today + 1), I'm just not sure where in the script to add the + 1

thanks in advance all..

6 Replies

  • WaveyDavey's avatar
    WaveyDavey
    Occasional Contributor
    I'd be really interested in this also:
    I run a REST request to update a db column to now()+30days, then a second REST query to get the row back - I'd like to set an assertion
    returned value (in xml) = date_value (to within a minute or so)

    I assume the assertion would need to be a groovy script.

    the within a minute or so is because column on db is datetime, and there's naturally some lag between updating and re-checking.

    David
  • WaveyDavey's avatar
    WaveyDavey
    Occasional Contributor
    Ok, I did this. Probably much nicer ways to do it, but ...

    //Parse table row returned
    def dbNewDbu = context.expand( '${check DBU really changed#ResponseAsXml#//root[1]/data[1]/reset[1]}' )

    dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date dbValAsDate = dateFormat.parse(dbNewDbu);

    // Set up a pair of calendar object for date manipulation
    def a = Calendar.getInstance();
    def b = Calendar.getInstance();
    // set them now + 30 days +/- 5 minutes in case of time sync differences
    a.add(Calendar.DATE,30);
    b.add(Calendar.DATE,30);
    a.add(Calendar.MINUTE,-5);
    b.add(Calendar.MINUTE,5);

    // get 2 date objects from Calendar objects, so we can do assertion with before and after methods
    Date aDate = a.getTime();
    Date bDate = b.getTime();

    s1= dateFormat.format(aDate);
    s2= dateFormat.format(dbValAsDate);
    s3= dateFormat.format(bDate);

    assert (dbValAsDate.after(aDate) && dbValAsDate.before(bDate));


    so, for smartTest's problem above, use the Calendar object to do the manipulation, and then convert to date object with Calendar.getTime()

    Hope this helps.

    David
  • Hi,
    you can put it directly in the xml. it is more readable.

    Example :

    <!-- text within dateEffectiveFrom tag is replaced with a date 1 day from today in yyyy-MM-dd format -->
    <dateStart>${= String.format('%tF', new Date() + 1) }</dateStart>

    Omar EL Mandour
  • Thanks the the updates - i've been using the following now as a way of adding times in case anyone else is interested:

    def long oneDay = (long) 1000.0 * 60 * 60 * 24;
    def long oneWeek = (long) 1000.0 * 60 * 60 * 24 * 7;
    def long twoWeek = (long) 1000.0 * 60 * 60 * 24 * 14;
    testRunner.testCase.setPropertyValue("TODAY", new Date(System.currentTimeMillis()+oneDay).format("yyyy-MM-dd"));
    testRunner.testCase.setPropertyValue("DepDateTime", new Date(System.currentTimeMillis()+twoWeek).format("yyyy-MM-dd'T'00:00:00"));

    This has been working well and allows me to use a property transfer to get a particular date added into a request message.

    Does anyone know how to handle making these specific dates? i.e. If I wanted the date to always be the next Sunday in the calendar. As I said before, really new to scripting so any help is good help!
  • Your question is irrelevant here and i will explain why .

    Grooy is compatible with java. In other words, you can add plain old java code in your groovy script.
    IN other other words, the question of the next sunday is a java question.

    According to google, this is a suitable response http://www.coderanch.com/t/411465/java/java/date-next-sunday

    For more complicated date manipulation i would strongly recommand the Joda Time libray.
    Any jars can be use if the it is present in th ext directory of soapui.

    Omar EL Mandour
  • Omar, firstly many thanks for your reply.

    However, as I mentioned, I am not a programmer and am therefore unable to code in Groovy or Java, I am simply a QA looking for ways to make tests repeatable in an agile environment. Unfortunately, the request messages I have to use are fixed and require only updates to attributes to gain the required response from an external (and very old) db.

    In case anyone else is interested later, I now have a very simple piece of code which sets a property value containing the date for the next coming Sunday:

    def nextDay = new Date()
    for (i=1;i<8;i++)
    {
    nextDay = nextDay + 1
    if (nextDay.format("E")=="Sun") break
    }
    log.info "Next Sunday is " + nextDay.toString()
    testRunner.testCase.setPropertyValue("NextSunday", nextDay.format("yyyy-MM-dd") );


    By changing the "Sun" element to another day (i.e. Mon) you can automatically grab the required date. As I am testing a booking system I needed a way of auto-updating request and booking dates as required during CI testing.

    Hope I've helped someone else with this..........