Launching multiple browser url in groovy script - groovy

In jenkins pipeline,
I was launch multiple URL in side groovy script, like as below
stages {
stage("Launch URL") {
steps {
script {
def url1 = "https://www.paypal.com/us/home".toURL().getText()
def url2 = "https://www.ebay.com".toURL().getText()
def url3 = "https://www.yahoo.com/".toURL().getText()
}
}
}
}
Is there a better way to do this one.
Is it possible to use one variable and execute all three urls?

In the inner part of your code, you can do the following in groovy:
def texts = ["https://www.paypal.com/us/home",
"https://www.ebay.com",
"https://www.yahoo.com/".collect {
it.toURL().text
}
where texts will be a List<String> containing the string content returned from each url respectively.

Related

Convert WebService Response into Json Arrary and Jsobobject using Groovy

I am testing RESTful webservice using SoapUI. We use Groovy for that.
I am using jsonslurper to parse the response as Object type.
Our reponse is similar to this:
{
"language":[
{
"result":"PASS",
"name":"ENGLISH",
"fromAndToDate":null
},
{
"result":"FAIL",
"name":"MATHS",
"fromAndToDate": {
"from":"02/09/2016",
"end":"02/09/2016"
}
},
{
"result":"PASS",
"name":"PHYSICS",
"fromAndToDate":null
}
]
}
After this, I stuck up on how to.
Get Array (because this is array (starts with -language)
How to get value from this each array cell by passing the key (I should get the value of result key, if name='MATHS' only.)
I could do it using Java, but as just now learning Groovy I could not understand this. We have different keys with same names.
You can just parse it in to a map, then use standard groovy functions:
def response = '''{
"language":[
{"result":"PASS","name":"ENGLISH","fromAndToDate":null},
{"result":"FAIL","name":"MATHS","fromAndToDate":{"from":"02/09/2016","end":"02/09/2016"}},
{"result":"PASS","name":"PHYSICS","fromAndToDate":null}
]
}'''
import groovy.json.*
// Parse the Json string
def parsed = new JsonSlurper().parseText(response)
// Get the value of "languages" (the list of results)
def listOfCourses = parsed.language
// For this list of results, find the one where name equals 'MATHS'
def maths = listOfCourses.find { it.name == 'MATHS' }

How can I retrieve the build parameters from a queued job?

I would like to write a system groovy script which inspects the queued jobs in Jenkins, and extracts the build parameters (and build cause as a bonus) supplied as the job was scheduled. Ideas?
Specifically:
def q = Jenkins.instance.queue
q.items.each { println it.task.name }
retrieves the queued items. I can't for the life of me figure out where the build parameters live.
The closest I am getting is this:
def q = Jenkins.instance.queue
q.items.each {
println("${it.task.name}:")
it.task.properties.each { key, val ->
println(" ${key}=${val}")
}
}
This gets me this:
4.1.next-build-launcher:
com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty$ScannerJobPropertyDescriptor#b299407=com.sonyericsson.jenkins.plugins.bfa.model.ScannerJobProperty#5e04bfd7
com.chikli.hudson.plugin.naginator.NaginatorOptOutProperty$DescriptorImpl#40d04eaa=com.chikli.hudson.plugin.naginator.NaginatorOptOutProperty#16b308db
hudson.model.ParametersDefinitionProperty$DescriptorImpl#b744c43=hudson.mod el.ParametersDefinitionProperty#440a6d81
...
The params property of the queue element itself contains a string with the parameters in a property file format -- key=value with multiple parameters separated by newlines.
def q = Jenkins.instance.queue
q.items.each {
println("${it.task.name}:")
println("Parameters: ${it.params}")
}
yields:
dbacher params:
Parameters:
MyParameter=Hello world
BoolParameter=true
I'm no Groovy expert, but when exploring the Jenkins scripting interface, I've found the following functions to be very helpful:
def showProps(inst, prefix="Properties:") {
println prefix
for (prop in inst.properties) {
def pc = ""
if (prop.value != null) {
pc = prop.value.class
}
println(" $prop.key : $prop.value ($pc)")
}
}
def showMethods(inst, prefix="Methods:") {
println prefix
inst.metaClass.methods.name.unique().each {
println " $it"
}
}
The showProps function reveals that the queue element has another property named causes that you'll need to do some more decoding on:
causes : [hudson.model.Cause$UserIdCause#56af8f1c] (class java.util.Collections$UnmodifiableRandomAccessList)

Writing dynamic query results into file

I am trying to write a generic program in Groovy that will get the SQL from config file along with other parameters and put them into file.
here is the program:
def config = new ConfigSlurper().parse(new File("config.properties").toURL())
Sql sql = Sql.newInstance(config.db.url, config.db.login, config.db.password, config.db.driver);
def fileToWrite = new File(config.copy.location)
def writer = fileToWrite.newWriter()
writer.write(config.file.headers)
sql.eachRow(config.sql){ res->
writer.write(config.file.rows)
}
in the config the sql is something like this:
sql="select * from mydb"
and
file.rows="${res.column1}|${res.column2}|${res.column3}\n"
when I run it I get
[:]|[:]|[:]
[:]|[:]|[:]
[:]|[:]|[:]
in the file. If I substitute
writer.write(config.file.rows)
to
writer.write("${res.column1}|${res.column2}|${res.column3}\n")
it outputs the actual results. What do I need to do different to get the results?
You accomplish this by using lazy evaluation of the Gstring combined with altering the delegate.
First make the Gstring lazy by making the values be the results of calling Closures:
file.rows="${->res.column1}|${->res.column2}|${-> res.column3}"
Then prior to evaluating alter the delegate of the closures:
config.file.rows.values.each {
if (Closure.class.isAssignableFrom(it.getClass())) {
it.resolveStrategy = Closure.DELEGATE_FIRST
it.delegate = this
}
}
The delegate must have the variable res in scope. Here is a full working example:
class Test {
Map res
void run() {
String configText = '''file.rows="${->res.column1}|${->res.column2}|${-> res.column3}"
sql="select * from mydb"'''
def slurper = new ConfigSlurper()
def config = slurper.parse(configText)
config.file.rows.values.each {
if (Closure.class.isAssignableFrom(it.getClass())) {
it.resolveStrategy = Closure.DELEGATE_FIRST
it.delegate = this
}
}
def results = [
[column1: 1, column2: 2, column3: 3],
[column1: 4, column2: 5, column3: 6],
]
results.each {
res = it
println config.file.rows.toString()
}
}
}
new Test().run()
The good news is that the ConfigSlurper is more than capable of doing the GString variable substitution for you as intended. The bad news is that it does this substitution when it calls the parse() method, way up above, long before you have a res variable to substitute into the parser. The other bad news is that if the variables being substituted are not defined in the config file itself, then you have to supply them to the slurper in advance, via the binding property.
So, to get the effect you want you have to parse the properties through each pass of eachRow. Does that mean you have to create a new ConfigSlurper re-read the file once for every row? No. You will have to create a new ConfigObject for each pass, but you can reuse the ConfigSlurper and the file text, as follows:
def slurper = new ConfigSlurper();
def configText = new File("scripts/config.properties").text
def config = slurper.parse(configText)
Sql sql = Sql.newInstance(config.db.url, config.db.login, config.db.password, config.db.driver);
def fileToWrite = new File(config.copy.location)
def writer = fileToWrite.newWriter()
writer.write(config.file.headers)
sql.eachRow(config.sql){ result ->
slurper.binding = [res:result]
def reconfig = slurper.parse(configText)
print(reconfig.file.rows)
}
Please notice that I changed the name of the Closure parameter from res to result. I did this to emphasize that the slurper was drawing the name res from the binding map key, not from the closure parameter name.
If you want to reduce wasted "reparsing" time and effort, you could separate the file.rows property into its own separate file. i would still read in that file text once and reuse the text in the "per row" parsing.

Can I use nested scoping when specifying environments in ConfigSlurper?

Using Groovy 2.0.7, when I have a config.groovy such as:-
def configText = """
switch(environment) {
case 'localhost':
PROXY {
HOST = "localproxy"
}
break
}
PROXY {
HOST = "defaultproxy"
}"""
def config = new ConfigSlurper("localhost").parse(configText)
, I get an assertion failure when I do this:-
assert "localproxy" == config.PROXY.HOST
If I remove the "defaultproxy" line then the environment value is correctly returned.
Am I doing something wrong? This to me is a standard requirement, to have a default value specified for config.PROXY.HOST but be able to override it in the environments switch block.
I know I can use the environments constructor to override the values but that is no use to me as it doesn't allow me to evaluate values, e.g. if I had:-
PROXY {
HOST = "defaultproxy"
URL = "http://" + HOST
}
then the URL would always be http://defaultproxy even if I specified the "localhost" environment.
I need the features from both really! Anyone know how I can achieve this?
You could also put the switch statement after the default proxy.host property...
def configText = """
PROXY {
HOST = "defaultproxy"
}
switch(environment) {
case 'localhost':
PROXY {
HOST = "localproxy"
}
break
}
"""
def config = new ConfigSlurper("localhost").parse(configText)
assert "localproxy" == config.PROXY.HOST
or you could leverage the environment property like this:
def configText = """
PROXY {
HOST = "defaultproxy"
}
environments{
localhost{
PROXY.HOST='localproxy'
}
}
"""
def config = new ConfigSlurper("localhost").parse(configText)
assert "localproxy" == config.PROXY.HOST
OK, I have a solution which works.
I wrapped the switch statement into a function, then called the function at the top of the script (below the function, above the nested scoped properties) and then called the function again at the bottom.
Crude, horrible, but works. If anyone has a better solution, please let me know!

Testing URLs in groovy

How can we check whether urls are working or not in groovy?
when we click a button, i will get all the urls from existing db from 'urls' table and need to check which url is working
Ex:
http://baldwinfilter.com/products/start.html - not working
http://www.subaru.com/ - working
and so many urls from db.
My aim is to get all urls and check which one is working and which is not .
do we need to check on the status it returns ??
Can any one help me giving idea ...
thanks in advance
sri...
You could use HttpBuilder like so:
#Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.0-RC2' )
import groovyx.net.http.HTTPBuilder
def urls = [
"http://baldwinfilter.com/products/start.html",
"http://www.subaru.com/"
]
def up = urls.collect { url ->
try {
new HTTPBuilder( url ).get( path:'' ) { response ->
response.statusLine.statusCode == 200
}
}
catch( e ) { false }
}
println up

Resources