HostUpdateHelper { void updateHostsForAlertsInDB() { AmbariManagementController ambariManagementController = injector.getInstance(AmbariManagementController.class); AlertsDAO alertsDAO = injector.getInstance(AlertsDAO.class); AlertDefinitionDAO alertDefinitionDAO = injector.getInstance(AlertDefinitionDAO.class); Clusters clusters = ambariManagementController.getClusters(); if (clusters != null) { Map<String, Cluster> clusterMap = clusters.getClusters(); if (clusterMap != null) { for (Map.Entry<String, Map<String,String>> clusterHosts : hostChangesFileMap.entrySet()) { List<String> currentHostNames = new ArrayList<>(); Map<String, String> hostMapping = clusterHosts.getValue(); Long clusterId = clusterMap.get(clusterHosts.getKey()).getClusterId(); for (Map.Entry<String, String> hostPair : hostMapping.entrySet()) { currentHostNames.add(hostPair.getKey()); } List<AlertCurrentEntity> currentEntities = alertsDAO.findCurrentByCluster(clusterId); for (AlertCurrentEntity alertCurrentEntity : currentEntities) { AlertHistoryEntity alertHistoryEntity = alertCurrentEntity.getAlertHistory(); if (currentHostNames.contains(alertHistoryEntity.getHostName())) { alertHistoryEntity.setHostName(hostMapping.get(alertHistoryEntity.getHostName())); alertsDAO.merge(alertHistoryEntity); } } List<AlertDefinitionEntity> alertDefinitionEntities = alertDefinitionDAO.findAll(clusterId); for (AlertDefinitionEntity alertDefinitionEntity : alertDefinitionEntities) { alertDefinitionEntity.setHash(UUID.randomUUID().toString()); alertDefinitionDAO.merge(alertDefinitionEntity); } } } } } @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 testUpdateHostsForAlertsInDB() throws AmbariException { EasyMockSupport easyMockSupport = new EasyMockSupport(); final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class); final AlertsDAO mockAlertsDAO = easyMockSupport.createNiceMock(AlertsDAO.class); final AlertDefinitionDAO mockAlertDefinitionDAO = easyMockSupport.createNiceMock(AlertDefinitionDAO.class); final AlertDispatchDAO mockAlertDispatchDAO = easyMockSupport.createNiceMock(AlertDispatchDAO.class); final EntityManager entityManager = createNiceMock(EntityManager.class); final DBAccessor dbAccessor = createNiceMock(DBAccessor.class); final ClusterDAO mockClusterDAO = easyMockSupport.createNiceMock(ClusterDAO.class); final Clusters mockClusters = easyMockSupport.createNiceMock(Clusters.class); Cluster mockCluster = easyMockSupport.createNiceMock(Cluster.class); AlertCurrentEntity mockAlertCurrentEntity1 = easyMockSupport.createNiceMock(AlertCurrentEntity.class); AlertCurrentEntity mockAlertCurrentEntity2 = easyMockSupport.createNiceMock(AlertCurrentEntity.class); AlertHistoryEntity mockAlertHistoryEntity1 = easyMockSupport.createNiceMock(AlertHistoryEntity.class); AlertHistoryEntity mockAlertHistoryEntity2 = easyMockSupport.createNiceMock(AlertHistoryEntity.class); AlertDefinitionEntity mockAlertDefinitionEntity = easyMockSupport.createNiceMock(AlertDefinitionEntity.class); Map<String, Map<String, String>> clusterHostsToChange = new HashMap<>(); List<AlertCurrentEntity> alertCurrentEntities = new ArrayList<>(); List<AlertDefinitionEntity> alertDefinitionEntities = new ArrayList<>(); Map<String, Cluster> clusterMap = new HashMap<>(); Map<String, String> hosts = new HashMap<>(); hosts.put("host1","host10"); hosts.put("host2","host11"); clusterHostsToChange.put("cl1", hosts); clusterMap.put("cl1", mockCluster); alertCurrentEntities.add(mockAlertCurrentEntity1); alertCurrentEntities.add(mockAlertCurrentEntity2); alertDefinitionEntities.add(mockAlertDefinitionEntity); 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(Clusters.class).toInstance(mockClusters); bind(AmbariManagementController.class).toInstance(mockAmbariManagementController); bind(AlertDispatchDAO.class).toInstance(mockAlertDispatchDAO); bind(AlertsDAO.class).toInstance(mockAlertsDAO); bind(AlertDefinitionDAO.class).toInstance(mockAlertDefinitionDAO); } }); expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once(); expect(mockClusters.getClusters()).andReturn(clusterMap).once(); expect(mockCluster.getClusterId()).andReturn(1L).once(); expect(mockAlertsDAO.findCurrentByCluster(1L)).andReturn(alertCurrentEntities).once(); expect(mockAlertCurrentEntity1.getAlertHistory()).andReturn(mockAlertHistoryEntity1).once(); expect(mockAlertCurrentEntity2.getAlertHistory()).andReturn(mockAlertHistoryEntity2).once(); expect(mockAlertHistoryEntity1.getHostName()).andReturn("host1").atLeastOnce(); expect(mockAlertHistoryEntity2.getHostName()).andReturn("host2").atLeastOnce(); expect(mockAlertDefinitionDAO.findAll(1L)).andReturn(alertDefinitionEntities).once(); mockAlertHistoryEntity1.setHostName("host10"); expectLastCall(); mockAlertHistoryEntity2.setHostName("host11"); expectLastCall(); mockAlertDefinitionEntity.setHash(anyString()); expectLastCall(); HostUpdateHelper hostUpdateHelper = new HostUpdateHelper(null, null, mockInjector); hostUpdateHelper.setHostChangesFileMap(clusterHostsToChange); easyMockSupport.replayAll(); hostUpdateHelper.updateHostsForAlertsInDB(); easyMockSupport.verifyAll(); }