Converting Date to String in groovy - groovy

I want to convert def date= new Date() to string so that i could make this expression def text=data+" "+"http://hjghjghj.ge(Service) /https:jsonparces getting data from taxservice:Successfully received response and then use text as an string i have tries toString() but it wasn't helpful any better ideas?

You don't even need toString(), just using it in a String concatenation transforms it, but you have to use String + Date, not Date + String, just like in Java. So either use
def text=""+date+" "+"http://hjghjghj.ge(Service) /https:jsonparces getting data from taxservice:Successfully received response
or
def text=(data as String)+" "+"http://hjghjghj.ge(Service) /https:jsonparces getting data from taxservice:Successfully received response
or
def text=data.toString()+" "+"http://hjghjghj.ge(Service) /https:jsonparces getting data from taxservice:Successfully received response`

Related

how to correlate using groovy code in jmeter?

1) In my response body comes like json format.
2) Some expected reason ,i have changed that body json to normal text using below code and working expected way
import groovy.json.*
String js = vars.get("cAccountDetails")
def data = new JsonSlurper().parseText(js)
log.info("the value is "+ data)
vars.putObject('data', data)
3) This code meaning converted json to normal text and stored in some variable thats "data"
4) so my response stored in "data" variable .
5) From "data", how can i extract **specific data** using groovy code or some other code?
import java.util.regex.*
import java.util.regex.Matcher
import java.util.regex.Pattern
def matches = (data =~ '{accountDetails=\\[(.*)\\],')
vars.putObject('matches', matches)
The above code using for correlation purpose {"matches" VARIABLE will store extracted value}
but above code is not working ,how can i fix this issue ?
Thanks in advance!!
We cannot help you unless you share your cAccountDetails variable value and indicate what do you need to extract from it.
From the first glance you regular expression should look a little bit different, i.e.
def matches = (data =~ /accountDetails=[(.*)],/)
More information:
Apache Groovy - Find Operator
Apache Groovy - Why and How You Should Use It

Execute Groovy Script to transform date in Nifi

I'm trying to transform my JSON dates using Nifi. They are imported in this format:
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
def ff = session.get()
if(!ff)return
ff = session.write(ff, {rawIn, rawOut->
// transform streams into reader and writer
rawIn.withReader("UTF-8"){reader->
rawOut.withWriter("UTF-8"){writer->
//parse reader into Map
def json = new JsonSlurper().parse(reader)
// set my variable and define what format it is in
json.date = new Date(json.date as Long).format('HH:mm yyyy-MM-dd')
// Reformat it
json.date = DateFormat.parse("yyyy-MM-dd HH:mm", json.date)
//write changed object to writer
new JsonBuilder(json).writeTo(writer)
}
}
} as StreamCallback)
session.transfer(ff, REL_SUCCESS)
The incoming flowfile has this body:
[{"date":"09:00 2019-05-29","data":460.0,"name":"login"},{"date":"10:00 2019-05-29","data":548.0,"name":"login"},{"date":"11:00 2019-05-14","data":0.0,"name":"login"},{"date":"00:00 2019-06-15","data":0.0,"name":"login"}]
I want this output:
[{"date":"2019-05-29 09:00","data":460.0,"name":"login"},{"date":"2019-05-29 10:00","data":548.0,"name":"login"},{"date":"2019-05-14 11:00","data":0.0,"name":"login"},{"date":"2019-06-15 00:00","data":0.0,"name":"login"}]
The error I get is this:
Can anyone please help me understand where I am going wrong?
The input is a list of the objects in question. The incoming date is a
String -- not a Long.
So the first error is to use json.date as it implies json*.date
(which gives a list of all date).
Next casting the date to Long, create a new Date and then format it is
the wrong way around.
So to change the format of all the date something like this is needed:
json.each{
it.date = Date.parse('HH:mm yyyy-MM-dd', it.date).format('yyyy-MM-dd HH:mm')
}

SoapUI to get attachment from response in Groovy

