From 18eb5db134ecac28efb175641ba711b078ff4650 Mon Sep 17 00:00:00 2001 From: Saman Ehsan Date: Tue, 16 Jan 2024 20:01:01 -0500 Subject: [PATCH] Change PolicyServiceConfiguration to class This could not be mocked as a record --- .../PolicyServiceConfiguration.java | 15 +++++++++++- .../service/policy/PolicyApiService.java | 2 +- .../bio/terra/pact/consumer/TpsPactTest.java | 24 ++++++++++--------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/src/main/java/bio/terra/app/configuration/PolicyServiceConfiguration.java b/src/main/java/bio/terra/app/configuration/PolicyServiceConfiguration.java index 9cca5f6570..1cce404b32 100644 --- a/src/main/java/bio/terra/app/configuration/PolicyServiceConfiguration.java +++ b/src/main/java/bio/terra/app/configuration/PolicyServiceConfiguration.java @@ -5,12 +5,25 @@ import java.io.IOException; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; /** Configuration for managing connection to Terra Policy Service. * */ +@Configuration +@EnableConfigurationProperties @ConfigurationProperties(prefix = "tps") -public record PolicyServiceConfiguration(String basePath) { +public class PolicyServiceConfiguration { private static final List POLICY_SCOPES = List.of("openid", "email", "profile"); + private String basePath; + + public String getBasePath() { + return basePath; + } + + public void setBasePath(String basePath) { + this.basePath = basePath; + } public String getAccessToken() throws IOException { GoogleCredentials credentials = diff --git a/src/main/java/bio/terra/service/policy/PolicyApiService.java b/src/main/java/bio/terra/service/policy/PolicyApiService.java index b7b22de288..34e92123b3 100644 --- a/src/main/java/bio/terra/service/policy/PolicyApiService.java +++ b/src/main/java/bio/terra/service/policy/PolicyApiService.java @@ -25,7 +25,7 @@ public PolicyApiService(PolicyServiceConfiguration policyServiceConfiguration) { private ApiClient getApiClient() { return new ApiClient() .setHttpClient(sharedHttpClient) - .setBasePath(policyServiceConfiguration.basePath()); + .setBasePath(policyServiceConfiguration.getBasePath()); } private ApiClient getApiClient(String accessToken) { diff --git a/src/test/java/bio/terra/pact/consumer/TpsPactTest.java b/src/test/java/bio/terra/pact/consumer/TpsPactTest.java index 5b6768a6a4..b19a19197a 100644 --- a/src/test/java/bio/terra/pact/consumer/TpsPactTest.java +++ b/src/test/java/bio/terra/pact/consumer/TpsPactTest.java @@ -2,6 +2,8 @@ import static au.com.dius.pact.consumer.dsl.LambdaDsl.newJsonBody; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import au.com.dius.pact.consumer.MockServer; import au.com.dius.pact.consumer.dsl.PactDslWithProvider; @@ -23,6 +25,7 @@ import bio.terra.service.policy.PolicyService; import java.util.Map; import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -36,8 +39,7 @@ @PactTestFor(providerName = "tps", pactVersion = PactSpecVersion.V3) class TpsPactTest { - private PolicyServiceConfiguration policyServiceConfiguration; - private PolicyApiService policyApiService; + private TpsApi tps; private final UUID snapshotId = UUID.randomUUID(); private final TpsPolicyInput protectedDataPolicy = new TpsPolicyInput() @@ -47,6 +49,15 @@ class TpsPactTest { static Map contentTypeJsonHeader = Map.of("Content-Type", "application/json"); + @BeforeEach + void setup(MockServer mockServer) throws Exception { + var tpsConfig = mock(PolicyServiceConfiguration.class); + when(tpsConfig.getAccessToken()).thenReturn("dummyToken"); + when(tpsConfig.getBasePath()).thenReturn(mockServer.getUrl()); + PolicyApiService policyApiService = new PolicyApiService(tpsConfig); + tps = policyApiService.getPolicyApi(); + } + @Pact(consumer = "datarepo") RequestResponsePact createPao(PactDslWithProvider builder) { String snapshotId = UUID.randomUUID().toString(); @@ -84,7 +95,6 @@ RequestResponsePact createPao(PactDslWithProvider builder) { @Pact(consumer = "datarepo") RequestResponsePact deletePaoThatDoesNotExist(PactDslWithProvider builder) { - // String nonExistentPolicyId = UUID.randomUUID().toString(); return builder .given("default") .uponReceiving("create PAO with ID ") @@ -100,10 +110,6 @@ RequestResponsePact deletePaoThatDoesNotExist(PactDslWithProvider builder) { @PactTestFor(pactMethod = "createPao") void createPaoSuccess(MockServer mockServer) throws ApiException { // call createPao with the snapshot id - policyServiceConfiguration = new PolicyServiceConfiguration(mockServer.getUrl()); - // when(policyServiceConfiguration.basePath()).thenReturn(mockServer.getUrl()); - policyApiService = new PolicyApiService(policyServiceConfiguration); - TpsApi tps = policyApiService.getPolicyApi(); tps.createPao( new TpsPaoCreateRequest() .objectId(snapshotId) @@ -125,10 +131,6 @@ void createPaoSuccess(MockServer mockServer) throws ApiException { @Test @PactTestFor(pactMethod = "deletePaoThatDoesNotExist") void deletePaoThatDoesNotExist(MockServer mockServer) { - policyServiceConfiguration = new PolicyServiceConfiguration(mockServer.getUrl()); - // when(policyServiceConfiguration.basePath()).thenReturn(mockServer.getUrl()); - policyApiService = new PolicyApiService(policyServiceConfiguration); - TpsApi tps = policyApiService.getPolicyApi(); assertThrows( ApiException.class, () -> tps.deletePao(snapshotId),