use of org.talend.daikon.exception.TalendRuntimeException in project components by Talend.
the class SimpleFileIOOutputErrorTest method testUnauthorizedOverwrite.
/**
* Basic unit test using all default values (except for the path) on an in-memory DFS cluster.
*/
@Test
public void testUnauthorizedOverwrite() throws IOException, URISyntaxException {
Path parent = new Path(mini.newFolder().toString());
Path dst = new Path(parent, "output");
String fileSpec = mini.getLocalFs().getUri().resolve(dst.toUri()).toString();
// Write something to the file before trying to run.
try (OutputStream out = mini.getLocalFs().create(new Path(dst, "part-00000"))) {
out.write(0);
}
// Ensure that the destination is unwritable.
FileUtil.chmod(dst.toUri().toString(), "000", true);
// Trying to overwrite an unmodifiable destination throws an exception.
thrown.expect(TalendRuntimeException.class);
thrown.expect(hasProperty("code", is(SimpleFileIOErrorCode.OUTPUT_NOT_AUTHORIZED)));
thrown.expectMessage("Can not write to " + fileSpec + ". Please check user permissions or existence of base directory.");
// Now try using the component.
try {
// Configure the component.
SimpleFileIOOutputProperties props = SimpleFileIOOutputRuntimeTest.createOutputComponentProperties();
props.getDatasetProperties().path.setValue(fileSpec);
props.overwrite.setValue(true);
// Create the runtime.
SimpleFileIOOutputRuntime runtime = new SimpleFileIOOutputRuntime();
runtime.initialize(null, props);
// Use the runtime in a direct pipeline to test.
final Pipeline p = beam.createPipeline();
PCollection<IndexedRecord> input = //
p.apply(//
Create.of(//
ConvertToIndexedRecord.convertToAvro(new String[] { "1", "one" }), //
ConvertToIndexedRecord.convertToAvro(new String[] { "2", "two" })));
input.apply(runtime);
// And run the test.
runtime.runAtDriver(null);
p.run().waitUntilFinish();
} catch (Pipeline.PipelineExecutionException e) {
if (e.getCause() instanceof TalendRuntimeException)
throw (TalendRuntimeException) e.getCause();
throw e;
}
}
use of org.talend.daikon.exception.TalendRuntimeException in project components by Talend.
the class SimpleFileIOInputErrorTest method testUnauthorizedRead.
/**
* Basic unit test using all default values (except for the path) on an in-memory DFS cluster.
*/
@Test
public void testUnauthorizedRead() throws IOException, URISyntaxException {
String inputFile = writeRandomCsvFile(mini.getFs(), "/user/test/input.csv", 0, 0, 10, 10, 6, ";", "\n");
String fileSpec = mini.getFs().getUri().resolve("/user/test/input.csv").toString();
Path filePath = new Path(fileSpec);
// Ensure that the parent is unreadable.
mini.getFs().setPermission(filePath.getParent(), new FsPermission(FsAction.ALL, FsAction.NONE, FsAction.NONE));
mini.getFs().setOwner(filePath.getParent(), "gooduser", "gooduser");
// Configure the component.
SimpleFileIOInputProperties inputProps = SimpleFileIOInputRuntimeTest.createInputComponentProperties();
inputProps.getDatasetProperties().path.setValue(fileSpec);
inputProps.getDatasetProperties().getDatastoreProperties().userName.setValue("baduser");
// Create the runtime.
SimpleFileIOInputRuntime runtime = new SimpleFileIOInputRuntime();
runtime.initialize(null, inputProps);
// The exception that should be thrown.
thrown.expect(TalendRuntimeException.class);
thrown.expect(hasProperty("code", is(SimpleFileIOErrorCode.INPUT_NOT_AUTHORIZED)));
thrown.expectMessage("baduser can not read from " + fileSpec + ". Please check user permissions or existence of base directory.");
try {
// Use the runtime in a direct pipeline to test.
final Pipeline p = beam.createPipeline();
PCollection<IndexedRecord> readLines = p.apply(runtime);
// Check the expected values.
List<IndexedRecord> expected = new ArrayList<>();
for (String record : inputFile.split("\n")) {
expected.add(ConvertToIndexedRecord.convertToAvro(record.split(";")));
}
PAssert.that(readLines).containsInAnyOrder(expected);
// And run the test.
p.run().waitUntilFinish();
} catch (Pipeline.PipelineExecutionException e) {
if (e.getCause() instanceof TalendRuntimeException)
throw (TalendRuntimeException) e.getCause();
throw e;
}
}
use of org.talend.daikon.exception.TalendRuntimeException in project components by Talend.
the class RuntimeControllerImpl method writeData.
@Override
public void writeData(InputStream rawPayload) throws IOException {
// 1) Read payload (with data as a stream of course)
DatasetWritePayload payload = DatasetWritePayload.readData(rawPayload, mapper);
String definitionName = payload.getConfiguration().getDefinitionName();
// 2) Create properties
Properties properties = propertiesHelpers.propertiesFromDto(payload.getConfiguration());
if (properties instanceof ComponentProperties) {
ComponentProperties componentProperties = (ComponentProperties) properties;
// 3) Retrieve component definition to be able to create the runtime
final ComponentDefinition definition = propertiesHelpers.getDefinition(ComponentDefinition.class, definitionName);
// 4) Get the execution engine
ExecutionEngine executionEngine;
if (definition.isSupportingExecutionEngines(DI)) {
executionEngine = DI;
// 5) Create the sandbox
try (SandboxedInstance instance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(executionEngine, componentProperties, INCOMING), definition.getClass().getClassLoader())) {
Sink datasetRuntimeInstance = (Sink) instance.getInstance();
datasetRuntimeInstance.initialize(null, componentProperties);
Iterator<IndexedRecord> data = payload.getData();
WriteOperation writeOperation = datasetRuntimeInstance.createWriteOperation();
// Supplier return null to signify end of data stream => see WriterDataSupplier.writeData
WriterDataSupplier<?, IndexedRecord> stringWriterDataSupplier = new WriterDataSupplier<Object, IndexedRecord>(writeOperation, () -> data.hasNext() ? data.next() : null, null);
stringWriterDataSupplier.writeData();
}
} else if (definition.isSupportingExecutionEngines(BEAM)) {
throw new UnsupportedOperationException("Beam runtime is not available for dataset write through HTTP API.");
} else {
throw new TalendRuntimeException(CommonErrorCodes.UNREGISTERED_DEFINITION);
}
} else if (properties instanceof DatasetProperties) {
throw new UnsupportedOperationException("HTTP API is only able to write using component implementations. Not " + properties.getClass());
}
}
use of org.talend.daikon.exception.TalendRuntimeException in project components by Talend.
the class DatasetContentWriter method apply.
@Override
public Void apply(DatasetRuntime<DatasetProperties<DatastoreProperties>> dr) {
try {
final Encoder[] encoder = { null };
dr.getSample(limit == null ? MAX_VALUE : limit, getWritingConsumer(encoder));
if (encoder[0] != null)
encoder[0].flush();
} catch (RuntimeException | IOException e) {
log.error("Couldn't create Avro records JSon encoder.", e);
throw new TalendRuntimeException(CommonErrorCodes.UNEXPECTED_EXCEPTION, e);
}
return null;
}
use of org.talend.daikon.exception.TalendRuntimeException in project components by Talend.
the class TSalesforceInputProperties method validateGuessQuery.
public ValidationResult validateGuessQuery() {
ValidationResultMutable validationResult = new ValidationResultMutable();
try (SandboxedInstance sandboxedInstance = getSandboxedInstance(SOURCE_OR_SINK_CLASS)) {
SalesforceRuntimeSourceOrSink salesforceSourceOrSink = (SalesforceRuntimeSourceOrSink) sandboxedInstance.getInstance();
salesforceSourceOrSink.initialize(null, this);
Schema schema = module.main.schema.getValue();
String moduleName = module.moduleName.getValue();
if (!schema.getFields().isEmpty()) {
String soqlQuery = ((SalesforceSchemaHelper<Schema>) salesforceSourceOrSink).guessQuery(schema, moduleName);
query.setValue(soqlQuery);
validationResult.setStatus(ValidationResult.Result.OK);
} else {
String errorMessage = getI18nMessage("errorMessage.validateGuessQueryError");
validationResult.setStatus(ValidationResult.Result.ERROR).setMessage(errorMessage);
query.setValue("");
}
} catch (TalendRuntimeException tre) {
validationResult.setStatus(ValidationResult.Result.ERROR);
validationResult.setMessage(getI18nMessage("errorMessage.validateGuessQuerySoqlError", tre.getMessage()));
}
return validationResult;
}
Aggregations