-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TPS pact consumer tests - first pass
- Loading branch information
1 parent
b64b537
commit 68ee2e1
Showing
2 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |