### Question:
HostVersionOutOfSyncListener { @Subscribe @Transactional public void onHostEvent(HostsAddedEvent event) { if (LOG.isDebugEnabled()) { LOG.debug(event.toString()); } @Experimental(feature=ExperimentalFeature.PATCH_UPGRADES, comment="Eventually take into account deleted repositories") List<RepositoryVersionEntity> repos = repositoryVersionDAO.get().findAll(); for (String hostName : event.getHostNames()) { HostEntity hostEntity = hostDAO.get().findByName(hostName); for (RepositoryVersionEntity repositoryVersion : repos) { HostVersionEntity missingHostVersion = new HostVersionEntity(hostEntity, repositoryVersion, RepositoryVersionState.NOT_REQUIRED); LOG.info("Creating host version for {}, state={}, repo={} (repo_id={})", missingHostVersion.getHostName(), missingHostVersion.getState(), missingHostVersion.getRepositoryVersion().getVersion(), missingHostVersion.getRepositoryVersion().getId()); hostVersionDAO.get().create(missingHostVersion); hostDAO.get().merge(hostEntity); hostEntity.getHostVersionEntities().add(missingHostVersion); hostEntity = hostDAO.get().merge(hostEntity); } } } @Inject  HostVersionOutOfSyncListener(AmbariEventPublisher ambariEventPublisher, LockFactory lockFactory); @Subscribe @Transactional void onServiceComponentEvent(ServiceComponentInstalledEvent event); @Subscribe @Transactional void onServiceComponentHostEvent(ServiceComponentUninstalledEvent event); @Subscribe @Transactional void onServiceEvent(ServiceInstalledEvent event); @Subscribe @Transactional void onHostEvent(HostsAddedEvent event); @Subscribe @Transactional void onHostEvent(HostsRemovedEvent event);  }### Answer:
@Test public void testOnHostEvent() throws AmbariException { Host h1 = clusters.getHost("h1"); h1.setState(HostState.HEALTHY); StackId stackId = new StackId(this.stackId); RepositoryVersionEntity repositoryVersionEntity = helper.getOrCreateRepositoryVersion(stackId,"2.2.0-1000"); RepositoryVersionEntity repositoryVersionEntity2 = helper.getOrCreateRepositoryVersion(stackId,"2.2.0-2000"); c1.setCurrentStackVersion(stackId); assertRepoVersionState("2.2.0-1000", RepositoryVersionState.INSTALLING); assertRepoVersionState("2.2.0-2086", RepositoryVersionState.CURRENT); helper.createHostVersion("h1", repositoryVersionEntity, RepositoryVersionState.INSTALLED); helper.createHostVersion("h1", repositoryVersionEntity2, RepositoryVersionState.INSTALLED); assertRepoVersionState("2.2.0-1000", RepositoryVersionState.INSTALLED); assertRepoVersionState("2.2.0-2000", RepositoryVersionState.INSTALLED); assertRepoVersionState("2.2.0-2086", RepositoryVersionState.CURRENT); addHost("h2"); clusters.mapHostToCluster("h2", "c1"); List<HostVersionEntity> h2Versions = hostVersionDAO.findByHost("h2"); for (HostVersionEntity hostVersionEntity : h2Versions) { if (hostVersionEntity.getRepositoryVersion().toString().equals("2.2.0-2086")) { assertEquals(hostVersionEntity.getState(), RepositoryVersionState.CURRENT); } else { assertEquals(hostVersionEntity.getState(), RepositoryVersionState.OUT_OF_SYNC); } } }