Search in sources :

Example 1 with DriverDefInfo

use of org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo in project kie-wb-common by kiegroup.

the class DataSourceDefEditorServiceImpl method testConnection.

@Override
public TestResult testConnection(DataSourceDef dataSourceDef, Module module) {
    TestResult result = new TestResult(false);
    if (isEmpty(dataSourceDef.getConnectionURL())) {
        result.setMessage("A valid connection url is required");
        return result;
    }
    if (isEmpty(dataSourceDef.getUser()) || isEmpty(dataSourceDef.getPassword())) {
        result.setMessage("A valid user and password are required");
        return result;
    }
    DriverDefInfo driverDefInfo = null;
    if (isEmpty(dataSourceDef.getDriverUuid())) {
        result.setMessage("A valid driver is required");
        return result;
    }
    if (module != null) {
        driverDefInfo = dataSourceDefQueryService.findModuleDriver(dataSourceDef.getDriverUuid(), module.getRootPath());
    } else {
        driverDefInfo = dataSourceDefQueryService.findGlobalDriver(dataSourceDef.getDriverUuid());
    }
    if (driverDefInfo == null) {
        result.setMessage("Data source driver: " + dataSourceDef.getUuid() + " was not found");
        return result;
    }
    DriverDefEditorContent driverDefEditorContent = driverDefService.loadContent(driverDefInfo.getPath());
    DriverDef driverDef = driverDefEditorContent.getDef();
    URI uri;
    try {
        uri = artifactResolver.resolve(driverDef.getGroupId(), driverDef.getArtifactId(), driverDef.getVersion());
    } catch (Exception e) {
        result.setMessage("Connection could not be tested due to the following error: " + e.getMessage());
        return result;
    }
    if (uri == null) {
        result.setMessage("Driver artifact: " + driverDef.getGroupId() + ":" + driverDef.getArtifactId() + ":" + driverDef.getVersion() + " was not found");
        return result;
    }
    try {
        Properties properties = new Properties();
        properties.put("user", dataSourceDef.getUser());
        properties.put("password", dataSourceDef.getPassword());
        URLConnectionFactory connectionFactory = new URLConnectionFactory(uri.toURL(), driverDef.getDriverClass(), dataSourceDef.getConnectionURL(), properties);
        Connection conn = connectionFactory.createConnection();
        if (conn == null) {
            result.setMessage("It was not possible to open connection");
        } else {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("Connection was successfully obtained: " + conn);
            stringBuilder.append("\n");
            stringBuilder.append("*** DatabaseProductName: " + conn.getMetaData().getDatabaseProductName());
            stringBuilder.append("\n");
            stringBuilder.append("*** DatabaseProductVersion: " + conn.getMetaData().getDatabaseProductVersion());
            stringBuilder.append("\n");
            stringBuilder.append("*** DriverName: " + conn.getMetaData().getDriverName());
            stringBuilder.append("\n");
            stringBuilder.append("*** DriverVersion: " + conn.getMetaData().getDriverVersion());
            stringBuilder.append("\n");
            conn.close();
            stringBuilder.append("Connection was successfully released.");
            stringBuilder.append("\n");
            result.setTestPassed(true);
            result.setMessage(stringBuilder.toString());
        }
        return result;
    } catch (Exception e) {
        result.setMessage(e.getMessage());
        return result;
    }
}
Also used : URLConnectionFactory(org.kie.workbench.common.screens.datasource.management.util.URLConnectionFactory) DriverDefInfo(org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo) DriverDefEditorContent(org.kie.workbench.common.screens.datasource.management.model.DriverDefEditorContent) Connection(java.sql.Connection) TestResult(org.kie.workbench.common.screens.datasource.management.model.TestResult) DriverDef(org.kie.workbench.common.screens.datasource.management.model.DriverDef) Properties(java.util.Properties) URI(java.net.URI)

Example 2 with DriverDefInfo

use of org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo in project kie-wb-common by kiegroup.

the class DataSourceDefQueryServiceTest method setupExpectedResults.

private void setupExpectedResults() {
    expectedDataSources = new ArrayList<>();
    expectedDataSources.add(new DataSourceDefInfo("ds1Id", "DS1", Paths.convert(nioDataSourcesPath.resolve("DS1.datasource")), null));
    expectedDataSources.add(new DataSourceDefInfo("ds2Id", "DS2", Paths.convert(nioDataSourcesPath.resolve("DS2.datasource")), null));
    expectedDrivers = new ArrayList<>();
    expectedDrivers.add(new DriverDefInfo("driver1Id", "Driver1", Paths.convert(nioDataSourcesPath.resolve("Driver1.driver")), null));
    expectedDrivers.add(new DriverDefInfo("driver2Id", "Driver2", Paths.convert(nioDataSourcesPath.resolve("Driver2.driver")), null));
}
Also used : DriverDefInfo(org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo) DataSourceDefInfo(org.kie.workbench.common.screens.datasource.management.model.DataSourceDefInfo)

Example 3 with DriverDefInfo

use of org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo in project kie-wb-common by kiegroup.

the class DataSourceDefQueryServiceTest method testFindModuleDriverByUuid.

@Test
public void testFindModuleDriverByUuid() {
    when(moduleService.resolveModule(modulePath)).thenReturn(module);
    when(serviceHelper.getModuleDataSourcesContext(module)).thenReturn(dataSourcesPath);
    DriverDefInfo driverDefInfo = queryService.findModuleDriver("driver2Id", modulePath);
    assertEquals(expectedDrivers.get(1), driverDefInfo);
}
Also used : DriverDefInfo(org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo) Test(org.junit.Test)

Example 4 with DriverDefInfo

use of org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo in project kie-wb-common by kiegroup.

the class DataSourceDefEditorHelper method onDriverChange.

public void onDriverChange() {
    DriverDefInfo driverDef = driverDefMap.get(mainPanel.getDriver());
    driverValid = driverDef != null;
    if (!driverValid) {
        mainPanel.setDriverErrorMessage(getMessage(DataSourceManagementConstants.DataSourceDefEditor_DriverRequiredMessage));
        dataSourceDef.setDriverUuid(null);
    } else {
        mainPanel.clearDriverErrorMessage();
        dataSourceDef.setDriverUuid(driverDef.getUuid());
    }
    if (handler != null) {
        handler.onDriverChange();
    }
}
Also used : DriverDefInfo(org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo)

Example 5 with DriverDefInfo

use of org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo in project kie-wb-common by kiegroup.

the class DataSourceDefEditorHelperTest method setup.

@Before
public void setup() {
    clientValidationService = new ClientValidationServiceMock();
    editorServiceCaller = new CallerMock<>(editorService);
    queryServiceCaller = new CallerMock<>(queryService);
    when(driverDefInfo.getUuid()).thenReturn(DRIVER_UUID);
    when(driverDefInfo.getName()).thenReturn("DriverName");
    List<DriverDefInfo> drivers = new ArrayList<>();
    drivers.add(driverDefInfo);
    when(queryService.findGlobalDrivers()).thenReturn(drivers);
    when(queryService.findModuleDrivers(any(Path.class))).thenReturn(drivers);
    editorHelper = new DataSourceDefEditorHelper(translationService, editorServiceCaller, queryServiceCaller, clientValidationService, popupsUtil);
    editorHelper.setHandler(handler);
    editorHelper.init(mainPanel);
    editorHelper.loadDrivers(new Command() {

        @Override
        public void execute() {
        // do nothing
        }
    }, new ParameterizedCommand<Throwable>() {

        @Override
        public void execute(Throwable parameter) {
        // do nothing
        }
    });
    dataSourceDef = new DataSourceDef();
    editorHelper.setDataSourceDef(dataSourceDef);
    verify(mainPanel, times(1)).clear();
    verify(mainPanel, times(1)).setName(dataSourceDef.getName());
    verify(mainPanel, times(1)).setConnectionURL(dataSourceDef.getConnectionURL());
    verify(mainPanel, times(1)).setUser(dataSourceDef.getUser());
    verify(mainPanel, times(1)).setPassword(dataSourceDef.getPassword());
    verify(mainPanel, times(1)).setDriver(dataSourceDef.getDriverUuid());
}
Also used : Path(org.uberfire.backend.vfs.Path) DriverDefInfo(org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo) ParameterizedCommand(org.uberfire.mvp.ParameterizedCommand) Command(org.uberfire.mvp.Command) ArrayList(java.util.ArrayList) DataSourceDef(org.kie.workbench.common.screens.datasource.management.model.DataSourceDef) ClientValidationServiceMock(org.kie.workbench.common.screens.datasource.management.client.util.ClientValidationServiceMock) Before(org.junit.Before)

Aggregations

DriverDefInfo (org.kie.workbench.common.screens.datasource.management.model.DriverDefInfo)10 ArrayList (java.util.ArrayList)3 Before (org.junit.Before)2 DataSourceDefInfo (org.kie.workbench.common.screens.datasource.management.model.DataSourceDefInfo)2 DriverDef (org.kie.workbench.common.screens.datasource.management.model.DriverDef)2 Path (org.uberfire.backend.vfs.Path)2 URI (java.net.URI)1 Connection (java.sql.Connection)1 Properties (java.util.Properties)1 WorkspaceProject (org.guvnor.common.services.project.model.WorkspaceProject)1 Branch (org.guvnor.structure.repositories.Branch)1 Test (org.junit.Test)1 ClientValidationServiceMock (org.kie.workbench.common.screens.datasource.management.client.util.ClientValidationServiceMock)1 DataSourceDef (org.kie.workbench.common.screens.datasource.management.model.DataSourceDef)1 DriverDefEditorContent (org.kie.workbench.common.screens.datasource.management.model.DriverDefEditorContent)1 DriverDeploymentInfo (org.kie.workbench.common.screens.datasource.management.model.DriverDeploymentInfo)1 TestResult (org.kie.workbench.common.screens.datasource.management.model.TestResult)1 URLConnectionFactory (org.kie.workbench.common.screens.datasource.management.util.URLConnectionFactory)1 Pair (org.uberfire.commons.data.Pair)1 Command (org.uberfire.mvp.Command)1