@Before
  public void setUp()
      throws ConnectionServiceException, ObjectFactoryException, DBDatasourceServiceException {
    doReturn(nativeConnection).when(sqlConnection).getNativeConnection();
    doReturn(SimpleDataAccessPermissionHandler.class.getName())
        .when(loader)
        .getPluginSetting(this.anyClass(), anyString(), anyString());

    when(pentahoObjectFactory.objectDefined(anyString())).thenReturn(true);
    when(pentahoObjectFactory.get(this.anyClass(), anyString(), any(IPentahoSession.class)))
        .thenAnswer(
            new Answer<Object>() {
              @Override
              public Object answer(InvocationOnMock invocation) throws Throwable {
                if (invocation.getArguments()[0].equals(IPluginResourceLoader.class)) {
                  return loader;
                }
                if (invocation.getArguments()[0].equals(IPentahoConnection.class)) {
                  return sqlConnection;
                }
                if (invocation.getArguments()[0].equals(IDBDatasourceService.class)) {
                  return datasourceService;
                }
                if (invocation.getArguments()[0].equals(PooledDatasourceHelper.class)) {
                  return pdh;
                }
                return null;
              }
            });
    PentahoSystem.registerObjectFactory(pentahoObjectFactory);

    doReturn(databaseType).when(mockDBConnection).getDatabaseType();
    doReturn(CONN_NAME).when(mockDBConnection).getName();
    doNothing().when(mockDBConnection).setPassword(anyString());

    connectionServiceImpl = spy(new ConnectionServiceImpl());
    connectionServiceImpl.datasourceMgmtSvc = mock(IDatasourceMgmtService.class);
    connectionServiceImpl.dialectService = mock(DatabaseDialectService.class);
    connectionServiceImpl.datasourceService = datasourceService;
  }
  public void testAuditConnection_cannot_establish_connection() {
    startTest();
    try {
      // Load mock object factory with mock datasource service that produces null datasources
      StandaloneSpringPentahoObjectFactory factory = new StandaloneSpringPentahoObjectFactory();
      factory.init(
          "test-res/solution/system/pentahoObjects.datasourceservice.null.spring.xml", null);
      PentahoSystem.registerObjectFactory(factory);

      AuditConnection auditConnection = new AuditConnection();
      auditConnection.setUseNewDatasourceService(
          true); // make sure we get a datasource from the object factory
      auditConnection.initialize();
      auditConnection.getAuditConnection();
      fail("Expected exception when no audit connection could be established");
    } catch (SQLException ex) {
      ex.printStackTrace();
      assertTrue("Expected AUDSQLENT.ERROR_0001", ex.getMessage().contains("AUDSQLENT.ERROR_0001"));
    } finally {
      finishTest();
    }
  }
  public static synchronized void initializePentahoSystem(String solutionPath) throws Exception {

    // this setting is useful only for running the integration tests from within IntelliJ
    // this same property is set for integration tests via the pom when running with mvn
    String folderPaths = "target/spoon/plugins";
    File f = new File(folderPaths);
    System.setProperty("KETTLE_PLUGIN_BASE_FOLDERS", f.getAbsolutePath());

    StandaloneApplicationContext appContext = new StandaloneApplicationContext(solutionPath, "");
    PentahoSystem.setSystemSettingsService(new PathBasedSystemSettings());
    if (solutionPath == null) {
      throw new MetaverseException(
          Messages.getString("ERROR.MetaverseInit.BadConfigPath", solutionPath));
    }

    try {
      ClassLoader cl = Thread.currentThread().getContextClassLoader();

      Thread.currentThread().setContextClassLoader(MetaverseUtil.class.getClassLoader());
      IPentahoObjectFactory pentahoObjectFactory = new StandaloneSpringPentahoObjectFactory();
      pentahoObjectFactory.init(solutionPath, PentahoSystem.getApplicationContext());
      PentahoSystem.registerObjectFactory(pentahoObjectFactory);

      // Restore context classloader
      Thread.currentThread().setContextClassLoader(cl);
    } catch (Exception e) {
      throw new MetaverseException(Messages.getString("ERROR.MetaverseInit.CouldNotInit"), e);
    }
    PentahoSystem.init(appContext);
    PentahoSessionHolder.setSession(new StandaloneSession());

    registerKettlePlugins();

    try {
      KettleEnvironment.init();
    } catch (KettleException e) {
      e.printStackTrace();
    }
  }
  @Test
  public void testSyncWebService() throws Exception {

    // first init kettle

    KettleEnvironment.init(false);
    BasePropertyHandler.getInstance().notify(new TestPropertyHandler());
    File f = new File(Const.getKettleDirectory());
    f.mkdirs();

    // second init platform
    PentahoSystem.registerObjectFactory(new SimpleObjectFactory());
    PentahoSystem.init(new TestAppContext(), null);
    PentahoSystem.setSystemSettingsService(
        new ISystemSettings() {
          public String getSystemCfgSourceName() {
            return null;
          }

          public String getSystemSetting(String arg0, String arg1) {
            if ("singleDiServerInstance".equals(arg0)) {
              return "false";
            }
            return arg1;
          }

          public String getSystemSetting(String arg0, String arg1, String arg2) {
            return null;
          }

          public List getSystemSettings(String arg0) {
            return null;
          }

          public List getSystemSettings(String arg0, String arg1) {
            return null;
          }

          public Document getSystemSettingsDocument(String arg0) {
            return null;
          }

          public Properties getSystemSettingsProperties(String arg0) {
            return null;
          }

          public void resetSettingsCache() {}
        });

    // now test the webservice
    IRepositorySyncWebService webservice = getRepositorySyncWebService();

    // first without the plugin available
    try {
      webservice.sync("test id", "http://localhost:8080/pentaho-di");
      Assert.fail();
    } catch (RepositorySyncException e) {
      Assert.assertTrue(
          e.getMessage().indexOf("unable to load the PentahoEnterpriseRepository plugin") >= 0);
    }

    // second with plugin but not registered
    RepositoryPluginType.getInstance()
        .registerCustom(
            TestRepositoryMeta.class,
            "PentahoEnterpriseRepository",
            "PentahoEnterpriseRepository",
            "PentahoEnterpriseRepository",
            "PentahoEnterpriseRepository",
            "");
    PluginRegistry.getInstance()
        .getPlugin(RepositoryPluginType.class, "PentahoEnterpriseRepository")
        .getClassMap()
        .put(
            RepositoryMeta.class,
            "com.pentaho.pdi.ws.RepositorySyncWebServiceTest$TestRepositoryMeta");

    RepositorySyncStatus status = webservice.sync("test id", "http://localhost:8080/pentaho-di");

    Assert.assertEquals(RepositorySyncStatus.REGISTERED, status);

    // third after already registered
    status = webservice.sync("test id", "http://localhost:8080/pentaho-di");

    Assert.assertEquals(RepositorySyncStatus.ALREADY_REGISTERED, status);

    // forth test with different url
    try {
      webservice.sync("test id", "http://localhost:9090/pentaho-di");
      Assert.fail();
    } catch (RepositorySyncException e) {
      Assert.assertTrue(e.getMessage().indexOf("with the URL:") >= 0);
    }

    // fifth test different base-url
    fullyQualifiedServerUrl = "http://localhost:9090/pentaho-di";
    try {
      webservice.sync("test id", "http://localhost:8080/pentaho-di");
      Assert.fail();
    } catch (RepositorySyncException e) {
      Assert.assertTrue(e.getMessage().indexOf("fully qualified server url") >= 0);
    }
  }