Skip to content

Commit

Permalink
Change PolicyServiceConfiguration to class
Browse files Browse the repository at this point in the history
This could not be mocked as a record
  • Loading branch information
samanehsan committed Jan 17, 2024
1 parent 938cd9a commit 18eb5db
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 13 additions & 11 deletions src/test/java/bio/terra/pact/consumer/TpsPactTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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()
Expand All @@ -47,6 +49,15 @@ class TpsPactTest {

static Map<String, String> 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();
Expand Down Expand Up @@ -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 <uuid>")
Expand All @@ -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)
Expand All @@ -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),
Expand Down

0 comments on commit 18eb5db

Please sign in to comment.