Skip to content

Commit 49705f6

Browse files
committed
Mock one layer lower.
This will allow the `@State` to better track stateful behavior, and hopefully prevent future collision/disagreement between stateful setups that might happen when mocking at the service level.
1 parent 1fffefb commit 49705f6

File tree

3 files changed

+51
-39
lines changed

3 files changed

+51
-39
lines changed

.github/workflows/verify_consumer_pacts.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ name: Verify consumer pacts
1717
# They are managed by Atlantis and were added to Terraform here:
1818
# https://github.com/broadinstitute/terraform-ap-deployments/pull/1086
1919
env:
20-
PACT_BROKER_URL: pact-broker.dsp-eng-tools.broadinstitute.org
20+
PACT_BROKER_URL: https://pact-broker.dsp-eng-tools.broadinstitute.org
2121
spring_profiles_active: human-readable-logging
2222
on:
2323
pull_request:

service/src/test/java/bio/terra/workspace/pact/WdsContractVerificationTest.java

+49-37
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,48 @@
1111
import au.com.dius.pact.provider.junitsupport.loader.PactBroker;
1212
import au.com.dius.pact.provider.spring.junit5.MockMvcTestTarget;
1313
import au.com.dius.pact.provider.spring.junit5.PactVerificationSpringProvider;
14-
import bio.terra.landingzone.db.LandingZoneDao;
15-
import bio.terra.landingzone.job.LandingZoneJobService;
1614
import bio.terra.policy.model.TpsPaoConflict;
17-
import bio.terra.policy.model.TpsPaoDescription;
1815
import bio.terra.policy.model.TpsPaoUpdateResult;
1916
import bio.terra.policy.model.TpsUpdateMode;
20-
import bio.terra.workspace.common.BaseAzureUnitTest;
17+
import bio.terra.workspace.common.BaseUnitTestMocks;
18+
import bio.terra.workspace.db.WorkspaceDao;
19+
import bio.terra.workspace.service.danglingresource.DanglingResourceCleanupService;
2120
import bio.terra.workspace.service.iam.AuthenticatedUserRequest;
22-
import bio.terra.workspace.service.job.JobService;
21+
import bio.terra.workspace.service.iam.AuthenticatedUserRequestFactory;
22+
import bio.terra.workspace.service.resource.model.WsmResourceState;
23+
import bio.terra.workspace.service.resource.referenced.ReferencedResourceService;
2324
import bio.terra.workspace.service.resource.referenced.model.ReferencedResource;
2425
import bio.terra.workspace.service.workspace.model.Workspace;
2526
import bio.terra.workspace.service.workspace.model.WorkspaceStage;
2627
import java.util.Map;
2728
import java.util.UUID;
29+
import javax.servlet.http.HttpServletRequest;
2830
import org.broadinstitute.dsde.workbench.client.sam.model.UserStatusInfo;
2931
import org.junit.jupiter.api.BeforeEach;
3032
import org.junit.jupiter.api.Tag;
3133
import org.junit.jupiter.api.TestTemplate;
3234
import org.junit.jupiter.api.extension.ExtendWith;
3335
import org.springframework.beans.factory.annotation.Autowired;
3436
import org.springframework.boot.test.mock.mockito.MockBean;
37+
import org.springframework.test.context.ActiveProfiles;
3538
import org.springframework.test.web.servlet.MockMvc;
36-
import org.springframework.transaction.TransactionManager;
3739

