Search in sources :

Example 1 with Context

use of com.alibaba.citrus.turbine.Context in project plugins by qlangtech.

the class TestDataxMySQLReader method testGetSubTasks.

public void testGetSubTasks() {
    MySQLDataSourceFactory mysqlDs = new MySQLDataSourceFactory() {

        // @Override
        // protected Connection getConnection(String jdbcUrl, String username, String password) throws SQLException {
        // throw new UnsupportedOperationException();
        // }
        @Override
        public List<ColumnMetaData> getTableMetadata(String table) {
            switch(table) {
                case TestSelectedTabs.tabNameOrderDetail:
                    return TestSelectedTabs.tabColsMetaOrderDetail;
                case TestSelectedTabs.tabNameTotalpayinfo:
                    return TestSelectedTabs.tabColsMetaTotalpayinfo;
                default:
                    throw new IllegalArgumentException("table:" + table);
            }
        }
    };
    mysqlDs.dbName = dbName;
    mysqlDs.port = 3306;
    mysqlDs.encode = "utf8";
    mysqlDs.userName = userName;
    mysqlDs.password = password;
    mysqlDs.nodeDesc = "192.168.28.200[0-7]";
    Descriptor.ParseDescribable<DataSourceFactory> desc = new Descriptor.ParseDescribable<>(mysqlDs);
    Context context = EasyMock.createMock("context", Context.class);
    EasyMock.expect(context.hasErrors()).andReturn(false);
    IPluginContext pluginContext = EasyMock.createMock("pluginContext", IPluginContext.class);
    pluginContext.addDb(desc, dbName, context, true);
    EasyMock.replay(pluginContext, context);
    DataSourceFactoryPluginStore dbStore = TIS.getDataBasePluginStore(new PostedDSProp(dbName));
    assertTrue("save mysql db Config faild", dbStore.setPlugins(pluginContext, Optional.of(context), Collections.singletonList(desc)));
    DataxMySQLReader mySQLReader = new DataxMySQLReader() {

        @Override
        public MySQLDataSourceFactory getDataSourceFactory() {
            return mysqlDs;
        }
    };
    mySQLReader.dataXName = this.dataXName;
    List<SelectedTab> selectedTabs = TestSelectedTabs.createSelectedTabs();
    mySQLReader.setSelectedTabs(selectedTabs);
    List<SelectedTab> selectedTabs2 = mySQLReader.getSelectedTabs();
    assertEquals(2, selectedTabs2.size());
    for (SelectedTab tab : selectedTabs2) {
        tab.getCols().forEach((c) -> {
            assertNotNull("table:" + tab.getName() + "'s col " + c.getName() + " relevant type can not be null", c.getType());
        });
    }
    List<String> tabs = Lists.newArrayList();
    for (SelectedTab tab : selectedTabs) {
        for (int i = 0; i < 8; i++) {
            tabs.add(tab.name);
        }
    }
    int readerContextCount = 0;
    IDataxReaderContext readerContext = null;
    IGroupChildTaskIterator subTasks = mySQLReader.getSubTasks();
    while (subTasks.hasNext()) {
        readerContext = subTasks.next();
        assertEquals(tabs.get(readerContextCount), readerContext.getSourceEntityName());
        assertEquals(tabs.get(readerContextCount) + "_" + readerContextCount, readerContext.getTaskName());
        System.out.println(readerContext.getSourceEntityName() + " " + readerContext.getTaskName());
        assertNotNull(readerContext);
        readerContextCount++;
    }
    assertEquals(16, readerContextCount);
    Map<String, List<String>> groupedInfo = subTasks.getGroupedInfo();
    assertNotNull("groupedInfo can not be null", groupedInfo);
    List<String> subTabs = groupedInfo.get(TestSelectedTabs.tabNameOrderDetail);
    assertEquals(8, subTabs.size());
    subTabs = groupedInfo.get(TestSelectedTabs.tabNameTotalpayinfo);
    assertEquals(8, subTabs.size());
    EasyMock.verify(pluginContext, context);
}
Also used : Context(com.alibaba.citrus.turbine.Context) IDataxReaderContext(com.qlangtech.tis.datax.IDataxReaderContext) IPluginContext(com.qlangtech.tis.util.IPluginContext) MySQLDataSourceFactory(com.qlangtech.tis.plugin.ds.mysql.MySQLDataSourceFactory) IPluginContext(com.qlangtech.tis.util.IPluginContext) IDataxReaderContext(com.qlangtech.tis.datax.IDataxReaderContext) IGroupChildTaskIterator(com.qlangtech.tis.datax.IGroupChildTaskIterator) Descriptor(com.qlangtech.tis.extension.Descriptor) List(java.util.List) MySQLDataSourceFactory(com.qlangtech.tis.plugin.ds.mysql.MySQLDataSourceFactory)

Example 2 with Context

use of com.alibaba.citrus.turbine.Context in project plugins by qlangtech.

the class TestDataxMySQLWriter method testTempateGenerate.

public void testTempateGenerate() throws Exception {
    Optional<PluginExtraProps> extraProps = PluginExtraProps.load(DataxMySQLWriter.class);
    assertTrue("DataxMySQLWriter extraProps shall exist", extraProps.isPresent());
    IPluginContext pluginContext = EasyMock.createMock("pluginContext", IPluginContext.class);
    Context context = EasyMock.createMock("context", Context.class);
    EasyMock.expect(context.hasErrors()).andReturn(false);
    MySQLDataSourceFactory mysqlDs = new MySQLDataSourceFactory() {

        @Override
        public Connection getConnection(String jdbcUrl) throws SQLException {
            return null;
        }
    };
    mysqlDs.dbName = dbWriterName;
    mysqlDs.port = 3306;
    mysqlDs.encode = "utf8";
    mysqlDs.userName = "root";
    mysqlDs.password = "123456";
    mysqlDs.nodeDesc = "192.168.28.200";
    Descriptor.ParseDescribable<DataSourceFactory> desc = new Descriptor.ParseDescribable<>(mysqlDs);
    pluginContext.addDb(desc, dbWriterName, context, true);
    EasyMock.replay(pluginContext, context);
    DataSourceFactoryPluginStore dbStore = TIS.getDataBasePluginStore(new PostedDSProp(dbWriterName));
    assertTrue("save mysql db Config faild", dbStore.setPlugins(pluginContext, Optional.of(context), Collections.singletonList(desc)));
    DataxMySQLWriter mySQLWriter = new DataxMySQLWriter();
    mySQLWriter.dataXName = dataXName;
    mySQLWriter.writeMode = "replace";
    mySQLWriter.dbName = dbWriterName;
    mySQLWriter.template = DataxMySQLWriter.getDftTemplate();
    mySQLWriter.batchSize = 1001;
    mySQLWriter.preSql = "delete from test";
    mySQLWriter.postSql = "delete from test1";
    mySQLWriter.session = "set session sql_mode='ANSI'";
    validateConfigGenerate("mysql-datax-writer-assert.json", mySQLWriter);
    // System.out.println(mySQLWriter.getTemplate());
    // 将非必须输入的值去掉再测试一遍
    mySQLWriter.batchSize = null;
    mySQLWriter.preSql = null;
    mySQLWriter.postSql = null;
    mySQLWriter.session = null;
    validateConfigGenerate("mysql-datax-writer-assert-without-option-val.json", mySQLWriter);
    mySQLWriter.preSql = " ";
    mySQLWriter.postSql = " ";
    mySQLWriter.session = " ";
    validateConfigGenerate("mysql-datax-writer-assert-without-option-val.json", mySQLWriter);
}
Also used : IDataxContext(com.qlangtech.tis.datax.IDataxContext) Context(com.alibaba.citrus.turbine.Context) IPluginContext(com.qlangtech.tis.util.IPluginContext) DataSourceFactory(com.qlangtech.tis.plugin.ds.DataSourceFactory) MySQLDataSourceFactory(com.qlangtech.tis.plugin.ds.mysql.MySQLDataSourceFactory) IPluginContext(com.qlangtech.tis.util.IPluginContext) PostedDSProp(com.qlangtech.tis.plugin.ds.PostedDSProp) DataSourceFactoryPluginStore(com.qlangtech.tis.plugin.ds.DataSourceFactoryPluginStore) Descriptor(com.qlangtech.tis.extension.Descriptor) PluginExtraProps(com.qlangtech.tis.extension.util.PluginExtraProps) MySQLDataSourceFactory(com.qlangtech.tis.plugin.ds.mysql.MySQLDataSourceFactory)

Example 3 with Context

use of com.alibaba.citrus.turbine.Context in project plugins by qlangtech.

the class TestDefaultK8SImage method testHostAliasesValidate.

public void testHostAliasesValidate() {
    // https://blog.csdn.net/weixin_47729423/article/details/114288431
    DefaultK8SImage k8SImage = new DefaultK8SImage();
    DefaultK8SImage.DescriptorImpl descriptor = (DefaultK8SImage.DescriptorImpl) k8SImage.getDescriptor();
    assertNotNull("descriptor can not be null", descriptor);
    IFieldErrorHandler msgHandler = EasyMock.createMock("msgHandler", IFieldErrorHandler.class);
    Context context = EasyMock.createMock("context", Context.class);
    String fieldName = "hostAliases";
    EasyMock.replay(msgHandler, context);
    assertTrue(descriptor.validateHostAliases(msgHandler, context, fieldName, IOUtils.loadResourceFromClasspath(DefaultK8SImage.class, "DefaultK8SImage-hostaliases-content.yaml")));
    EasyMock.verify(msgHandler, context);
}
Also used : Context(com.alibaba.citrus.turbine.Context) IFieldErrorHandler(com.qlangtech.tis.runtime.module.misc.IFieldErrorHandler)

Example 4 with Context

use of com.alibaba.citrus.turbine.Context in project plugins by qlangtech.

the class DataXOssReader method verifyFormOSSRelative.

public static boolean verifyFormOSSRelative(IControlMsgHandler msgHandler, Context context, Descriptor.PostFormVals postFormVals) {
    String endpoint = postFormVals.getField(FIELD_ENDPOINT);
    String bucket = postFormVals.getField(FIELD_BUCKET);
    HttpEndpoint end = ParamsConfig.getItem(endpoint, HttpEndpoint.KEY_DISPLAY_NAME);
    try {
        OSS ossClient = new OSSClientBuilder().build(end.getEndpoint(), end.getAccessKeyId(), end.getAccessKeySecret());
        List<Bucket> buckets = ossClient.listBuckets();
        if (buckets.size() < 1) {
            msgHandler.addErrorMessage(context, "buckets不能为空");
            return false;
        }
        Optional<Bucket> bucketFind = buckets.stream().filter((b) -> StringUtils.equals(bucket, b.getName())).findFirst();
        if (!bucketFind.isPresent()) {
            // msgHandler.addErrorMessage(context, );
            msgHandler.addFieldError(context, FIELD_BUCKET, "还未创建bucket:" + bucket);
            return false;
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return true;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) ParamsConfig(com.qlangtech.tis.config.ParamsConfig) LoggerFactory(org.slf4j.LoggerFactory) FormField(com.qlangtech.tis.plugin.annotation.FormField) Context(com.alibaba.citrus.turbine.Context) IFieldErrorHandler(com.qlangtech.tis.runtime.module.misc.IFieldErrorHandler) Matcher(java.util.regex.Matcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultContext(com.alibaba.citrus.turbine.impl.DefaultContext) IGroupChildTaskIterator(com.qlangtech.tis.datax.IGroupChildTaskIterator) HttpEndpoint(com.qlangtech.tis.plugin.aliyun.HttpEndpoint) IDataxReaderContext(com.qlangtech.tis.datax.IDataxReaderContext) FormFieldType(com.qlangtech.tis.plugin.annotation.FormFieldType) Validator(com.qlangtech.tis.plugin.annotation.Validator) TISExtension(com.qlangtech.tis.extension.TISExtension) Logger(org.slf4j.Logger) IHttpToken(com.qlangtech.tis.config.aliyun.IHttpToken) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder) Descriptor(com.qlangtech.tis.extension.Descriptor) OSS(com.aliyun.oss.OSS) List(java.util.List) DataxReader(com.qlangtech.tis.datax.impl.DataxReader) IOUtils(com.qlangtech.tis.extension.impl.IOUtils) Public(com.qlangtech.tis.annotation.Public) IControlMsgHandler(com.qlangtech.tis.runtime.module.misc.IControlMsgHandler) PluginFieldValidators(com.qlangtech.tis.plugin.datax.common.PluginFieldValidators) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Bucket(com.aliyun.oss.model.Bucket) Collections(java.util.Collections) StringEscapeUtils(org.apache.commons.lang.StringEscapeUtils) Bucket(com.aliyun.oss.model.Bucket) HttpEndpoint(com.qlangtech.tis.plugin.aliyun.HttpEndpoint) OSS(com.aliyun.oss.OSS) OSSClientBuilder(com.aliyun.oss.OSSClientBuilder)

Example 5 with Context

use of com.alibaba.citrus.turbine.Context in project plugins by qlangtech.

the class TestRockMqPluginValidate method testDigital_Alpha_CharacterValidate.

public void testDigital_Alpha_CharacterValidate() throws Exception {
    final DefaultContext context = new DefaultContext();
    final Map<String, Object> fieldErrors = context.getContextMap();
    validatePluginPostForm("rockmq_plugin_from_invalid.json", context);
    List<List<DefaultFieldErrorHandler.FieldError>> /**
     * item
     */
    itemsErrorList = (List<List<DefaultFieldErrorHandler.FieldError>>) fieldErrors.get(IFieldErrorHandler.ACTION_ERROR_FIELDS);
    assertNotNull(itemsErrorList);
    assertEquals(1, itemsErrorList.size());
    List<DefaultFieldErrorHandler.FieldError> fieldErrors1 = itemsErrorList.get(0);
    assertEquals(2, fieldErrors1.size());
    Optional<DefaultFieldErrorHandler.FieldError> mqTopicErr = fieldErrors1.stream().filter((r) -> mqTopic.equals(r.getFieldName())).findFirst();
    assertTrue(mqTopicErr.isPresent());
    assertEquals(ValidatorCommons.MSG_IDENTITY_ERROR, mqTopicErr.get().getMsg());
    assertNull(mqTopicErr.get().itemsErrorList);
    assertEquals(mqTopic, mqTopicErr.get().getFieldName());
    Optional<DefaultFieldErrorHandler.FieldError> deserializeErr = fieldErrors1.stream().filter((r) -> deserialize.equals(r.getFieldName())).findFirst();
    assertTrue(deserializeErr.isPresent());
    DefaultFieldErrorHandler.FieldError dErr = deserializeErr.get();
    assertEquals(deserialize, dErr.getFieldName());
    assertNull(dErr.getMsg());
    assertNotNull(dErr.itemsErrorList);
    assertEquals(1, dErr.itemsErrorList.size());
    List<DefaultFieldErrorHandler.FieldError> /**
     * item
     */
    dValsItem = dErr.itemsErrorList.get(0);
    assertEquals(1, dValsItem.size());
    DefaultFieldErrorHandler.FieldError testName = dValsItem.get(0);
    assertEquals(testProp, testName.getFieldName());
    assertEquals("ddd", testName.getMsg());
    assertNull(testName.itemsErrorList);
}
Also used : DelegateControl4JsonPostMsgHandler(com.qlangtech.tis.runtime.module.misc.impl.DelegateControl4JsonPostMsgHandler) Descriptor(com.qlangtech.tis.extension.Descriptor) AttrValMap(com.qlangtech.tis.util.AttrValMap) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Context(com.alibaba.citrus.turbine.Context) IFieldErrorHandler(com.qlangtech.tis.runtime.module.misc.IFieldErrorHandler) JSONArray(com.alibaba.fastjson.JSONArray) IOUtils(org.apache.commons.io.IOUtils) ValidatorCommons(com.qlangtech.tis.plugin.ValidatorCommons) List(java.util.List) DefaultContext(com.alibaba.citrus.turbine.impl.DefaultContext) Map(java.util.Map) TisUTF8(com.qlangtech.tis.manage.common.TisUTF8) Optional(java.util.Optional) DefaultFieldErrorHandler(com.qlangtech.tis.runtime.module.misc.impl.DefaultFieldErrorHandler) InputStream(java.io.InputStream) List(java.util.List) DefaultContext(com.alibaba.citrus.turbine.impl.DefaultContext) DefaultFieldErrorHandler(com.qlangtech.tis.runtime.module.misc.impl.DefaultFieldErrorHandler)

Aggregations

Context (com.alibaba.citrus.turbine.Context)30 Collectors (java.util.stream.Collectors)15 Descriptor (com.qlangtech.tis.extension.Descriptor)14 IControlMsgHandler (com.qlangtech.tis.runtime.module.misc.IControlMsgHandler)13 TIS (com.qlangtech.tis.TIS)12 IOException (java.io.IOException)12 DataxReader (com.qlangtech.tis.datax.impl.DataxReader)11 Func (com.qlangtech.tis.manage.spring.aop.Func)11 Validator (com.qlangtech.tis.plugin.annotation.Validator)11 java.util (java.util)11 StringUtils (org.apache.commons.lang.StringUtils)11 JSONArray (com.alibaba.fastjson.JSONArray)10 Lists (com.google.common.collect.Lists)10 Maps (com.google.common.collect.Maps)10 IPluginStore (com.qlangtech.tis.plugin.IPluginStore)10 IFieldErrorHandler (com.qlangtech.tis.runtime.module.misc.IFieldErrorHandler)10 Pattern (java.util.regex.Pattern)9 JSON (com.alibaba.fastjson.JSON)8 DelegateControl4JsonPostMsgHandler (com.qlangtech.tis.runtime.module.misc.impl.DelegateControl4JsonPostMsgHandler)8 JSONObject (com.alibaba.fastjson.JSONObject)7