11
11
import au .com .dius .pact .provider .junitsupport .loader .PactBroker ;
12
12
import au .com .dius .pact .provider .spring .junit5 .MockMvcTestTarget ;
13
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
14
import bio .terra .policy .model .TpsPaoConflict ;
17
- import bio .terra .policy .model .TpsPaoDescription ;
18
15
import bio .terra .policy .model .TpsPaoUpdateResult ;
19
16
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 ;
21
20
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 ;
23
24
import bio .terra .workspace .service .resource .referenced .model .ReferencedResource ;
24
25
import bio .terra .workspace .service .workspace .model .Workspace ;
25
26
import bio .terra .workspace .service .workspace .model .WorkspaceStage ;
26
27
import java .util .Map ;
27
28
import java .util .UUID ;
29
+ import javax .servlet .http .HttpServletRequest ;
28
30
import org .broadinstitute .dsde .workbench .client .sam .model .UserStatusInfo ;
29
31
import org .junit .jupiter .api .BeforeEach ;
30
32
import org .junit .jupiter .api .Tag ;
31
33
import org .junit .jupiter .api .TestTemplate ;
32
34
import org .junit .jupiter .api .extension .ExtendWith ;
33
35
import org .springframework .beans .factory .annotation .Autowired ;
34
36
import org .springframework .boot .test .mock .mockito .MockBean ;
37
+ import org .springframework .test .context .ActiveProfiles ;
35
38
import org .springframework .test .web .servlet .MockMvc ;
36
- import org .springframework .transaction .TransactionManager ;
37
39
40
+ // set ActiveProfile to "unit-test" to disable some unnecessary dependencies (like
41
+ // GoogleCredentials) that would otherwise require mocking
42
+ @ ActiveProfiles ("unit-test" )
38
43
@ Tag ("pact-verification" )
39
44
@ Provider ("workspacemanager" ) // should match the terra chart name
40
45
@ PactBroker ()
41
- public class WdsContractVerificationTest extends BaseAzureUnitTest {
46
+ public class WdsContractVerificationTest extends BaseUnitTestMocks {
42
47
@ Autowired private MockMvc mockMvc ;
43
48
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 ;
46
51
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 ;
56
56
57
57
@ TestTemplate
58
58
@ ExtendWith (PactVerificationSpringProvider .class )
@@ -68,47 +68,53 @@ void setPactVerificationContext(PactVerificationContext context) {
68
68
@ State ({"authenticated with the given email" })
69
69
public void authenticatedAsAccount (Map <?, ?> parameters ) throws InterruptedException {
70
70
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 );
71
81
72
82
// for recording the creator of the snapshot reference...
73
- when (mockSamService ()
74
- .getUserEmailFromSamAndRethrowOnInterrupt (any (AuthenticatedUserRequest .class )))
83
+ when (mockSamService ().getUserEmailFromSamAndRethrowOnInterrupt (eq (stubbedAuthenticatedRequest )))
75
84
.thenReturn (authenticatedEmail );
76
85
77
86
// for activity logging...
78
- when (mockSamService ().getUserStatusInfo (any ( AuthenticatedUserRequest . class )))
87
+ when (mockSamService ().getUserStatusInfo (eq ( stubbedAuthenticatedRequest )))
79
88
.thenReturn (new UserStatusInfo ().userEmail (authenticatedEmail ).userSubjectId ("subjectid" ));
80
89
}
81
90
82
91
@ State ({"a workspace with the given id exists" })
83
92
public void workspaceIdExists (Map <?, ?> parameters ) {
84
93
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 )))
88
95
.thenReturn (
89
96
Workspace .builder ()
90
97
.workspaceId (workspaceUuid )
91
98
.workspaceStage (WorkspaceStage .RAWLS_WORKSPACE )
92
99
.userFacingId ("workspaceid" )
93
100
.
createdByEmail (
"[email protected] " )
101
+ .state (WsmResourceState .READY )
94
102
.build ());
95
103
}
96
104
97
105
@ State ({"policies allowing snapshot reference creation" })
98
106
public void allowSnapshotReferenceCreation () throws InterruptedException {
99
107
when (mockFeatureConfiguration ().isTpsEnabled ()).thenReturn (true );
100
108
var policyUpdateSuccessful = new TpsPaoUpdateResult ().updateApplied (true );
101
- when (mockWorkspaceService ()
102
- .linkPolicies (
109
+ when (mockTpsApiDispatch ()
110
+ .linkPao (
103
111
/* 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 )))
107
114
.thenReturn (policyUpdateSuccessful );
108
115
109
- when (mockReferencedResourceService ()
110
- .createReferenceResource (
111
- any (ReferencedResource .class ), any (AuthenticatedUserRequest .class )))
116
+ when (mockReferencedResourceService .createReferenceResource (
117
+ any (ReferencedResource .class ), any (AuthenticatedUserRequest .class )))
112
118
// just echo back the resource that the controller attempted to create to simulate success
113
119
.thenAnswer (invocation -> invocation .getArgument (0 ));
114
120
}
@@ -120,12 +126,18 @@ public void preventSnapshotReferenceCreation() throws InterruptedException {
120
126
new TpsPaoUpdateResult ()
121
127
.updateApplied (false )
122
128
.addConflictsItem (new TpsPaoConflict ().name ("some conflict" ));
123
- when (mockWorkspaceService ()
124
- .linkPolicies (
129
+ when (mockTpsApiDispatch ()
130
+ .linkPao (
125
131
/* 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 )))
129
134
.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." ));
130
142
}
131
143
}
0 commit comments