Mercurial > hg > release > thermostat-2.0
changeset 2545:b75afb726267
Update VmIdCompleterService for Declarative Services
Reviewed-by: jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-December/021834.html
author | Andrew Azores <aazores@redhat.com> |
---|---|
date | Fri, 09 Dec 2016 08:44:58 -0500 |
parents | 4933c471f51e |
children | 26055a3e53dd |
files | launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdCompleterService.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdsFinder.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdsFinderImpl.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/ActivatorTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/VmIdCompleterServiceTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/VmIdsFinderImplTest.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/VmIdsFinderTest.java |
diffstat | 8 files changed, 369 insertions(+), 394 deletions(-) [+] |
line wrap: on
line diff
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java Thu Dec 08 10:23:22 2016 -0500 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java Fri Dec 09 08:44:58 2016 -0500 @@ -56,8 +56,6 @@ import com.redhat.thermostat.shared.config.CommonPaths; import com.redhat.thermostat.shared.config.SSLConfiguration; import com.redhat.thermostat.storage.core.DbService; -import com.redhat.thermostat.storage.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.utils.keyring.Keyring; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -142,7 +140,6 @@ private MultipleServiceTracker launcherDepsTracker; private MultipleServiceTracker shellTracker; - private MultipleServiceTracker vmIdCompleterDepsTracker; private CommandRegistry registry; @@ -269,28 +266,7 @@ dbServiceTracker.open(); registry.registerCommand("help", helpCommand); - final VmIdCompleterService vmIdCompleterService = new VmIdCompleterService(); - final Class<?>[] vmIdCompleterDeps = new Class[] { VmInfoDAO.class, AgentInfoDAO.class }; - vmIdCompleterDepsTracker = new MultipleServiceTracker(context, vmIdCompleterDeps, new Action() { - - @Override - public void dependenciesAvailable(DependencyProvider services) { - VmInfoDAO vmDao = services.get(VmInfoDAO.class); - AgentInfoDAO agentDao = services.get(AgentInfoDAO.class); - vmIdCompleterService.setVmInfoDAO(vmDao); - vmIdCompleterService.setAgentInfoDAO(agentDao); - } - - @Override - public void dependenciesUnavailable() { - vmIdCompleterService.setVmInfoDAO(null); - vmIdCompleterService.setAgentInfoDAO(null); - } - }); - vmIdCompleterDepsTracker.open(); - context.registerService(CompleterService.class.getName(), helpCommandCompleterService, null); - context.registerService(CompleterService.class.getName(), vmIdCompleterService, null); } @Override @@ -307,9 +283,6 @@ if (shellTracker != null) { shellTracker.close(); } - if (vmIdCompleterDepsTracker != null) { - vmIdCompleterDepsTracker.close(); - } registry.unregisterCommands(); } }
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdCompleterService.java Thu Dec 08 10:23:22 2016 -0500 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdCompleterService.java Fri Dec 09 08:44:58 2016 -0500 @@ -36,21 +36,27 @@ package com.redhat.thermostat.launcher.internal; -import com.redhat.thermostat.common.cli.AbstractCompleterService; import com.redhat.thermostat.common.cli.CliCommandOption; +import com.redhat.thermostat.common.cli.CompleterService; import com.redhat.thermostat.common.cli.CompletionFinderTabCompleter; import com.redhat.thermostat.common.cli.TabCompleter; -import com.redhat.thermostat.storage.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.dao.VmInfoDAO; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; import java.util.Collections; import java.util.Map; import java.util.Set; -public class VmIdCompleterService extends AbstractCompleterService { +@Component(immediate = true) +@Service +public class VmIdCompleterService implements CompleterService { public static final CliCommandOption VM_ID_OPTION = new CliCommandOption("v", "vmId", true, "VM ID", false); + @Reference + private VmIdsFinder vmIdsFinder; + @Override public Set<String> getCommands() { return TabCompletion.ALL_COMMANDS_COMPLETER; @@ -58,17 +64,20 @@ @Override public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - TabCompleter completer = new CompletionFinderTabCompleter(new VmIdsFinder(dependencyServices)); - - return Collections.singletonMap(VM_ID_OPTION, completer); + return Collections.singletonMap(VM_ID_OPTION, new CompletionFinderTabCompleter(vmIdsFinder)); } - void setAgentInfoDAO(AgentInfoDAO agentDao) { - setService(AgentInfoDAO.class, agentDao); + @Override + public Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters() { + return Collections.emptyMap(); } - void setVmInfoDAO(VmInfoDAO vmInfoDao) { - setService(VmInfoDAO.class, vmInfoDao); + void bindVmIdsFinder(VmIdsFinder vmIdsFinder) { + this.vmIdsFinder = vmIdsFinder; + } + + void unbindVmIdsFinder(VmIdsFinder vmIdsFinder) { + this.vmIdsFinder = null; } }
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdsFinder.java Thu Dec 08 10:23:22 2016 -0500 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdsFinder.java Fri Dec 09 08:44:58 2016 -0500 @@ -36,61 +36,7 @@ package com.redhat.thermostat.launcher.internal; -import com.redhat.thermostat.common.cli.AbstractCompletionFinder; -import com.redhat.thermostat.common.cli.CompletionInfo; -import com.redhat.thermostat.common.cli.DependencyServices; -import com.redhat.thermostat.storage.core.AgentId; -import com.redhat.thermostat.storage.core.VmId; -import com.redhat.thermostat.storage.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.dao.VmInfoDAO; -import com.redhat.thermostat.storage.model.AgentInformation; -import com.redhat.thermostat.storage.model.VmInfo; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Set; - -public class VmIdsFinder extends AbstractCompletionFinder { - - public VmIdsFinder(DependencyServices dependencyServices) { - super(dependencyServices); - } - - @Override - protected Class<?>[] getRequiredDependencies() { - return new Class<?>[]{ VmInfoDAO.class, AgentInfoDAO.class }; - } +import com.redhat.thermostat.common.cli.CompletionFinder; - @Override - public List<CompletionInfo> findCompletions() { - if (!allDependenciesAvailable()) { - return Collections.emptyList(); - } - - VmInfoDAO vmDao = getService(VmInfoDAO.class); - AgentInfoDAO agentDao = getService(AgentInfoDAO.class); - - return findVmIds(vmDao, agentDao, agentDao.getAgentIds()); - } - - private List<CompletionInfo> findVmIds(VmInfoDAO vmsDAO, AgentInfoDAO agentInfoDAO, Set<AgentId> agentIds) { - List<CompletionInfo> vmIds = new ArrayList<>(); - for (AgentId agentId : agentIds) { - AgentInformation agentInfo = agentInfoDAO.getAgentInformation(agentId); - if (agentInfo != null) { - Collection<VmId> vms = vmsDAO.getVmIds(agentId); - for (VmId vm : vms) { - VmInfo info = vmsDAO.getVmInfo(vm); - vmIds.add(new CompletionInfo(info.getVmId(), getUserVisibleText(info, agentInfo))); - } - } - } - return vmIds; - } - - private String getUserVisibleText(VmInfo info, AgentInformation agentInfo) { - return info.getMainClass() + "(" + info.isAlive(agentInfo).toString() + ")"; - } +interface VmIdsFinder extends CompletionFinder { }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/VmIdsFinderImpl.java Fri Dec 09 08:44:58 2016 -0500 @@ -0,0 +1,105 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.launcher.internal; + +import com.redhat.thermostat.common.cli.CompletionInfo; +import com.redhat.thermostat.storage.core.AgentId; +import com.redhat.thermostat.storage.core.VmId; +import com.redhat.thermostat.storage.dao.AgentInfoDAO; +import com.redhat.thermostat.storage.dao.VmInfoDAO; +import com.redhat.thermostat.storage.model.AgentInformation; +import com.redhat.thermostat.storage.model.VmInfo; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Component +@Service +public class VmIdsFinderImpl implements VmIdsFinder { + + @Reference + private VmInfoDAO vmInfoDao; + + @Reference + private AgentInfoDAO agentInfoDao; + + @Override + public List<CompletionInfo> findCompletions() { + if (vmInfoDao == null || agentInfoDao == null) { + return Collections.emptyList(); + } + + List<CompletionInfo> vmIds = new ArrayList<>(); + for (AgentId agentId : agentInfoDao.getAgentIds()) { + AgentInformation agentInfo = agentInfoDao.getAgentInformation(agentId); + if (agentInfo != null) { + Collection<VmId> vms = vmInfoDao.getVmIds(agentId); + for (VmId vm : vms) { + VmInfo info = vmInfoDao.getVmInfo(vm); + vmIds.add(new CompletionInfo(info.getVmId(), getUserVisibleText(info, agentInfo))); + } + } + } + return vmIds; + } + + private String getUserVisibleText(VmInfo info, AgentInformation agentInfo) { + return info.getMainClass() + "(" + info.isAlive(agentInfo).toString() + ")"; + } + + void bindVmInfoDao(VmInfoDAO vmInfoDAO) { + this.vmInfoDao = vmInfoDAO; + } + + void unbindVmInfoDao(VmInfoDAO vmInfoDAO) { + this.vmInfoDao = null; + } + + void bindAgentInfoDao(AgentInfoDAO agentInfoDAO) { + this.agentInfoDao = agentInfoDAO; + } + + void unindAgentInfoDao(AgentInfoDAO agentInfoDAO) { + this.agentInfoDao = null; + } + +}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ActivatorTest.java Thu Dec 08 10:23:22 2016 -0500 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ActivatorTest.java Fri Dec 09 08:44:58 2016 -0500 @@ -41,7 +41,6 @@ import com.redhat.thermostat.common.MultipleServiceTracker.Action; import com.redhat.thermostat.common.MultipleServiceTracker.DependencyProvider; import com.redhat.thermostat.common.cli.Command; -import com.redhat.thermostat.common.cli.CompleterService; import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; import com.redhat.thermostat.launcher.BundleManager; import com.redhat.thermostat.launcher.Launcher; @@ -156,12 +155,12 @@ assertCommandIsRegistered(context, "help", HelpCommand.class); - verify(mockTracker, times(3)).open(); + verify(mockTracker, times(2)).open(); Action action = actionCaptor.getValue(); assertNotNull(action); activator.stop(context); - verify(mockTracker, times(3)).close(); + verify(mockTracker, times(2)).close(); } @Test @@ -312,50 +311,6 @@ assertFalse(context.isServiceRegistered(Launcher.class.getName(), LauncherImpl.class)); } - @Test - public void testVmIdCompleterServiceAvailability() throws Exception { - StubBundleContext context = new StubBundleContext(); - MultipleServiceTracker unusedTracker = mock(MultipleServiceTracker.class); - ArgumentCaptor<Action> unusedCaptor = ArgumentCaptor.forClass(Action.class); - ArgumentCaptor<Action> vmCaptor = ArgumentCaptor.forClass(Action.class); - Class<?>[] launcherDeps = new Class[] { - Keyring.class, - CommonPaths.class, - SSLConfiguration.class, - }; - whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), - eq(launcherDeps), unusedCaptor.capture()).thenReturn(unusedTracker); - - Class<?>[] shellDeps = new Class[] { - CommonPaths.class, - ConfigurationInfoSource.class, - }; - whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), - eq(shellDeps), unusedCaptor.capture()).thenReturn(unusedTracker); - Class<?>[] vmIdCompleterDeps = new Class[] { - VmInfoDAO.class, - AgentInfoDAO.class - }; - whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), - eq(vmIdCompleterDeps), vmCaptor.capture()).thenReturn(unusedTracker); - - Activator activator = new Activator(); - activator.start(context); - - Action action = vmCaptor.getValue(); - - Map<String, Object> services = new HashMap<>(); - services.put(AgentInfoDAO.class.getName(), mock(AgentInfoDAO.class)); - services.put(VmInfoDAO.class.getName(), mock(VmInfoDAO.class)); - action.dependenciesAvailable(new DependencyProvider(services)); - - assertTrue(context.isServiceRegistered(CompleterService.class.getName(), VmIdCompleterService.class)); - - action.dependenciesUnavailable(); - - assertTrue(context.isServiceRegistered(CompleterService.class.getName(), VmIdCompleterService.class)); - } - private Path createStubThermostatHome() throws Exception { Path tempDir = Files.createTempDirectory("test"); tempDir.toFile().deleteOnExit();
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/VmIdCompleterServiceTest.java Thu Dec 08 10:23:22 2016 -0500 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/VmIdCompleterServiceTest.java Fri Dec 09 08:44:58 2016 -0500 @@ -49,6 +49,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.not; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; public class VmIdCompleterServiceTest { @@ -57,6 +58,7 @@ @Before public void setup() { service = new VmIdCompleterService(); + service.bindVmIdsFinder(mock(VmIdsFinder.class)); } @Test
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/VmIdsFinderImplTest.java Fri Dec 09 08:44:58 2016 -0500 @@ -0,0 +1,238 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.launcher.internal; + +import com.redhat.thermostat.common.cli.CompletionInfo; +import com.redhat.thermostat.storage.core.AgentId; +import com.redhat.thermostat.storage.core.VmId; +import com.redhat.thermostat.storage.dao.AgentInfoDAO; +import com.redhat.thermostat.storage.dao.VmInfoDAO; +import com.redhat.thermostat.storage.model.AgentInformation; +import com.redhat.thermostat.storage.model.VmInfo; +import org.junit.Before; +import org.junit.Test; + +import java.text.Collator; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Set; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class VmIdsFinderImplTest { + + private VmIdsFinderImpl vmIdsFinder; + private final String id0 = "pequ-14as-54yt"; + private final String id1 = "412345-56789"; + private final String id2 = "111111-22222"; + private final String id3 = "98765-543210"; + private final String id4 = "abcdef-01234564-848156"; + private final String id5 = "456-879-4512"; + private final String id6 = "4101-1010-0111"; + private final String mainClass0 = "com.redhat.thermostat.main"; + private final String mainClass1 = "com.redhat.thermostat.blue.launcher.main"; + private final String mainClass2 = "com.redhat.thermostat.vmIdsFinder.main"; + private final String mainClass3 = "com.redhat.thermostat.distribution.main"; + private final String mainClass4 = "com.redhat.thermostat.orange.main"; + private final String mainClass5 = "com.redhat.thermostat.look.search.main"; + private final String mainClass6 = "com.redhat.thermostat.gui.chartspanel.chart.main"; + private final VmInfo.AliveStatus aliveStatus0 = VmInfo.AliveStatus.RUNNING; + private final VmInfo.AliveStatus aliveStatus1 = VmInfo.AliveStatus.RUNNING; + private final VmInfo.AliveStatus aliveStatus2 = VmInfo.AliveStatus.EXITED; + private final VmInfo.AliveStatus aliveStatus3 = VmInfo.AliveStatus.EXITED; + private final VmInfo.AliveStatus aliveStatus4 = VmInfo.AliveStatus.UNKNOWN; + private final VmInfo.AliveStatus aliveStatus5 = VmInfo.AliveStatus.RUNNING; + private final VmInfo.AliveStatus aliveStatus6 = VmInfo.AliveStatus.UNKNOWN; + + private VmIdsFinderImpl vmIdsFinderWithOnlyOneVm; + + @Before + public void setupVmIdsFinder() { + AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); + VmInfoDAO vmsInfoDAO = mock(VmInfoDAO.class); + + vmIdsFinder = new VmIdsFinderImpl(); + vmIdsFinder.bindAgentInfoDao(agentInfoDAO); + vmIdsFinder.bindVmInfoDao(vmsInfoDAO); + + Set<AgentId> agentIds = new HashSet<>(); + AgentId agentId1 = mock(AgentId.class); + AgentId agentId2 = mock(AgentId.class); + agentIds.add(agentId1); + agentIds.add(agentId2); + + when(agentInfoDAO.getAgentIds()).thenReturn(agentIds); + AgentInformation agentInformation1 = mock(AgentInformation.class); + when(agentInfoDAO.getAgentInformation(agentId1)).thenReturn(agentInformation1); + AgentInformation agentInformation2 = mock(AgentInformation.class); + when(agentInfoDAO.getAgentInformation(agentId2)).thenReturn(agentInformation2); + + Set<VmId> vms1 = new HashSet<>(); + VmId vm0 = mock(VmId.class); + VmId vm1 = mock(VmId.class); + VmId vm2 = mock(VmId.class); + VmId vm3 = mock(VmId.class); + vms1.add(vm0); + vms1.add(vm1); + vms1.add(vm2); + vms1.add(vm3); + VmInfo info0 = mock(VmInfo.class); + VmInfo info1 = mock(VmInfo.class); + VmInfo info2 = mock(VmInfo.class); + VmInfo info3 = mock(VmInfo.class); + when(vmsInfoDAO.getVmInfo(vm0)).thenReturn(info0); + when(vmsInfoDAO.getVmInfo(vm1)).thenReturn(info1); + when(vmsInfoDAO.getVmInfo(vm2)).thenReturn(info2); + when(vmsInfoDAO.getVmInfo(vm3)).thenReturn(info3); + + Set<VmId> vms2 = new HashSet<>(); + VmId vm4 = mock(VmId.class); + VmId vm5 = mock(VmId.class); + VmId vm6 = mock(VmId.class); + vms2.add(vm4); + vms2.add(vm5); + vms2.add(vm6); + VmInfo info4 = mock(VmInfo.class); + VmInfo info5 = mock(VmInfo.class); + VmInfo info6 = mock(VmInfo.class); + when(vmsInfoDAO.getVmInfo(vm4)).thenReturn(info4); + when(vmsInfoDAO.getVmInfo(vm5)).thenReturn(info5); + when(vmsInfoDAO.getVmInfo(vm6)).thenReturn(info6); + + when(info0.getVmId()).thenReturn(id0); + when(info1.getVmId()).thenReturn(id1); + when(info2.getVmId()).thenReturn(id2); + when(info3.getVmId()).thenReturn(id3); + when(info4.getVmId()).thenReturn(id4); + when(info5.getVmId()).thenReturn(id5); + when(info6.getVmId()).thenReturn(id6); + + when(info0.getMainClass()).thenReturn(mainClass0); + when(info1.getMainClass()).thenReturn(mainClass1); + when(info2.getMainClass()).thenReturn(mainClass2); + when(info3.getMainClass()).thenReturn(mainClass3); + when(info4.getMainClass()).thenReturn(mainClass4); + when(info5.getMainClass()).thenReturn(mainClass5); + when(info6.getMainClass()).thenReturn(mainClass6); + + when(info0.isAlive(agentInformation1)).thenReturn(aliveStatus0); + when(info1.isAlive(agentInformation1)).thenReturn(aliveStatus1); + when(info2.isAlive(agentInformation1)).thenReturn(aliveStatus2); + when(info3.isAlive(agentInformation1)).thenReturn(aliveStatus3); + + when(info4.isAlive(agentInformation2)).thenReturn(aliveStatus4); + when(info5.isAlive(agentInformation2)).thenReturn(aliveStatus5); + when(info6.isAlive(agentInformation2)).thenReturn(aliveStatus6); + + when(vmsInfoDAO.getVmIds(agentId1)).thenReturn(vms1); + when(vmsInfoDAO.getVmIds(agentId2)).thenReturn(vms2); + + setupVmIdsFinderWithOnlyOneVm(); + } + + private void setupVmIdsFinderWithOnlyOneVm() { + AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); + VmInfoDAO vmsInfoDAO = mock(VmInfoDAO.class); + + vmIdsFinderWithOnlyOneVm = new VmIdsFinderImpl(); + vmIdsFinderWithOnlyOneVm.bindAgentInfoDao(agentInfoDAO); + vmIdsFinderWithOnlyOneVm.bindVmInfoDao(vmsInfoDAO); + + Set<AgentId> agentIds = new HashSet<>(); + AgentId agentId = new AgentId(id0); + agentIds.add(agentId); + + when(agentInfoDAO.getAgentIds()).thenReturn(agentIds); + AgentInformation agentInformation1 = mock(AgentInformation.class); + when(agentInfoDAO.getAgentInformation(agentId)).thenReturn(agentInformation1); + + Set<VmId> vms1 = new HashSet<>(); + VmId vm1 = mock(VmId.class); + vms1.add(vm1); + VmInfo info1 = mock(VmInfo.class); + when(vmsInfoDAO.getVmInfo(vm1)).thenReturn(info1); + + when(info1.getVmId()).thenReturn(id0); + when(info1.getMainClass()).thenReturn(mainClass0); + when(info1.isAlive(agentInformation1)).thenReturn(aliveStatus0); + + when(vmsInfoDAO.getVmIds(agentId)).thenReturn(vms1); + AgentInformation agentInfo0 = mock(AgentInformation.class); + agentInfo0.setAgentId(id0); + } + + @Test + public void testFindIds() { + List<CompletionInfo> result = vmIdsFinder.findCompletions(); + + // Sort to get some predictability in result order + final Collator collator = Collator.getInstance(Locale.US); + + Collections.sort(result, new Comparator<CompletionInfo>() { + @Override + public int compare(CompletionInfo o1, CompletionInfo o2) { + return collator.compare(o1.getActualCompletion(), o2.getActualCompletion()); + } + }); + + assertEquals(7, result.size()); + assertEquals(formatExpected(id2, mainClass2, aliveStatus2), result.get(0).getCompletionWithUserVisibleText()); + assertEquals(formatExpected(id6, mainClass6, aliveStatus6), result.get(1).getCompletionWithUserVisibleText()); + assertEquals(formatExpected(id1, mainClass1, aliveStatus1), result.get(2).getCompletionWithUserVisibleText()); + assertEquals(formatExpected(id5, mainClass5, aliveStatus5), result.get(3).getCompletionWithUserVisibleText()); + assertEquals(formatExpected(id3, mainClass3, aliveStatus3), result.get(4).getCompletionWithUserVisibleText()); + assertEquals(formatExpected(id4, mainClass4, aliveStatus4), result.get(5).getCompletionWithUserVisibleText()); + assertEquals(formatExpected(id0, mainClass0, aliveStatus0), result.get(6).getCompletionWithUserVisibleText()); + } + + @Test + public void testFindsIdsWithOnlyOneVm() { + List<CompletionInfo> result = vmIdsFinderWithOnlyOneVm.findCompletions(); + assertEquals(1, result.size()); + assertEquals(formatExpected(id0, mainClass0, aliveStatus0), result.get(0).getCompletionWithUserVisibleText()); + } + + private String formatExpected(String id, String mainClass, VmInfo.AliveStatus aliveStatus) { + return id + " [" + mainClass + "(" + aliveStatus.toString() + ")]"; + } + +}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/VmIdsFinderTest.java Thu Dec 08 10:23:22 2016 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,253 +0,0 @@ -/* - * Copyright 2012-2016 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.launcher.internal; - -import com.redhat.thermostat.common.cli.CompletionInfo; -import com.redhat.thermostat.common.cli.DependencyServices; -import com.redhat.thermostat.storage.core.AgentId; -import com.redhat.thermostat.storage.core.VmId; -import com.redhat.thermostat.storage.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.dao.VmInfoDAO; -import com.redhat.thermostat.storage.model.AgentInformation; -import com.redhat.thermostat.storage.model.VmInfo; -import org.junit.Before; -import org.junit.Test; - -import java.text.Collator; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.List; -import java.util.Locale; -import java.util.Set; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class VmIdsFinderTest { - - private VmIdsFinder vmIdsFinder; - private final String id0 = "pequ-14as-54yt"; - private final String id1 = "412345-56789"; - private final String id2 = "111111-22222"; - private final String id3 = "98765-543210"; - private final String id4 = "abcdef-01234564-848156"; - private final String id5 = "456-879-4512"; - private final String id6 = "4101-1010-0111"; - private final String mainClass0 = "com.redhat.thermostat.main"; - private final String mainClass1 = "com.redhat.thermostat.blue.launcher.main"; - private final String mainClass2 = "com.redhat.thermostat.vmIdsFinder.main"; - private final String mainClass3 = "com.redhat.thermostat.distribution.main"; - private final String mainClass4 = "com.redhat.thermostat.orange.main"; - private final String mainClass5 = "com.redhat.thermostat.look.search.main"; - private final String mainClass6 = "com.redhat.thermostat.gui.chartspanel.chart.main"; - private final VmInfo.AliveStatus aliveStatus0 = VmInfo.AliveStatus.RUNNING; - private final VmInfo.AliveStatus aliveStatus1 = VmInfo.AliveStatus.RUNNING; - private final VmInfo.AliveStatus aliveStatus2 = VmInfo.AliveStatus.EXITED; - private final VmInfo.AliveStatus aliveStatus3 = VmInfo.AliveStatus.EXITED; - private final VmInfo.AliveStatus aliveStatus4 = VmInfo.AliveStatus.UNKNOWN; - private final VmInfo.AliveStatus aliveStatus5 = VmInfo.AliveStatus.RUNNING; - private final VmInfo.AliveStatus aliveStatus6 = VmInfo.AliveStatus.UNKNOWN; - - private VmIdsFinder vmIdsFinderWithOnlyOneVm; - - @Before - public void setupVmIdsFinder() { - DependencyServices dependencyServices = mock(DependencyServices.class); - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); - when(dependencyServices.hasService(AgentInfoDAO.class)).thenReturn(true); - when(dependencyServices.getService(AgentInfoDAO.class)).thenReturn(agentInfoDAO); - VmInfoDAO vmsInfoDAO = mock(VmInfoDAO.class); - when(dependencyServices.hasService(VmInfoDAO.class)).thenReturn(true); - when(dependencyServices.getService(VmInfoDAO.class)).thenReturn(vmsInfoDAO); - - vmIdsFinder = new VmIdsFinder(dependencyServices); - - Set<AgentId> agentIds = new HashSet<>(); - AgentId agentId1 = mock(AgentId.class); - AgentId agentId2 = mock(AgentId.class); - agentIds.add(agentId1); - agentIds.add(agentId2); - - when(agentInfoDAO.getAgentIds()).thenReturn(agentIds); - AgentInformation agentInformation1 = mock(AgentInformation.class); - when(agentInfoDAO.getAgentInformation(agentId1)).thenReturn(agentInformation1); - AgentInformation agentInformation2 = mock(AgentInformation.class); - when(agentInfoDAO.getAgentInformation(agentId2)).thenReturn(agentInformation2); - - Set<VmId> vms1 = new HashSet<>(); - VmId vm0 = mock(VmId.class); - VmId vm1 = mock(VmId.class); - VmId vm2 = mock(VmId.class); - VmId vm3 = mock(VmId.class); - vms1.add(vm0); - vms1.add(vm1); - vms1.add(vm2); - vms1.add(vm3); - VmInfo info0 = mock(VmInfo.class); - VmInfo info1 = mock(VmInfo.class); - VmInfo info2 = mock(VmInfo.class); - VmInfo info3 = mock(VmInfo.class); - when(vmsInfoDAO.getVmInfo(vm0)).thenReturn(info0); - when(vmsInfoDAO.getVmInfo(vm1)).thenReturn(info1); - when(vmsInfoDAO.getVmInfo(vm2)).thenReturn(info2); - when(vmsInfoDAO.getVmInfo(vm3)).thenReturn(info3); - - Set<VmId> vms2 = new HashSet<>(); - VmId vm4 = mock(VmId.class); - VmId vm5 = mock(VmId.class); - VmId vm6 = mock(VmId.class); - vms2.add(vm4); - vms2.add(vm5); - vms2.add(vm6); - VmInfo info4 = mock(VmInfo.class); - VmInfo info5 = mock(VmInfo.class); - VmInfo info6 = mock(VmInfo.class); - when(vmsInfoDAO.getVmInfo(vm4)).thenReturn(info4); - when(vmsInfoDAO.getVmInfo(vm5)).thenReturn(info5); - when(vmsInfoDAO.getVmInfo(vm6)).thenReturn(info6); - - when(info0.getVmId()).thenReturn(id0); - when(info1.getVmId()).thenReturn(id1); - when(info2.getVmId()).thenReturn(id2); - when(info3.getVmId()).thenReturn(id3); - when(info4.getVmId()).thenReturn(id4); - when(info5.getVmId()).thenReturn(id5); - when(info6.getVmId()).thenReturn(id6); - - when(info0.getMainClass()).thenReturn(mainClass0); - when(info1.getMainClass()).thenReturn(mainClass1); - when(info2.getMainClass()).thenReturn(mainClass2); - when(info3.getMainClass()).thenReturn(mainClass3); - when(info4.getMainClass()).thenReturn(mainClass4); - when(info5.getMainClass()).thenReturn(mainClass5); - when(info6.getMainClass()).thenReturn(mainClass6); - - when(info0.isAlive(agentInformation1)).thenReturn(aliveStatus0); - when(info1.isAlive(agentInformation1)).thenReturn(aliveStatus1); - when(info2.isAlive(agentInformation1)).thenReturn(aliveStatus2); - when(info3.isAlive(agentInformation1)).thenReturn(aliveStatus3); - - when(info4.isAlive(agentInformation2)).thenReturn(aliveStatus4); - when(info5.isAlive(agentInformation2)).thenReturn(aliveStatus5); - when(info6.isAlive(agentInformation2)).thenReturn(aliveStatus6); - - when(vmsInfoDAO.getVmIds(agentId1)).thenReturn(vms1); - when(vmsInfoDAO.getVmIds(agentId2)).thenReturn(vms2); - - setupVmIdsFinderWithOnlyOneVm(); - } - - private void setupVmIdsFinderWithOnlyOneVm() { - DependencyServices dependencyServices = mock(DependencyServices.class); - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); - when(dependencyServices.hasService(AgentInfoDAO.class)).thenReturn(true); - when(dependencyServices.getService(AgentInfoDAO.class)).thenReturn(agentInfoDAO); - VmInfoDAO vmsInfoDAO = mock(VmInfoDAO.class); - when(dependencyServices.hasService(VmInfoDAO.class)).thenReturn(true); - when(dependencyServices.getService(VmInfoDAO.class)).thenReturn(vmsInfoDAO); - - vmIdsFinderWithOnlyOneVm = new VmIdsFinder(dependencyServices); - - Set<AgentId> agentIds = new HashSet<>(); - AgentId agentId = new AgentId(id0); - agentIds.add(agentId); - - when(agentInfoDAO.getAgentIds()).thenReturn(agentIds); - AgentInformation agentInformation1 = mock(AgentInformation.class); - when(agentInfoDAO.getAgentInformation(agentId)).thenReturn(agentInformation1); - - Set<VmId> vms1 = new HashSet<>(); - VmId vm1 = mock(VmId.class); - vms1.add(vm1); - VmInfo info1 = mock(VmInfo.class); - when(vmsInfoDAO.getVmInfo(vm1)).thenReturn(info1); - - when(info1.getVmId()).thenReturn(id0); - when(info1.getMainClass()).thenReturn(mainClass0); - when(info1.isAlive(agentInformation1)).thenReturn(aliveStatus0); - - when(vmsInfoDAO.getVmIds(agentId)).thenReturn(vms1); - AgentInformation agentInfo0 = mock(AgentInformation.class); - agentInfo0.setAgentId(id0); - } - - @Test - public void testFindIds() { - List<CompletionInfo> result = vmIdsFinder.findCompletions(); - - // Sort to get some predictability in result order - final Collator collator = Collator.getInstance(Locale.US); - - Collections.sort(result, new Comparator<CompletionInfo>() { - @Override - public int compare(CompletionInfo o1, CompletionInfo o2) { - return collator.compare(o1.getActualCompletion(), o2.getActualCompletion()); - } - }); - - assertEquals(7, result.size()); - assertEquals(formatExpected(id2, mainClass2, aliveStatus2), result.get(0).getCompletionWithUserVisibleText()); - assertEquals(formatExpected(id6, mainClass6, aliveStatus6), result.get(1).getCompletionWithUserVisibleText()); - assertEquals(formatExpected(id1, mainClass1, aliveStatus1), result.get(2).getCompletionWithUserVisibleText()); - assertEquals(formatExpected(id5, mainClass5, aliveStatus5), result.get(3).getCompletionWithUserVisibleText()); - assertEquals(formatExpected(id3, mainClass3, aliveStatus3), result.get(4).getCompletionWithUserVisibleText()); - assertEquals(formatExpected(id4, mainClass4, aliveStatus4), result.get(5).getCompletionWithUserVisibleText()); - assertEquals(formatExpected(id0, mainClass0, aliveStatus0), result.get(6).getCompletionWithUserVisibleText()); - } - - @Test - public void testFindsIdsWithOnlyOneVm() { - List<CompletionInfo> result = vmIdsFinderWithOnlyOneVm.findCompletions(); - assertEquals(1, result.size()); - assertEquals(formatExpected(id0, mainClass0, aliveStatus0), result.get(0).getCompletionWithUserVisibleText()); - } - - private String formatExpected(String id, String mainClass, VmInfo.AliveStatus aliveStatus) { - return id + " [" + mainClass + "(" + aliveStatus.toString() + ")]"; - } - - @Test - public void testListDependencies() { - assertThat(vmIdsFinder.getRequiredDependencies(), is(equalTo(new Class[]{ VmInfoDAO.class, AgentInfoDAO.class }))); - } - -}