use of bio.terra.workspace.model.ClonedControlledGcpBigQueryDataset in project terra-workspace-manager by DataBiosphere.
the class ControlledBigQueryDatasetLifecycle method testCloneBigQueryDataset.
private void testCloneBigQueryDataset(GcpBigQueryDatasetResource sourceDataset, TestUserSpecification cloningUser, ControlledGcpResourceApi cloningUserResourceApi) throws Exception {
final String destinationDatasetName = ("clone_" + UUID.randomUUID().toString()).replace('-', '_');
// clone the dataset as the cloning user
final String clonedDatasetDescription = "Clone of " + destinationDatasetName;
final String jobId = UUID.randomUUID().toString();
final CloneControlledGcpBigQueryDatasetRequest cloneRequest = new CloneControlledGcpBigQueryDatasetRequest().cloningInstructions(CloningInstructionsEnum.RESOURCE).description(clonedDatasetDescription).location(// keep same
null).destinationWorkspaceId(getDestinationWorkspaceId()).name("MyClonedDataset").jobControl(new JobControl().id(jobId)).destinationDatasetName(// keep same
null);
final ResourceMetadata sourceDatasetMetadata = sourceDataset.getMetadata();
logger.info("Cloning BigQuery dataset\n\tname: {}\n\tresource ID: {}\n\tworkspace: {}\n\t" + "projectID: {}\ninto destination \n\tname: {}\n\tworkspace: {}\n\tprojectID: {}", sourceDatasetMetadata.getName(), sourceDatasetMetadata.getResourceId(), sourceDatasetMetadata.getWorkspaceId(), getSourceProjectId(), sourceDatasetMetadata.getName(), getDestinationWorkspaceId(), getDestinationProjectId());
// Submit clone request and poll for async result
CloneControlledGcpBigQueryDatasetResult cloneResult = cloningUserResourceApi.cloneBigQueryDataset(cloneRequest, sourceDatasetMetadata.getWorkspaceId(), sourceDatasetMetadata.getResourceId());
cloneResult = ClientTestUtils.pollWhileRunning(cloneResult, () -> cloningUserResourceApi.getCloneBigQueryDatasetResult(cloneRequest.getDestinationWorkspaceId(), cloneRequest.getJobControl().getId()), CloneControlledGcpBigQueryDatasetResult::getJobReport, Duration.ofSeconds(5));
ClientTestUtils.assertJobSuccess("clone BigQuery dataset", cloneResult.getJobReport(), cloneResult.getErrorReport());
assertEquals(sourceDatasetMetadata.getWorkspaceId(), cloneResult.getDataset().getSourceWorkspaceId());
assertEquals(sourceDatasetMetadata.getResourceId(), cloneResult.getDataset().getSourceResourceId());
// unwrap the result one layer at a time
final ClonedControlledGcpBigQueryDataset clonedControlledGcpBigQueryDataset = cloneResult.getDataset();
assertEquals(CloningInstructionsEnum.RESOURCE, clonedControlledGcpBigQueryDataset.getEffectiveCloningInstructions());
final GcpBigQueryDatasetResource clonedResource = clonedControlledGcpBigQueryDataset.getDataset();
final ResourceMetadata clonedDatasetMetadata = clonedResource.getMetadata();
assertEquals(sourceDatasetMetadata.getCloningInstructions(), clonedDatasetMetadata.getCloningInstructions());
assertEquals(sourceDatasetMetadata.getCloudPlatform(), clonedDatasetMetadata.getCloudPlatform());
assertEquals(sourceDatasetMetadata.getResourceType(), clonedDatasetMetadata.getResourceType());
assertEquals(sourceDatasetMetadata.getStewardshipType(), clonedDatasetMetadata.getStewardshipType());
assertEquals(sourceDatasetMetadata.getControlledResourceMetadata().getManagedBy(), clonedDatasetMetadata.getControlledResourceMetadata().getManagedBy());
assertEquals(sourceDatasetMetadata.getControlledResourceMetadata().getAccessScope(), clonedDatasetMetadata.getControlledResourceMetadata().getAccessScope());
assertNotEquals(sourceDataset.getAttributes().getProjectId(), clonedResource.getAttributes().getProjectId());
assertEquals(sourceDataset.getAttributes().getDatasetId(), clonedResource.getAttributes().getDatasetId());
assertEquals(sourceDataset.getMetadata().getName(), sourceDataset.getAttributes().getDatasetId());
// compare dataset contents
final BigQuery bigQueryClient = ClientTestUtils.getGcpBigQueryClient(cloningUser, getDestinationProjectId());
// Create an empty table to hold results
TableId resultTableId = TableId.of(getDestinationProjectId(), clonedResource.getAttributes().getDatasetId(), "results_table");
final QueryJobConfiguration employeeQueryJobConfiguration = QueryJobConfiguration.newBuilder("SELECT * FROM `" + getDestinationProjectId() + "." + clonedResource.getAttributes().getDatasetId() + ".employee`;").setDestinationTable(resultTableId).setWriteDisposition(WriteDisposition.WRITE_TRUNCATE).build();
final TableResult employeeTableResult = bigQueryClient.query(employeeQueryJobConfiguration);
final long numRows = StreamSupport.stream(employeeTableResult.getValues().spliterator(), false).count();
assertThat(numRows, is(greaterThanOrEqualTo(2L)));
final TableResult departmentTableResult = bigQueryClient.query(QueryJobConfiguration.newBuilder("SELECT * FROM `" + getDestinationProjectId() + "." + clonedResource.getAttributes().getDatasetId() + ".department` " + "WHERE department_id = 201;").setDestinationTable(resultTableId).setWriteDisposition(WriteDisposition.WRITE_TRUNCATE).build());
final FieldValueList row = StreamSupport.stream(departmentTableResult.getValues().spliterator(), false).findFirst().orElseThrow(() -> new RuntimeException("Can't find expected result row"));
// Assert data matches the expected values from BqDatasetUtils.populateBigQueryDataset
final FieldValue nameFieldValue = row.get("name");
assertEquals("ocean", nameFieldValue.getStringValue());
final FieldValue managerFieldValue = row.get("manager_id");
assertEquals(101, managerFieldValue.getLongValue());
}
Aggregations