3

Is it possible to transform the following groovy request in a one-line dynamic property? That question is for using the result in a SOAPui request without using a groovy script. Because i have a lot of differents dates to put in my request, and also in my assertions.

import groovy.time.TimeCategory
use (TimeCategory) {
    date = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date() -5.month)
}

I would like to have a dynamic property that i could, off course, change. For example:

<ech:date1>${Today - 5 month}</ech:date1>
<ech:date2>${Today - 4 month}</ech:date2>
<ech:date3>${Today - 3 week}</ech:date3>

EDIT : More information

Actually, i have a dynamic property that give me today's date, minus or plus x days, that i can put in a xml request in SOAPui:

[Here, it return me today's date plus twenty days]

${=new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date()+20)}

Sample application:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ech="http://echange.service.open.bodet.com">
   <soapenv:Header/>
   <soapenv:Body>
      <ech:importRequests01>
         <ech:Requests01ToImport>
            <!--Zero or more repetitions:-->
            <ech:Request01>

               <ech:requestDate>${=new java.text.SimpleDateFormat("yyyy-MM-dd").format(new Date() +20)}</ech:requestDate>

               <!--You may enter ANY elements at this point-->
            </ech:Request01>
         </ech:Requests01ToImport>
      </ech:importRequests01>
   </soapenv:Body>
</soapenv:Envelope>

So, i would like to have a same thing, but for asking today's date minus/plus x month...etc.

Vetea
  • 169
  • 1
  • 3
  • 14

3 Answers3

1

see the exellent post for details:

import static java.util.Calendar.*

Date now = new Date()
now.clearTime()

Date nowMin5Month = now.updated( MONTH:now[ MONTH ] - 5 )
String nowMin3Week = now.updated( WEEK_OF_YEAR:now[ WEEK_OF_YEAR ] - 3 ).format( 'yyyy-MM-dd' )
injecteer
  • 20,038
  • 4
  • 45
  • 89
  • thanks for your response, but i can't put this on a test request! if i take your solution, like mine, , i have to pass through a groovy script...that i don't want to – Vetea Aug 20 '14 at 15:52
1

I don't know Soap UI, but does mixing the Category into the respective types help?

import groovy.time.TimeCategory

class TimeCategoryMixer {
    static global() {
        TimeCategory.metaClass.methods
            .findAll { it.isStatic() && !it.name.startsWith("__") && it.name != "global" }
            .each { it.nativeParameterTypes[0].mixin TimeCategory }
    }
}

TimeCategoryMixer.global()

def d = Date.parse('yyyy-MM-dd', '2000-01-01')

assert d - 5.month == Date.parse('yyyy-MM-dd', '1999-08-01')

Update

As per your update, what about this:

b="${use(groovy.time.TimeCategory) { Date.parse('yyyy-MM-dd', '2000-01-01') - 5.month} }"

assert b == Date.parse('yyyy-MM-dd', '1999-08-01').toString()

I think SoapUI will be happy with something like this:

<ech:requestDate>${=use(groovy.time.TimeCategory) { Date.parse('yyyy-MM-dd', '2000-01-01') - 5.month}}</ech:requestDate>
Community
  • 1
  • 1
Will
  • 14,348
  • 1
  • 42
  • 44
  • Hi, i add more details to my original question. Maybe i described a bad problem... – Vetea Aug 21 '14 at 06:44
  • it doesn't works on Soapui xml Request. I don't know if it's possible to insert "groovy.time.TimeCategory". But if it works, the other problem is that the date will not be time-varying, 'cause the date is written on the variable...I need to have a variable that take today's date (plus or minus x month), because i will pass this tests several times on a year. And i don't want this to be referenced to a date. Tell me if it's not clear. – Vetea Aug 21 '14 at 16:00
  • Can you add a method to the Date object, using metaprogramming, which abstracts that logic and call it on soapui? – Will Aug 21 '14 at 16:30
0

I find this, that give me for example "2014-7-10":

usable from groovy :

(Calendar.getInstance().get(Calendar.YEAR) - 0) +"-"+(Calendar.getInstance().get(Calendar.MONTH) +1)+"-"+(Calendar.getInstance().get(Calendar.DATE) - 0)

Usable from XML, and also from SoapUI test request :

${= (Calendar.getInstance().get(Calendar.YEAR) - 0) +"-"+(Calendar.getInstance().get(Calendar.MONTH) +1)+"-"+(Calendar.getInstance().get(Calendar.DATE) - 0)}

With this, i can specify "MONTH -5" or "MONTH +2". As i use the same for the YEAR or the DAY, i can do the same for them.

WARN : Don't forget that in gregorian calendar, the first month, january, is equal to 0. That's why i put "...get(Calendar.MONTH) +1..."

Vetea
  • 169
  • 1
  • 3
  • 14