Search in sources :

Example 1 with MetadataFillFactory

use of org.talend.metadata.managment.model.MetadataFillFactory in project tdq-studio-se by Talend.

the class SoftwareSystemManagerTest method testUpdateSoftwareSystem.

/**
 * Test method for
 * {@link org.talend.cwm.management.api.SoftwareSystemManager#updateSoftwareSystem(java.sql.Connection, orgomg.cwm.foundation.softwaredeployment.SoftwareSystem)}
 * .
 */
@Test
public void testUpdateSoftwareSystem() {
    SoftwareSystemManager softwareSystemManger = SoftwareSystemManager.getInstance();
    PowerMockito.mockStatic(EMFSharedResources.class);
    EMFSharedResources emfSharedResourceMock = mock(EMFSharedResources.class);
    when(EMFSharedResources.getInstance()).thenReturn(emfSharedResourceMock).thenReturn(emfSharedResourceMock);
    Resource softwareSysResource = mock(Resource.class);
    when(emfSharedResourceMock.getSoftwareDeploymentResource()).thenReturn(softwareSysResource);
    EList<EObject> existedSystems = new BasicEList<EObject>();
    when(softwareSysResource.getContents()).thenReturn(existedSystems);
    when(emfSharedResourceMock.saveResource(softwareSysResource)).thenReturn(Boolean.TRUE);
    try {
        softwareSystemManger.updateSoftwareSystem(null);
        Connection mockConnection = mock(Connection.class);
        DatabaseMetaData mockDBMetaData = mock(DatabaseMetaData.class);
        // Mocking Static Class
        PowerMockito.mockStatic(DriverManager.class);
        // $NON-NLS-1$
        String sql = "jdbc:mysql://localhost:3306/tbi";
        when(DriverManager.getConnection(sql)).thenReturn(mockConnection);
        when(mockConnection.getMetaData()).thenReturn(mockDBMetaData);
        // Mock database product name.
        // $NON-NLS-1$
        String mysqlDB = "MYSQL";
        when(mockDBMetaData.getDatabaseProductName()).thenReturn(mysqlDB);
        // Mock database product version
        // $NON-NLS-1$
        String version = "5.5";
        when(mockDBMetaData.getDatabaseProductVersion()).thenReturn(version);
        when(mockDBMetaData.getDatabaseMinorVersion()).thenReturn(5);
        when(mockDBMetaData.getDatabaseMajorVersion()).thenReturn(5);
        DatabaseConnection dbConn = ConnectionPackage.eINSTANCE.getConnectionFactory().createDatabaseConnection();
        TaggedValueHelper.setTaggedValue(dbConn, TaggedValueHelper.DB_PRODUCT_NAME, mysqlDB);
        TaggedValueHelper.setTaggedValue(dbConn, TaggedValueHelper.DB_PRODUCT_VERSION, version);
        PowerMockito.mockStatic(ConvertionHelper.class);
        // $NON-NLS-1$
        when(ConvertionHelper.convert(dbConn, false, "")).thenReturn(null);
        PowerMockito.mockStatic(MetadataFillFactory.class);
        MetadataFillFactory metadataFillFactory = mock(MetadataFillFactory.class);
        when(MetadataFillFactory.getDBInstance()).thenReturn(metadataFillFactory);
        TypedReturnCode<java.sql.Connection> returnCode = new TypedReturnCode<Connection>(Boolean.TRUE);
        returnCode.setObject(mockConnection);
        when(metadataFillFactory.createConnection(null)).thenReturn(returnCode);
        softwareSystemManger.updateSoftwareSystem(dbConn);
        Assert.assertTrue(existedSystems.size() > 0);
        SoftwareSystem newSoftwareSys = (SoftwareSystem) existedSystems.get(0);
        Assert.assertEquals(mysqlDB, newSoftwareSys.getSubtype());
        Assert.assertEquals(version, newSoftwareSys.getVersion());
    } catch (SQLException e) {
        Assert.fail(e.getMessage());
    }
}
Also used : SQLException(java.sql.SQLException) BasicEList(org.eclipse.emf.common.util.BasicEList) Resource(org.eclipse.emf.ecore.resource.Resource) Connection(java.sql.Connection) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) DatabaseMetaData(java.sql.DatabaseMetaData) EMFSharedResources(org.talend.dq.writer.EMFSharedResources) EObject(org.eclipse.emf.ecore.EObject) TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) TdSoftwareSystem(org.talend.cwm.softwaredeployment.TdSoftwareSystem) SoftwareSystem(orgomg.cwm.foundation.softwaredeployment.SoftwareSystem) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) MetadataFillFactory(org.talend.metadata.managment.model.MetadataFillFactory) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with MetadataFillFactory

