Search in sources :

Example 1 with DatasetModel

use of bio.terra.model.DatasetModel in project jade-data-repo by DataBiosphere.

the class CreateDatasetAuthzBqJobUserStep method doStep.

@Override
public StepResult doStep(FlightContext context) throws InterruptedException {
    FlightMap workingMap = context.getWorkingMap();
    UUID datasetId = workingMap.get(DatasetWorkingMapKeys.DATASET_ID, UUID.class);
    Map<IamRole, String> policies = workingMap.get(DatasetWorkingMapKeys.POLICY_EMAILS, Map.class);
    Dataset dataset = datasetService.retrieve(datasetId);
    DatasetModel datasetModel = datasetService.retrieveModel(dataset);
    // The underlying service provides retries so we do not need to retry this operation
    resourceService.grantPoliciesBqJobUser(datasetModel.getDataProject(), policies.values());
    return StepResult.getStepResultSuccess();
}
Also used : Dataset(bio.terra.service.dataset.Dataset) IamRole(bio.terra.service.iam.IamRole) FlightMap(bio.terra.stairway.FlightMap) UUID(java.util.UUID) DatasetModel(bio.terra.model.DatasetModel)

Example 2 with DatasetModel

use of bio.terra.model.DatasetModel in project jade-data-repo by DataBiosphere.

the class BigQueryVisitor method visitTable_expr.

@Override
public String visitTable_expr(SQLParser.Table_exprContext ctx) {
    String datasetName = getNameFromContext(ctx.dataset_name());
    DatasetModel dataset = getDatasetByName(datasetName);
    String dataProjectId = dataset.getDataProject();
    String bqDatasetName = PdaoConstant.PDAO_PREFIX + getNameFromContext(ctx.dataset_name());
    String tableName = getNameFromContext(ctx.table_name());
    String alias = generateAlias(bqDatasetName, tableName);
    return String.format("`%s.%s.%s` AS `%s`", dataProjectId, bqDatasetName, tableName, alias);
}
Also used : DatasetModel(bio.terra.model.DatasetModel)

Example 3 with DatasetModel

use of bio.terra.model.DatasetModel in project jade-data-repo by DataBiosphere.

the class DatasetConnectedTest method testExcludeLockedFromDatasetLookups.

@Test
public void testExcludeLockedFromDatasetLookups() throws Exception {
    // check that the dataset metadata row is unlocked
    UUID datasetId = UUID.fromString(summaryModel.getId());
    String exclusiveLock = datasetDao.getExclusiveLock(datasetId);
    assertNull("dataset row is not exclusively locked", exclusiveLock);
    String[] sharedLocks = datasetDao.getSharedLocks(datasetId);
    assertEquals("dataset row has no shared lock", 0, sharedLocks.length);
    // retrieve the dataset and check that it finds it
    DatasetModel datasetModel = connectedOperations.getDataset(summaryModel.getId());
    assertEquals("Lookup unlocked dataset succeeds", summaryModel.getName(), datasetModel.getName());
    // enumerate datasets and check that this dataset is included in the set
    EnumerateDatasetModel enumerateDatasetModel = connectedOperations.enumerateDatasets(summaryModel.getName());
    List<DatasetSummaryModel> enumeratedDatasets = enumerateDatasetModel.getItems();
    boolean foundDatasetWithMatchingId = false;
    for (DatasetSummaryModel enumeratedDataset : enumeratedDatasets) {
        if (enumeratedDataset.getId().equals(summaryModel.getId())) {
            foundDatasetWithMatchingId = true;
            break;
        }
    }
    assertTrue("Unlocked included in enumeration", foundDatasetWithMatchingId);
    // NO ASSERTS inside the block below where hang is enabled to reduce chance of failing before disabling the hang
    // ====================================================
    // enable hang in DeleteDatasetPrimaryDataStep
    configService.setFault(ConfigEnum.DATASET_DELETE_LOCK_CONFLICT_STOP_FAULT.name(), true);
    // kick off a request to delete the dataset. this should hang before unlocking the dataset object.
    MvcResult deleteResult = mvc.perform(delete("/api/repository/v1/datasets/" + summaryModel.getId())).andReturn();
    // give the flight time to launch
    TimeUnit.SECONDS.sleep(5);
    // check that the dataset metadata row has an exclusive lock
    // note: asserts are below outside the hang block
    exclusiveLock = datasetDao.getExclusiveLock(datasetId);
    sharedLocks = datasetDao.getSharedLocks(datasetId);
    // retrieve the dataset, should return not found
    // note: asserts are below outside the hang block
    MvcResult retrieveResult = mvc.perform(get("/api/repository/v1/datasets/" + datasetId)).andReturn();
    // enumerate datasets, this dataset should not be included in the set
    // note: asserts are below outside the hang block
    MvcResult enumerateResult = connectedOperations.enumerateDatasetsRaw(summaryModel.getName());
    // disable hang in DeleteDatasetPrimaryDataStep
    configService.setFault(ConfigEnum.DATASET_DELETE_LOCK_CONFLICT_CONTINUE_FAULT.name(), true);
    // ====================================================
    // check that the dataset metadata row has an exclusive lock after kicking off the delete
    assertNotNull("dataset row is exclusively locked", exclusiveLock);
    assertEquals("dataset row has no shared lock", 0, sharedLocks.length);
    // check that the retrieve request returned not found
    connectedOperations.handleFailureCase(retrieveResult.getResponse(), HttpStatus.NOT_FOUND);
    // check that the enumerate request returned successfully and that this dataset is not included in the set
    enumerateDatasetModel = connectedOperations.handleSuccessCase(enumerateResult.getResponse(), EnumerateDatasetModel.class);
    enumeratedDatasets = enumerateDatasetModel.getItems();
    foundDatasetWithMatchingId = false;
    for (DatasetSummaryModel enumeratedDataset : enumeratedDatasets) {
        if (enumeratedDataset.getId().equals(summaryModel.getId())) {
            foundDatasetWithMatchingId = true;
            break;
        }
    }
    assertFalse("Exclusively locked not included in enumeration", foundDatasetWithMatchingId);
    // check the response from the delete request
    MockHttpServletResponse deleteResponse = connectedOperations.validateJobModelAndWait(deleteResult);
    DeleteResponseModel deleteResponseModel = connectedOperations.handleSuccessCase(deleteResponse, DeleteResponseModel.class);
    assertEquals("Dataset delete returned successfully", DeleteResponseModel.ObjectStateEnum.DELETED, deleteResponseModel.getObjectState());
    // try to fetch the dataset again and confirm nothing is returned
    connectedOperations.getDatasetExpectError(summaryModel.getId(), HttpStatus.NOT_FOUND);
}
Also used : DatasetSummaryModel(bio.terra.model.DatasetSummaryModel) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) UUID(java.util.UUID) MvcResult(org.springframework.test.web.servlet.MvcResult) EnumerateDatasetModel(bio.terra.model.EnumerateDatasetModel) DatasetModel(bio.terra.model.DatasetModel) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) DeleteResponseModel(bio.terra.model.DeleteResponseModel) EnumerateDatasetModel(bio.terra.model.EnumerateDatasetModel) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 4 with DatasetModel

