Rachid G
Rachid G

Reputation: 229

Karate: how to convert object/map in string from javascript to Java?

I Have a file json named production_2.json

[
  {
     "v":{
        "id":"rep_01564526",
        "name":"tuttoverde.pgmx",
        "type":"PRODUCTION_STARTED",
        "ute":"CDL",
        "ver":"1.0",
        "status":"EXE"
     },
     "ts":"2020-11-19T08:00:00Z"
  },
  {
     "v":{
        "id":"rep_01564526",
        "name":"tuttoverde.pgmx",
        "type":"PRODUCTION_ENDED",
        "ute":"CDL",
        "ver":"1.0",
        "status":"EXE"
        
     },
     "ts":"2020-11-19T17:00:00Z"
  }
]

And have the folling Karate code, that:

  1. Read the file production_2.json
  2. and for each element of the array send a topic
I  * def sendtopics = 
     """
         function(i){
           
            var topic = "data." + machineID + ".Production";
            var payload = productions[i];
            karate.log('topic: ', topic )
            karate.log('payload: ', payload )
            return  mqtt.sendMessage(payload, topic);
            
         }
     """

    * def productions =  read('this:productions_json/production_2.json')
    * def totalProductionEvents = productions.length
    * def isTopicWasSent =  karate.repeat(totalProductionEvents, sendtopics)
    * match each isTopicWasSent == true

The function

mqtt.sendMessage(payload, topic);

is a function in java, that have the following segnature

public Boolean sendMessage(String payload, String topic) {

        System.out.println("Publishing message: ");
        System.out.println("payload " + payload);
        System.out.println("topic " + topic);
        return true;
      }

the problem is that the value of the "payload" inside the javascript function is correct and is printed correctly, while inside the "sendMessage" function the value of the payload is formatted incorrectly.

For example here is what it prints inside karate.log('payload: ', payload )

payload:  {
  "v": {
    "id": "rep_01564526",
    "name": "tuttoverde.pgmx",
    "type": "PRODUCTION_STARTED",
    "ute": "CDL",
    "ver": "1.0",
    "status": "EXE"
  },
  "ts": "2021-01-08T08:00:00Z"
}

And Here instead what is printed on the function "sendMessage" of the java class

payload {v={id=rep_01564526, name=tuttoverde.pgmx, type=PRODUCTION_STARTED, ute=CDL, ver=1.0, status=EXE,  ts=2021-01-08T08:00:00Z}

I don't understand why the payload is formatted incorrectly (= instead of : ) and is it not a string. I also tried using the following solution and it doesn't work for me

 * def sendtopics = 
     """
         function(i){
           
            var topic = "data." + machineID + ".Production";
            var payload = productions[i];
            var payload2 = JSON.stringify(payload);
            return  mqtt.sendMessage(payload2, topic);
            
         }
     """

How do I convert an object inside javascript to a string so I can pass it to java?

Upvotes: 1

Views: 3960

Answers (1)

Peter Thomas
Peter Thomas

Reputation: 58058

You are doing some really advanced stuff in Karate. I strongly suggest you start looking at the new version (close to release) and you can find details here: https://github.com/intuit/karate/wiki/1.0-upgrade-guide

The reason is because the async and Java interop has some breaking changes - and there are some new API-s you can call on the karate object in JS to do format conversions:

var temp = karate.fromString(payload);

And karate.log() should work better and not give you that odd formatting you are complaining about. With the current version you can try karate.toJson() if that gives you the conversion you expect.

Given your advanced usage, I recommend you start using the new version and provide feedback on anything that may be still needed.

Upvotes: 1

Related Questions