use of org.talend.metadata.managment.model.MetadataFillFactory in project tdq-studio-se by Talend.

the class UnitTestBuildHelper method getDB2DataManager.

/**
 * getDataManager of DB2
 *
 * @return
 */
public Connection getDB2DataManager() {
    // TypedProperties connectionParams = PropertiesLoader.getProperties(IndicatorEvaluator.class, "db.properties"); //$NON-NLS-1$
    // $NON-NLS-1$
    String driverClassName = "com.ibm.db2.jcc.DB2Driver";
    // $NON-NLS-1$
    String dbUrl = "jdbc:db2://192.168.31.135:50000/sample";
    // $NON-NLS-1$
    String sqlTypeName = "IBM DB2";
    DBConnectionParameter params = new DBConnectionParameter();
    // $NON-NLS-1$
    params.setName("DB2_Connection");
    params.setDriverClassName(driverClassName);
    params.setJdbcUrl(dbUrl);
    params.setSqlTypeName(sqlTypeName);
    Properties properties = new Properties();
    properties.setProperty(TaggedValueHelper.UNIVERSE, "");
    properties.setProperty(TaggedValueHelper.DATA_FILTER, "");
    properties.setProperty(TaggedValueHelper.USER, "db2inst1");
    properties.setProperty(TaggedValueHelper.PASSWORD, "db2inst1");
    params.setParameters(properties);
    // create connection
    ConnectionUtils.setTimeout(false);
    MetadataFillFactory instance = MetadataFillFactory.getDBInstance();
    IMetadataConnection metaConnection = instance.fillUIParams(ParameterUtil.toMap(params));
    ReturnCode rc = null;
    try {
        rc = instance.checkConnection(metaConnection);
    } catch (java.lang.RuntimeException e) {
        Assert.fail("connect to " + dbUrl + "failed," + e.getMessage());
    }
    Connection dataProvider = null;
    if (rc.isOk()) {
        dataProvider = instance.fillUIConnParams(metaConnection, null);
        dataProvider.setName("DB2_Connection");
        // because the DI side code is changed, modify the following code.
        metaConnection.setCurrentConnection(dataProvider);
        try {
            ProjectNodeHelper.fillCatalogAndSchemas(metaConnection, (DatabaseConnection) dataProvider);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    Assert.assertNotNull("Can not connect to Database: " + dbUrl, dataProvider);
    return dataProvider;
}
Also used : ReturnCode(org.talend.utils.sugars.ReturnCode) SQLException(java.sql.SQLException) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) DelimitedFileConnection(org.talend.core.model.metadata.builder.connection.DelimitedFileConnection) Connection(org.talend.core.model.metadata.builder.connection.Connection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) Properties(java.util.Properties) DBConnectionParameter(org.talend.dq.analysis.parameters.DBConnectionParameter) MetadataFillFactory(org.talend.metadata.managment.model.MetadataFillFactory)

Example 3 with MetadataFillFactory

use of org.talend.metadata.managment.model.MetadataFillFactory in project tdq-studio-se by Talend.

the class UnitTestBuildHelper method getRealOracleDatabase.

/**
 * get a real database connection,the connection parameters load from a propery file.
 *
 * @return
 */
public static Connection getRealOracleDatabase() {
    Properties connectionParams = DBPropertiesUtils.getDefault().getProperties();
    // $NON-NLS-1$
    String driverClassName = connectionParams.getProperty("driver_oracle");
    // $NON-NLS-1$
    String dbUrl = connectionParams.getProperty("url_oracle");
    // $NON-NLS-1$
    String sqlTypeName = connectionParams.getProperty("sqlTypeName_oracle");
    // $NON-NLS-1$
    String dbVersion = connectionParams.getProperty("dbVersion_oracle");
    // $NON-NLS-1$
    String userName = connectionParams.getProperty("user_oracle");
    // $NON-NLS-1$
    String password = connectionParams.getProperty("password_oracle");
    DBConnectionParameter params = new DBConnectionParameter();
    // $NON-NLS-1$
    params.setName("My connection");
    params.setDriverClassName(driverClassName);
    params.setJdbcUrl(dbUrl);
    params.setSqlTypeName(sqlTypeName);
    params.setParameters(connectionParams);
    params.getParameters();
    Properties properties = new Properties();
    properties.setProperty(TaggedValueHelper.USER, userName);
    properties.setProperty(TaggedValueHelper.PASSWORD, password);
    properties.setProperty(TaggedValueHelper.DB_PRODUCT_VERSION, dbVersion);
    params.setParameters(properties);
    // create connection
    ConnectionUtils.setTimeout(false);
    MetadataFillFactory instance = MetadataFillFactory.getDBInstance();
    IMetadataConnection metaConnection = instance.fillUIParams(ParameterUtil.toMap(params));
    metaConnection.setDbVersionString(dbVersion);
    ReturnCode rc = null;
    try {
        rc = instance.checkConnection(metaConnection);
    } catch (java.lang.RuntimeException e) {
        // $NON-NLS-1$ //$NON-NLS-2$
        Assert.fail("connect to " + dbUrl + "failed," + e.getMessage());
    }
    Connection dataProvider = null;
    if (rc.isOk()) {
        dataProvider = instance.fillUIConnParams(metaConnection, null);
        dataProvider.setName(DATA_PROVIDER_NAME);
        // because the DI side code is changed, modify the following code.
        metaConnection.setCurrentConnection(dataProvider);
        try {
            ProjectNodeHelper.fillCatalogAndSchemas(metaConnection, (DatabaseConnection) dataProvider);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    Assert.assertNotNull(dataProvider);
    return dataProvider;
}
Also used : ReturnCode(org.talend.utils.sugars.ReturnCode) SQLException(java.sql.SQLException) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) DelimitedFileConnection(org.talend.core.model.metadata.builder.connection.DelimitedFileConnection) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) Connection(org.talend.core.model.metadata.builder.connection.Connection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) Properties(java.util.Properties) EProperties(org.talend.repository.model.IRepositoryNode.EProperties) DBConnectionParameter(org.talend.dq.analysis.parameters.DBConnectionParameter) MetadataFillFactory(org.talend.metadata.managment.model.MetadataFillFactory)

Example 4 with MetadataFillFactory

use of org.talend.metadata.managment.model.MetadataFillFactory in project tdq-studio-se by Talend.

the class ExportConnectionToTOSAction method fillCatalogSchema.

/**
 * DOC gdbu Comment method "fillCatalogSchema".
 *
 * @deprecated Won't be used.
 * @param tdDataProvider
 */
@Deprecated
protected DatabaseConnection fillCatalogSchema(IMetadataConnection newMetadataConn) {
    MetadataFillFactory instance = MetadataFillFactory.getDBInstance(newMetadataConn);
    ReturnCode rc = instance.createConnection(newMetadataConn);
    DatabaseConnection dbConn = null;
    if (rc.isOk()) {
        dbConn = (DatabaseConnection) instance.fillUIConnParams(newMetadataConn, null);
        DatabaseMetaData dbMetadata = null;
        java.sql.Connection sqlConn = null;
        try {
            if (rc instanceof TypedReturnCode) {
                Object sqlConnObject = ((TypedReturnCode) rc).getObject();
                if (sqlConnObject instanceof java.sql.Connection) {
                    sqlConn = (java.sql.Connection) sqlConnObject;
                    dbMetadata = ExtractMetaDataUtils.getInstance().getConnectionMetadata(sqlConn);
                }
            }
            List<String> packageFilterCatalog = MetadataConnectionUtils.getPackageFilter(dbConn, dbMetadata, true);
            instance.fillCatalogs(dbConn, dbMetadata, packageFilterCatalog);
            List<String> packageFilterSchema = MetadataConnectionUtils.getPackageFilter(dbConn, dbMetadata, false);
            instance.fillSchemas(dbConn, dbMetadata, packageFilterSchema);
        } catch (SQLException e) {
            log.error(e, e);
        } finally {
            if (sqlConn != null) {
                ConnectionUtils.closeConnection(sqlConn);
            }
        }
    } else {
        log.error(rc.getMessage());
    }
    return dbConn;
}
Also used : TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) ReturnCode(org.talend.utils.sugars.ReturnCode) SQLException(java.sql.SQLException) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) DatabaseMetaData(java.sql.DatabaseMetaData) TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) MetadataFillFactory(org.talend.metadata.managment.model.MetadataFillFactory) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection)

Example 5 with MetadataFillFactory

use of org.talend.metadata.managment.model.MetadataFillFactory in project tdq-studio-se by Talend.

the class DQStructureComparer method getRefreshedDataProvider.

public static TypedReturnCode<Connection> getRefreshedDataProvider(Connection prevDataProvider) {
    // ADD xqliu 2010-03-29 bug 11951
    TypedReturnCode<Connection> returnProvider = new TypedReturnCode<Connection>();
    // ~11951
    // MOD by zshen 2012-07-05 for bug 5074 remove convert about DatabaseParameter instead
    // Connection->DatabaseParameter->ImetadataConnection into Connection->ImetadataConnection
    IMetadataConnection metadataConnection = ConvertionHelper.convert((DatabaseConnection) prevDataProvider, false, prevDataProvider.getContextName());
    Connection copyedConnection = null;
    EDatabaseTypeName currentEDatabaseType = EDatabaseTypeName.getTypeFromDbType(metadataConnection.getDbType());
    if (currentEDatabaseType != null) {
        MetadataFillFactory dbInstance = MetadataFillFactory.getDBInstance(metadataConnection);
        TypedReturnCode<?> trc = (TypedReturnCode<?>) dbInstance.createConnection(metadataConnection);
        Object sqlConnObject = trc.getObject();
        DatabaseMetaData dbJDBCMetadata = null;
        if (trc.isOk() && sqlConnObject instanceof java.sql.Connection) {
            java.sql.Connection sqlConn = (java.sql.Connection) sqlConnObject;
            // MOD sizhaoliu 2012-5-21 TDQ-4884 reload structure issue
            // dbJDBCMetadata = org.talend.utils.sql.ConnectionUtils.getConnectionMetadata(sqlConn);
            dbJDBCMetadata = ExtractMetaDataUtils.getInstance().getDatabaseMetaData(sqlConn, (DatabaseConnection) prevDataProvider);
            copyedConnection = EObjectHelper.deepCopy(prevDataProvider);
            copyedConnection.getDataPackage().clear();
            // MOD zshen the parameter for packageFiler need to differnent isCatalog or not.
            dbInstance.fillCatalogs(copyedConnection, dbJDBCMetadata, metadataConnection, MetadataConnectionUtils.getPackageFilter(copyedConnection, dbJDBCMetadata, true));
            dbInstance.fillSchemas(copyedConnection, dbJDBCMetadata, metadataConnection, MetadataConnectionUtils.getPackageFilter(copyedConnection, dbJDBCMetadata, false));
            ConnectionUtils.closeConnection(sqlConn);
        } else {
            returnProvider.setMessage(trc.getMessage());
        }
    }
    if (copyedConnection == null) {
        returnProvider.setOk(false);
    } else {
        returnProvider.setObject(copyedConnection);
    }
    // ~11951
    return returnProvider;
}
Also used : DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) Connection(org.talend.core.model.metadata.builder.connection.Connection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) DatabaseMetaData(java.sql.DatabaseMetaData) TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) MetadataFillFactory(org.talend.metadata.managment.model.MetadataFillFactory) EObject(org.eclipse.emf.ecore.EObject) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) EDatabaseTypeName(org.talend.core.database.EDatabaseTypeName)

Aggregations

MetadataFillFactory (org.talend.metadata.managment.model.MetadataFillFactory)8 SQLException (java.sql.SQLException)7 IMetadataConnection (org.talend.core.model.metadata.IMetadataConnection)7 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)7 Connection (org.talend.core.model.metadata.builder.connection.Connection)6 ReturnCode (org.talend.utils.sugars.ReturnCode)6 DatabaseMetaData (java.sql.DatabaseMetaData)4 DBConnectionParameter (org.talend.dq.analysis.parameters.DBConnectionParameter)4 TypedReturnCode (org.talend.utils.sugars.TypedReturnCode)4 Properties (java.util.Properties)3 DelimitedFileConnection (org.talend.core.model.metadata.builder.connection.DelimitedFileConnection)3 EObject (org.eclipse.emf.ecore.EObject)2 Connection (java.sql.Connection)1 BasicEList (org.eclipse.emf.common.util.BasicEList)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 EDatabaseTypeName (org.talend.core.database.EDatabaseTypeName)1 TdSoftwareSystem (org.talend.cwm.softwaredeployment.TdSoftwareSystem)1 EMFSharedResources (org.talend.dq.writer.EMFSharedResources)1