Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📝 add tests documenting deserialiation of types #203

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 71 additions & 1 deletion src/test/groovy/com/tomtom/http/GroovyAPISpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.github.tomakehurst.wiremock.client.WireMock
import spock.lang.Unroll

import static com.fasterxml.jackson.databind.DeserializationFeature.*
import static com.github.tomakehurst.wiremock.client.WireMock.*
import static com.tomtom.http.response.ResponseCode.OK

Expand Down Expand Up @@ -205,6 +206,56 @@ class GroovyAPISpec extends HttpClientSpec {
'OPTIONS' | WireMock.&options | http.&options
}

@Unroll
def 'parses decimals as doubles by default for #name'() {
given:
mock.givenThat(mockMethod(urlEqualTo('/info'))
.willReturn(ok('{"price": 10.00}')))

when:
def response = clientMethod(path: '/info', expecting: Map)

then:
response.statusCode == OK
response.body == [price: 10.0]
response.body.price instanceof Double

where:
name | mockMethod | clientMethod
'GET' | WireMock.&get | http.&get
'POST' | WireMock.&post | http.&post
'PUT' | WireMock.&put | http.&put
'DELETE' | WireMock.&delete | http.&delete
'TRACE' | WireMock.&trace | http.&trace
'PATCH' | WireMock.&patch | http.&patch
'OPTIONS' | WireMock.&options | http.&options
}

@Unroll
def 'parses decimals as given type for custom objects for #name'() {
given:
mock.givenThat(mockMethod(urlEqualTo('/info'))
.willReturn(ok('{"price": 10.00}')))

when:
def response = clientMethod(path: '/info', expecting: Info)

then:
response.statusCode == OK
response.body == new Info(price: 10.0)
response.body.price instanceof BigDecimal

where:
name | mockMethod | clientMethod
'GET' | WireMock.&get | http.&get
'POST' | WireMock.&post | http.&post
'PUT' | WireMock.&put | http.&put
'DELETE' | WireMock.&delete | http.&delete
'TRACE' | WireMock.&trace | http.&trace
'PATCH' | WireMock.&patch | http.&patch
'OPTIONS' | WireMock.&options | http.&options
}

@Unroll
def 'parses response body as generic for #name'() {
given:
Expand Down Expand Up @@ -253,7 +304,26 @@ class GroovyAPISpec extends HttpClientSpec {
'OPTIONS' | WireMock.&options | http.&options
}

def 'allows providing custom ObjectMapper and falls back to body as string for #name'() {
def 'allows providing custom ObjectMapper'() {
given:
def mapper = new ObjectMapper()
.enable(USE_BIG_DECIMAL_FOR_FLOATS)
def http = new HttpClient(mapper: mapper, baseUrl: "http://localhost:${mock.port()}")
mock.givenThat(get(urlEqualTo('/info'))
.willReturn(ok('{"price": 1.65}')))

when:
def response = http.get(path: '/info', expecting: Map)

then:
with(response) {
statusCode == OK
body == [price: 1.65]
body.price instanceof BigDecimal
}
}

def 'falls back to body as string for #name'() {
given:
def mapper = new ObjectMapper()
def http = new HttpClient(mapper: mapper, baseUrl: "http://localhost:${mock.port()}")
Expand Down
10 changes: 10 additions & 0 deletions src/test/groovy/com/tomtom/http/Info.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tomtom.http

import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString

@EqualsAndHashCode
@ToString
class Info {
BigDecimal price
}