Roman L.
Roman L.

Reputation: 79

Karate: strange issue with parsing variables in json

I found this issue today after update of IDE:

Scenario: test scenario
    * def amqpHost = '10.10.10.10'
    * def amqpPort = 5555
    * def amqpUser = 'tesetUser'
    * def amqpPass = 'testPass'
    * def amqpExchange = 'test.rabbitmq.exchange'
    * def amqpQueueName = 'test.rabbitmq.queue'
    * def amqpRoute = 'test.rabbitmq.route'
    * def amqpVirtualHost = '/'

    * print "PORT: " + amqpPort
    * print "PORT: " + amqpUser
    * print "PORT: " + amqpPass
    * print "PORT: " + amqpExchange
    * print "PORT: " + amqpQueueName

    * def inputParams = { fleetId: '#(fleetId)', host: '#(amqpHost)', port:'#(amqpPort)', user:'#(amqpUser)', password: '#(amqpPassword)', exchange: '#(amqpExchange)', route: '#(amqpRoute)', virtual_host: '#(amqpVirtualHost)' }
    * print "INPUT: " + inputParams

Output is:

[print] PORT: 5555
[print] PORT: tesetUser
[print] PORT: testPass
[print] PORT: test.rabbitmq.exchange
[print] PORT: test.rabbitmq.queue
[print] INPUT: {fleetId=#(fleetId), host=10.10.10.10, port=5555, user=tesetUser, password= '#(amqpPassword)', exchange= '#(amqpExchange)', route= '#(amqpRoute)', virtual_host= '#(amqpVirtualHost)'}

amqpPassword and rest of variables have not parsed correctly. But when I remove all spaces ("host: '#(amqpHst)'" > "host:'#(amqpHst)'") in json:

* def inputParams = { fleetId:'#(fleetId)', host:'#(amqpHost)', port:'#(amqpPort)', user:'#(amqpUser)', password:'#(amqpPassword)', exchange:'#(amqpExchange)', route:'#(amqpRoute)', virtual_host:'#(amqpVirtualHost)' }

I got the result:

{fleetId=#(fleetId), host=10.10.10.10, port=5555, user=tesetUser, password=#(amqpPassword), exchange=test.rabbitmq.exchange, route=test.rabbitmq.route, virtual_host=/}

Looks like this issue appears after my update of IntelliJ Idea or one of the plugins today. I use: Idea 2018.3.5 CE (build #IC-183.591221) Cucumber for Java v183.5429.1

Upvotes: 1

Views: 75

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58088

First - do note that there is a very convenient way to use the print keyword which pretty-prints JSON if applicable, see below:

* print "PORT:", amqpPort
* print "PORT:", amqpUser
* print "PORT:", amqpPass
* print "PORT:", amqpExchange
* print "PORT:", amqpQueueName

* def inputParams = { fleetId: '#(fleetId)', host: '#(amqpHost)', port:'#(amqpPort)', user:'#(amqpUser)', password: '#(amqpPass)', exchange: '#(amqpExchange)', route: '#(amqpRoute)', virtual_host: '#(amqpVirtualHost)' }
* print "INPUT:", inputParams

And I get:

[print] INPUT: {
  "fleetId": "#(fleetId)",
  "host": "10.10.10.10",
  "port": 5555,
  "user": "tesetUser",
  "password": "testPass",
  "exchange": "test.rabbitmq.exchange",
  "route": "test.rabbitmq.route",
  "virtual_host": "/"
}

So it looks fine to me. A suggestion, why not define the entire inputParams as a JSON itself ? You can even read this from a file. And note how you can use the foo.bar dot-notation:

* def inputParams = { fleetId: 'foo', host: '10.10.10.10', port: 5555, user: 'tesetUser', password: 'testPass', exchange: 'test.rabbitmq.exchange', route: 'test.rabbitmq.route', virtual_host: '/' }

* print "port:", inputParams.port
* print "user:", inputParams.user
* print "pass:", inputParams.password
* print "exch:", inputParams.exchange

* print "INPUT:", inputParams

I don't think this is related to the IDE at all - if you still see issues, please follow this process: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Upvotes: 1

Related Questions