use of bio.terra.model.DatasetModel in project jade-data-repo by DataBiosphere.

the class DatasetConnectedTest method testDuplicateName.

@Test
public void testDuplicateName() throws Exception {
    assertNotNull("created dataset successfully the first time", summaryModel);
    // fetch the dataset and confirm the metadata matches the request
    DatasetModel datasetModel = connectedOperations.getDataset(summaryModel.getId());
    assertNotNull("fetched dataset successfully after creation", datasetModel);
    assertEquals("fetched dataset name matches request", datasetRequest.getName(), datasetModel.getName());
    // check that the dataset metadata row is unlocked
    String exclusiveLock = datasetDao.getExclusiveLock(UUID.fromString(summaryModel.getId()));
    assertNull("dataset row is unlocked", exclusiveLock);
    // try to create the same dataset again and check that it fails
    ErrorModel errorModel = connectedOperations.createDatasetExpectError(datasetRequest, HttpStatus.BAD_REQUEST);
    assertThat("error message includes name conflict", errorModel.getMessage(), containsString("Dataset name already exists"));
    // delete the dataset and check that it succeeds
    connectedOperations.deleteTestDataset(summaryModel.getId());
    // try to fetch the dataset again and confirm nothing is returned
    connectedOperations.getDatasetExpectError(summaryModel.getId(), HttpStatus.NOT_FOUND);
}
Also used : ErrorModel(bio.terra.model.ErrorModel) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) EnumerateDatasetModel(bio.terra.model.EnumerateDatasetModel) DatasetModel(bio.terra.model.DatasetModel) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 5 with DatasetModel

use of bio.terra.model.DatasetModel in project jade-data-repo by DataBiosphere.

the class SnapshotTest method snapshotRowIdsHappyPathTest.

