Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
659 views
in Technique[技术] by (71.8m points)

apache kafka - Karate: Convert string to karate native variable in javascript

Our test automation needs to interact with kafka and we are looking at how we can achieve this with karate.

We have a java class which reads from kafka and puts records in an internal list. We then ask for these records from karate, filter out all messages from background traffic, and return the first message that matches our filter.

So our consumer looks like this (simplified):

// consume.js
function(bootstrapServers, topic, filter, timeout, interval) {
  var KafkaLib = Java.type('kafka.KafkaLib')
  var records = KafkaLib.getRecords(bootstrapServers, topic)

  for (record_id in records) {
    // TODO here we want to convert record to a json (and later xml for xml records) so that
    // we can access them as 'native' karate data types and use notation like: cat.cat.scores.score[1]
    var record = records[record_id]
    if (filter(record)) {
      karate.log("Record matched: " + record)
      return record
    }
  }

  throw "No records found matching the filter: " + filter
}

Records can be json, xml, or plain text, but looking in the json case now. In this case given that in kafka there is a message like this: {"correlationId":"b3e6bbc7-e5a6-4b2a-a8f9-a0ddf435de67","text":"Hello world"} This is loaded as a string in the record variable above.

We want to convert this to json so that a filter like this would work:

* def uuid = java.util.UUID.randomUUID() + ''
# This is what we are publishing to kafka
* def payload = ({ correlationId: uuid, text: "Hello world" })
* def filter = function(m) { return m.correlationId == uuid }

Is there a way to convert a string to a native karate variable in javascript? Might have missed it looking at https://intuit.github.io/karate/#the-karate-object. By the way var jsonRecord = karate.toJson(record) did not work and jsonRecord.uuid was undefined.


Edit: I have made an example of what I am trying to achieve here: https://github.com/KostasKgr/karate-issues/blob/java_json_interop/src/test/java/examples/consumption/consumption.feature

Many thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sometime ago I had put together a something that could be used to test Kafka from within Karate. Pls see if https://github.com/Sdaas/karate-kafka helps. Happy to enhance / improve if it helps you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...