Skip to content

Commit

Permalink
Merge branch 'master' into feature/update-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
irodzik authored Sep 22, 2023
2 parents c2136d5 + b59e878 commit ed0672d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 11 deletions.
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "gradle" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
2 changes: 1 addition & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ representative at an online or offline event.

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement at
[this email]([email protected]).
[this email](mailto:[email protected]).
All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the
Expand Down
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ For something that is bigger than a few line fix:

# How to report a bug

If you find a security vulnerability, do NOT open an issue. Email [maintainers]([email protected]) instead.
If you find a security vulnerability, do NOT open an issue. Email [maintainers](mailto:[email protected]) instead.


Any security issues should be submitted directly to [maintainers]([email protected])
Any security issues should be submitted directly to [maintainers](mailto:[email protected])
In order to determine whether you are dealing with a security issue, ask yourself these two questions:
* Can I access something that's not mine, or something I shouldn't have access to?
* Can I disable something for other people?

If the answer to either of those two questions are "yes", then you're probably dealing with a security issue. Note that even if you answer "no" to both questions, you may still be dealing with a security issue, so if you're unsure, just [email us]([email protected]).
If the answer to either of those two questions are "yes", then you're probably dealing with a security issue. Note that even if you answer "no" to both questions, you may still be dealing with a security issue, so if you're unsure, just [email us](mailto:[email protected]).

When filing an issue, make sure to answer these five questions:

Expand Down
13 changes: 7 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.gradle.api.JavaVersion.VERSION_11
import java.util.*

version = "0.3.2"
version = "0.4.0"
group = "com.bisnode.opa"

plugins {
Expand All @@ -19,14 +19,15 @@ java {
withSourcesJar()
}
dependencies {
implementation("com.fasterxml.jackson.core:jackson-databind:2.13.0")
implementation("com.fasterxml.jackson.core:jackson-databind:2.15.2")
implementation("org.slf4j:slf4j-api:1.7.32")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
testImplementation("org.junit.vintage:junit-vintage-engine:5.8.1")
testImplementation("org.codehaus.groovy:groovy-all:2.5.15")
testImplementation("org.spockframework:spock-core:1.3-groovy-2.5")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.10.0")
testImplementation("org.junit.vintage:junit-vintage-engine:5.10.0")
testImplementation("org.codehaus.groovy:groovy-all:3.0.19")
testImplementation("org.spockframework:spock-core:2.3-groovy-3.0")
testImplementation("com.github.tomakehurst:wiremock-jre8:2.31.0")
testImplementation("net.bytebuddy:byte-buddy:1.14.8")
}

repositories {
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/bisnode/opa/client/OpaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import com.bisnode.opa.client.query.QueryForDocumentRequest;
import com.bisnode.opa.client.rest.ObjectMapperFactory;
import com.bisnode.opa.client.rest.OpaRestClient;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.lang.reflect.ParameterizedType;
import java.net.http.HttpClient;
import java.util.Objects;
import java.util.Optional;

/**
* Opa client featuring {@link OpaDataApi}, {@link OpaQueryApi} and {@link OpaPolicyApi}
Expand Down Expand Up @@ -72,6 +74,7 @@ public void createOrUpdatePolicy(OpaPolicy policy) {
*/
public static class Builder {
private OpaConfiguration opaConfiguration;
private ObjectMapper objectMapper;

/**
* @param url URL including protocol and port
Expand All @@ -81,12 +84,19 @@ public Builder opaConfiguration(String url) {
return this;
}

public Builder objectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
return this;
}

public OpaClient build() {
Objects.requireNonNull(opaConfiguration, "build() called without opaConfiguration provided");
HttpClient httpClient = HttpClient.newBuilder()
.version(opaConfiguration.getHttpVersion())
.build();
OpaRestClient opaRestClient = new OpaRestClient(opaConfiguration, httpClient, ObjectMapperFactory.getInstance().create());
ObjectMapper objectMapper = Optional.ofNullable(this.objectMapper)
.orElseGet(ObjectMapperFactory.getInstance()::create);
OpaRestClient opaRestClient = new OpaRestClient(opaConfiguration, httpClient, objectMapper);
return new OpaClient(new OpaQueryClient(opaRestClient), new OpaDataClient(opaRestClient), new OpaPolicyClient(opaRestClient));
}
}
Expand Down
78 changes: 78 additions & 0 deletions src/test/groovy/com/bisnode/opa/client/OpaClientBuilderSpec.groovy
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
}
}

0 comments on commit ed0672d

Please sign in to comment.