Search in sources :

Example 1 with SalesforceSchemaHelper

use of org.talend.components.salesforce.schema.SalesforceSchemaHelper in project components by Talend.

the class TSalesforceInputPropertiesTest method testValidateGuessSchema.

@Test
public void testValidateGuessSchema() throws Exception {
    properties.init();
    String query = "\"SELECT Id, Name, BillingCity FROM Account\"";
    String invalidQuery = "qwerty";
    Schema schema = SchemaBuilder.record("Result").fields().name("Id").type().stringType().noDefault().name("Name").type().stringType().noDefault().name("BillingCity").type().stringType().noDefault().endRecord();
    Schema emptySchema = AvroUtils.createEmptySchema();
    properties.module.moduleName.setValue("Account");
    properties.module.main.schema.setValue(emptySchema);
    properties.query.setValue(query);
    try (MockRuntimeSourceOrSinkTestFixture testFixture = new MockRuntimeSourceOrSinkTestFixture(equalTo(properties), createDefaultTestDataset())) {
        testFixture.setUp();
        // Valid
        when(((SalesforceSchemaHelper) testFixture.runtimeSourceOrSink).guessSchema(eq(query))).thenReturn(schema);
        ValidationResult vr1 = properties.validateGuessSchema();
        assertEquals(ValidationResult.Result.OK, vr1.getStatus());
        assertEquals(schema, properties.module.main.schema.getValue());
        // Not valid / Error
        properties.query.setValue(invalidQuery);
        properties.module.main.schema.setValue(schema);
        when(((SalesforceSchemaHelper) testFixture.runtimeSourceOrSink).guessSchema(eq(invalidQuery))).thenThrow(TalendRuntimeException.createUnexpectedException("ERROR"));
        ValidationResult vr2 = properties.validateGuessSchema();
        assertEquals(ValidationResult.Result.ERROR, vr2.getStatus());
        assertEquals(schema, properties.module.main.schema.getValue());
        when(((SalesforceSchemaHelper) testFixture.runtimeSourceOrSink).guessSchema(eq(invalidQuery))).thenThrow(new RuntimeException("ERROR"));
        vr2 = properties.validateGuessSchema();
        assertEquals(ValidationResult.Result.ERROR, vr2.getStatus());
        assertEquals(schema, properties.module.main.schema.getValue());
        when(((SalesforceSchemaHelper) testFixture.runtimeSourceOrSink).guessSchema(eq(invalidQuery))).thenThrow(new IOException("I/O ERROR"));
        vr2 = properties.validateGuessSchema();
        assertEquals(ValidationResult.Result.ERROR, vr2.getStatus());
        assertEquals(schema, properties.module.main.schema.getValue());
    }
}
Also used : SalesforceSchemaHelper(org.talend.components.salesforce.schema.SalesforceSchemaHelper) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) Schema(org.apache.avro.Schema) IOException(java.io.IOException) ValidationResult(org.talend.daikon.properties.ValidationResult) Test(org.junit.Test)

Example 2 with SalesforceSchemaHelper

use of org.talend.components.salesforce.schema.SalesforceSchemaHelper 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)

Example 3 with SalesforceSchemaHelper

use of org.talend.components.salesforce.schema.SalesforceSchemaHelper in project components by Talend.

the class TSalesforceInputProperties method validateGuessSchema.

public ValidationResult validateGuessSchema() {
    ValidationResultMutable validationResult = new ValidationResultMutable();
    try (SandboxedInstance sandboxedInstance = getSandboxedInstance(SOURCE_OR_SINK_CLASS)) {
        SalesforceRuntimeSourceOrSink salesforceSourceOrSink = (SalesforceRuntimeSourceOrSink) sandboxedInstance.getInstance();
        salesforceSourceOrSink.initialize(null, this);
        Schema schema = ((SalesforceSchemaHelper<Schema>) salesforceSourceOrSink).guessSchema(query.getValue());
        module.main.schema.setValue(schema);
        validationResult.setStatus(ValidationResult.Result.OK);
    } catch (TalendRuntimeException tre) {
        String errorMessage = getI18nMessage("errorMessage.validateGuessSchemaSoqlError", tre.getMessage());
        validationResult.setStatus(ValidationResult.Result.ERROR).setMessage(errorMessage);
    } catch (RuntimeException e1) {
        String errorMessage = getI18nMessage("errorMessage.validateGuessSchemaRuntimeError", e1.getMessage());
        validationResult.setStatus(ValidationResult.Result.ERROR).setMessage(errorMessage);
    } catch (IOException e2) {
        String errorMessage = getI18nMessage("errorMessage.validateGuessSchemaConnectionError", e2.getMessage());
        validationResult.setStatus(ValidationResult.Result.ERROR).setMessage(errorMessage);
    }
    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) TalendRuntimeException(org.talend.daikon.exception.TalendRuntimeException) Schema(org.apache.avro.Schema) ValidationResultMutable(org.talend.daikon.properties.ValidationResultMutable) SalesforceRuntimeSourceOrSink(org.talend.components.salesforce.common.SalesforceRuntimeSourceOrSink) IOException(java.io.IOException)

Example 4 with SalesforceSchemaHelper

use of org.talend.components.salesforce.schema.SalesforceSchemaHelper in project components by Talend.

the class TSalesforceInputPropertiesTest method testValidateGuessQuery.

@Test
public void testValidateGuessQuery() throws Exception {
    properties.init();
    String query = "\"SELECT Id, Name, BillingCity FROM Account\"";
    Schema schema = SchemaBuilder.record("Result").fields().name("Id").type().stringType().noDefault().name("Name").type().stringType().noDefault().name("BillingCity").type().stringType().noDefault().endRecord();
    Schema emptySchema = AvroUtils.createEmptySchema();
    properties.module.moduleName.setValue("Account");
    properties.module.main.schema.setValue(schema);
    try (MockRuntimeSourceOrSinkTestFixture testFixture = new MockRuntimeSourceOrSinkTestFixture(equalTo(properties), createDefaultTestDataset())) {
        testFixture.setUp();
        // Valid
        when(((SalesforceSchemaHelper) testFixture.runtimeSourceOrSink).guessQuery(eq(schema), eq("Account"))).thenReturn(query);
        ValidationResult vr1 = properties.validateGuessQuery();
        assertEquals(ValidationResult.Result.OK, vr1.getStatus());
        assertEquals(query, properties.query.getValue());
        // Not valid
        properties.module.main.schema.setValue(emptySchema);
        ValidationResult vr2 = properties.validateGuessQuery();
        assertEquals(ValidationResult.Result.ERROR, vr2.getStatus());
        assertEquals("", properties.query.getValue());
        // Error
        when(((SalesforceSchemaHelper) testFixture.runtimeSourceOrSink).guessQuery(eq(schema), eq("Account"))).thenThrow(TalendRuntimeException.createUnexpectedException("ERROR"));
        properties.module.main.schema.setValue(schema);
        properties.query.setValue(query);
        ValidationResult vr3 = properties.validateGuessQuery();
        assertEquals(ValidationResult.Result.ERROR, vr3.getStatus());
        assertEquals(query, properties.query.getValue());
    }
}
Also used : SalesforceSchemaHelper(org.talend.components.salesforce.schema.SalesforceSchemaHelper) Schema(org.apache.avro.Schema) ValidationResult(org.talend.daikon.properties.ValidationResult) Test(org.junit.Test)

Aggregations

Schema (org.apache.avro.Schema)4 SalesforceSchemaHelper (org.talend.components.salesforce.schema.SalesforceSchemaHelper)4 TalendRuntimeException (org.talend.daikon.exception.TalendRuntimeException)3 IOException (java.io.IOException)2 Test (org.junit.Test)2 SalesforceDefinition.getSandboxedInstance (org.talend.components.salesforce.SalesforceDefinition.getSandboxedInstance)2 SalesforceRuntimeSourceOrSink (org.talend.components.salesforce.common.SalesforceRuntimeSourceOrSink)2 ValidationResult (org.talend.daikon.properties.ValidationResult)2 ValidationResultMutable (org.talend.daikon.properties.ValidationResultMutable)2 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)2