40+
// set ActiveProfile to "unit-test" to disable some unnecessary dependencies (like
41+
// GoogleCredentials) that would otherwise require mocking
42+
@ActiveProfiles("unit-test")
3843
@Tag("pact-verification")
3944
@Provider("workspacemanager") // should match the terra chart name
4045
@PactBroker()
41-
public class WdsContractVerificationTest extends BaseAzureUnitTest {
46+
public class WdsContractVerificationTest extends BaseUnitTestMocks {
4247
@Autowired private MockMvc mockMvc;
4348

44-
// required for dependency chain via azureConnectedTestUtils
45-
@MockBean private LandingZoneDao mockLandingZoneDao;
49+
// Mocked out to prevent an error with missing service credentials
50+
@MockBean private DanglingResourceCleanupService unusedDanglingResourceCleanupService;
4651

47-
// datasource not configured in this test runtime, so mock out the transaction manager
48-
@MockBean(name = "tlzTransactionManager")
49-
private TransactionManager mockTlzTransactionManager;
50-
51-
// for JobService.initialize call on startup
52-
@MockBean private JobService mockJobService;
53-
54-
// for LandingZone.main call on startup
55-
@MockBean private LandingZoneJobService mockLandingZoneJobService;
52+
// needed to mock behavior in this test
53+
@MockBean private WorkspaceDao mockWorkspaceDao;
54+
@MockBean private AuthenticatedUserRequestFactory mockAuthenticatedUserRequestFactory;
55+
@MockBean private ReferencedResourceService mockReferencedResourceService;
5656

5757
@TestTemplate
5858
@ExtendWith(PactVerificationSpringProvider.class)
@@ -68,47 +68,53 @@ void setPactVerificationContext(PactVerificationContext context) {
6868
@State({"authenticated with the given email"})
6969
public void authenticatedAsAccount(Map<?, ?> parameters) throws InterruptedException {
7070
var authenticatedEmail = parameters.get("email").toString();
71+
var stubbedAuthenticatedRequest = new AuthenticatedUserRequest().email(authenticatedEmail);
72+
when(mockAuthenticatedUserRequestFactory.from(any(HttpServletRequest.class)))
73+
.thenReturn(stubbedAuthenticatedRequest);
74+
when(mockSamService()
75+
.isAuthorized(
76+
eq(stubbedAuthenticatedRequest),
77+
/* iamResourceType= */ anyString(),
78+
/* resourceId= */ anyString(),
79+
/* action= */ anyString()))
80+
.thenReturn(true);
7181

7282
// for recording the creator of the snapshot reference...
73-
when(mockSamService()
74-
.getUserEmailFromSamAndRethrowOnInterrupt(any(AuthenticatedUserRequest.class)))
83+
when(mockSamService().getUserEmailFromSamAndRethrowOnInterrupt(eq(stubbedAuthenticatedRequest)))
7584
.thenReturn(authenticatedEmail);
7685

7786
// for activity logging...
78-
when(mockSamService().getUserStatusInfo(any(AuthenticatedUserRequest.class)))
87+
when(mockSamService().getUserStatusInfo(eq(stubbedAuthenticatedRequest)))
7988
.thenReturn(new UserStatusInfo().userEmail(authenticatedEmail).userSubjectId("subjectid"));
8089
}
8190

8291
@State({"a workspace with the given id exists"})
8392
public void workspaceIdExists(Map<?, ?> parameters) {
8493
var workspaceUuid = UUID.fromString(parameters.get("id").toString());
85-
when(mockWorkspaceService()
86-
.validateWorkspaceAndAction(
87-
any(AuthenticatedUserRequest.class), eq(workspaceUuid), /* action= */ anyString()))
94+
when(mockWorkspaceDao.getWorkspace(eq(workspaceUuid)))
8895
.thenReturn(
8996
Workspace.builder()
9097
.workspaceId(workspaceUuid)
9198
.workspaceStage(WorkspaceStage.RAWLS_WORKSPACE)
9299
.userFacingId("workspaceid")
93100
.createdByEmail("[email protected]")
101+
.state(WsmResourceState.READY)
94102
.build());
95103
}
96104

97105
@State({"policies allowing snapshot reference creation"})
98106
public void allowSnapshotReferenceCreation() throws InterruptedException {
99107
when(mockFeatureConfiguration().isTpsEnabled()).thenReturn(true);
100108
var policyUpdateSuccessful = new TpsPaoUpdateResult().updateApplied(true);
101-
when(mockWorkspaceService()
102-
.linkPolicies(
109+
when(mockTpsApiDispatch()
110+
.linkPao(
103111
/* workspaceUuid= */ any(UUID.class),
104-
any(TpsPaoDescription.class),
105-
any(TpsUpdateMode.class),
106-
any(AuthenticatedUserRequest.class)))
112+
/* sourceObjectId= */ any(UUID.class),
113+
any(TpsUpdateMode.class)))
107114
.thenReturn(policyUpdateSuccessful);
108115

109-
when(mockReferencedResourceService()
110-
.createReferenceResource(
111-
any(ReferencedResource.class), any(AuthenticatedUserRequest.class)))
116+
when(mockReferencedResourceService.createReferenceResource(
117+
any(ReferencedResource.class), any(AuthenticatedUserRequest.class)))
112118
// just echo back the resource that the controller attempted to create to simulate success
113119
.thenAnswer(invocation -> invocation.getArgument(0));
114120
}
@@ -120,12 +126,18 @@ public void preventSnapshotReferenceCreation() throws InterruptedException {
120126
new TpsPaoUpdateResult()
121127
.updateApplied(false)
122128
.addConflictsItem(new TpsPaoConflict().name("some conflict"));
123-
when(mockWorkspaceService()
124-
.linkPolicies(
129+
when(mockTpsApiDispatch()
130+
.linkPao(
125131
/* workspaceUuid= */ any(UUID.class),
126-
any(TpsPaoDescription.class),
127-
any(TpsUpdateMode.class),
128-
any(AuthenticatedUserRequest.class)))
132+
/* sourceObjectId= */ any(UUID.class),
133+
any(TpsUpdateMode.class)))
129134
.thenReturn(policiesConflicting);
135+
136+
when(mockReferencedResourceService.createReferenceResource(
137+
any(ReferencedResource.class), any(AuthenticatedUserRequest.class)))
138+
// just throw if an unexpected attempt is made to create a referenced resource
139+
.thenThrow(
140+
new RuntimeException(
141+
"mockTpsApiDispatch() should have prevented #createReferenceResource from being reached."));
130142
}
131143
}

service/src/test/resources/application-test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ landingzone:
6565
poolMaxIdle: 1
6666
uri: jdbc:${env.db.local-db}/landingzone_stairway_db
6767
pactbroker:
68-
url: https://pact-broker.dsp-eng-tools.broadinstitute.org:9292
68+
url: https://pact-broker.dsp-eng-tools.broadinstitute.org
6969
logging:
7070
level:
7171
au.com.dius.pact.core: debug

0 commit comments

Comments
 (0)