Search in sources :

Example 1 with InvalidAssetException

use of bio.terra.service.dataset.exception.InvalidAssetException in project jade-data-repo by DataBiosphere.

the class CreateDatasetAssetStep method doStep.

@Override
public StepResult doStep(FlightContext context) {
    // TODO: Asset columns and tables need to match things in the dataset schema
    Dataset dataset = getDataset(context);
    FlightMap map = context.getWorkingMap();
    // get the dataset assets that already exist --asset name needs to be unique
    AssetSpecification newAssetSpecification = getNewAssetSpec(context, dataset);
    // add a fault that forces an exception to make sure the undo works
    try {
        configService.fault(ConfigEnum.CREATE_ASSET_FAULT, () -> {
            throw new RuntimeException("fault insertion");
        });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    try {
        assetDao.create(newAssetSpecification, dataset.getId());
    } catch (InvalidAssetException e) {
        FlightUtils.setErrorResponse(context, e.getMessage(), HttpStatus.BAD_REQUEST);
        map.put(DatasetWorkingMapKeys.ASSET_NAME_COLLISION, true);
        return new StepResult(StepStatus.STEP_RESULT_FAILURE_FATAL, e);
    }
    map.put(JobMapKeys.STATUS_CODE.getKeyName(), HttpStatus.CREATED);
    return StepResult.getStepResultSuccess();
}
Also used : Dataset(bio.terra.service.dataset.Dataset) FlightMap(bio.terra.stairway.FlightMap) AssetSpecification(bio.terra.service.dataset.AssetSpecification) StepResult(bio.terra.stairway.StepResult) InvalidAssetException(bio.terra.service.dataset.exception.InvalidAssetException) InvalidAssetException(bio.terra.service.dataset.exception.InvalidAssetException)

Example 2 with InvalidAssetException

use of bio.terra.service.dataset.exception.InvalidAssetException in project jade-data-repo by DataBiosphere.

the class AssetDao method create.

/**
 * Create a new AssetSpecification. If you try to create an asset with the same name as an existing
 * one for the same dataset, this method throws an InvalidAssetException.
 * @param assetSpecification the AssetSpecification being created
 * @param datasetId the ID of the dataset corresponding to the AssetSpecification being created
 * @return
 */
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE)
public UUID create(AssetSpecification assetSpecification, UUID datasetId) {
    String sql = "INSERT INTO asset_specification (dataset_id, name, root_table_id, root_column_id) " + "VALUES (:dataset_id, :name, :root_table_id, :root_column_id)";
    MapSqlParameterSource params = new MapSqlParameterSource();
    params.addValue("dataset_id", datasetId);
    params.addValue("name", assetSpecification.getName());
    params.addValue("root_table_id", assetSpecification.getRootTable().getTable().getId());
    params.addValue("root_column_id", assetSpecification.getRootColumn().getDatasetColumn().getId());
    DaoKeyHolder keyHolder = new DaoKeyHolder();
    try {
        jdbcTemplate.update(sql, params, keyHolder);
    } catch (DuplicateKeyException e) {
        throw new InvalidAssetException("Asset name already exists: " + assetSpecification.getName(), e);
    }
    UUID assetSpecId = keyHolder.getId();
    assetSpecification.id(assetSpecId);
    createAssetColumns(assetSpecification);
    createAssetRelationships(assetSpecification);
    return assetSpecId;
}
Also used : MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) DaoKeyHolder(bio.terra.common.DaoKeyHolder) UUID(java.util.UUID) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) InvalidAssetException(bio.terra.service.dataset.exception.InvalidAssetException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with InvalidAssetException

use of bio.terra.service.dataset.exception.InvalidAssetException in project jade-data-repo by DataBiosphere.

the class DatasetServiceTest method addDatasetBadAssetSpecification.

@Test
public void addDatasetBadAssetSpecification() throws Exception {
    UUID datasetId = createDataset("dataset-create-test.json");
    // This asset name already exists
    String assetName = "sample";
    // get created dataset
    Dataset createdDataset = datasetDao.retrieve(datasetId);
    assertThat("dataset already has two asset specs", createdDataset.getAssetSpecifications().size(), equalTo(2));
    AssetModel assetModel = new AssetModel().name(assetName).rootTable("sample").rootColumn("participant_id").tables(Arrays.asList(DatasetFixtures.buildAssetParticipantTable(), DatasetFixtures.buildAssetSampleTable())).follow(Collections.singletonList("participant_sample"));
    // add asset to dataset
    String jobId = datasetService.addDatasetAssetSpecifications(datasetId.toString(), assetModel, testUser);
    flightIdsList.add(jobId);
    TestUtils.eventualExpect(5, 60, true, () -> jobService.retrieveJob(jobId, testUser).getJobStatus().equals(JobModel.JobStatusEnum.FAILED));
    try {
        try {
            jobService.retrieveJobResult(jobId, ErrorModel.class, testUser);
            fail("Expected invalid asset exception");
        } catch (InvalidAssetException ex) {
            assertThat("error message is correct", ex.getMessage(), equalTo("Asset name already exists: sample"));
            // get dataset
            Dataset dataset = datasetDao.retrieve(datasetId);
            // make sure the dataset has the expected asset
            assertThat("dataset has no additional asset spec", dataset.getAssetSpecifications().size(), equalTo(2));
        }
    } finally {
        datasetDao.delete(datasetId);
    }
}
Also used : AssetModel(bio.terra.model.AssetModel) UUID(java.util.UUID) InvalidAssetException(bio.terra.service.dataset.exception.InvalidAssetException) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

InvalidAssetException (bio.terra.service.dataset.exception.InvalidAssetException)3 UUID (java.util.UUID)2 DaoKeyHolder (bio.terra.common.DaoKeyHolder)1 AssetModel (bio.terra.model.AssetModel)1 AssetSpecification (bio.terra.service.dataset.AssetSpecification)1 Dataset (bio.terra.service.dataset.Dataset)1 FlightMap (bio.terra.stairway.FlightMap)1 StepResult (bio.terra.stairway.StepResult)1 Test (org.junit.Test)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)1 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)1 Transactional (org.springframework.transaction.annotation.Transactional)1