Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dexamundsen committed Jul 20, 2023
1 parent 4203709 commit bad4362
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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());
}

Expand Down Expand Up @@ -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(),
Expand All @@ -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,
Expand All @@ -409,26 +405,36 @@ 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"));
}

@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,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -478,36 +484,33 @@ 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(),
EUROPE_LOCATION,
DEFAULT_TABLE_LIFETIME,
DEFAULT_PARTITION_LIFETIME);
mockGcpApi.deleteBqDataset(
userAccessUtils.defaultUserAuthRequest(),
userRequest,
workspaceId2,
gotResource.getMetadata().getResourceId(),
/*isControlled=*/ StewardshipType.REFERENCED);
}

@Test
void clone_copyResource_sameWorkspace() throws Exception {
AuthenticatedUserRequest userRequest = userAccessUtils.defaultUserAuthRequest();
// Source resource is in us-west4

// Clone resource to europe location
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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);
}

Expand Down Expand Up @@ -714,36 +713,66 @@ 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,
ApiCloningInstructionsEnum.RESOURCE,
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,
ApiCloningInstructionsEnum.REFERENCE,
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
Loading

0 comments on commit bad4362

Please sign in to comment.