Testing URLs in groovy - 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

Related

Launching multiple browser url in groovy script

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.

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' }

multipleCompilationError when parsing config file using ConfigSlurper

I need to access config file in my groovy code using ConfigSlurper as am using blocks inside the file to access each one depending on the user info !
so i need to use the properties in one block in the file ( based in the user info ) and set it to an object in my code !
I used something like this
def pc = ConfigSlurper().parse(newFile(configManager.config.priceInfo.filepath).toURI().toURL())
my file contains closures as blocks as following :
employee {
sth = 1
other =2
}
student {
sth = 10
other =20 }
default
{
sth = 100
other =200
}
I get multipleCompilationError exception when parsing the file !
i was using a block in my file with the name default !
employee {
sth = 1
other =2
}
student {
sth = 10
other =20
}
default{
sth = 100
other =200
}
This was causing this exception ! it seems like default is a keyword for a class in Groovy ! i changed 'default' to other name and it works now for me anyhow :)

How to access to web page using Groovy?

i try to connect to webpage using Groovy,
i tried this, it works when the URL is www.google.fr
String.metaClass.browse {
def handler = [(~/^Mac OS.*/) : { "open $it".execute() }, (~/^Windows.*/) : { "cmd /C start $it".execute() },
(~/.*/) : {
//--- assume Unix or Linux
def browsers = [ 'firefox'.'chrome' ]
//--- find a browser we know the location of
def browser = browsers.find {
"which $it".execute().waitFor() == 0
}
//--- and run it if one found
if( browser )
"$browser $it".execute()
}
]
def k = handler.find { k, v -> k.matcher( System.properties.'os.name' ).matches() }
k?.value( delegate )
}
www.google.fr".browse.()
if i put URL which download a file it throw Compilation error. Thank you for your help.
If you want to add it as a method to the String class, you can do:
String.metaClass.browse = { ->
java.awt.Desktop.desktop.browse(new URI(delegate))
}
Then calling
"http://www.google.com".browse()
Will open your default browser

grails: assigning a domain class to another domain class

I have a scenario where users are assigned to team.
Different ClientServices are allocated to different teams and
we need to assign user Of these teams to clientservice in RoundRobin fashion
I was trying to solve it as follows to get a map where team name and a list of ClientServiceInstance will be mapped so I can do further processing on it
def teamMap = [:]
clientServicesList.each {clientServiceInstance->
if(teamMap[clientServiceInstance.ownerTeam] == null){
teamMap.putAt(clientServiceInstance.ownerTeam, new ArrayList().push(clientServiceInstance))
}else{
def tmpList = teamMap[clientServiceInstance.ownerTeam]
tmpList.push(clientServiceInstance)
teamMap[clientServiceInstance.ownerTeam] = tmpList
}
}
but instead of pushing clientServiceInstance it pushes true.
Any idea?
I believe another version would be:
def teamMap = clientServicesList.inject( [:].withDefault { [] } ) { map, instance ->
map[ instance.ownerTeam ] << instance
map
}
new ArrayList().push(clientServiceInstance) returns true, which means you're putting that into your teamMap instead of what I assume should be a list? Instead you might want
teamMap.putAt(clientServiceInstance.ownerTeam, [clientServiceInstance])
By the way, your code is not very Groovy ;)
You could rewrite it as something like
def teamMap = [:]
clientServicesList.each { clientServiceInstance ->
if (teamMap[clientServiceInstance.ownerTeam]) {
teamMap[clientServiceInstance.ownerTeam] << clientServiceInstance
} else {
teamMap[clientServiceInstance.ownerTeam] = [clientServiceInstance]
}
}
Although I'm sure there are even better ways to write that.

Resources