Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number token placeholder limit by length #245

Closed
dinesh76in opened this issue May 31, 2019 · 8 comments
Closed

Number token placeholder limit by length #245

dinesh76in opened this issue May 31, 2019 · 8 comments
Assignees

Comments

@dinesh76in
Copy link
Collaborator

dinesh76in commented May 31, 2019

Hello Folks,
I am looking for a number token place holder which can resolve to a random number , but limited by length/size.
e.g.

${RANDOM.NUMBER:3} - resolves to 101, 543, 999 etc

${RANDOM.NUMBER:2} - resolves to 11, 34, 98 etc

${RANDOM.NUMBER:1} - resolves to 3, 9, 2 etc

This will help me to create some custom ID etc where the number is just a suffix or prefix.

e.g.

CUST_ID_${RANDOM.NUMBER:2}
  - - resolves to CUST_ID_11, CUST_ID_34, CUST_ID_98 etc

CUST_ID_${RANDOM.NUMBER:1}
  - - resolves to CUST_ID_1, CUST_ID_5, CUST_ID_9 etc

${RANDOM.NUMBER:3}_MIB
  - - resolves to 101_MIB, 100_MIB, 339_MIB etc

etc

The uniqueness need not be guaranteed across multiple session, but yes, (guaranteed) for the same JVM session

Update(21-Jun-2019, Friday):

  • If the random numbers run out, the tests should not break. Instead it's ok to have a repeated one.

In this case, my team(basically testers)'s responsibility is to make sure they choose a good length, rather than just 1 digit.

  • In a scenario having n number of steps, if this place holder is used n times, once in each step, then all n places will be replaced by different random numbers(not the same one n times).

    • Here it's the tester's(my team) job to make sure to use correct length so that the they don't run out of the random numbers.
  • Our current test usecase is - we have a 2 digit random number requirements to generate people's identity with a Text + Number pattern, where we will randomize both text and number

@jneate
Copy link
Collaborator

jneate commented Jun 18, 2019

@authorjapps I've picked this up

I've created a HashMap to store all the numbers generated for a specific length in the current session.

The only downside to the approach is that the user can't generate different IDs in the same step in a scenario but I don't see that as an issue from my side as the user could create another step to use a new random number.

Main chunk of the code is below:
image

I've tested the implementation using the `HelloWorldGitHubSuite" test and I've confirmed that a unique number is generated across all of the steps/scenarios.

My only outstanding Q is what would be best to return if the user has ran out of unique numbers, i.e. ${RANDOM.NUMBER:1} and they have executed more than 9 tests then the next number would be 10 which is two characters long, I'm presuming it'd be easier to throw an exception or would you prefer a warning to be thrown? Maybe this is a Q for the OP @dinesh76in

Thanks

@jneate jneate self-assigned this Jun 18, 2019
@santhoshTpixler
Copy link
Collaborator

If random number generation of a fixed length is the requirement here then something like this can be done.

public static void main(String[] args) {
   
    int length = 3;
    int lowerBound = (int) Math.pow(10, (length -1));
    int upperBound = (int) Math.pow(10, length);
    System.out.println("lower - > " + lowerBound );
    System.out.println("upper - > " + upperBound );
    int random = ThreadLocalRandom.current().nextInt(lowerBound, upperBound);
    System.out.println("random --> " + random ); 
  }

Why we need a hashmap? Is there anything I am missing from the requirement? @dinesh76in @jneate

@dinesh76in
Copy link
Collaborator Author

@jneate, @santhoshTpixler , Thanks!
I have updated the issue description a bit which would answer your below question!

if the user has ran out of unique numbers...

@authorjapps
Copy link
Owner

authorjapps commented Jun 22, 2019

@jneate ,

...downside to the approach is that the user can't generate different IDs in the same step in a scenario but I don't see that as an issue from my side...

That's the default behavior. That won't cause any issues(we are giving an extra handy stuff as part of this issue), e.g.
In case of a Person object, the id 111( random number e.g. only) belongs to two different entities which should not be an issue in my opinion.

{
   "id": "111",
   "address":{
      "id": "111"
   }
}

Even in case, a test-engineers want to make both random, he can simply execute a Java method anyways!
Here the ids can be one or as many in numbers.

{
   "id": "${$.java_step.response.PERSON_ID}",
   "address":{
      "id": "${$.java_step.response.ADDRESS_ID}"
   }
}

resolved as below(so we are good here I think),

{
   "id": "321",
   "address":{
      "id": "757"
   }
}

And to keep the solution simple, it shd be good enough to use Java/Jdk provided random method here(imho).

@jneate
Copy link
Collaborator

jneate commented Jun 27, 2019

Sorry about the delay, my work was pretty hectic.

@authorjapps @dinesh76in - Myself and @santhoshTpixler took the conversation offline in Gitter over the course of a few days, as @santhoshTpixler mentioned, the Random method doesn't guarantee uniqueness therefore if the generated number has to be unique then we need to keep track of the numbers previously generated hence the HashMap.

Admittedly the chances are slim but in theory it could happen where the Java random method generates the same number twice, I'm more than happy to change the above implementation to use the Random method then we just need to be clear in the documentation that the RANDOM.NUMBER:LENGTH has a 1 in 10^LENGTH chance of generating the same number twice.

We should also include the max LENGTH in the documents, using the long type gives a length of 18 which should be sufficient unless others disagree?

@dinesh76in - Thanks for the update, it helped clarify a few things.

@santhoshTpixler
Copy link
Collaborator

@jneate you can raise a pull request with the discussed changes.

@authorjapps
Copy link
Owner

authorjapps commented Jul 16, 2019

Thank you both @jneate , @santhoshTpixler for the solution around this.

@jneate , for the below issue, we will raise a Tech-Debt(you can pick it when you get chance)-

The only downside to the approach is that the user can't generate different IDs in the same step in a scenario but I don't see that as an issue from my side as the user could create another step to use a new random number.

@santhoshTpixler, can you cover PR with unit tests, please?

And a Wiki Page around this will help everyone as @jneate suggested!

I'm more than happy to change the above implementation to use the Random method then we just need to be clear in the documentation that the RANDOM.NUMBER:LENGTH has a 1 in 10^LENGTH chance of generating the same number twice.

Wiki(TODO) link is here:
https://github.com/authorjapps/zerocode/wiki/Random-Number-Token-Generator-Usage-and-Limits

Already added an example into the http module. 👍

@authorjapps
Copy link
Owner

authorjapps commented Jul 27, 2019

Available in version 1.3.11 and higher.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants