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

Tokens in csvSource parameter sources computed at usage #659

Open
Tobolov opened this issue Apr 9, 2024 · 6 comments
Open

Tokens in csvSource parameter sources computed at usage #659

Tobolov opened this issue Apr 9, 2024 · 6 comments
Assignees
Labels
feature-request New feature which can be introduced good first issue

Comments

@Tobolov
Copy link

Tobolov commented Apr 9, 2024

Solution to Implement:

YAML View of Scenario:

---
scenarioName: Demonstrate bug
parameterized:
  csvSource:
  - "|firstRandom|, |secondRandom|, |myName|"
  - "${RANDOM.NUMBER}, ${RANDOM.NUMBER}, Foo"

Usage:

steps:
- name: create_app
  url: kafka-topic:APP
  operation: produce
  request:
    recordType: JSON
    records:
    - key: "${PARAM.firstRandom}"
      value:
        appId: "${PARAM.firstRandom}", <------- instead of ${0}, 
        // and retain`${0}` working as usual for not to break existing tests (keep backward compatible)
        userId: "${PARAM.secondRandom}", <------- instead of ${1}
        userName: "${PARAM.myName}" <------- instead of ${2}

When it runs, it should replace the random values like below:

---
scenarioName: Demonstrate bug
parameterized:
  csvSource:
  - "|firstRandom|, |secondRandom|"
  - 20240506, 20230507, Foo
steps:
- name: create_app
  url: kafka-topic:APP
  operation: produce
  request:
    recordType: JSON
    records:
    - key: '20240506' <------ from the csvSource
      value:
        appId: '20240506' <------ from the csvSource
        userId: '20230507' <------ from the csvSource
        userName: 'Foo' <------ from csvSource
  verify:
    status: Ok

JSON View of Scenario:

{
  "scenarioName": "Demonstrate bug",
  "parameterized": {
    "csvSource": [
      "|firstRandom|, |secondRandom|, |myName|",
      "${RANDOM.NUMBER}, ${RANDOM.NUMBER}, Foo"
    ]
  }

Usage:

  "steps": [
    {
      "name": "create_app",
      "url": "kafka-topic:APP",
      "operation": "produce",
      "request": {
        "recordType": "JSON",
        "records": [
          {
            "key": "${PARAM.firstRandom}",
            "value": {
              "appId": "${PARAM.firstRandom}", <------- instead of ${0}
              "userId": "${PARAM.secondRandom}" <------- instead of ${1}
              "userName": "${PARAM.myName}" <------- instead of ${2}
            }
          }
        ]
      },
      "verify": {
        "status": "Ok"
      }
    }

When it runs, it should replace with the random values like below:

{
  "scenarioName": "Demonstrate bug",
  "parameterized": {
    "csvSource": [
      "|firstRandom|, |secondRandom|",
      "20240506, 20230507, Foo"
    ]
  },
  "steps": [
    {
      "name": "create_app",
      "url": "kafka-topic:APP",
      "operation": "produce",
      "request": {
        "recordType": "JSON",
        "records": [
          {
            "key": "20240506",
            "value": {
              "appId": "20240506",
              "userId": "20230507",
              "userName": "Foo",
            }
          }
        ]
      },
      "verify": {
        "status": "Ok"
      }
    },

---------------- End of Solution -----------------


************ ISSUE DESCRIPTION ************
Goal:
I want to generate a random ID and use it throughout a test scenario, without bloating the scenario file (lookups such as $.create_app.request.records[0].key too bloaty).

How I want to achieve this:
Generate random id's in a csvSource and reference them throughout the scenario.
Bonus points if the values in the csvSource can be named and referenced in a manner like ${PARAM:APP_ID} or even #.APP_ID.

Issue:
Tokens such as ${RANDOM.NUMBER} are only computed at usage in parameter sources, as opposed to when the scenario starts.

Scenario to replicate:

---
scenarioName: Demonstrate bug
parameterized:
  csvSource:
    #  {0}: App ID                    {1}: User ID
    - "${RANDOM.NUMBER}, ${RANDOM.NUMBER}"
steps:
  - name: create_app
    url: "kafka-topic:APP"
    operation: produce
    request:
      recordType: JSON
      records:
        - key: ${0}
          value:
            appId: ${0}
            userId: ${1}
    verify:
      status: Ok
  - name: verify_app
    url: "kafka-topic:APP"
    operation: consume
    retry:
      max: 3
      delay: 1000 #ms
    request:
      consumerLocalConfigs:
        recordType: JSON
        commitSync: true
        showRecordsConsumed: true
        maxNoOfRetryPollsOrTimeouts: 3
    verify:
      size: 1
      records:
        - key: ${0}
          value:
            appId: ${0}
            userId: ${1}

Exception:

java.lang.RuntimeException: Assertion failed for :- 

[Demonstrate bug] 
	|
	|
	+---Step --> [verify_app] 

Failures:
--------- 
Assertion jsonPath '$.records[0].value.userId' with actual value '2425071383995341726' did not match the expected value '5053246237145541956'
----------------------------------------------------------------------------------------------------------------------------------
Assertion jsonPath '$.records[0].value.appId' with actual value '3469817706342973769' did not match the expected value '2758064321223210465'
----------------------------------------------------------------------------------------------------------------------------------
Assertion jsonPath '$.records[0].key' with actual value '4194790416790024356' did not match the expected value '3658409644815306301'


	at org.jsmart.zerocode.core.runner.StepNotificationHandler.handleAssertionFailed(StepNotificationHandler.java:37)
	at org.jsmart.zerocode.core.runner.StepNotificationHandler.handleAssertion(StepNotificationHandler.java:71)
	at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.executeRetry(ZeroCodeMultiStepsScenarioRunnerImpl.java:316)
	at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.executeRetryWithSteps(ZeroCodeMultiStepsScenarioRunnerImpl.java:191)
	at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.executeSteps(ZeroCodeMultiStepsScenarioRunnerImpl.java:173)
	at org.jsmart.zerocode.core.runner.ZeroCodeMultiStepsScenarioRunnerImpl.runScenario(ZeroCodeMultiStepsScenarioRunnerImpl.java:136)
	at org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner.runLeafJsonTest(ZeroCodeUnitRunner.java:223)
	at org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner.runChild(ZeroCodeUnitRunner.java:127)
	at org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner.runChild(ZeroCodeUnitRunner.java:51)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.jsmart.zerocode.core.runner.ZeroCodeUnitRunner.run(ZeroCodeUnitRunner.java:107)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
	at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
@authorjapps authorjapps added good first issue feature-request New feature which can be introduced labels Apr 12, 2024
@luke-zhou
Copy link

Hi, just wondering if this issue is still open. If so, can I work on this issue?

@authorjapps
Copy link
Owner

Hi, just wondering if this issue is still open. If so, can I work on this issue?

@luke-zhou , It's open. You can start working please.
Thanks

@luke-zhou
Copy link

cool, that is good. just want to confirm my idea here.
I am thinking to add something like "#name-tag" to the end of the following 5 type of tokens:

	RANDOM.UUID
    RANDOM.UUID.FIXED
    RANDOM.NUMBER
    RANDOM.NUMBER.FIXED
    RANDOM.STRING

so when the user need to use the same value as the previous random value, he can just quote the name-tag.
For example:

---
scenarioName: Demonstrate bug
parameterized:
  csvSource:
    #  {0}: App ID                    {1}: User ID
    - "${RANDOM.NUMBER#0}, ${RANDOM.NUMBER#first}"
steps:
  - name: create_app
    url: "kafka-topic:APP"
    operation: produce
    request:
      recordType: JSON
      records:
        - key: ${RANDOM.NUMBER#0}
          value:
            appId: ${RANDOM.NUMBER#0}
            userId: ${RANDOM.NUMBER#first}
    verify:
      status: Ok
  - name: verify_app
    url: "kafka-topic:APP"
    operation: consume
    retry:
      max: 3
      delay: 1000 #ms
    request:
      consumerLocalConfigs:
        recordType: JSON
        commitSync: true
        showRecordsConsumed: true
        maxNoOfRetryPollsOrTimeouts: 3
    verify:
      size: 1
      records:
        - key: ${RANDOM.NUMBER#0}
          value:
            appId: ${RANDOM.NUMBER#0}
            userId: ${RANDOM.NUMBER#first}

@authorjapps
Copy link
Owner

authorjapps commented Jun 3, 2024

cool, that is good. just want to confirm my idea here. I am thinking to add something like "#name-tag" to the end of the following 5 type of tokens:

Hello @luke-zhou , really appreciate you have been trying to solutionize it. Good to see your ideas, prefix "#first" etc might create confusion imo.

Can you have a look at the Issue description section "Solution to Implement:", which could be simpler ?
#659 (comment)

Basically the 1st line has got words (without spaces) wrapped by "|", will be treated as a headers.

  • "|firstRandom|, |secondRandom|, |myName|"

Everytime a scenario runs it should pick the value under each header (e.g "${PARAM.firstRandom}" <------- instead of ${0} which is the current behavior).

Also it will keep the scenario clean and readable.

Can you give it a try to implement it?

@nirmalchandra
Copy link
Collaborator

appId: "${PARAM.firstRandom}", <------- instead of ${0}

Also, keep ${0} working as usual for not to break existing tests (keep backward compatible I mean)

@luke-zhou
Copy link

ok, no problem, will give a shot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature which can be introduced good first issue
Projects
None yet
Development

No branches or pull requests

4 participants