Skip to content

Commit

Permalink
Optimize Fakes in Misk to reuse injectors for tests
Browse files Browse the repository at this point in the history
GitOrigin-RevId: e52c38e4fd0fe47620255bce9acfd51967acbc3c
  • Loading branch information
katukota authored and svc-squareup-copybara committed Sep 27, 2024
1 parent 7ff7052 commit 13dcba0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion misk-policy/api/misk-policy.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ public final class misk/policy/opa/FakeOpaModule : misk/inject/KAbstractModule {
public fun <init> ()V
}

public final class misk/policy/opa/FakeOpaPolicyEngine : misk/policy/opa/OpaPolicyEngine {
public final class misk/policy/opa/FakeOpaPolicyEngine : misk/testing/FakeFixture, misk/policy/opa/OpaPolicyEngine {
public fun <init> ()V
public final fun addOverride (Ljava/lang/String;Lmisk/policy/opa/OpaResponse;)V
public final fun addOverrideForInput (Ljava/lang/String;Ljava/lang/String;Lmisk/policy/opa/OpaResponse;)V
Expand Down
1 change: 1 addition & 0 deletions misk-policy/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ dependencies {
testFixturesApi(libs.jakartaInject)
testFixturesApi(project(":misk-inject"))
testFixturesApi(project(":misk-policy"))
testFixturesApi(project(":misk-testing-api"))
testFixturesImplementation(libs.dockerTransport)
testFixturesImplementation(libs.dockerTransportCore)
testFixturesImplementation(libs.guice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package misk.policy.opa

import misk.inject.KAbstractModule
import jakarta.inject.Inject
import misk.testing.TestFixture

class FakeOpaModule @Inject constructor(): KAbstractModule() {
override fun configure() {
bind<OpaPolicyEngine>().to<FakeOpaPolicyEngine>()
multibind<TestFixture>().to<FakeOpaPolicyEngine>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package misk.policy.opa

import jakarta.inject.Inject
import jakarta.inject.Singleton
import misk.testing.FakeFixture

@Singleton
class FakeOpaPolicyEngine @Inject constructor(): OpaPolicyEngine {
class FakeOpaPolicyEngine @Inject constructor(): OpaPolicyEngine, FakeFixture() {
override fun <T : OpaRequest, R : OpaResponse> evaluateWithInput(
document: String,
input: T,
Expand All @@ -18,7 +19,7 @@ class FakeOpaPolicyEngine @Inject constructor(): OpaPolicyEngine {
return opaResponse as R
}

override fun <R: OpaResponse> evaluateRawJsonInput(
override fun <R : OpaResponse> evaluateRawJsonInput(
document: String,
input: String,
returnType: Class<R>
Expand All @@ -41,24 +42,24 @@ class FakeOpaPolicyEngine @Inject constructor(): OpaPolicyEngine {
return opaResponse as R
}

private val responses = mutableMapOf<String, OpaResponse>()
private val responses by resettable { mutableMapOf<String, OpaResponse>() }
fun addOverride(document: String, obj: OpaResponse) {
responses[document] = obj
}

private val responsesForJsonInput = mutableMapOf<String, MutableMap<String,OpaResponse>>()
private val responsesForJsonInput by resettable { mutableMapOf<String, MutableMap<String, OpaResponse>>() }
fun addOverrideForInput(document: String, key: String, obj: OpaResponse) {
if (responsesForJsonInput.containsKey(document)) {
responsesForJsonInput[document]?.put(key,obj)
responsesForJsonInput[document]?.put(key, obj)
} else {
responsesForJsonInput[document] = mutableMapOf(Pair(key, obj))
}
}

private val responsesForInput = mutableMapOf<String, MutableMap<OpaRequest,OpaResponse>>()
private val responsesForInput by resettable { mutableMapOf<String, MutableMap<OpaRequest, OpaResponse>>() }
fun addOverrideForInput(document: String, key: OpaRequest, obj: OpaResponse) {
if(responsesForInput.containsKey(document)) {
responsesForInput[document]?.put(key,obj)
if (responsesForInput.containsKey(document)) {
responsesForInput[document]?.put(key, obj)
} else {
responsesForInput[document] = mutableMapOf(Pair(key, obj))
}
Expand Down

0 comments on commit 13dcba0

Please sign in to comment.