@Test public void testUpdateHostsInDB() throws AmbariException { EasyMockSupport easyMockSupport = new EasyMockSupport(); final HostDAO mockHostDAO = easyMockSupport.createNiceMock(HostDAO.class); final ClusterDAO mockClusterDAO = easyMockSupport.createNiceMock(ClusterDAO.class); final EntityManager entityManager = createNiceMock(EntityManager.class); final DBAccessor dbAccessor = createNiceMock(DBAccessor.class); ClusterEntity mockClusterEntity = easyMockSupport.createNiceMock(ClusterEntity.class); HostEntity mockHostEntity1 = easyMockSupport.createNiceMock(HostEntity.class); HostEntity mockHostEntity2 = easyMockSupport.createNiceMock(HostEntity.class); Map<String, Map<String, String>> clusterHostsToChange = new HashMap<>(); List<HostEntity> hostEntities = new ArrayList<>(); Map<String, String> hosts = new HashMap<>(); hosts.put("host1","host10"); hosts.put("host2","host11"); clusterHostsToChange.put("cl1", hosts); hostEntities.add(mockHostEntity1); hostEntities.add(mockHostEntity2); final Injector mockInjector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(DBAccessor.class).toInstance(dbAccessor); bind(EntityManager.class).toInstance(entityManager); bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class)); bind(ClusterDAO.class).toInstance(mockClusterDAO); bind(HostDAO.class).toInstance(mockHostDAO); } }); expect(mockClusterDAO.findByName("cl1")).andReturn(mockClusterEntity).once(); expect(mockClusterEntity.getHostEntities()).andReturn(hostEntities).once(); expect(mockHostEntity1.getHostName()).andReturn("host1").atLeastOnce(); expect(mockHostEntity2.getHostName()).andReturn("host2").atLeastOnce(); mockHostEntity1.setHostName("host10"); expectLastCall(); mockHostEntity2.setHostName("host11"); expectLastCall(); HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, mockInjector); hostUpdateHelper.setHostChangesFileMap(clusterHostsToChange); easyMockSupport.replayAll(); hostUpdateHelper.updateHostsInDB(); easyMockSupport.verifyAll(); }
void updateHostsInDB() { ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class); HostDAO hostDAO = injector.getInstance(HostDAO.class); for (Map.Entry<String, Map<String,String>> clusterHosts : hostChangesFileMap.entrySet()) { String clusterName = clusterHosts.getKey(); Map<String, String> hostMapping = clusterHosts.getValue(); ClusterEntity clusterEntity = clusterDAO.findByName(clusterName); List<String> currentHostNames = new ArrayList<>(); for (Map.Entry<String, String> hostPair : hostMapping.entrySet()) { currentHostNames.add(hostPair.getKey()); } if (clusterEntity != null) { Collection<HostEntity> hostEntities = clusterEntity.getHostEntities(); for (HostEntity hostEntity : hostEntities) { if (currentHostNames.contains(hostEntity.getHostName())) { hostEntity.setHostName(hostMapping.get(hostEntity.getHostName())); hostDAO.merge(hostEntity); } } } } }
HostUpdateHelper { void updateHostsInDB() { ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class); HostDAO hostDAO = injector.getInstance(HostDAO.class); for (Map.Entry<String, Map<String,String>> clusterHosts : hostChangesFileMap.entrySet()) { String clusterName = clusterHosts.getKey(); Map<String, String> hostMapping = clusterHosts.getValue(); ClusterEntity clusterEntity = clusterDAO.findByName(clusterName); List<String> currentHostNames = new ArrayList<>(); for (Map.Entry<String, String> hostPair : hostMapping.entrySet()) { currentHostNames.add(hostPair.getKey()); } if (clusterEntity != null) { Collection<HostEntity> hostEntities = clusterEntity.getHostEntities(); for (HostEntity hostEntity : hostEntities) { if (currentHostNames.contains(hostEntity.getHostName())) { hostEntity.setHostName(hostMapping.get(hostEntity.getHostName())); hostDAO.merge(hostEntity); } } } } } }
HostUpdateHelper { void updateHostsInDB() { ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class); HostDAO hostDAO = injector.getInstance(HostDAO.class); for (Map.Entry<String, Map<String,String>> clusterHosts : hostChangesFileMap.entrySet()) { String clusterName = clusterHosts.getKey(); Map<String, String> hostMapping = clusterHosts.getValue(); ClusterEntity clusterEntity = clusterDAO.findByName(clusterName); List<String> currentHostNames = new ArrayList<>(); for (Map.Entry<String, String> hostPair : hostMapping.entrySet()) { currentHostNames.add(hostPair.getKey()); } if (clusterEntity != null) { Collection<HostEntity> hostEntities = clusterEntity.getHostEntities(); for (HostEntity hostEntity : hostEntities) { if (currentHostNames.contains(hostEntity.getHostName())) { hostEntity.setHostName(hostMapping.get(hostEntity.getHostName())); hostDAO.merge(hostEntity); } } } } } @Inject  HostUpdateHelper(PersistService persistService,
                          Configuration configuration,
                          Injector injector); }
HostUpdateHelper { void updateHostsInDB() { ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class); HostDAO hostDAO = injector.getInstance(HostDAO.class); for (Map.Entry<String, Map<String,String>> clusterHosts : hostChangesFileMap.entrySet()) { String clusterName = clusterHosts.getKey(); Map<String, String> hostMapping = clusterHosts.getValue(); ClusterEntity clusterEntity = clusterDAO.findByName(clusterName); List<String> currentHostNames = new ArrayList<>(); for (Map.Entry<String, String> hostPair : hostMapping.entrySet()) { currentHostNames.add(hostPair.getKey()); } if (clusterEntity != null) { Collection<HostEntity> hostEntities = clusterEntity.getHostEntities(); for (HostEntity hostEntity : hostEntities) { if (currentHostNames.contains(hostEntity.getHostName())) { hostEntity.setHostName(hostMapping.get(hostEntity.getHostName())); hostDAO.merge(hostEntity); } } } } } @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); }
HostUpdateHelper { void updateHostsInDB() { ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class); HostDAO hostDAO = injector.getInstance(HostDAO.class); for (Map.Entry<String, Map<String,String>> clusterHosts : hostChangesFileMap.entrySet()) { String clusterName = clusterHosts.getKey(); Map<String, String> hostMapping = clusterHosts.getValue(); ClusterEntity clusterEntity = clusterDAO.findByName(clusterName); List<String> currentHostNames = new ArrayList<>(); for (Map.Entry<String, String> hostPair : hostMapping.entrySet()) { currentHostNames.add(hostPair.getKey()); } if (clusterEntity != null) { Collection<HostEntity> hostEntities = clusterEntity.getHostEntities(); for (HostEntity hostEntity : hostEntities) { if (currentHostNames.contains(hostEntity.getHostName())) { hostEntity.setHostName(hostMapping.get(hostEntity.getHostName())); hostDAO.merge(hostEntity); } } } } } @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);  }