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();
}
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;
}
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);
}
}
Aggregations