cancel
Showing results for 
Search instead for 
Did you mean: 

ReadyAPI script assertion 'is not equal' to for JSON response

SOLVED
Highlighted
Community Manager

Re: ReadyAPI script assertion 'is not equal' to for JSON response

Hi @AboveAndBeyond ! Did the recent suggestions help you?🙂


Sonya Mihaljova
Community and Education Specialist

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
Highlighted
Frequent Contributor

Re: ReadyAPI script assertion 'is not equal' to for JSON response

Hello @sonya_m 

I don't know yet. I'm currently working on another project. I will look at it next week.

 

Kind regards,

AboveAndBeyond

Highlighted
Frequent Contributor

Re: ReadyAPI script assertion 'is not equal' to for JSON response

Hello @HimanshuTayal 

Thanks for your response but I'm getting error 

 

"Error in assertion script of the [REST Request] test step: startup failed: Script4.groovy: 11: Unexpected input: 'not'; Expecting @ line 11, column 19. assert checkForEN not in language, "Not expecting value ${checkFor} for language, but found" ^ org.codehaus.groovy.syntax.SyntaxException: Unexpected input: 'not'; Expecting @ line 11, column 19. at org.apache.groovy.parser.antlr4.AstBuilder$1.syntaxError(AstBuilder.java:4542) at groovyjarjarantlr4.v4.runtime.ProxyErrorListener.syntaxError(ProxyErrorListener.java:44) at groovyjarjarantlr4.v4.runtime.Parser.notifyErrorListeners(Parser.java:543) at groovyjarjarantlr4.v4.runtime.DefaultErrorStrategy.notifyErrorListeners(DefaultErrorStrategy.java:154) at org.apache.groovy.parser.antlr4.internal.DescriptiveErrorStrategy.reportInputMismatch(DescriptiveErrorStrategy.java:104) at org.apache.groovy.parser.antlr4.internal.DescriptiveErrorStrategy.recover(DescriptiveErrorStrategy.java:55) at............"

 

Code

import groovy.json.JsonSlurper

def rawjson = context.expand( '${REST Request#Response}' )
def json = new groovy.json.JsonSlurper().parseText(rawjson)
def language = json.pdcData.concepts.description.language
log.info language
def checkForFR = "fr"
def checkForEN = "en"
def checkForDE = "de" //here I've included the values in an array cos theres >1 value to assert against
//assert languageVals.every {it != checkFor}, "Not expecting value ${checkFor} for language, but found"
assert checkForEN not in language, "Not expecting value ${checkFor} for language, but found"
assert checkForEN not in language, "Not expecting value ${checkFor} for language, but found"
assert checkForEN not in language, "Not expecting value ${checkFor} for language, but found"

@sonya_m  no, I still didn't upgrade to newer version of ReadyAPI as the bugfixing doesn't help my case (went looking on the website which fixes). Also, I do understand that ReadyAPI is upgrading, but it seem that a few of our code isn't working anymore, thus we need to re-work all the time. This is time and resource consuming for the client. We can't afford this. Also when we aren't on the project anymore, this project needs to sustain itself without human interference. So if ReadyAPI updates each time and mingles itself with the codes of the users, there is no positive use to update. Sorry 😞 

 

Kr,

AboveAndBeyond

 

 

 

 

Frequent Contributor

Re: ReadyAPI script assertion 'is not equal' to for JSON response

Hello,

 

I've looked into this with a developper. For our response we see that we actually have an array in an array. Therefore the code needed to be adapted a bit.

The response looks like this:

Tue Sep 22 16:30:58 CEST 2020: INFO: [[nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], [nl], ......etc..]

Assertion second part:

 

.....

def checkForLanguage = "nl"

def onlyExpectedLanguage = true
language.each {
  element -> element.each {
  		languageUnderTest -> {
  				if (!languageUnderTest.equals(checkForLanguage)) {
  						onlyExpectedLanguage = false
  						log.info "Found unexpected language: ${languageUnderTest}"
  				}
  		}
  }
}

assert onlyExpectedLanguage, "Found a different language than ${checkForLanguage}"

 

In this case I was, with the previous code, asking to compare a string (def checkForLanguage = "nl")  with an array ( def language = json.pdcData.concepts.description.language ) and thus of course the code passed each time because it was comparing something (array) with nothing (string).

So in order to be able to compare, I needed to loop over each array in that array.

Mostly the code is written like this:

 

def expected = true
x.each{
     y -> y.each{
           variableName -> {
                             //your statement here
                           }
          }
     }
assert expected, "error message here"

 

 

Thanks for your help anyways 😄 

Kind regards,

AboveAndBeyond

View solution in original post

New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors