Ask a Question

Random Date Project Property

SOLVED
jonrey
Occasional Contributor

Random Date Project Property

Hello all!

 

I'm hoping that somebody can help me with this request - I'm a complete novice at this type of coding.  I need to create a Project Property variable to randomize a date in my Soap Request with the following parameters:

  • Year is 2014 or 2015 only
  • Any month 01-12 (has to have the leading 0 if single digit)
  • Day is always 01
  • Format example: YYYY-MM-DD (2014-06-01)

So far - without even toggling between year of 2014 and 2015 - I have a value of ${=String.valueOf('2014-'&'(Math.Random(01-12))'&'-01')}, but get error:

 

The string 'No signature of method: java.lang.String.and() is applicable for argument types: (java.lang.String) values: [(Math.Random(01-12))]
Possible solutions: any(), find(java.lang.String), any(groovy.lang.Closure), find(), find(groovy.lang.Closure), find(java.lang.String, groovy.lang.Closure)' is not a valid AllXsd value

 

Another way to accomplish this could be using an array, but I'm not sure how to set that up in a Property one-liner either.

 

Thank you.  Any help or guidance would be greatly appreciated!!

16 REPLIES 16
rupert_anderson
Valued Contributor

Hi,

 

Well, there are probably a few ways to do this, here is one for your to try:

 

//To get either 2014 or 2015, I started with 2014 and added either 0 or 1 (randomly generated) to it.

def randomYear = new Random().nextInt(2)+2014

 

//To get a 0 padded month between 01 and 12, I generate an integer between 1 and 12.

def randomMonth = String.valueOf(new Random().nextInt(12)+1)

 

//Then finally combine the parts, whilst padding the month (as a String) to 2 digits. 
log.info randomYear + '-' + randomMonth.padLeft( 2, '0' ) + '-01'

 

To get this in a one liner:

 

log.info new Random().nextInt(2)+2014 + '-' + String.valueOf(new Random().nextInt(12)+1).padLeft( 2, '0' ) + '-01'

 

prints

  • Thu May 21 19:57:40 UTC 2015:INFO:2014-06-01

or as a property expansion like you had:

 

${=new Random().nextInt(2)+2014 + '-' + String.valueOf(new Random().nextInt(12)+1).padLeft( 2, '0' ) + '-01'}

 

Does this do what you want?

 

Cheers,

Rupert

 

Author of SoapUI Cookbook
jonrey
Occasional Contributor

Hello Rupert,

 

Thank you very much for your well thought-out and impressive reply!

 

This is SO close!  The property expansion returns 2014-02-01T00:00:00 and I'm not sure where the extra "T00:00:00" part is coming from.  Certainly gets me much farther than I was - and helps me learn the syntax - so I'm going to play around with it some more to see if I can get it the last 5% 🙂

 

Cheers!

Hi,

You're welcome, happy to help! 🙂

I'm not sure, but think the 00:00:00 is the HH:MM:SS part of the XML DateTime datatype e.g.

 

<startdate>2002-05-30T09:00:00</startdate>

see http://www.w3schools.com/schema/schema_dtypes_date.asp

 

Are you using a SOAP or XML request with that data type? If so, it will probably force your date to include the time portion.

 

Cheers,

Rup

Author of SoapUI Cookbook

PS If you're happy with the solution, please can you mark it as accepted if you get a chance so that others might follow it?

Cheers,
Rup

Author of SoapUI Cookbook
jonrey
Occasional Contributor

Haha, I was actually coming back to the board to suggest this very thing.  I'm using a SOAP request and it certainly looks like XML DateTime.  So now I just need to figure out how to tell it to stop being smart and use it as a string.  

 

I will gladly mark your previous response as the answer, as it is definitely correct (and informative!) for the request.  

Cool, thanks for accepting it! 🙂

One option to stop the datetime casting would be to use xs:string instead of the xs:datetime, assuming thats acceptable to your interface.

Cheers,

Rup

Author of SoapUI Cookbook
jonrey
Occasional Contributor

Argh, I figured out the problem....  it is because only dates between 6/1/2014 and 12/1/2015 are valid >.<

 

Now I have the syntax though, I will try to figure out  how to change the start date!

Ah,ok.

Might be a good excercise anyway, to make sure I have explained how the random integers can be used well enough...

Let me know if you need any help, otherwise good luck with it!

Cheers,

Rup

Author of SoapUI Cookbook
jonrey
Occasional Contributor

Thanks for all the help, Rupert!

 

Just trying to wrap my head around the logic, really.  I can't just change the (12)+1 to (6)+1 because then it won't do 1-5 for 2015.

 

I wonder..  is there a method to set up an array and randomly return from that?

 

Could just set up the 18 dates in the array that way...

cancel
Showing results for 
Search instead for 
Did you mean: