Forum Discussion

vpnkumar's avatar
vpnkumar
Occasional Contributor
4 years ago
Solved

Compare Json Response from the data source with the actual response in event

In my correct setup I have scenarios where I need to compare the JSON response from the data source to actual response, this can be done in a separate groovy but because of implementation structure we want to compare it in RequestFilter.afterRequest even.

The problem we are running into is in event we are not able to fetch expected response from datasource, can someone please have a look into this and suggest how this can be achived.  

14 Replies

  • richie's avatar
    richie
    Community Hero
    Hey vpnkumar,

    No ones answered this yet and ive only ever used events intermittently but i'll try and help out.

    You mention " because of implementation structure" you want to do the compare using an event handler rather than individual groovy scripts. Can you clarify what you mean by "implementation structure"? Ive no idea what that even means.

    You included the tag SOAP, but mention JSON in your post. Is it SOAP or is it JSON? SOAP can only be XML, so just want to be clear about what youre after...especially if youre talking about comparing the xml payload within a soap container.

    I understand how having one event handler script rather than a script in each testcase makes things a lot easier to setup/create and maintain but im struggling to think how this would work. If the groovy is doing a comparison, i take it youd need >=1 assertions in your script. How would having assertions in an event handler script work? What i mean by this is, if this comparison assertion was in an event that fires after the request and was the only assertion in the test, would the tests status get updated correctly? Another thing to think about, wouldnt having assertions in the event skew the reporting?

    You mention that youre struggling to fetch the expected reaponse from the Datasource, but why? Why arent you able to fetch the response from the Datasource in the event groovy? The same objects are available in the event handler groovy as in the normal groovy steps, so im struggling to understand why this ismt working.

    It would probably help tremendously if you published the event handler groovy you have already got, as the groovy experts on here (@nmrao, HimanshuTayal, groovyguy) will more likely be able to help if they see what groovy you have tried already.

    Nice one,

    Rich
    • vpnkumar's avatar
      vpnkumar
      Occasional Contributor

      Thanks Rich please find more details below and in case if I am not clear please let me know I will try to explain more

      From Implementation structure, I mean at current for my Json request things like updating it with dynamic values, removing empty (key, values, array of empty objects) all are handled with events.

      For response verification I already have a expected response in the data source there are few dynamics id that’s need to be removed before comparing actual and expected Json response, this is already implemented in a groovy script but instead of adding this groovy script after ever Rest request for me it looks it will be better if this can be handled in events, at current I can get the current response in the events but when I am using same approach  as listed below but It didn’t work in event because of unavailability of testrunner in events.

       

      def ds = testRunner.testCase.testSteps['DataSource']

      def ExpJson = ds.getPropertyValue("ExpectedResponse"); 

       

      for Assertion point I was thinking of one simple status code assertion in the rest request and other will be JSON response comparison in event, it will use the existing groovy function where input parameter is expected json from datsource and actual one from REST response

       

      Below is the sample code I had used in event for request handling.

       

      if(request.getName()=="Request1" || request.getName()=="Request2" || request.getName()=="Request3" ){
      def requestContent = (String) context.getProperty(BaseHttpRequestTransport.REQUEST_CONTENT)
      log.info("Request Body BEFORE " + requestContent)
      def finalrequest = ModifiedRequest(requestContent);
      log.info("final request is " + finalrequest);
      log.info("Request Body AFTER " + finalrequest)
      context.setProperty(BaseHttpRequestTransport.REQUEST_CONTENT, finalrequest);
      def httpMethod = context.getProperty(BaseHttpRequestTransport.HTTP_METHOD)
      httpMethod.setEntity(new ByteArrayEntity(finalrequest.getBytes(Charset.defaultCharset())))

      }

       

       
      • richie's avatar
        richie
        Community Hero
        vpnkumar,

        As far as i was aware, testRunner IS available within events....it even says its available in the title of thr editable event field????

        Ta

        Rich