Unable to understand the Jenkins pipeline groovy condition - if-statement

Unable to understand following commands output which is as follow.
def status123=sh (script: '<some python which returns "Aborted">' ',returnStdout: true)
println status123.getClass()
println "Aborted".getClass()
println (status123=="Aborted")
println (status123.toString()=="Aborted")
println ("Aborted"=="Aborted")
Expected:
class java.lang.String
class java.lang.String
true
true
true
Actual:
class java.lang.String
class java.lang.String
false
false
true
Expecting (status123=="Aborted") to be true but getting as false.
Any input on this would be very useful

Related

if false, do nothing in tickscript

In tickscript the norwal way to do if else is in the following:
if(condition, true expression, false expression)
However, for the false expression, I want my code to do nothing as in the following:
if(condition, true print("that is true"), false -do nothing- )
I already tried putting some blank and/or deleting false expression part but it did not work.
Is there a way to do that in tickscript?

Double-not of an empty regex literal (!!//) is false, is this a parsing error? [duplicate]

This question already has answers here:
Why is a Regexp object considered to be "falsy" in Ruby?
(2 answers)
Closed 2 years ago.
In Ruby, only false and nil are falsey; everything else is truthy. You can use two not operators to check an object's truthiness:
!!false # false
!!nil # false
!![] # true
!!{} # true
!!'' # true
!!0 # true
But then I found that empty-regex literal // is falsey, but as a variable, it's truthy!:
!!// # false!
not not // # false
x = //
x.class # Regex
!!x # true
I think this is a quirk of the parser. How can I ask the parser what it's doing?
This is not just applicable to empty regex literal, but rather all regex literals;
!!/(.*)/
=> false
x = /(.*)/
!!x
=> true
At first this appeared to be an issue with the way the regexp is being constructed
!!Regexp.new('')
=> true
x = Regexp.new('')
!!x
=> true
!!//.freeze
=> true
But digging deeper, this appears to be the cause:
!!//
=> false
$_ = 'b'
!!//
=> true
Setting '$_' back to nil will reset the outcome. The value for $_ is set from Kernel.readline or Kernel.get siblings. In a new console:
Kernel.readline
a
"a\n"
!!//
=> true
So instantiating a Regexp object with // appears to match it against the value of $_
To be honest, I'm not 100% sure, but my idea is that Ruby is doing the next thing when you run !!x
puts !!// # this is doing a phantom .match(nil), like below, so, returns false
x = //
puts !!x.match(nil)

Laravel 5.3 Validation Regex check for True or False fails

In Laravel 5.1, I used to do a validation check to see if a posted value is set as true or false using Regex Match. The posted value is something like this:
acceptTerms true
I then check the value from Laravel validation like this:
$validator = Validator::make($postData, [
'acceptTerms' => ["regex:(true|false)"],
]);
Since updating to Laravel 5.3, the above validation rules fails and always throws the error: acceptTerms format is invalid..
Why is it failing and how do I check if a value is set as true or false in Laravel 5.3?
EDIT 1:
I forgot to add, the data posted is sent as JSON.stringify(data) and in Laravel I receive it as Input::json('data'). Would that change anything?
EDIT 2:
dd($postData) returns this:
array:31 [
"id" => "8"
"appId" => "1"
"name" => "Asdsad"
"acceptTerms" => true
]
If it's boolean, you can use boolean.
I guess it's more readable than regex.
The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and "0".
You can try this with in() as:
'acceptTerms' => 'in:true,false',

Groovy / Mocking Sql

I'm trying to mock the Sql instance in Groovy the following way, I'm using the spock framework for testing. However the test fails, please see below:
class SQLStatsStorageManagerTest extends Specification {
def mockSql
def setup() {
mockSql = GroovyMock(Sql, global: true)
}
void "SQLStatsStorageManager instantiation succeed"() {
def c
when: "SQLStatsStorageManager is instantiated"
c = new SQLStatsStorageManager("test", [hostname: "localhost", port: 666, database: "db", login: "root", password: "pass"])
then: "there is no error and name is set"
1 * mockSql.newInstance('jdbc:mysql://localhost:666/db', 'root', 'pass', 'com.mysql.jdbc.Driver')
assert c.getName() == "test"
}
}
The test fails with the following error:
Too few invocations for:
1 * mockSql.newInstance('jdbc:mysql://localhost:666/db', 'root', 'pass', 'com.mysql.jdbc.Driver') (0 invocations)
Unmatched invocations (ordered by similarity):
1 * mockSql.newInstance(jdbc:mysql://localhost:666/db, 'root', 'pass', 'com.mysql.jdbc.Driver')
Any idea ?
Thanks.
Pay attention to the fact that the only argument that does not match is db link.
You try to verify it as an instance of String:
'jdbc:mysql://localhost:666/db'
but in unmatched invocation it is:
jdbc:mysql://localhost:666/db
so here's the question, what it actually is? Verify the types and you'll have the problem solved.

How to test the keys and values of api response for Request specs

I am writing Request specs, and having trouble with to test api respond in json formate. I am using capybara and fabricator, here is my code which i trying...
context 'discounts in api' do
let(:user) { Fabricate(:user, activated: true) }
let(:api_token) { user.api_token }
before { visit api_url_for('/v1/discount_coupons', api_token) }
it 'returns coupons collection' do
Fabricate(:discount_coupon, code: 'Discount One', global: true)
save_screenshot("tmp/capybara/screenshot-#{Time::now.strftime('%Y%m%d%H%M%S%N')}.png")
save_and_open_page
expect(json_response['total_records']).to eq 1
expect(json_response['total_pages']).to eq 1
expect(json_response['page']).to eq 0
expect(json_response['discount_coupons'].size).to eq 1
expect(json_response['discount_coupons'][0]['code']).to eq 'Discount One'
end
end
the responce i getting is this
{"discount_coupons":[{"id":11,"code":"Discount One","type":"DiscountPercentage","amount_off":1.5,"global":true,"expires_on":null,"vendors":[]}],"page":0,"total_pages":1,"total_records":1}
and error goes to stop me for run a successful test,
Failure/Error: expect(json_response['total_pages']).to eq 1
NoMethodError:
undefined method `body' for nil:NilClass
I think my expect to json_response is wrong or something missing, can somone help me to do it handsome way please, hint to that how can i test using key and value.
Best way to test an API is use rspec as you just need to do this:
it "should return the expected information" do
get "/url"
parsed_response = JSON.parse(response.body)
expect(parsed_response["key"]).to eq(whatever)
end
it "should update the expected entity" do
post "/url", body, headers
parsed_response = JSON.parse(response.body)
expect(parsed_response["key"]).to eq(whatever)
end
And your tests are failing because you are trying to parse a response that is empty. The Fabric can be failing or the call might be wrong.
I think the issue may be that you are visiting the page in the before block and then generating the discount coupon later on in the assertion block. Try moving the code around like this and see if it yields a better result.
context 'discounts in api' do
let(:user) { Fabricate(:user, activated: true) }
let(:api_token) { user.api_token }
before do
Fabricate(:discount_coupon, code: 'Discount One', global: true)
visit api_url_for('/v1/discount_coupons', api_token)
end
it 'returns coupons collection' do
save_screenshot("tmp/capybara/screenshot-#{Time::now.strftime('%Y%m%d%H%M%S%N')}.png")
save_and_open_page
expect(json_response['total_records']).to eq 1
expect(json_response['total_pages']).to eq 1
expect(json_response['page']).to eq 0
expect(json_response['discount_coupons'].size).to eq 1
expect(json_response['discount_coupons'][0]['code']).to eq 'Discount One'
end