I tried to use following code to get attachment from reponse as text in Groovy.
def testStep = testRunner.testCase.getTestStepByName("getData")
def response = testStep.testRequest.response
def ins = response.attachments[0].inputStream
log.info(ins);
It contains some binary information too, so it is not fully human readable, but got following in output:
java.io.ByteArrayInputStream#5eca74
It is easy to simply encode it to base64 and store it as a property value.
def ins = response.attachments[0].inputStream
String encoded = ins.bytes.encodeBase64().toString()

Convert binary response to ASCII string

I use SoapUI to send a request to webservice and get response.
How could I convert this binary response convert to ASCII string with Groovy script?
What binary form takes your response? Is it byte[]? If so you can easily convert it to String with following constructor:
new String(byte[] bytes, Charset charset)
An example you can try in groovyConsole:
import java.nio.charset.Charset
byte[] response = "Some response".bytes
assert new String(response, Charset.forName("UTF-8")) == "Some response"

Setting fields on a new HL7 Message wrapped in Terser

When attempting to use ca.uhn.hl7v2.util.Terser to set empty fields on a given specific subclass of ca.uhn.hl7v2.model.Message (in this case ca.uhn.hl7v2.model.v251.message.ORU_R01), I receive no error messages during the .each{} closure and afterwards the message object has no fields populated.
hl7Map is populated on class instantiation, with values like:
def hl7Map= [ "HL7MessageFields":['PID-3-1':"internal order map key",
'PID-3-4':"internal order map key",etc.]]
Code below:
def buildHL7Message(order){
def date = new Date()
def format = new SimpleDateFormat(hl7Map["dateFormat"]).format(date)
//Set date on the Message Header Object
hl7Map["MSH"]["-7"]= format
def message = (context.getModelClassFactory().getMessageClass(hl7Map["MessageInstantiationMap"]["messageType"],
hl7Map["MessageInstantiationMap"]["version"],
true) as Class).newInstance()
Terser terser = new Terser(message)
hl7Map["HL7MessageFields"].each{
terser.set(it.key, order[it.value])
}
println message
return message
}
The end of method results in no output and error logged about encoding, MSH-1 is a required field, pipe delminator but is empty. If the code above uses message.initQuickstart("ORU", "R01", "T"), only the default initQuickstart fields are populated.
If hl7Map["HL7MessageFields"] contains a 'it.key' that is not a valid Group/Segment field, an error is logged by terser that it failed to find the value, the above code with a properly formatted map does not cause an error.
Can anyone help explain why I am not receiving errors yet my message is empty, and help me to populate the message with the appropriate terser.set(params)?
Found solution that worked for me after a couple of hours of searching.
The message object's internal representation has a tree like structure where the MSH Segment is the parent, and the segments located after the MSH segment are child segments. Because of this structuring, MSH fields must be set as my original code does, but child segment fields must be set with "/." prepended to the map key devised (i.e. "PID-3-1" must become "/.PID-3-1" in the terser.set() line.
The hl7Map format was updated to better support this terser.set() syntactical requirement.
From the terser documentation, the / indicates search should start at the root of the message, and from an answer on a HAPI mail list link I've now lost, the . indicates search should include child elements of the MSH.
Full code below:
def buildHL7Message(order){
def date = new Date()
def format = new SimpleDateFormat(hl7Map["dateFormat"]).format(date)
//Set date on the Message Header Object
hl7Map["MSH"]["-7"]= format
//See http://stackoverflow.com/questions/576955/groovy-way-to-dynamically-invoke-a-static-method
//And
//http://stackoverflow.com/questions/7758398/groovy-way-to-dynamically-instantiate-a-class-from-string
def message = (context.getModelClassFactory().getMessageClass(hl7Map["MessageInstantiationMap"]["messageType"],
hl7Map["MessageInstantiationMap"]["version"],
true) as Class).newInstance()
Terser terser = new Terser(message)
hl7Map["MSH"].each{
terser.set("MSH"+it.key, it.value)
}
hl7Map["HL7MSHChildSegmentMap"].each{
terser.set(("/."+it.key) as String, order[it.value] as String)
}
println message
return message
}

Resources