Skip to content

Commit

Permalink
TPS pact consumer tests - first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
samanehsan committed Jan 17, 2024
1 parent b64b537 commit 68ee2e1
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 0 deletions.
10 changes: 10 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,16 @@ task testAll(type: Test) {
outputs.upToDateWhen { false }
}

// PACT

task pactTests(type: Test) {
useJUnitPlatform {
includeTags "pact-test"
}
environment.put('pact.rootDir', "$buildDir/pacts")
environment.put('pact.provider.version', "$project.version")
}

task verifyPacts(type: Test) {
useJUnitPlatform {
includeTags 'bio.terra.common.category.Pact'
Expand Down
137 changes: 137 additions & 0 deletions src/test/java/bio/terra/pact/consumer/TpsPactTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package bio.terra.pact.consumer;

import static au.com.dius.pact.consumer.dsl.LambdaDsl.newJsonBody;
import static org.junit.jupiter.api.Assertions.assertThrows;

import au.com.dius.pact.consumer.MockServer;
import au.com.dius.pact.consumer.dsl.PactDslWithProvider;
import au.com.dius.pact.consumer.junit5.PactConsumerTest;
import au.com.dius.pact.consumer.junit5.PactConsumerTestExt;
import au.com.dius.pact.consumer.junit5.PactTestFor;
import au.com.dius.pact.core.model.PactSpecVersion;
import au.com.dius.pact.core.model.RequestResponsePact;
import au.com.dius.pact.core.model.annotations.Pact;
import bio.terra.app.configuration.PolicyServiceConfiguration;
import bio.terra.policy.api.TpsApi;
import bio.terra.policy.client.ApiException;
import bio.terra.policy.model.TpsComponent;
import bio.terra.policy.model.TpsObjectType;
import bio.terra.policy.model.TpsPaoCreateRequest;
import bio.terra.policy.model.TpsPolicyInput;
import bio.terra.policy.model.TpsPolicyInputs;
import bio.terra.service.policy.PolicyApiService;
import bio.terra.service.policy.PolicyService;
import java.util.Map;
import java.util.UUID;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.ActiveProfiles;

@Tag("pact-test")
// @Tag(bio.terra.common.category.Pact.TAG)
@PactConsumerTest
@ActiveProfiles(bio.terra.common.category.Pact.PROFILE)
@ExtendWith(PactConsumerTestExt.class)
@PactTestFor(providerName = "tps", pactVersion = PactSpecVersion.V3)
class TpsPactTest {

private PolicyServiceConfiguration policyServiceConfiguration;
private PolicyApiService policyApiService;
private final UUID snapshotId = UUID.randomUUID();
private final TpsPolicyInput protectedDataPolicy =
new TpsPolicyInput()
.namespace(PolicyService.POLICY_NAMESPACE)
.name(PolicyService.PROTECTED_DATA_POLICY_NAME);
private final TpsPolicyInputs policies = new TpsPolicyInputs().addInputsItem(protectedDataPolicy);

static Map<String, String> contentTypeJsonHeader = Map.of("Content-Type", "application/json");

@Pact(consumer = "datarepo")
RequestResponsePact createPao(PactDslWithProvider builder) {
String snapshotId = UUID.randomUUID().toString();
return builder
.given("default")
.uponReceiving("create PAO with ID <uuid>")
.method("POST")
.path("/api/policy/v1alpha1/pao")
.body(
newJsonBody(
object -> {
object.stringType("objectId", snapshotId);
object.stringType("component", TpsComponent.TDR.getValue());
object.stringType("objectType", TpsObjectType.SNAPSHOT.getValue());
object.object(
"attributes",
(attributes) ->
attributes.array(
"inputs",
(inputs) ->
inputs.object(
i -> {
i.stringType(
"namespace", PolicyService.POLICY_NAMESPACE);
i.stringType(
"name", PolicyService.PROTECTED_DATA_POLICY_NAME);
})));
})
.build())
.headers(contentTypeJsonHeader)
.willRespondWith()
.status(204)
.toPact();
}

@Pact(consumer = "datarepo")
RequestResponsePact deletePaoThatDoesNotExist(PactDslWithProvider builder) {
// String nonExistentPolicyId = UUID.randomUUID().toString();
return builder
.given("default")
.uponReceiving("create PAO with ID <uuid>")
.method("DELETE")
.path("/api/policy/v1alpha1/pao/" + snapshotId)
.willRespondWith()
.status(404)
.toPact();
}

// create snapshot protected data policy - success
@Test
@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)
.component(TpsComponent.TDR)
.objectType(TpsObjectType.SNAPSHOT)
.attributes(policies));
}

// create snapshot group policy - success

// create snapshot policy -- conflict error

// update existing policy (already protected data, update with group policy)
// state requires policy with this id to already exist

// delete a policy (exists - requires TPS state to have this policy)

// delete a policy (does not exist)
@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),
"nonexistent policy should return 404");
}
}

0 comments on commit 68ee2e1

Please sign in to comment.