|
| 1 | +package bio.terra.workspace.pact; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.any; |
| 4 | +import static org.mockito.ArgumentMatchers.anyString; |
| 5 | +import static org.mockito.ArgumentMatchers.eq; |
| 6 | +import static org.mockito.Mockito.when; |
| 7 | + |
| 8 | +import au.com.dius.pact.provider.junit5.PactVerificationContext; |
| 9 | +import au.com.dius.pact.provider.junitsupport.Provider; |
| 10 | +import au.com.dius.pact.provider.junitsupport.State; |
| 11 | +import au.com.dius.pact.provider.junitsupport.loader.PactBroker; |
| 12 | +import au.com.dius.pact.provider.spring.junit5.MockMvcTestTarget; |
| 13 | +import au.com.dius.pact.provider.spring.junit5.PactVerificationSpringProvider; |
| 14 | +import bio.terra.landingzone.db.LandingZoneDao; |
| 15 | +import bio.terra.landingzone.job.LandingZoneJobService; |
| 16 | +import bio.terra.policy.model.TpsPaoConflict; |
| 17 | +import bio.terra.policy.model.TpsPaoDescription; |
| 18 | +import bio.terra.policy.model.TpsPaoUpdateResult; |
| 19 | +import bio.terra.policy.model.TpsUpdateMode; |
| 20 | +import bio.terra.workspace.common.BaseAzureUnitTest; |
| 21 | +import bio.terra.workspace.service.iam.AuthenticatedUserRequest; |
| 22 | +import bio.terra.workspace.service.job.JobService; |
| 23 | +import bio.terra.workspace.service.resource.referenced.model.ReferencedResource; |
| 24 | +import bio.terra.workspace.service.workspace.model.Workspace; |
| 25 | +import bio.terra.workspace.service.workspace.model.WorkspaceStage; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.UUID; |
| 28 | +import org.broadinstitute.dsde.workbench.client.sam.model.UserStatusInfo; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Tag; |
| 31 | +import org.junit.jupiter.api.TestTemplate; |
| 32 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 35 | +import org.springframework.test.web.servlet.MockMvc; |
| 36 | +import org.springframework.transaction.TransactionManager; |
| 37 | + |
| 38 | +@Tag("pact-verification") |
| 39 | +@Provider("terra-workspace-manager") |
| 40 | +@PactBroker() |
| 41 | +public class WdsContractVerificationTest extends BaseAzureUnitTest { |
| 42 | + @Autowired private MockMvc mockMvc; |
| 43 | + |
| 44 | + // this block of @MockBeans sprouted up as a result of playing whackamole with failed dependency |
| 45 | + // injection errors, mostly due to NPEs through by `DataSourceInitializer` |
| 46 | + @MockBean |
| 47 | + private LandingZoneDao |
| 48 | + mockLandingZoneDao; // required for dependency chain up to azureConnectedTestUtils |
| 49 | + |
| 50 | + @MockBean(name = "tlzTransactionManager") |
| 51 | + private TransactionManager |
| 52 | + mockTlzTransactionManager; // this bean fails on missing datasource otherwise |
| 53 | + |
| 54 | + @MockBean private JobService mockJobService; // for JobService.initialize call on startup |
| 55 | + |
| 56 | + @MockBean |
| 57 | + private LandingZoneJobService mockLandingZoneJobService; // for LandingZone.main call on startup |
| 58 | + |
| 59 | + @TestTemplate |
| 60 | + @ExtendWith(PactVerificationSpringProvider.class) |
| 61 | + void pactVerificationTestTemplate(PactVerificationContext context) { |
| 62 | + context.verifyInteraction(); |
| 63 | + } |
| 64 | + |
| 65 | + @BeforeEach |
| 66 | + void setPactVerificationContext(PactVerificationContext context) { |
| 67 | + context.setTarget(new MockMvcTestTarget(mockMvc)); |
| 68 | + } |
| 69 | + |
| 70 | + @State({"authenticated with the given email"}) |
| 71 | + public void authenticatedAsAccount(Map<?, ?> parameters) throws InterruptedException { |
| 72 | + var authenticatedEmail = parameters.get("email").toString(); |
| 73 | + |
| 74 | + // for recording the creator of the snapshot reference... |
| 75 | + when(mockSamService() |
| 76 | + .getUserEmailFromSamAndRethrowOnInterrupt(any(AuthenticatedUserRequest.class))) |
| 77 | + .thenReturn(authenticatedEmail); |
| 78 | + |
| 79 | + // for activity logging... |
| 80 | + when(mockSamService().getUserStatusInfo(any(AuthenticatedUserRequest.class))) |
| 81 | + .thenReturn(new UserStatusInfo().userEmail(authenticatedEmail).userSubjectId("subjectid")); |
| 82 | + } |
| 83 | + |
| 84 | + @State({"a workspace with the given id exists"}) |
| 85 | + public void workspaceIdExists(Map<?, ?> parameters) { |
| 86 | + var workspaceUuid = UUID.fromString(parameters.get("id").toString()); |
| 87 | + when(mockWorkspaceService() |
| 88 | + .validateWorkspaceAndAction( |
| 89 | + any(AuthenticatedUserRequest.class), eq(workspaceUuid), /* action= */ anyString())) |
| 90 | + .thenReturn( |
| 91 | + Workspace.builder() |
| 92 | + .workspaceId(workspaceUuid) |
| 93 | + .workspaceStage(WorkspaceStage.RAWLS_WORKSPACE) |
| 94 | + .userFacingId("workspaceid") |
| 95 | + . createdByEmail( "[email protected]") |
| 96 | + .build()); |
| 97 | + } |
| 98 | + |
| 99 | + @State({"policies allowing snapshot reference creation"}) |
| 100 | + public void allowSnapshotReferenceCreation() throws InterruptedException { |
| 101 | + when(mockFeatureConfiguration().isTpsEnabled()).thenReturn(true); |
| 102 | + var policyUpdateSuccessful = new TpsPaoUpdateResult().updateApplied(true); |
| 103 | + when(mockWorkspaceService() |
| 104 | + .linkPolicies( |
| 105 | + /* workspaceUuid= */ any(UUID.class), |
| 106 | + any(TpsPaoDescription.class), |
| 107 | + any(TpsUpdateMode.class), |
| 108 | + any(AuthenticatedUserRequest.class))) |
| 109 | + .thenReturn(policyUpdateSuccessful); |
| 110 | + |
| 111 | + when(mockReferencedResourceService() |
| 112 | + .createReferenceResource( |
| 113 | + any(ReferencedResource.class), any(AuthenticatedUserRequest.class))) |
| 114 | + // just echo back the resource that the controller attempted to create to simulate success |
| 115 | + .thenAnswer(invocation -> invocation.getArgument(0)); |
| 116 | + } |
| 117 | + |
| 118 | + @State({"policies preventing snapshot reference creation"}) |
| 119 | + public void preventSnapshotReferenceCreation() throws InterruptedException { |
| 120 | + when(mockFeatureConfiguration().isTpsEnabled()).thenReturn(true); |
| 121 | + var policiesConflicting = |
| 122 | + new TpsPaoUpdateResult() |
| 123 | + .updateApplied(false) |
| 124 | + .addConflictsItem(new TpsPaoConflict().name("some conflict")); |
| 125 | + when(mockWorkspaceService() |
| 126 | + .linkPolicies( |
| 127 | + /* workspaceUuid= */ any(UUID.class), |
| 128 | + any(TpsPaoDescription.class), |
| 129 | + any(TpsUpdateMode.class), |
| 130 | + any(AuthenticatedUserRequest.class))) |
| 131 | + .thenReturn(policiesConflicting); |
| 132 | + } |
| 133 | +} |
0 commit comments