Skip to content

Commit

Permalink
PI-1859 update flip client
Browse files Browse the repository at this point in the history
  • Loading branch information
anthony-britton-moj committed Feb 7, 2024
1 parent a35c18d commit a1c5edd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
4 changes: 3 additions & 1 deletion libs/commons/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation(libs.flipt)
implementation(libs.flipt) {
exclude("org.apache.httpcomponents", "httpclient")
}

testImplementation("org.springframework.boot:spring-boot-starter-data-ldap")
testImplementation("org.springframework.boot:spring-boot-starter-test")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package uk.gov.justice.digital.hmpps.config

import com.flipt.api.FliptApiClient
import com.flipt.api.core.BearerAuth
import com.flipt.api.core.Environment
import io.flipt.api.FliptClient
import io.flipt.api.authentication.ClientTokenAuthenticationStrategy
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.context.annotation.Bean
Expand All @@ -15,5 +14,6 @@ class FliptConfig(
@Value("\${flipt.token}") private val token: String
) {
@Bean
fun fliptApiClient() = FliptApiClient(Environment.custom(url), BearerAuth.of(token))
fun fliptApiClient(): FliptClient =
FliptClient.builder().url(url).authentication(ClientTokenAuthenticationStrategy(token)).build()
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package uk.gov.justice.digital.hmpps.flags

import com.flipt.api.FliptApiClient
import com.flipt.api.client.flags.endpoints.Get
import io.flipt.api.FliptClient
import io.flipt.api.evaluation.models.EvaluationRequest
import org.springframework.stereotype.Service

@Service
class FeatureFlags(
private val client: FliptApiClient?
private val client: FliptClient?
) {
fun enabled(key: String) = try {
client == null || client.flags()[Get.Request.builder().key(key).build()].enabled
client == null || client.evaluation().evaluateBoolean(EvaluationRequest.builder().flagKey(key).build()).isEnabled
} catch (e: Exception) {
throw FeatureFlagException(key, e)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package uk.gov.justice.digital.hmpps.flags

import com.flipt.api.FliptApiClient
import com.flipt.api.client.flags.FlagsClient
import com.flipt.api.client.flags.endpoints.Get
import com.flipt.api.client.flags.exceptions.GetException
import com.flipt.api.client.flags.types.Flag
import io.flipt.api.FliptClient
import io.flipt.api.error.Error
import io.flipt.api.evaluation.Evaluation
import io.flipt.api.evaluation.models.BooleanEvaluationResponse
import io.flipt.api.evaluation.models.EvaluationReason
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
Expand All @@ -14,24 +14,25 @@ import org.junit.jupiter.api.extension.ExtendWith
import org.mockito.InjectMocks
import org.mockito.Mock
import org.mockito.junit.jupiter.MockitoExtension
import org.mockito.kotlin.any
import org.mockito.kotlin.whenever
import uk.gov.justice.digital.hmpps.flags.FeatureFlags.FeatureFlagException
import java.time.ZonedDateTime
import java.time.LocalTime

@ExtendWith(MockitoExtension::class)
class FeatureFlagsTest {
@Mock
private lateinit var fliptApiClient: FliptApiClient
private lateinit var fliptClient: FliptClient

@Mock
private lateinit var flagsClient: FlagsClient
private lateinit var evaluation: Evaluation

@InjectMocks
private lateinit var featureFlags: FeatureFlags

@BeforeEach
fun setup() {
whenever(fliptApiClient.flags()).thenReturn(flagsClient)
whenever(fliptClient.evaluation()).thenReturn(evaluation)
}

@Test
Expand All @@ -48,21 +49,19 @@ class FeatureFlagsTest {

@Test
fun `throws error if feature flag is not defined`() {
whenever(flagsClient.get(Get.Request.builder().key("feature-flag-3").build()))
.thenThrow(GetException.other("Not found", 404))
whenever(evaluation.evaluateBoolean(any())).thenThrow(RuntimeException(Error(404, "Not Found")))
assertThrows<FeatureFlagException> { featureFlags.enabled("feature-flag-3") }
}

private fun withFlag(key: String, enabled: Boolean) {
whenever(flagsClient.get(Get.Request.builder().key(key).build())).thenReturn(flag(key, enabled))
whenever(evaluation.evaluateBoolean(any())).thenReturn(flag(key, enabled))
}

private fun flag(key: String, enabled: Boolean) = Flag.builder()
.key(key)
.name("Name")
.description("Description")
.enabled(enabled)
.createdAt(ZonedDateTime.now().toString())
.updatedAt(ZonedDateTime.now().toString())
.build()
private fun flag(key: String, enabled: Boolean) = BooleanEvaluationResponse(
enabled,
key,
EvaluationReason.MATCH_EVALUATION_REASON,
100F,
LocalTime.now().toString()
)
}
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ dependencyResolutionManagement {
library("wiremock", "com.github.tomakehurst:wiremock-jre8-standalone:3.0.1")
library("mapstruct", "org.mapstruct:mapstruct:1.5.5.Final")
library("mapstructprocessor", "org.mapstruct:mapstruct-processor:1.5.5.Final")
library("flipt", "io.flipt:flipt-java:0.1.8")
library("flipt", "io.flipt:flipt-java:1.0.1")
}
}
}
Expand Down

0 comments on commit a1c5edd

Please sign in to comment.