@Test
public void snapshotRowIdsHappyPathTest() throws Exception {
    // fetch rowIds from the ingested dataset by querying the participant table
    DatasetModel dataset = dataRepoFixtures.getDataset(steward(), datasetId);
    String datasetProject = dataset.getDataProject();
    String bqDatasetName = PdaoConstant.PDAO_PREFIX + dataset.getName();
    String participantTable = "participant";
    String sampleTable = "sample";
    BigQuery bigQuery = BigQueryFixtures.getBigQuery(dataset.getDataProject(), stewardToken);
    String sql = String.format("SELECT %s FROM `%s.%s.%s`", PdaoConstant.PDAO_ROW_ID_COLUMN, datasetProject, bqDatasetName, participantTable);
    TableResult participantIds = BigQueryFixtures.query(sql, bigQuery);
    List<String> participantIdList = StreamSupport.stream(participantIds.getValues().spliterator(), false).map(v -> v.get(0).getStringValue()).collect(Collectors.toList());
    sql = String.format("SELECT %s FROM `%s.%s.%s`", PdaoConstant.PDAO_ROW_ID_COLUMN, datasetProject, bqDatasetName, sampleTable);
    TableResult sampleIds = BigQueryFixtures.query(sql, bigQuery);
    List<String> sampleIdList = StreamSupport.stream(sampleIds.getValues().spliterator(), false).map(v -> v.get(0).getStringValue()).collect(Collectors.toList());
    // swap in these row ids in the request
    SnapshotRequestModel requestModel = jsonLoader.loadObject("ingest-test-snapshot-row-ids-test.json", SnapshotRequestModel.class);
    requestModel.getContents().get(0).getRowIdSpec().getTables().get(0).setRowIds(participantIdList);
    requestModel.getContents().get(0).getRowIdSpec().getTables().get(1).setRowIds(sampleIdList);
    SnapshotSummaryModel snapshotSummary = dataRepoFixtures.createSnapshotWithRequest(steward(), dataset.getName(), requestModel);
    TimeUnit.SECONDS.sleep(10);
    createdSnapshotIds.add(snapshotSummary.getId());
    SnapshotModel snapshot = dataRepoFixtures.getSnapshot(steward(), snapshotSummary.getId());
    assertEquals("new snapshot has been created", snapshot.getName(), requestModel.getName());
    assertEquals("new snapshot has the correct number of tables", requestModel.getContents().get(0).getRowIdSpec().getTables().size(), snapshot.getTables().size());
    // TODO: get the snapshot and make sure the number of rows matches with the row ids input
    assertThat("one relationship comes through", snapshot.getRelationships().size(), equalTo(1));
    assertThat("the right relationship comes through", snapshot.getRelationships().get(0).getName(), equalTo("sample_participants"));
}
Also used : SnapshotModel(bio.terra.model.SnapshotModel) RunWith(org.junit.runner.RunWith) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveProfiles(org.springframework.test.context.ActiveProfiles) BigQuery(com.google.cloud.bigquery.BigQuery) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) DataRepoResponse(bio.terra.integration.DataRepoResponse) SnapshotRequestModel(bio.terra.model.SnapshotRequestModel) After(org.junit.After) JsonLoader(bio.terra.common.fixtures.JsonLoader) StreamSupport(java.util.stream.StreamSupport) TableResult(com.google.cloud.bigquery.TableResult) SpringRunner(org.springframework.test.context.junit4.SpringRunner) Before(org.junit.Before) Logger(org.slf4j.Logger) AuthService(bio.terra.common.auth.AuthService) Test(org.junit.Test) EnumerateSnapshotModel(bio.terra.model.EnumerateSnapshotModel) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) DatasetSummaryModel(bio.terra.model.DatasetSummaryModel) IamRole(bio.terra.service.iam.IamRole) TimeUnit(java.util.concurrent.TimeUnit) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) UsersBase(bio.terra.integration.UsersBase) IngestRequestModel(bio.terra.model.IngestRequestModel) AutoConfigureMockMvc(org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) BigQueryFixtures(bio.terra.integration.BigQueryFixtures) JobModel(bio.terra.model.JobModel) DataRepoClient(bio.terra.integration.DataRepoClient) SnapshotSummaryModel(bio.terra.model.SnapshotSummaryModel) Matchers.equalTo(org.hamcrest.Matchers.equalTo) PdaoConstant(bio.terra.common.PdaoConstant) Integration(bio.terra.common.category.Integration) DataRepoFixtures(bio.terra.integration.DataRepoFixtures) DatasetModel(bio.terra.model.DatasetModel) Assert.assertEquals(org.junit.Assert.assertEquals) BigQuery(com.google.cloud.bigquery.BigQuery) TableResult(com.google.cloud.bigquery.TableResult) SnapshotSummaryModel(bio.terra.model.SnapshotSummaryModel) DatasetModel(bio.terra.model.DatasetModel) SnapshotRequestModel(bio.terra.model.SnapshotRequestModel) SnapshotModel(bio.terra.model.SnapshotModel) EnumerateSnapshotModel(bio.terra.model.EnumerateSnapshotModel) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

DatasetModel (bio.terra.model.DatasetModel)18 Test (org.junit.Test)14 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)14 EnumerateDatasetModel (bio.terra.model.EnumerateDatasetModel)11 BigQuery (com.google.cloud.bigquery.BigQuery)7 DatasetSummaryModel (bio.terra.model.DatasetSummaryModel)6 SnapshotModel (bio.terra.model.SnapshotModel)6 SnapshotSummaryModel (bio.terra.model.SnapshotSummaryModel)6 SnapshotRequestModel (bio.terra.model.SnapshotRequestModel)5 DataDeletionRequest (bio.terra.model.DataDeletionRequest)4 IngestRequestModel (bio.terra.model.IngestRequestModel)4 DataDeletionTableModel (bio.terra.model.DataDeletionTableModel)3 EnumerateSnapshotModel (bio.terra.model.EnumerateSnapshotModel)3 JobModel (bio.terra.model.JobModel)3 IamRole (bio.terra.service.iam.IamRole)3 TableResult (com.google.cloud.bigquery.TableResult)3 UUID (java.util.UUID)3 Before (org.junit.Before)3 PdaoConstant (bio.terra.common.PdaoConstant)2 AuthService (bio.terra.common.auth.AuthService)2