UpgradeCatalog221 extends AbstractUpgradeCatalog { protected void updateRangerKmsDbksConfigs() throws AmbariException { AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class); for (final Cluster cluster : getCheckedClusterMap(ambariManagementController.getClusters()).values()) { Map<String, String> newRangerKmsProps = new HashMap<>(); Config rangerKmsDbConfigs = cluster.getDesiredConfigByType(RANGER_KMS_PROPERTIES); if (rangerKmsDbConfigs != null) { String dbFlavor = rangerKmsDbConfigs.getProperties().get(RANGER_KMS_DB_FLAVOR); String dbHost = rangerKmsDbConfigs.getProperties().get(RANGER_KMS_DB_HOST); String dbName = rangerKmsDbConfigs.getProperties().get(RANGER_KMS_DB_NAME); String dbConnectionString = null; String dbDriver = null; if (dbFlavor != null && dbHost != null && dbName != null) { if ("MYSQL".equalsIgnoreCase(dbFlavor)) { dbConnectionString = "jdbc:mysql: dbDriver = "com.mysql.jdbc.Driver"; } else if ("ORACLE".equalsIgnoreCase(dbFlavor)) { dbConnectionString = "jdbc:oracle:thin:@ dbDriver = "oracle.jdbc.driver.OracleDriver"; } else if ("POSTGRES".equalsIgnoreCase(dbFlavor)) { dbConnectionString = "jdbc:postgresql: dbDriver = "org.postgresql.Driver"; } else if ("MSSQL".equalsIgnoreCase(dbFlavor)) { dbConnectionString = "jdbc:sqlserver: dbDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; } else if ("SQLA".equalsIgnoreCase(dbFlavor)) { dbConnectionString = "jdbc:sqlanywhere:database="+dbName+";host="+dbHost; dbDriver = "sap.jdbc4.sqlanywhere.IDriver"; } newRangerKmsProps.put(RANGER_KMS_JDBC_URL, dbConnectionString); newRangerKmsProps.put(RANGER_KMS_JDBC_DRIVER, dbDriver); updateConfigurationPropertiesForCluster(cluster, RANGER_KMS_DBKS_CONFIG, newRangerKmsProps, true, false); } } } } @Inject  UpgradeCatalog221(Injector injector); @Override String getTargetVersion(); @Override String getSourceVersion();  }
@Test public void testUpdateRangerKmsDbksConfigs() throws Exception { EasyMockSupport easyMockSupport = new EasyMockSupport(); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class); final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class); final Map<String, String> propertiesRangerKmsDbConfigs = new HashMap<String, String>(); propertiesRangerKmsDbConfigs.put("DB_FLAVOR", "MYSQL"); propertiesRangerKmsDbConfigs.put("db_host", "localhost"); propertiesRangerKmsDbConfigs.put("db_name", "testdb"); final Config mockrangerKmsDbConfigs = easyMockSupport.createNiceMock(Config.class); final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AmbariManagementController.class).toInstance(mockAmbariManagementController); bind(Clusters.class).toInstance(mockClusters); bind(EntityManager.class).toInstance(entityManager); bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class)); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); } }); expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{ put("normal", mockClusterExpected); }}).atLeastOnce(); expect(mockClusterExpected.getDesiredConfigByType("kms-properties")).andReturn(mockrangerKmsDbConfigs).atLeastOnce(); expect(mockrangerKmsDbConfigs.getProperties()).andReturn(propertiesRangerKmsDbConfigs).times(3); easyMockSupport.replayAll(); mockInjector.getInstance(UpgradeCatalog221.class).updateRangerKmsDbksConfigs(); easyMockSupport.verifyAll(); }