Bagesh Sharma
Bagesh Sharma

Reputation: 889

Groovy DSL Spring cloud contract throwing java.lang.IllegalStateException for query parameter having Unicode Characters

I created a groovy DSL contract like below

import org.springframework.cloud.contract.spec.Contract

Contract.make {
    final def NAME_REGEX = '[A-Za-z0-9\\u00C0-\\u00FF\'\\- ]{1,70}'

    request {
        method 'GET'
        url('/api/getEmployess') {
            queryParameters {
                parameter 'name': $(c(regex(NAME_REGEX)), p('\u00CAdward J\u00F5hnson'))
            }
        }
        headers {
            contentType("application/json;charset=UTF-8")
        }
    }
    response {
        status OK()
        body([
                [
                 id           : $(p(regex(nonBlank())), c('5a0eaf2012a9a12f1c98947a')),
                 name         : fromRequest().query("name")
               ]
        ])
        headers { contentType("application/json;charset=UTF-8") }
    }
}

My service implementation returns 'name' and 'id' in response. In response, 'name' is Unicode value 'Êdward Jõhnson' which fails to match with the request parameter value.

I am getting below error -

Parsed JSON [[{"id":"5a0eaf2012a9a12f1c98947a","name":"Êdward Jõhnson"}]] doesn't match the JSON path [$[*][?(@.['name'] == 'Êdward Jõhnson')]]
java.lang.IllegalStateException: Parsed JSON [[{"id":"5a0eaf2012a9a12f1c98947a","name":"Êdward Jõhnson" }]] doesn't match the JSON path [$[*][?(@.['name'] == 'Êdward Jõhnson')]]
    at com.toomuchcoding.jsonassert.JsonAsserter.check(JsonAsserter.java:228)
    at com.toomuchcoding.jsonassert.JsonAsserter.checkBufferedJsonPathString(JsonAsserter.java:267)

I tried to pass the Unicode value in two ways in 'name' request query param -

  1. Putting Unicode characters as Unicode numbers like in the above example - parameter 'name': $(c(regex(NAME_REGEX)), p('\u00CAdward J\u00F5hnson'))
  2. Putting Unicode characters as it is parameter 'name': $(c(regex(NAME_REGEX)), p('Êdward Jõhnson'))

But for both cases, I am getting the same error. There looks some encoding issue because my value Êdward Jõhnson is changed to Êdward Jõhnson as mentioned in error.

Please help me to resolve this issue.

Upvotes: 0

Views: 642

Answers (1)

Bagesh Sharma
Bagesh Sharma

Reputation: 889

I found a workaround for this. In response 'name' field producer I put the same value which was in the request 'name' field producer. It was failing due to different encoding applied by groovy on unicode value. It's just a workaround to fix the problem, not a proper final solution.

import org.springframework.cloud.contract.spec.Contract

Contract.make {
     final def NAME_REGEX = '^[A-Za-z0-9À-ÿ'\-\s]{1,70}$'

     request {
                   method 'GET'
                   url('/api/getEmployess') {
                      queryParameters {
                          parameter 'name': $(c(regex(NAME_REGEX)), p('Êdward Jõhnson'))
                      }
                   }
                   headers {
                      contentType("application/json;charset=UTF-8")
                   }
           }
    response {
        status OK()
        body([
               [
                 id : $(p(regex(nonBlank())), c('4b0eaf2012a9a12f1c98567c')),
                 name : $(p("Êdward Jõhnson"), c(fromRequest().body('$.name'))),
               ]
           ])
           headers { contentType("application/json;charset=UTF-8") }
    }
 }

Upvotes: 0

Related Questions