I have a method to generate an 8 characters alpha numeric string.
log.info(
new Random().with {
(1..8).collect { (('A'..'Z')+('0'..'9')).join()[ nextInt( (('A'..'Z')+('0'..'9')).join().length() ) ] }.join()
})
I ran it as test case set up script and it returns:
Thu Sep 18 10:37:18 NZST 2014:INFO:L7S76IP1
When I include this in the test request as one of the attribute:
${new Random().with {(1..8).collect { (('A'..'Z')+('0'..'9')).join()[ nextInt( (('A'..'Z')+('0'..'9')).join().length() ) ] }.join()}}
Then it did not return the random string as I expected.
Can you please help me out?
Thanks.
You need to use the equal sign in the property expansion, like so: ${=new Random .....}
Also, you might want to look at Apache RandomStringUtils.
Related
[Answered by: Loulou BadWeed]
Hi #NickP. , did you stringify/serialize your payload before passing
it in the fetch body ? Does not seem the case from your example and I
don't think fetch does that for you. Try JSON.stringify(config) in the
body argument of the fetch function
I have a "small" problem.
I can't figure out, why nodejs uses single quotes for the string value instead of double quotes...
My Code
let config = {}
config["active"] = 1
config["domain"] = req.body.domain
config["mailboxes"] = req.body.mailboxes
config["defquota"] = req.body.defquota
config["maxquota"] = req.body.maxquota
config["quota"] = req.body.quota
config["restart_sogo"] = 10
console.log(req.body.domain)
console.log(config)
What I input:
{
"domain": "domain.tld",
"mailboxes": 10,
"defquota": 512,
"maxquota": 1024,
"quota": 10240
}
What I get:
{
active: 1,
domain: 'domain.tld',
mailboxes: 10,
defquota: 512,
maxquota: 1024,
quota: 10240,
restart_sogo: 10
}
The problem I have with this, is that the mailcow api doesn't like single quotes in the request body. :) Does anyone know, why this happens and can help me?
I tried replacing the single quotes with str.replace() or str.replaceAll(). But that didn't work. I tried using both types where I used the '' outside of the "".
I also already asked ChatGPT for help, but the results were just the replace and replaceAll...
PS: It's 4:30 AM when I wrote this. I'm not at 100% of my usual capacity. So please don't hate me for my question.
The problem came up when getting the result of a web service returning json with Greek characters in it. Actually it is the city of Mykonos. The challenge is whatever encoding or conversion I'm using it is always displayed as:ΜΎΚΟxCE?ΟΣ . But it should show: ΜΎΚΟΝΟΣ
With Powershell I was able to verify, that the web service is returning the correct characters.
I narrowed the problem down when the byte array gets converted to a String in Groovy. Below is code that reproduces the issue I have. myUTF8String holds the byte array I get from URLConnection.content.text. The UTF8 byte sequence to look at is 0xce, 0x9d. After converting this to a string and back to a byte array the byte sequence for that character is 0xce, 0x3f. The result of below code will show the difference at position 9 of the original byte array and the one from the converted string. For the below test I'm using Groovy Console 4.0.6.
Any hints on this one?
import java.nio.charset.StandardCharsets;
def myUTF8String = "ce9cce8ece9ace9fce9dce9fcea3"
def bytes = myUTF8String.decodeHex();
content = new String(bytes).getBytes()
for ( i = 0; i < content.length; i++ ) {
if ( bytes[i] != content[i] ) {
println "Different... at pos " + i
hex = Long.toUnsignedString( bytes[i], 16).toUpperCase()
print hex.substring(hex.length()-2,hex.length()) + " != "
hex = Long.toUnsignedString( content[i], 16).toUpperCase()
println hex.substring(hex.length()-2,hex.length())
}
}
Thanks a lot
Andreas
you have to specify charset name when building String from bytes otherwise default java charset will be used - and it's not necessary urf-8.
Charset.defaultCharset() - Returns the default charset of this Java virtual machine.
The same problem with String.getBytes() - use charset parameter to get correct byte sequence.
Just change the following line in your code and issue will disappear:
content = new String(bytes, "UTF-8").getBytes("UTF-8")
as an option you can set default charset for the whole JVM instance with the following command line parameter:
java -Dfile.encoding=UTF-8 <your application>
but be careful because it will affect whole JVM instance!
https://docs.oracle.com/en/java/javase/19/intl/supported-encodings.html#GUID-DC83E43D-52F6-41D9-8F16-318F3F39D54F
I have input data of type
abc 12d
uy 76d
ce 12a
with the lines being separated by \n and the values by \t.
The data comes from a shell command:
brlist = 'mycommand'.execute().text
Then I want to get this into a map:
brmap = brlist.split("\n").collectEntries {
tkns = it.tokenize("\t")
[ (tkns[0]): tkns[1] ]
}
I also tried
brmap = brlist.split("\n").collectEntries {
it.tokenize("\t").with { [ (it[0]): it[1] ] }
}
Both ways gave the same result, which is a map with a single entry:
brmap.toString()
# prints "[abc:12d]"
Why does only the first line of the input data end up being in the map?
Your code works, which means the input String brlist isn't what you say it is...
Are you sure that's what you have? Try printing brlist, and then it inside collectEntries
As an aside, this does the same thing as your code:
brlist.split('\n')*.split('\t')*.toList().collectEntries()
Or you could try (incase it's spaces not tabs, this will expect both)
brlist.split('\n')*.split(/\s+/)*.toList().collectEntries()
This code works
// I use 4 spaces as tab.
def text = 'sh abc.sh'.execute().text.replaceAll(" " * 4, "\t")
brmap = text.split("\n").collectEntries {
tkns = it.tokenize("\t")
[(tkns[0]) : tkns[1]]
}
assert[abc:"12d", uy:"76d", ce:"12a"] == brmap
abc.sh
#!/bin/sh
echo "abc 12d"
echo "uy 76d"
echo "ce 12a
Also, I think your groovy code is correct. maybe your mycommand has some problem.
Ok, thanks for the hints, it is a bug in Jenkins: https://issues.jenkins-ci.org/browse/JENKINS-26481.
And it has been mentioned here before: Groovy .each only iterates one time
I have code like this:
def updateSensor(List<String> boardIds, SensorShort sensor) {
for (String boardId : boardIds) {
println("Working on ${boardId} for ${sensor.sensorId}")
pool.submit({
println("[${Thread.currentThread().name}] Working on ${boardId} for ${sensor.sensorId}")
})
}
}
result of this code is:
Working on 400 for 11
Working on 100 for 11
Working on 101 for 11
Working on 300 for 11
[pool-4-thread-4] Working on 300 for 11
[pool-4-thread-1] Working on 300 for 11
[pool-4-thread-3] Working on 300 for 11
[pool-3-thread-1] Working on 300 for 11
but is wrong. It seams boardId object has been rewritten
You're submitting a job based on a non-final local variable, instead try:
def updateSensor(List<String> boardIds, SensorShort sensor) {
boardIds.each { String boardId ->
println("Working on ${boardId} for ${sensor.sensorId}")
pool.submit {
println("[${Thread.currentThread().name}] Working on ${boardId} for ${sensor.sensorId}")
}
}
}
The problem with the original code is that by the time the Closure is evaluated on a separate thread, the loop has finished, and the local boardId variable has the value of the last item in the list. So each job runs with the last element, rather than the element you required.
In Java, you would declare (indeed, Java would force you to declare the variable as final):
for( final String boardId : boardIds ) {
However, groovy does not have local final variables :-/
By doing it with boardIds.each, the local boardId variable inside the each closure has the value you need...
Hope that explains it?
When try this sample code:
use(groovy.time.TimeCategory) {
800.millisecond + 300.millisecond
}
in groovy web console, I get a funny result:
0.1100 seconds
Does any one know why this happens or how to fix it?
That looks like a bug, the TimeDuration contains 1100 milliseconds, but when it prints it out, it converts it wrongly to seconds.
I've added it to the Groovy JIRA as a bug EDIT It's now marked as FIXED for versions 2.0.6, 1.8.9 and 2.1.0
In the mean time, I guess you'll need to do your own converter from TimeDuration to String :-/
Edit
You could do something like this (and there is probably a neater way of doing it)
groovy.time.TimeDuration.metaClass.normalize = { ->
def newdmap = ['days','hours','minutes','seconds','millis'].collectEntries {
[ (it):delegate."$it" ]
}.with { dmap ->
[millis:1000,seconds:60,minutes:60,hours:24,days:-1].inject( [ dur:[ days:0, hours:0, minutes:0, seconds:0, millis:0 ], roll:0 ] ) { val, field ->
val.dur."$field.key" = dmap."$field.key" + val.roll
val.roll = val.dur."$field.key".intdiv( field.value )
val.dur."$field.key" = field.value < 0 ?
val.dur."$field.key" :
val.dur."$field.key" % field.value
val
}.dur
}
new TimeDuration( newdmap.days, newdmap.hours, newdmap.minutes, newdmap.seconds, newdmap.millis )
}
That adds a normalize method to TimeDuration, so then doing:
use(groovy.time.TimeCategory) {
800.millisecond + 300.millisecond
}.normalize()
Shows 1.100 seconds
I haven't done a huge amount of testing on that method, so be warned it could do with some unit tests to make sure it doesn't fall over with other situations.