-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows for configuring a custom ObjectMapper (#47)
* Allow for configuring a custom ObjectMapper This change modifies the OpaClient builder to optionally take a custom ObjectMapper.
- Loading branch information
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/test/groovy/com/bisnode/opa/client/OpaClientBuilderSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.bisnode.opa.client | ||
|
||
|
||
import com.bisnode.opa.client.query.QueryForDocumentRequest | ||
import com.bisnode.opa.client.rest.ContentType | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.github.tomakehurst.wiremock.WireMockServer | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
import static com.bisnode.opa.client.rest.ContentType.Values.APPLICATION_JSON | ||
import static com.github.tomakehurst.wiremock.client.WireMock.* | ||
|
||
class OpaClientBuilderSpec extends Specification { | ||
|
||
private static int PORT = 8181 | ||
private static String url = "http://localhost:$PORT" | ||
|
||
@Shared | ||
private WireMockServer wireMockServer = new WireMockServer(PORT) | ||
|
||
def setupSpec() { | ||
wireMockServer.start() | ||
} | ||
|
||
def cleanupSpec() { | ||
wireMockServer.stop() | ||
} | ||
|
||
def 'should configure OpaClient with custom ObjectMapper'() { | ||
|
||
given: | ||
def objectMapper = Spy(ObjectMapper) | ||
def path = 'someDocument' | ||
def endpoint = "/v1/data/$path" | ||
wireMockServer | ||
.stubFor(post(urlEqualTo(endpoint)) | ||
.withHeader(ContentType.HEADER_NAME, equalTo(APPLICATION_JSON)) | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withHeader(ContentType.HEADER_NAME, APPLICATION_JSON) | ||
.withBody('{"result": {"authorized": true}}'))) | ||
def opaClient = OpaClient.builder() | ||
.opaConfiguration(url) | ||
.objectMapper(objectMapper) | ||
.build(); | ||
|
||
when: | ||
opaClient.queryForDocument(new QueryForDocumentRequest([shouldPass: true], path), Object.class) | ||
|
||
then: | ||
1 * objectMapper.writeValueAsString(_) | ||
} | ||
|
||
def 'should revert to default ObjectMapper if null ObjectMapper supplied'() { | ||
given: | ||
def path = 'someDocument' | ||
def endpoint = "/v1/data/$path" | ||
wireMockServer | ||
.stubFor(post(urlEqualTo(endpoint)) | ||
.withHeader(ContentType.HEADER_NAME, equalTo(APPLICATION_JSON)) | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withHeader(ContentType.HEADER_NAME, APPLICATION_JSON) | ||
.withBody('{"result": {"authorized": true}}'))) | ||
def opaClient = OpaClient.builder() | ||
.opaConfiguration(url) | ||
.objectMapper(null) | ||
.build(); | ||
|
||
when: | ||
def result = opaClient.queryForDocument(new QueryForDocumentRequest([shouldPass: true], path), Map.class) | ||
|
||
then: | ||
result != null | ||
result.get("authorized") == true | ||
} | ||
} |