Forum Discussion

ricardo_moura's avatar
ricardo_moura
Occasional Contributor
7 years ago

How can i validate json fields from the rest request in my mock service script?

hello

 

i have a Rest MockService with groovy scripts to read json requests and dispatch specific responses, but i need add assertions on requests to validate if requests incoming from webservice client are ok.

 

how can i do this?

i think about something on onRequest Script which read all Json requests received, validate them and put results on a external file, it's possible?

i'm using soapUI version 5.3.0 and readyAPI 1.9

 

Regards.

3 Replies

    • ricardo_moura's avatar
      ricardo_moura
      Occasional Contributor

      i did the code right now:

       

      i use above script to put on "script log" Pass or Fail information to each json field to be validate

      the script works, but i have a problem:

       

      my original switch case have a lot of cases and each request have a lot of validations,

      if i do this on this way, the script will be to big and hard to maintain, Ideally I would have a validation file for each case, but i not know how.

       

       

      my actual onRequest Script:

       

       

      import com.eviware.soapui.support.XmlHolder
      import groovy.json.*
      
      //define request
      def request = mockRequest.getRequestContent()
      
      //define "json" which will be the name of my variable
      def json = new JsonSlurper().parseText(request)
      
      //define the request identificator for ConsultStatement requests
      def identificator = json.address.postalCode
      
      //define file directory
      def directory = "c:\\dump"
      
      def timestamp = System.currentTimeMillis()
      def requestFile = new File(directory, identificator + ".xml")
      
      switch (identificator){
      
      	case '063842134':
      		requestFile.write("PostalCode: " + identificator)
      		if (json.address.streetName == "test avenue"){
      			requestFile.append("\nPass - streetName: " + json.address.streetName)
      		}
      		else {
      			requestFile.write("\nFail - streetName: " + json.address.streetName + ", expected: test avenue")
      		}
      		if (json.address.number == "123"){
      			requestFile.append("\nPass - number: " + json.address.number)
      		}
      		else {
      			requestFile.append("\nFail - number: " + json.address.number + ", expected: 123")
      		}
      		// if-else will be repeat for all request fields
      		// next case contain same validations...
      }

       

      the output file should help testers to identify defects

      • nmrao's avatar
        nmrao
        Champion Level 3
        Well. Below pointers on quick glance:

        1. Not sure the reason behind appending pass / fail message for the request in Mock Service. IMO, it may not be useful.
        2. Instead, catch error and send the reply accordingly and put assertions in the test case. That way, it would be easy to know if the application is behaving correct or not.
        3. Keep less if / else conditions (if at all possible) to avoid confusion and that could also lead potential logic issues.
        4. Modularize the code instead of putting lots of code, referring to "case", for example use a method / function and just call that