HostUpdateHelper { void updateHostsInConfigurations() throws AmbariException { ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class); AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class); Clusters clusters = ambariManagementController.getClusters(); if (clusters != null) { for (Map.Entry<String, Map<String,String>> clusterHosts : hostChangesFileMap.entrySet()) { String clusterName = clusterHosts.getKey(); ClusterEntity clusterEntity = clusterDAO.findByName(clusterName); Cluster cluster = clusters.getCluster(clusterName); Map<String, String> hostMapping = clusterHosts.getValue(); List<String> currentHostNames = new ArrayList<>(); String updatedPropertyValue; for (Map.Entry<String, String> hostPair : hostMapping.entrySet()) { currentHostNames.add(hostPair.getKey()); } if (clusterEntity != null) { Collection<ClusterConfigEntity> clusterConfigEntities = clusterEntity.getClusterConfigEntities(); boolean configUpdated; ConfigFactory configFactory = injector.getInstance(ConfigFactory.class); for (ClusterConfigEntity clusterConfigEntity : clusterConfigEntities) { Config config = configFactory.createExisting(cluster, clusterConfigEntity); configUpdated = false; for (Map.Entry<String,String> property : config.getProperties().entrySet()) { updatedPropertyValue = replaceHosts(property.getValue(), currentHostNames, hostMapping); if (updatedPropertyValue != null) { Map<String,String> propertiesWithUpdates = config.getProperties(); propertiesWithUpdates.put(property.getKey(), updatedPropertyValue); config.setProperties(propertiesWithUpdates); configUpdated = true; } } if (configUpdated) { config.save(); } } } } } } @Inject  HostUpdateHelper(PersistService persistService,
                          Configuration configuration,
                          Injector injector); void startPersistenceService(); void stopPersistenceService(); String getHostChangesFile(); void setHostChangesFile(String hostChangesFile); Map<String, Map<String, String>> getHostChangesFileMap(); void setHostChangesFileMap(Map<String, Map<String, String>> hostChangesFileMap); static void main(String[] args);  }
@Test public void testUpdateHostsInConfigurations() throws AmbariException { EasyMockSupport easyMockSupport = new EasyMockSupport(); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class); final ClusterDAO mockClusterDAO = easyMockSupport.createNiceMock(ClusterDAO.class); final EntityManager entityManager = createNiceMock(EntityManager.class); final DBAccessor dbAccessor = createNiceMock(DBAccessor.class); Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class); Cluster mockCluster = easyMockSupport.createNiceMock(Cluster.class); ClusterEntity mockClusterEntity1 = easyMockSupport.createNiceMock(ClusterEntity.class); ClusterConfigEntity mockClusterConfigEntity1 = easyMockSupport.createNiceMock(ClusterConfigEntity.class); ClusterConfigEntity mockClusterConfigEntity2 = easyMockSupport.createNiceMock(ClusterConfigEntity.class); StackEntity mockStackEntity = easyMockSupport.createNiceMock(StackEntity.class); Map<String, Map<String, String>> clusterHostsToChange = new HashMap<>(); Map<String, String> hosts = new HashMap<>(); List<ClusterConfigEntity> clusterConfigEntities1 = new ArrayList<>(); final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(AmbariManagementController.class).toInstance(mockAmbariManagementController); bind(DBAccessor.class).toInstance(dbAccessor); bind(EntityManager.class).toInstance(entityManager); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); bind(ClusterDAO.class).toInstance(mockClusterDAO); install(new FactoryModuleBuilder().implement(Config.class, ConfigImpl.class).build(ConfigFactory.class)); } }); hosts.put("host11","host55"); hosts.put("host5","host1"); hosts.put("host1","host5"); hosts.put("host55","host11"); clusterConfigEntities1.add(mockClusterConfigEntity1); clusterConfigEntities1.add(mockClusterConfigEntity2); clusterHostsToChange.put("cl1", hosts); expect(mockClusterDAO.findByName("cl1")).andReturn(mockClusterEntity1).atLeastOnce(); expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getCluster("cl1")).andReturn(mockCluster).once(); expect(mockCluster.getClusterId()).andReturn(1L).anyTimes(); expect(mockClusterEntity1.getClusterConfigEntities()).andReturn(clusterConfigEntities1).atLeastOnce(); expect(mockClusterConfigEntity1.getClusterId()).andReturn(1L).atLeastOnce(); expect(mockClusterConfigEntity1.getConfigId()).andReturn(1L).atLeastOnce(); expect(mockClusterConfigEntity1.getStack()).andReturn(mockStackEntity).atLeastOnce(); expect(mockClusterConfigEntity1.getData()).andReturn("{\"testProperty1\" : \"testValue_host1\", " + "\"testProperty2\" : \"testValue_host5\", \"testProperty3\" : \"testValue_host11\", " + "\"testProperty4\" : \"testValue_host55\"}").atLeastOnce(); expect(mockClusterConfigEntity1.getTag()).andReturn("testTag1").atLeastOnce(); expect(mockClusterConfigEntity1.getType()).andReturn("testType1").atLeastOnce(); expect(mockClusterConfigEntity1.getVersion()).andReturn(1L).atLeastOnce(); expect(mockClusterDAO.findConfig(1L)).andReturn(mockClusterConfigEntity1).atLeastOnce(); expect(mockClusterConfigEntity2.getClusterId()).andReturn(1L).atLeastOnce(); expect(mockClusterConfigEntity2.getConfigId()).andReturn(2L).anyTimes(); expect(mockClusterConfigEntity2.getStack()).andReturn(mockStackEntity).atLeastOnce(); expect(mockClusterConfigEntity2.getData()).andReturn("{\"testProperty5\" : \"test_host1_test_host5_test_host11_test_host55\"}").atLeastOnce(); expect(mockClusterConfigEntity2.getTag()).andReturn("testTag2").atLeastOnce(); expect(mockClusterConfigEntity2.getType()).andReturn("testType2").atLeastOnce(); expect(mockClusterConfigEntity2.getVersion()).andReturn(2L).atLeastOnce(); expect(mockClusterDAO.findConfig(2L)).andReturn(mockClusterConfigEntity2).atLeastOnce(); Capture<String> dataCapture = EasyMock.newCapture(); mockClusterConfigEntity1.setData(EasyMock.capture(dataCapture)); expectLastCall(); mockClusterConfigEntity2.setData("{\"testProperty5\":\"test_host5_test_host1_test_host55_test_host11\"}"); expectLastCall(); HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, mockInjector); hostUpdateHelper.setHostChangesFileMap(clusterHostsToChange); easyMockSupport.replayAll(); hostUpdateHelper.updateHostsInConfigurations(); easyMockSupport.verifyAll(); Assert.assertTrue(CollectionPresentationUtils.isJsonsEquals("{\"testProperty4\":\"testValue_host11\",\"testProperty3\":\"testValue_host55\"," + "\"testProperty2\":\"testValue_host1\",\"testProperty1\":\"testValue_host5\"}", dataCapture.getValue())); }