diff --git a/service/src/test/java/bio/terra/workspace/app/controller/ControlledGcpResourceApiControllerBqDatasetConnectedTest.java b/service/src/test/java/bio/terra/workspace/app/controller/ControlledGcpResourceApiControllerBqDatasetConnectedTest.java index a143ab1f10..25ef124908 100644 --- a/service/src/test/java/bio/terra/workspace/app/controller/ControlledGcpResourceApiControllerBqDatasetConnectedTest.java +++ b/service/src/test/java/bio/terra/workspace/app/controller/ControlledGcpResourceApiControllerBqDatasetConnectedTest.java @@ -24,6 +24,7 @@ import bio.terra.workspace.common.utils.TestUtils; import bio.terra.workspace.connected.UserAccessUtils; import bio.terra.workspace.generated.model.ApiAccessScope; +import bio.terra.workspace.generated.model.ApiCloneControlledGcpBigQueryDatasetResult; import bio.terra.workspace.generated.model.ApiCloningInstructionsEnum; import bio.terra.workspace.generated.model.ApiCloudPlatform; import bio.terra.workspace.generated.model.ApiErrorReport; @@ -291,13 +292,10 @@ public void update() throws Exception { public void update_throws409() throws Exception { var oldName = sourceResource.getMetadata().getName(); var newName = TestUtils.appendRandomNumber("newdatatableresourcename"); + AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest(); mockGcpApi.createReferencedBqDataset( - userAccessUtils.defaultUserAuthRequest(), - workspaceId, - newName, - projectId, - sourceDatasetName); + userRequest, workspaceId, newName, projectId, sourceDatasetName); mockMvcUtils.updateResource( ApiGcpBigQueryDatasetResource.class, @@ -306,14 +304,12 @@ public void update_throws409() throws Exception { sourceResource.getMetadata().getResourceId(), objectMapper.writeValueAsString( new ApiUpdateControlledGcpBigQueryDatasetRequestBody().name(newName)), - userAccessUtils.defaultUserAuthRequest(), + userRequest, HttpStatus.SC_CONFLICT); ApiGcpBigQueryDatasetResource gotResource = mockGcpApi.getControlledBqDataset( - userAccessUtils.defaultUserAuthRequest(), - workspaceId, - sourceResource.getMetadata().getResourceId()); + userRequest, workspaceId, sourceResource.getMetadata().getResourceId()); assertEquals(oldName, gotResource.getMetadata().getName()); } @@ -373,8 +369,9 @@ public void clone_userWithWriteAccessOnDestWorkspace_succeeds() throws Exception var destResourceName = TestUtils.appendRandomNumber("clonedbq"); AuthenticatedUserRequest userRequest = userAccessUtils.secondUser().getAuthenticatedRequest(); logger.info(">>Cloning user is {}", userRequest.getEmail()); + ApiGcpBigQueryDatasetResource clonedBqDataset = - mockGcpApi.cloneControlledBqDataset( + mockGcpApi.cloneControlledBqDatasetAndWait( userRequest, workspaceId2, source2Resource.getMetadata().getResourceId(), @@ -385,7 +382,6 @@ public void clone_userWithWriteAccessOnDestWorkspace_succeeds() throws Exception /*destLocation=*/ null, // should get location from source resource /*defaultTableLifetime=*/ null, /*defaultPartitionLifetime=*/ null); - assertClonedControlledBqDataset( clonedBqDataset, workspaceId, @@ -409,16 +405,24 @@ public void clone_userWithWriteAccessOnDestWorkspace_succeeds() throws Exception // Tests getUniquenessCheckAttributes() works @Test void clone_duplicateDatasetName_jobThrows409() throws Exception { - ApiErrorReport errorReport = - mockGcpApi.cloneControlledBqDataset_jobError( - userAccessUtils.defaultUserAuthRequest(), + AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest(); + ApiCloneControlledGcpBigQueryDatasetResult result = + mockGcpApi.cloneControlledBqDatasetAsync( + userRequest, /*sourceWorkspaceId=*/ workspaceId, sourceResource.getMetadata().getResourceId(), /*destWorkspaceId=*/ workspaceId, ApiCloningInstructionsEnum.RESOURCE, /*destResourceName=*/ null, /*destDatasetName=*/ sourceDatasetName, - HttpStatus.SC_CONFLICT); + /*destLocation=*/ null, + /*defaultTableLifetime=*/ null, + /*defaultPartitionLifetime=*/ null, + List.of(HttpStatus.SC_ACCEPTED), + /*shouldUndo=*/ false); + ApiErrorReport errorReport = + mockGcpApi.cloneControlledBqDatasetAndExpectError( + userRequest, workspaceId, result.getJobReport().getId(), HttpStatus.SC_CONFLICT); assertThat( errorReport.getMessage(), equalTo("A resource with matching attributes already exists")); } @@ -426,9 +430,11 @@ void clone_duplicateDatasetName_jobThrows409() throws Exception { @Test void clone_copyNothing() throws Exception { String destResourceName = TestUtils.appendRandomNumber("dest-resource-name"); + AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest(); + ApiGcpBigQueryDatasetResource clonedResource = mockGcpApi.cloneControlledBqDataset( - userAccessUtils.defaultUserAuthRequest(), + userRequest, /*sourceWorkspaceId=*/ workspaceId, sourceResource.getMetadata().getResourceId(), /*destWorkspaceId=*/ workspaceId, @@ -440,12 +446,12 @@ void clone_copyNothing() throws Exception { assertNull(clonedResource); // Assert clone doesn't exist. There's no resource ID, so search on resource name. - mockMvcUtils.assertNoResourceWithName( - userAccessUtils.defaultUserAuthRequest(), workspaceId, destResourceName); + mockMvcUtils.assertNoResourceWithName(userRequest, workspaceId, destResourceName); } @Test void clone_copyDefinition() throws Exception { + AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest(); // Source resource is in us-west4 // Clone resource to EUROPE_LOCATION @@ -454,8 +460,8 @@ void clone_copyDefinition() throws Exception { Long defaultPartitionLifetime = 6201L; String destResourceName = TestUtils.appendRandomNumber("dest-resource-name"); ApiGcpBigQueryDatasetResource clonedResource = - mockGcpApi.cloneControlledBqDataset( - userAccessUtils.defaultUserAuthRequest(), + mockGcpApi.cloneControlledBqDatasetAndWait( + userRequest, /*sourceWorkspaceId=*/ workspaceId, sourceResource.getMetadata().getResourceId(), /*destWorkspaceId=*/ workspaceId2, @@ -478,21 +484,17 @@ void clone_copyDefinition() throws Exception { /*expectedCreatedBy=*/ userAccessUtils.getDefaultUserEmail(), workspaceId, sourceResource.getMetadata().getResourceId(), - userAccessUtils.defaultUserAuthRequest()); + userRequest); // Assert resource returned by get ApiGcpBigQueryDatasetResource gotResource = mockGcpApi.getControlledBqDataset( - userAccessUtils.defaultUserAuthRequest(), - workspaceId2, - clonedResource.getMetadata().getResourceId()); + userRequest, workspaceId2, clonedResource.getMetadata().getResourceId()); assertApiBqDatasetEquals(clonedResource, gotResource); // Call GCP directly cloudUtils.assertDatasetHasNoTables( - userAccessUtils.defaultUserAuthRequest(), - projectId2, - gotResource.getAttributes().getDatasetId()); + userRequest, projectId2, gotResource.getAttributes().getDatasetId()); assertBqDatasetAttributes( projectId2, gotResource.getAttributes().getDatasetId(), @@ -500,7 +502,7 @@ void clone_copyDefinition() throws Exception { DEFAULT_TABLE_LIFETIME, DEFAULT_PARTITION_LIFETIME); mockGcpApi.deleteBqDataset( - userAccessUtils.defaultUserAuthRequest(), + userRequest, workspaceId2, gotResource.getMetadata().getResourceId(), /*isControlled=*/ StewardshipType.REFERENCED); @@ -508,6 +510,7 @@ void clone_copyDefinition() throws Exception { @Test void clone_copyResource_sameWorkspace() throws Exception { + AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest(); // Source resource is in us-west4 // Clone resource to europe location @@ -517,8 +520,8 @@ void clone_copyResource_sameWorkspace() throws Exception { String destResourceName = TestUtils.appendRandomNumber("dest-resource-name"); String destDatasetName = TestUtils.appendRandomNumber("dest-dataset-name"); ApiGcpBigQueryDatasetResource clonedResource = - mockGcpApi.cloneControlledBqDataset( - userAccessUtils.defaultUserAuthRequest(), + mockGcpApi.cloneControlledBqDatasetAndWait( + userRequest, /*sourceWorkspaceId=*/ workspaceId, sourceResource.getMetadata().getResourceId(), /*destWorkspaceId=*/ workspaceId, @@ -541,14 +544,12 @@ void clone_copyResource_sameWorkspace() throws Exception { /*expectedCreatedBy=*/ userAccessUtils.getDefaultUserEmail(), workspaceId, sourceResource.getMetadata().getResourceId(), - userAccessUtils.defaultUserAuthRequest()); + userRequest); // Assert resource returned by get ApiGcpBigQueryDatasetResource gotResource = mockGcpApi.getControlledBqDataset( - userAccessUtils.defaultUserAuthRequest(), - workspaceId, - clonedResource.getMetadata().getResourceId()); + userRequest, workspaceId, clonedResource.getMetadata().getResourceId()); assertApiBqDatasetEquals(clonedResource, gotResource); // Call GCP directly @@ -566,6 +567,7 @@ void clone_copyResource_sameWorkspace() throws Exception { @Test void clone_copyResource_differentWorkspace() throws Exception { + AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest(); // Source resource is in us-west4 // Clone resource to europe location @@ -576,8 +578,8 @@ void clone_copyResource_differentWorkspace() throws Exception { String destDatasetName = TestUtils.appendRandomNumber("dest-dataset-name"); ApiGcpBigQueryDatasetResource clonedResource = - mockGcpApi.cloneControlledBqDataset( - userAccessUtils.defaultUserAuthRequest(), + mockGcpApi.cloneControlledBqDatasetAndWait( + userRequest, /*sourceWorkspaceId=*/ workspaceId, sourceResource.getMetadata().getResourceId(), /*destWorkspaceId=*/ workspaceId2, @@ -600,14 +602,12 @@ void clone_copyResource_differentWorkspace() throws Exception { userAccessUtils.getDefaultUserEmail(), workspaceId, sourceResource.getMetadata().getResourceId(), - userAccessUtils.defaultUserAuthRequest()); + userRequest); // Assert resource returned by get ApiGcpBigQueryDatasetResource gotResource = mockGcpApi.getControlledBqDataset( - userAccessUtils.defaultUserAuthRequest(), - workspaceId2, - clonedResource.getMetadata().getResourceId()); + userRequest, workspaceId2, clonedResource.getMetadata().getResourceId()); assertApiBqDatasetEquals(clonedResource, gotResource); // Call GCP directly @@ -625,13 +625,14 @@ void clone_copyResource_differentWorkspace() throws Exception { @Test void clone_copyReference() throws Exception { + AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest(); // Source resource is COPY_DEFINITION // Clone resource String destResourceName = TestUtils.appendRandomNumber("dest-resource-name"); ApiGcpBigQueryDatasetResource clonedResource = mockGcpApi.cloneControlledBqDataset( - userAccessUtils.defaultUserAuthRequest(), + userRequest, /*sourceWorkspaceId=*/ workspaceId, sourceResource.getMetadata().getResourceId(), /*destWorkspaceId=*/ workspaceId, @@ -654,14 +655,12 @@ void clone_copyReference() throws Exception { /*expectedCreatedBy=*/ userAccessUtils.getDefaultUserEmail(), workspaceId, sourceResource.getMetadata().getResourceId(), - userAccessUtils.defaultUserAuthRequest()); + userRequest); // Assert resource returned by get ApiGcpBigQueryDatasetResource gotResource = mockGcpApi.getReferencedBqDataset( - userAccessUtils.defaultUserAuthRequest(), - workspaceId, - clonedResource.getMetadata().getResourceId()); + userRequest, workspaceId, clonedResource.getMetadata().getResourceId()); assertApiBqDatasetEquals(clonedResource, gotResource); } @@ -714,11 +713,41 @@ void clone_policiesMerged() throws Exception { mockMvcUtils.deletePolicies(userRequest, workspaceId2); } + private void cloneControlledBqDataset_undo( + AuthenticatedUserRequest userRequest, + UUID sourceWorkspaceId, + UUID sourceResourceId, + UUID destWorkspaceId, + ApiCloningInstructionsEnum cloningInstructions, + String destResourceName) + throws Exception { + ApiCloneControlledGcpBigQueryDatasetResult result = + mockGcpApi.cloneControlledBqDatasetAsync( + userRequest, + sourceWorkspaceId, + sourceResourceId, + destWorkspaceId, + cloningInstructions, + destResourceName, + /*destDatasetName=*/ null, + /*destLocation=*/ null, + /*defaultTableLifetime=*/ null, + /*defaultPartitionLifetime=*/ null, + List.of(HttpStatus.SC_ACCEPTED), + /*shouldUndo=*/ true); + mockGcpApi.cloneControlledBqDatasetAndExpectError( + userRequest, + sourceWorkspaceId, + result.getJobReport().getId(), + HttpStatus.SC_INTERNAL_SERVER_ERROR); + } + @Test void clone_copyResource_undo() throws Exception { + AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest(); String destResourceName = TestUtils.appendRandomNumber("dest-resource-name"); - mockGcpApi.cloneControlledBqDataset_undo( - userAccessUtils.defaultUserAuthRequest(), + cloneControlledBqDataset_undo( + userRequest, /*sourceWorkspaceId=*/ workspaceId, sourceResource.getMetadata().getResourceId(), /*destWorkspaceId=*/ workspaceId2, @@ -726,15 +755,16 @@ void clone_copyResource_undo() throws Exception { destResourceName); // Assert clone doesn't exist. There's no resource ID, so search on resource name. - mockMvcUtils.assertNoResourceWithName( - userAccessUtils.defaultUserAuthRequest(), workspaceId2, destResourceName); + mockMvcUtils.assertNoResourceWithName(userRequest, workspaceId2, destResourceName); } @Test void clone_copyReference_undo() throws Exception { String destResourceName = TestUtils.appendRandomNumber("dest-resource-name"); - mockGcpApi.cloneControlledBqDataset_undo( - userAccessUtils.defaultUserAuthRequest(), + AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest(); + + cloneControlledBqDataset_undo( + userRequest, /*sourceWorkspaceId=*/ workspaceId, sourceResource.getMetadata().getResourceId(), /*destWorkspaceId=*/ workspaceId2, @@ -742,8 +772,7 @@ void clone_copyReference_undo() throws Exception { destResourceName); // Assert clone doesn't exist. There's no resource ID, so search on resource name. - mockMvcUtils.assertNoResourceWithName( - userAccessUtils.defaultUserAuthRequest(), workspaceId, destResourceName); + mockMvcUtils.assertNoResourceWithName(userRequest, workspaceId, destResourceName); } private void assertBqDataset( diff --git a/service/src/test/java/bio/terra/workspace/app/controller/ReferencedGcpResourceControllerBqDataTableConnectedTest.java b/service/src/test/java/bio/terra/workspace/app/controller/ReferencedGcpResourceControllerBqDataTableConnectedTest.java index e4cf137527..5c55803a41 100644 --- a/service/src/test/java/bio/terra/workspace/app/controller/ReferencedGcpResourceControllerBqDataTableConnectedTest.java +++ b/service/src/test/java/bio/terra/workspace/app/controller/ReferencedGcpResourceControllerBqDataTableConnectedTest.java @@ -2,7 +2,6 @@ import static bio.terra.workspace.common.fixtures.ControlledResourceFixtures.RESOURCE_DESCRIPTION; import static bio.terra.workspace.common.utils.MockGcpApi.REFERENCED_GCP_BQ_DATA_TABLE_PATH_FORMAT; -import static bio.terra.workspace.common.utils.MockGcpApi.assertApiBqDataTableEquals; import static bio.terra.workspace.common.utils.MockMvcUtils.assertResourceMetadata; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsInAnyOrder; @@ -212,7 +211,7 @@ public void update_throws409() throws Exception { @Test public void clone_requesterNoReadAccessOnSourceWorkspace_throws403() throws Exception { - mockGcpApi.cloneReferencedBqDataTable( + mockGcpApi.cloneReferencedBqDataTableAndExpect( userAccessUtils.secondUserAuthRequest(), /*sourceWorkspaceId=*/ workspaceId, /*sourceResourceId=*/ sourceResource.getMetadata().getResourceId(), @@ -235,7 +234,7 @@ public void clone_requesterNoWriteAccessOnDestWorkspace_throws403() throws Excep WsmIamRole.READER, userAccessUtils.getSecondUserEmail()); - mockGcpApi.cloneReferencedBqDataTable( + mockGcpApi.cloneReferencedBqDataTableAndExpect( userAccessUtils.secondUserAuthRequest(), /*sourceWorkspaceId=*/ workspaceId, /*sourceResourceId=*/ sourceResource.getMetadata().getResourceId(), @@ -500,4 +499,12 @@ private void assertClonedBqTable( assertEquals(expectedDatasetName, actualResource.getAttributes().getDatasetId()); assertEquals(expectedTableId, actualResource.getAttributes().getDataTableId()); } + + private static void assertApiBqDataTableEquals( + ApiGcpBigQueryDataTableResource expectedDataTable, + ApiGcpBigQueryDataTableResource actualDataTable) { + MockMvcUtils.assertResourceMetadataEquals( + expectedDataTable.getMetadata(), actualDataTable.getMetadata()); + assertEquals(expectedDataTable.getAttributes(), actualDataTable.getAttributes()); + } } diff --git a/service/src/test/java/bio/terra/workspace/app/controller/ReferencedGcpResourceControllerBqDatasetConnectedTest.java b/service/src/test/java/bio/terra/workspace/app/controller/ReferencedGcpResourceControllerBqDatasetConnectedTest.java index 2aaf9c795c..58f8bcfcde 100644 --- a/service/src/test/java/bio/terra/workspace/app/controller/ReferencedGcpResourceControllerBqDatasetConnectedTest.java +++ b/service/src/test/java/bio/terra/workspace/app/controller/ReferencedGcpResourceControllerBqDatasetConnectedTest.java @@ -205,7 +205,7 @@ public void update_throws409() throws Exception { @Test public void clone_requesterNoReadAccessOnSourceWorkspace_throws403() throws Exception { - mockGcpApi.cloneReferencedBqDataset( + mockGcpApi.cloneReferencedBqDatasetAndExpect( userAccessUtils.secondUserAuthRequest(), /*sourceWorkspaceId=*/ workspaceId, /*sourceResourceId=*/ sourceResource.getMetadata().getResourceId(), @@ -228,7 +228,7 @@ public void clone_requesterNoWriteAccessOnDestWorkspace_throws403() throws Excep WsmIamRole.READER, userAccessUtils.getSecondUserEmail()); - mockGcpApi.cloneReferencedBqDataset( + mockGcpApi.cloneReferencedBqDatasetAndExpect( userAccessUtils.secondUserAuthRequest(), /*sourceWorkspaceId=*/ workspaceId, /*sourceResourceId=*/ sourceResource.getMetadata().getResourceId(), diff --git a/service/src/test/java/bio/terra/workspace/app/controller/ReferencedGcpResourceControllerTest.java b/service/src/test/java/bio/terra/workspace/app/controller/ReferencedGcpResourceControllerTest.java index 79d09d2ea0..f05581a877 100644 --- a/service/src/test/java/bio/terra/workspace/app/controller/ReferencedGcpResourceControllerTest.java +++ b/service/src/test/java/bio/terra/workspace/app/controller/ReferencedGcpResourceControllerTest.java @@ -57,7 +57,6 @@ public void setUp() throws InterruptedException { @Test public void cloneReferencedDataRepoSnapshot_copyDefinition_throws400() throws Exception { UUID workspaceId = mockMvcUtils.createWorkspaceWithoutCloudContext(USER_REQUEST).getId(); - mockMvcUtils.cloneReferencedDataRepoSnapshot( USER_REQUEST, workspaceId, @@ -71,7 +70,6 @@ public void cloneReferencedDataRepoSnapshot_copyDefinition_throws400() throws Ex @Test public void cloneReferencedDataRepoSnapshot_copyResource_throws400() throws Exception { UUID workspaceId = mockMvcUtils.createWorkspaceWithoutCloudContext(USER_REQUEST).getId(); - mockMvcUtils.cloneReferencedDataRepoSnapshot( USER_REQUEST, workspaceId, @@ -85,8 +83,7 @@ public void cloneReferencedDataRepoSnapshot_copyResource_throws400() throws Exce @Test public void cloneReferencedBqDataset_copyDefinition_throws400() throws Exception { UUID workspaceId = mockMvcUtils.createWorkspaceWithoutCloudContext(USER_REQUEST).getId(); - - mockMvcUtils.cloneReferencedBqDataset( + mockGcpApi.cloneReferencedBqDatasetAndExpect( USER_REQUEST, workspaceId, /*sourceResourceId=*/ UUID.randomUUID(), @@ -99,8 +96,7 @@ public void cloneReferencedBqDataset_copyDefinition_throws400() throws Exception @Test public void cloneReferencedBqDataset_copyResource_throws400() throws Exception { UUID workspaceId = mockMvcUtils.createWorkspaceWithoutCloudContext(USER_REQUEST).getId(); - - mockMvcUtils.cloneReferencedBqDataset( + mockGcpApi.cloneReferencedBqDatasetAndExpect( USER_REQUEST, workspaceId, /*sourceResourceId=*/ UUID.randomUUID(), @@ -113,7 +109,7 @@ public void cloneReferencedBqDataset_copyResource_throws400() throws Exception { @Test public void cloneReferencedBqTable_copyDefinition_throws400() throws Exception { UUID workspaceId = mockMvcUtils.createWorkspaceWithoutCloudContext(USER_REQUEST).getId(); - mockGcpApi.cloneReferencedBqDataTable( + mockGcpApi.cloneReferencedBqDataTableAndExpect( USER_REQUEST, workspaceId, /*sourceResourceId=*/ UUID.randomUUID(), @@ -126,7 +122,7 @@ public void cloneReferencedBqTable_copyDefinition_throws400() throws Exception { @Test public void cloneReferencedBqTable_copyResource_throws400() throws Exception { UUID workspaceId = mockMvcUtils.createWorkspaceWithoutCloudContext(USER_REQUEST).getId(); - mockGcpApi.cloneReferencedBqDataTable( + mockGcpApi.cloneReferencedBqDataTableAndExpect( USER_REQUEST, workspaceId, /*sourceResourceId=*/ UUID.randomUUID(), diff --git a/service/src/test/java/bio/terra/workspace/common/utils/MockGcpApi.java b/service/src/test/java/bio/terra/workspace/common/utils/MockGcpApi.java index a0e3af0fa6..2daafd7c8e 100644 --- a/service/src/test/java/bio/terra/workspace/common/utils/MockGcpApi.java +++ b/service/src/test/java/bio/terra/workspace/common/utils/MockGcpApi.java @@ -233,7 +233,7 @@ public ApiGcpBigQueryDatasetResource cloneControlledBqDataset( @Nullable String destResourceName, @Nullable String destDatasetName) throws Exception { - return cloneControlledBqDataset( + return cloneControlledBqDatasetAndWait( userRequest, sourceWorkspaceId, sourceResourceId, @@ -246,8 +246,7 @@ public ApiGcpBigQueryDatasetResource cloneControlledBqDataset( /*defaultPartitionLifetime=*/ null); } - /** Call cloneBigQueryDataset() and wait for flight to finish. */ - public ApiGcpBigQueryDatasetResource cloneControlledBqDataset( + public ApiGcpBigQueryDatasetResource cloneControlledBqDatasetAndWait( AuthenticatedUserRequest userRequest, UUID sourceWorkspaceId, UUID sourceResourceId, @@ -275,10 +274,17 @@ public ApiGcpBigQueryDatasetResource cloneControlledBqDataset( // to avoid flakes. MockMvcUtils.JOB_SUCCESS_CODES, /*shouldUndo=*/ false); + String jobId = result.getJobReport().getId(); while (StairwayTestUtils.jobIsRunning(result.getJobReport())) { Thread.sleep(/*millis=*/ 5000); - result = getCloneControlledBqDatasetResult(userRequest, sourceWorkspaceId, jobId); + result = + mockMvcUtils.getCreateResourceJobResult( + ApiCloneControlledGcpBigQueryDatasetResult.class, + userRequest, + CLONE_RESULT_CONTROLLED_GCP_BQ_DATASETS_PATH_FORMAT, + sourceWorkspaceId, + jobId); } assertEquals(StatusEnum.SUCCEEDED, result.getJobReport().getStatus()); logger.info( @@ -286,64 +292,6 @@ public ApiGcpBigQueryDatasetResource cloneControlledBqDataset( return result.getDataset().getDataset(); } - /** Call cloneBigQueryDataset(), wait for flight to finish, return JobError. */ - public ApiErrorReport cloneControlledBqDataset_jobError( - AuthenticatedUserRequest userRequest, - UUID sourceWorkspaceId, - UUID sourceResourceId, - UUID destWorkspaceId, - ApiCloningInstructionsEnum cloningInstructions, - @Nullable String destResourceName, - @Nullable String destDatasetName, - int expectedCode) - throws Exception { - ApiCloneControlledGcpBigQueryDatasetResult result = - cloneControlledBqDatasetAsync( - userRequest, - sourceWorkspaceId, - sourceResourceId, - destWorkspaceId, - cloningInstructions, - destResourceName, - destDatasetName, - /*destLocation=*/ null, - /*defaultTableLifetime=*/ null, - /*defaultPartitionLifetime=*/ null, - List.of(HttpStatus.SC_ACCEPTED), - /*shouldUndo=*/ false); - return cloneControlledBqDataset_waitForJobError( - userRequest, sourceWorkspaceId, result.getJobReport().getId(), expectedCode); - } - - public void cloneControlledBqDataset_undo( - AuthenticatedUserRequest userRequest, - UUID sourceWorkspaceId, - UUID sourceResourceId, - UUID destWorkspaceId, - ApiCloningInstructionsEnum cloningInstructions, - String destResourceName) - throws Exception { - ApiCloneControlledGcpBigQueryDatasetResult result = - cloneControlledBqDatasetAsync( - userRequest, - sourceWorkspaceId, - sourceResourceId, - destWorkspaceId, - cloningInstructions, - destResourceName, - /*destDatasetName=*/ null, - /*destLocation=*/ null, - /*defaultTableLifetime=*/ null, - /*defaultPartitionLifetime=*/ null, - List.of(HttpStatus.SC_ACCEPTED), - /*shouldUndo=*/ true); - cloneControlledBqDataset_waitForJobError( - userRequest, - sourceWorkspaceId, - result.getJobReport().getId(), - HttpStatus.SC_INTERNAL_SERVER_ERROR); - } - /** Call cloneBigQueryDataset() and return immediately; don't wait for flight to finish. */ public ApiCloneControlledGcpBigQueryDatasetResult cloneControlledBqDatasetAsync( AuthenticatedUserRequest userRequest, @@ -416,14 +364,20 @@ public ApiCloneControlledGcpBigQueryDatasetResult cloneControlledBqDatasetAsync( serializedResponse, ApiCloneControlledGcpBigQueryDatasetResult.class); } - private ApiErrorReport cloneControlledBqDataset_waitForJobError( + public ApiErrorReport cloneControlledBqDatasetAndExpectError( AuthenticatedUserRequest userRequest, UUID workspaceId, String jobId, int expectedCode) throws Exception { // While job is running, cloneBigQueryDataset returns ApiCloneControlledGcpBigQueryDatasetResult // After job fails, cloneBigQueryDataset returns ApiCloneControlledGcpBigQueryDatasetResult OR // ApiErrorReport. ApiCloneControlledGcpBigQueryDatasetResult result = - getCloneControlledBqDatasetResult(userRequest, workspaceId, jobId); + mockMvcUtils.getCreateResourceJobResult( + ApiCloneControlledGcpBigQueryDatasetResult.class, + userRequest, + CLONE_RESULT_CONTROLLED_GCP_BQ_DATASETS_PATH_FORMAT, + workspaceId, + jobId); + ApiErrorReport errorReport; while (StairwayTestUtils.jobIsRunning(result.getJobReport())) { Thread.sleep(/*millis=*/ 3000); @@ -445,15 +399,6 @@ private ApiErrorReport cloneControlledBqDataset_waitForJobError( return result.getErrorReport(); } - private ApiCloneControlledGcpBigQueryDatasetResult getCloneControlledBqDatasetResult( - AuthenticatedUserRequest userRequest, UUID workspaceId, String jobId) throws Exception { - String serializedResponse = - mockMvcUtils.getSerializedResponseForGetJobResult( - userRequest, CLONE_RESULT_CONTROLLED_GCP_BQ_DATASETS_PATH_FORMAT, workspaceId, jobId); - return objectMapper.readValue( - serializedResponse, ApiCloneControlledGcpBigQueryDatasetResult.class); - } - // BQ Dataset (Referenced) public static final String CREATE_REFERENCED_GCP_BQ_DATASETS_PATH_FORMAT = "/api/workspaces/v1/%s/resources/referenced/gcp/bigquerydatasets"; @@ -523,7 +468,7 @@ public ApiGcpBigQueryDatasetResource cloneReferencedBqDataset( ApiCloningInstructionsEnum cloningInstructions, @Nullable String destResourceName) throws Exception { - return cloneReferencedBqDataset( + return cloneReferencedBqDatasetAndExpect( userRequest, sourceWorkspaceId, sourceResourceId, @@ -533,7 +478,7 @@ public ApiGcpBigQueryDatasetResource cloneReferencedBqDataset( HttpStatus.SC_OK); } - public ApiGcpBigQueryDatasetResource cloneReferencedBqDataset( + public ApiGcpBigQueryDatasetResource cloneReferencedBqDatasetAndExpect( AuthenticatedUserRequest userRequest, UUID sourceWorkspaceId, UUID sourceResourceId, @@ -570,20 +515,6 @@ public ApiGcpBigQueryDatasetResource cloneReferencedBqDataset( public static final String CLONE_REFERENCED_GCP_BQ_DATA_TABLE_PATH_FORMAT = REFERENCED_GCP_BQ_DATA_TABLE_PATH_FORMAT + "/clone"; - public static void assertApiBqDataTableEquals( - ApiGcpBigQueryDataTableResource expectedDataTable, - ApiGcpBigQueryDataTableResource actualDataTable) { - MockMvcUtils.assertResourceMetadataEquals( - expectedDataTable.getMetadata(), actualDataTable.getMetadata()); - assertEquals(expectedDataTable.getAttributes(), actualDataTable.getAttributes()); - } - - public void deleteBqDataTable( - AuthenticatedUserRequest userRequest, UUID workspaceId, UUID resourceId) throws Exception { - mockMvcUtils.deleteResource( - userRequest, workspaceId, resourceId, REFERENCED_GCP_BQ_DATA_TABLE_PATH_FORMAT); - } - public ApiGcpBigQueryDataTableResource createReferencedBqDataTable( AuthenticatedUserRequest userRequest, UUID workspaceId, @@ -612,6 +543,12 @@ public ApiGcpBigQueryDataTableResource createReferencedBqDataTable( return objectMapper.readValue(serializedResponse, ApiGcpBigQueryDataTableResource.class); } + public void deleteBqDataTable( + AuthenticatedUserRequest userRequest, UUID workspaceId, UUID resourceId) throws Exception { + mockMvcUtils.deleteResource( + userRequest, workspaceId, resourceId, REFERENCED_GCP_BQ_DATA_TABLE_PATH_FORMAT); + } + public ApiGcpBigQueryDataTableResource getReferencedBqDataTable( AuthenticatedUserRequest userRequest, UUID workspaceId, UUID resourceId) throws Exception { String serializedResponse = @@ -655,7 +592,7 @@ public ApiGcpBigQueryDataTableResource cloneReferencedBqDataTable( ApiCloningInstructionsEnum cloningInstructions, @Nullable String destResourceName) throws Exception { - return cloneReferencedBqDataTable( + return cloneReferencedBqDataTableAndExpect( userRequest, sourceWorkspaceId, sourceResourceId, @@ -665,7 +602,7 @@ public ApiGcpBigQueryDataTableResource cloneReferencedBqDataTable( HttpStatus.SC_OK); } - public ApiGcpBigQueryDataTableResource cloneReferencedBqDataTable( + public ApiGcpBigQueryDataTableResource cloneReferencedBqDataTableAndExpect( AuthenticatedUserRequest userRequest, UUID sourceWorkspaceId, UUID sourceResourceId, @@ -751,25 +688,23 @@ public ApiCreatedControlledGcpAiNotebookInstanceResult createAiNotebookInstanceA ApiCreatedControlledGcpAiNotebookInstanceResult result = objectMapper.readValue( serializedResponse, ApiCreatedControlledGcpAiNotebookInstanceResult.class); + String jobId = result.getJobReport().getId(); while (StairwayTestUtils.jobIsRunning(result.getJobReport())) { TimeUnit.SECONDS.sleep(5); - result = getAiNotebookInstanceResult(userRequest, workspaceId, jobId); + result = + mockMvcUtils.getCreateResourceJobResult( + ApiCreatedControlledGcpAiNotebookInstanceResult.class, + userRequest, + CREATE_RESULT_CONTROLLED_GCP_AI_NOTEBOOKS_PATH_FORMAT, + workspaceId, + jobId); } assertEquals(jobStatus, result.getJobReport().getStatus()); return result; } - private ApiCreatedControlledGcpAiNotebookInstanceResult getAiNotebookInstanceResult( - AuthenticatedUserRequest userRequest, UUID workspaceId, String jobId) throws Exception { - String serializedResponse = - mockMvcUtils.getSerializedResponseForGetJobResult( - userRequest, CREATE_RESULT_CONTROLLED_GCP_AI_NOTEBOOKS_PATH_FORMAT, workspaceId, jobId); - return objectMapper.readValue( - serializedResponse, ApiCreatedControlledGcpAiNotebookInstanceResult.class); - } - // GCE Instance public static final String CREATE_CONTROLLED_GCP_GCE_INSTANCES_PATH_FORMAT = "/api/workspaces/v1/%s/resources/controlled/gcp/gce-instances"; @@ -825,28 +760,23 @@ public ApiCreatedControlledGcpGceInstanceResult createGceInstanceAndExpect( objectMapper.writeValueAsString(request)); ApiCreatedControlledGcpGceInstanceResult result = objectMapper.readValue(serializedResponse, ApiCreatedControlledGcpGceInstanceResult.class); + String jobId = result.getJobReport().getId(); while (StairwayTestUtils.jobIsRunning(result.getJobReport())) { Thread.sleep(/*millis=*/ 5000); - result = getGceInstanceResult(userRequest, workspaceId, jobId); + result = + mockMvcUtils.getCreateResourceJobResult( + ApiCreatedControlledGcpGceInstanceResult.class, + userRequest, + CREATE_RESULT_CONTROLLED_GCP_GCE_INSTANCES_PATH_FORMAT, + workspaceId, + jobId); } assertEquals(jobStatus, result.getJobReport().getStatus()); return result; } - private ApiCreatedControlledGcpGceInstanceResult getGceInstanceResult( - AuthenticatedUserRequest userRequest, UUID workspaceId, String jobId) throws Exception { - String serializedResponse = - mockMvcUtils.getSerializedResponseForGetJobResult( - userRequest, - CREATE_RESULT_CONTROLLED_GCP_GCE_INSTANCES_PATH_FORMAT, - workspaceId, - jobId); - return objectMapper.readValue( - serializedResponse, ApiCreatedControlledGcpGceInstanceResult.class); - } - // DataProc Cluster public static final String CREATE_CONTROLLED_GCP_DATAPROC_CLUSTERS_PATH_FORMAT = "/api/workspaces/v1/%s/resources/controlled/gcp/dataproc-clusters"; @@ -920,25 +850,20 @@ public ApiCreatedControlledGcpDataprocClusterResult createDataprocClusterAndExpe ApiCreatedControlledGcpDataprocClusterResult result = objectMapper.readValue( serializedResponse, ApiCreatedControlledGcpDataprocClusterResult.class); + String jobId = result.getJobReport().getId(); while (StairwayTestUtils.jobIsRunning(result.getJobReport())) { Thread.sleep(/*millis=*/ 5000); - result = getDataprocClusterResult(userRequest, workspaceId, jobId); + result = + mockMvcUtils.getCreateResourceJobResult( + ApiCreatedControlledGcpDataprocClusterResult.class, + userRequest, + CREATE_RESULT_CONTROLLED_GCP_DATAPROC_CLUSTERS_PATH_FORMAT, + workspaceId, + jobId); } assertEquals(jobStatus, result.getJobReport().getStatus()); return result; } - - private ApiCreatedControlledGcpDataprocClusterResult getDataprocClusterResult( - AuthenticatedUserRequest userRequest, UUID workspaceId, String jobId) throws Exception { - String serializedResponse = - mockMvcUtils.getSerializedResponseForGetJobResult( - userRequest, - CREATE_RESULT_CONTROLLED_GCP_DATAPROC_CLUSTERS_PATH_FORMAT, - workspaceId, - jobId); - return objectMapper.readValue( - serializedResponse, ApiCreatedControlledGcpDataprocClusterResult.class); - } } diff --git a/service/src/test/java/bio/terra/workspace/common/utils/MockMvcUtils.java b/service/src/test/java/bio/terra/workspace/common/utils/MockMvcUtils.java index 88bc7f10a2..c271e446a0 100644 --- a/service/src/test/java/bio/terra/workspace/common/utils/MockMvcUtils.java +++ b/service/src/test/java/bio/terra/workspace/common/utils/MockMvcUtils.java @@ -913,7 +913,7 @@ public void cloneControlledGcsBucket_undo( // to avoid flakes. JOB_SUCCESS_CODES, /*shouldUndo=*/ true); - cloneControlledBqDataset_waitForJobError( + cloneControlledGcsBucket_waitForJobError( userRequest, sourceWorkspaceId, result.getJobReport().getId(), @@ -1989,6 +1989,18 @@ public ApiJobReport getJobReport(String path, AuthenticatedUserRequest userReque return objectMapper.readValue(serializedResponse, ApiJobResult.class).getJobReport(); } + public T getCreateResourceJobResult( + Class classType, + AuthenticatedUserRequest userRequest, + String path, + UUID workspaceId, + String jobId) + throws Exception { + String serializedResponse = + getSerializedResponseForGetJobResult(userRequest, path, workspaceId, jobId); + return objectMapper.readValue(serializedResponse, classType); + } + private String getSerializedResponseForGet( AuthenticatedUserRequest userRequest, String path, UUID workspaceId) throws Exception { return mockMvc diff --git a/service/src/test/java/bio/terra/workspace/service/resource/statetests/GcpResourceStateFailureTest.java b/service/src/test/java/bio/terra/workspace/service/resource/statetests/GcpResourceStateFailureTest.java index 5900e1ddf9..1810c75f45 100644 --- a/service/src/test/java/bio/terra/workspace/service/resource/statetests/GcpResourceStateFailureTest.java +++ b/service/src/test/java/bio/terra/workspace/service/resource/statetests/GcpResourceStateFailureTest.java @@ -18,6 +18,7 @@ import bio.terra.workspace.common.fixtures.ControlledGcpResourceFixtures; import bio.terra.workspace.common.fixtures.ControlledResourceFixtures; import bio.terra.workspace.common.fixtures.WorkspaceFixtures; +import bio.terra.workspace.common.utils.MockGcpApi; import bio.terra.workspace.common.utils.MockMvcUtils; import bio.terra.workspace.common.utils.TestUtils; import bio.terra.workspace.common.utils.WorkspaceUnitTestUtils; @@ -64,6 +65,7 @@ public class GcpResourceStateFailureTest extends BaseUnitTest { @Autowired private MockMvc mockMvc; @Autowired private MockMvcUtils mockMvcUtils; + @Autowired private MockGcpApi mockGcpApi; @Autowired ObjectMapper objectMapper; @Autowired ReferencedResourceService referencedResourceService; @Autowired ResourceDao resourceDao; @@ -310,7 +312,7 @@ void testGcpResourceCloneValidation() throws Exception { WorkspaceFixtures.DEFAULT_SPEND_PROFILE_ID, createContextFlightId); - mockMvcUtils.cloneControlledBqDatasetAsync( + mockGcpApi.cloneControlledBqDatasetAsync( USER_REQUEST, workspaceUuid, bqResource.getResourceId(),