Search in sources :

Example 6 with TalendRuntimeException

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;
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) SimpleFileIOOutputProperties(org.talend.components.simplefileio.output.SimpleFileIOOutputProperties) ConvertToIndexedRecord(org.talend.components.adapter.beam.transform.ConvertToIndexedRecord) IndexedRecord(org.apache.avro.generic.IndexedRecord) OutputStream(java.io.OutputStream) Pipeline(org.apache.beam.sdk.Pipeline) Test(org.junit.Test)

Example 7 with TalendRuntimeException

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;
    }
}
Also used : Path(org.apache.hadoop.fs.Path) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) ConvertToIndexedRecord(org.talend.components.adapter.beam.transform.ConvertToIndexedRecord) IndexedRecord(org.apache.avro.generic.IndexedRecord) ArrayList(java.util.ArrayList) SimpleFileIOInputProperties(org.talend.components.simplefileio.input.SimpleFileIOInputProperties) FsPermission(org.apache.hadoop.fs.permission.FsPermission) Pipeline(org.apache.beam.sdk.Pipeline) Test(org.junit.Test)

Example 8 with TalendRuntimeException

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());
    }
}
Also used : ComponentProperties(org.talend.components.api.properties.ComponentProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) DatasetProperties(org.talend.components.common.dataset.DatasetProperties) DatasetProperties(org.talend.components.common.dataset.DatasetProperties) DatastoreProperties(org.talend.components.common.datastore.DatastoreProperties) ComponentProperties(org.talend.components.api.properties.ComponentProperties) Properties(org.talend.daikon.properties.Properties) SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) ExecutionEngine(org.talend.components.api.component.runtime.ExecutionEngine) Sink(org.talend.components.api.component.runtime.Sink) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) WriterDataSupplier(org.talend.components.api.component.runtime.WriterDataSupplier) ComponentDefinition(org.talend.components.api.component.ComponentDefinition)

Example 9 with TalendRuntimeException

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;
}
Also used : TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) Encoder(org.apache.avro.io.Encoder) IOException(java.io.IOException)

Example 10 with TalendRuntimeException

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;
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) SalesforceDefinition.getSandboxedInstance(org.talend.components.salesforce.SalesforceDefinition.getSandboxedInstance) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) SalesforceSchemaHelper(org.talend.components.salesforce.schema.SalesforceSchemaHelper) Schema(org.apache.avro.Schema) ValidationResultMutable(org.talend.daikon.properties.ValidationResultMutable) SalesforceRuntimeSourceOrSink(org.talend.components.salesforce.common.SalesforceRuntimeSourceOrSink)

Aggregations

TalendRuntimeException (org.talend.daikon.exception.TalendRuntimeException)28 IndexedRecord (org.apache.avro.generic.IndexedRecord)10 IOException (java.io.IOException)8 Test (org.junit.Test)7 Pipeline (org.apache.beam.sdk.Pipeline)6 Schema (org.apache.avro.Schema)5 Path (org.apache.hadoop.fs.Path)5 ConvertToIndexedRecord (org.talend.components.adapter.beam.transform.ConvertToIndexedRecord)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 SimpleFileIOOutputProperties (org.talend.components.simplefileio.output.SimpleFileIOOutputProperties)4 BufferedWriter (java.io.BufferedWriter)3 OutputStream (java.io.OutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DateTimeException (java.time.DateTimeException)2 LocalDateTime (java.time.LocalDateTime)2 GenericRecord (org.apache.avro.generic.GenericRecord)2 StringUtils (org.apache.commons.lang.StringUtils)2 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)2