Mercurial > hg > release > thermostat-2.0
changeset 2531:5525742413c3
Update AgentId and PingCommand CompleterServices for Declarative Services
Reviewed-by: jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-November/021529.html
line wrap: on
line diff
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java Thu Nov 17 11:18:49 2016 -0500 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/Activator.java Thu Nov 17 12:38:08 2016 -0500 @@ -143,8 +143,6 @@ private MultipleServiceTracker launcherDepsTracker; private MultipleServiceTracker shellTracker; private MultipleServiceTracker vmIdCompleterDepsTracker; - private MultipleServiceTracker agentIdCompleterDepsTracker; - private MultipleServiceTracker pingCommandCompleterDepsTracker; private CommandRegistry registry; @@ -291,42 +289,8 @@ }); vmIdCompleterDepsTracker.open(); - final AgentIdCompleterService agentIdCompleterService = new AgentIdCompleterService(); - final Class<?>[] agentIdCompleterDeps = new Class[] { AgentInfoDAO.class }; - agentIdCompleterDepsTracker = new MultipleServiceTracker(context, agentIdCompleterDeps, new Action() { - @Override - public void dependenciesAvailable(DependencyProvider services) { - AgentInfoDAO agentDao = services.get(AgentInfoDAO.class); - agentIdCompleterService.setAgentInfoDAO(agentDao); - } - - @Override - public void dependenciesUnavailable() { - agentIdCompleterService.setAgentInfoDAO(null); - } - }); - agentIdCompleterDepsTracker.open(); - - final PingCommandCompleterService pingCommandCompleterService = new PingCommandCompleterService(); - final Class<?>[] pingCommandCompleterDeps = new Class[] { AgentInfoDAO.class }; - pingCommandCompleterDepsTracker = new MultipleServiceTracker(context, pingCommandCompleterDeps, new Action() { - @Override - public void dependenciesAvailable(DependencyProvider services) { - AgentInfoDAO agentDao = services.get(AgentInfoDAO.class); - pingCommandCompleterService.setAgentInfoDAO(agentDao); - } - - @Override - public void dependenciesUnavailable() { - pingCommandCompleterService.setAgentInfoDAO(null); - } - }); - pingCommandCompleterDepsTracker.open(); - context.registerService(CompleterService.class.getName(), helpCommandCompleterService, null); context.registerService(CompleterService.class.getName(), vmIdCompleterService, null); - context.registerService(CompleterService.class.getName(), agentIdCompleterService, null); - context.registerService(CompleterService.class.getName(), pingCommandCompleterService, null); } @Override @@ -346,12 +310,6 @@ if (vmIdCompleterDepsTracker != null) { vmIdCompleterDepsTracker.close(); } - if (agentIdCompleterDepsTracker != null) { - agentIdCompleterDepsTracker.close(); - } - if (pingCommandCompleterDepsTracker != null) { - pingCommandCompleterDepsTracker.close(); - } registry.unregisterCommands(); } }
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/AgentIdCompleterService.java Thu Nov 17 11:18:49 2016 -0500 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/AgentIdCompleterService.java Thu Nov 17 12:38:08 2016 -0500 @@ -36,20 +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 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 AgentIdCompleterService extends AbstractCompleterService { +@Component(immediate = true) +@Service +public class AgentIdCompleterService implements CompleterService { public static final CliCommandOption AGENT_ID_OPTION = new CliCommandOption("a", "agentId", true, "Agent ID", false); + @Reference + private AgentIdsFinder agentIdsFinder; + @Override public Set<String> getCommands() { return TabCompletion.ALL_COMMANDS_COMPLETER; @@ -57,13 +64,23 @@ @Override public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - TabCompleter completer = new CompletionFinderTabCompleter(new AgentIdsFinder(dependencyServices)); - - return Collections.singletonMap(AGENT_ID_OPTION, completer); + if (agentIdsFinder == null) { + return Collections.emptyMap(); + } + return Collections.singletonMap(AGENT_ID_OPTION, new CompletionFinderTabCompleter(agentIdsFinder)); } - void setAgentInfoDAO(AgentInfoDAO agentInfoDAO) { - setService(AgentInfoDAO.class, agentInfoDAO); + @Override + public Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters() { + return Collections.emptyMap(); + } + + public void bindAgentIdsFinder(AgentIdsFinder agentIdsFinder) { + this.agentIdsFinder = agentIdsFinder; + } + + public void unbindAgentIdsFinder(AgentIdsFinder agentIdsFinder) { + this.agentIdsFinder = null; } }
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/AgentIdsFinder.java Thu Nov 17 11:18:49 2016 -0500 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/AgentIdsFinder.java Thu Nov 17 12:38:08 2016 -0500 @@ -36,43 +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.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.model.AgentInformation; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -public class AgentIdsFinder extends AbstractCompletionFinder { - - public AgentIdsFinder(DependencyServices dependencyServices) { - super(dependencyServices); - } +import com.redhat.thermostat.common.cli.CompletionFinder; - @Override - protected Class<?>[] getRequiredDependencies() { - return new Class<?>[]{ AgentInfoDAO.class }; - } - - @Override - public List<CompletionInfo> findCompletions() { - if (!allDependenciesAvailable()) { - return Collections.emptyList(); - } - AgentInfoDAO agentInfoDAO = getService(AgentInfoDAO.class); - return getAgentIdCompletions(agentInfoDAO.getAllAgentInformation()); - } - - private List<CompletionInfo> getAgentIdCompletions(Collection<AgentInformation> agentInfos) { - List<CompletionInfo> agentIds = new ArrayList<>(); - for (AgentInformation agentInfo : agentInfos) { - agentIds.add(new CompletionInfo(agentInfo.getAgentId())); - } - return agentIds; - } - +interface AgentIdsFinder extends CompletionFinder { }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/AgentIdsFinderImpl.java Thu Nov 17 12:38:08 2016 -0500 @@ -0,0 +1,82 @@ +/* + * 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.dao.AgentInfoDAO; +import com.redhat.thermostat.storage.model.AgentInformation; +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 AgentIdsFinderImpl implements AgentIdsFinder { + + @Reference + private AgentInfoDAO agentInfoDAO; + + @Override + public List<CompletionInfo> findCompletions() { + if (agentInfoDAO == null) { + return Collections.emptyList(); + } + return getAgentIdCompletions(agentInfoDAO.getAllAgentInformation()); + } + + private List<CompletionInfo> getAgentIdCompletions(Collection<AgentInformation> agentInfos) { + List<CompletionInfo> agentIds = new ArrayList<>(); + for (AgentInformation agentInfo : agentInfos) { + agentIds.add(new CompletionInfo(agentInfo.getAgentId())); + } + return agentIds; + } + + public void bindAgentInfoDao(AgentInfoDAO agentInfoDAO) { + this.agentInfoDAO = agentInfoDAO; + } + + public void unbindAgentInfoDao(AgentInfoDAO agentInfoDAO) { + this.agentInfoDAO = null; + } + +}
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/PingCommandCompleterService.java Thu Nov 17 11:18:49 2016 -0500 +++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/PingCommandCompleterService.java Thu Nov 17 12:38:08 2016 -0500 @@ -36,11 +36,13 @@ 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 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; @@ -50,7 +52,12 @@ * http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=2876 * http://icedtea.classpath.org/bugzilla/show_bug.cgi?id=2877 */ -public class PingCommandCompleterService extends AbstractCompleterService { +@Component(immediate = true) +@Service +public class PingCommandCompleterService implements CompleterService { + + @Reference + private AgentIdsFinder agentIdsFinder; @Override public Set<String> getCommands() { @@ -59,14 +66,23 @@ @Override public Map<CliCommandOption, ? extends TabCompleter> getOptionCompleters() { - CliCommandOption option = CliCommandOption.POSITIONAL_ARG_COMPLETION; - TabCompleter completer = new CompletionFinderTabCompleter(new AgentIdsFinder(dependencyServices)); - - return Collections.singletonMap(option, completer); + if (agentIdsFinder == null) { + return Collections.emptyMap(); + } + return Collections.singletonMap(CliCommandOption.POSITIONAL_ARG_COMPLETION, new CompletionFinderTabCompleter(agentIdsFinder)); } - void setAgentInfoDAO(AgentInfoDAO agentInfoDAO) { - setService(AgentInfoDAO.class, agentInfoDAO); + @Override + public Map<String, Map<CliCommandOption, ? extends TabCompleter>> getSubcommandCompleters() { + return Collections.emptyMap(); + } + + public void bindAgentIdsFinder(AgentIdsFinder agentIdsFinder) { + this.agentIdsFinder = agentIdsFinder; + } + + public void unbindAgentIdsFinder(AgentIdsFinder agentIdsFinder) { + this.agentIdsFinder = null; } }
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ActivatorTest.java Thu Nov 17 11:18:49 2016 -0500 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ActivatorTest.java Thu Nov 17 12:38:08 2016 -0500 @@ -156,12 +156,12 @@ assertCommandIsRegistered(context, "help", HelpCommand.class); - verify(mockTracker, times(5)).open(); + verify(mockTracker, times(3)).open(); Action action = actionCaptor.getValue(); assertNotNull(action); activator.stop(context); - verify(mockTracker, times(5)).close(); + verify(mockTracker, times(3)).close(); } @Test @@ -195,11 +195,6 @@ }; whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), eq(agentIdCompleterDeps), actionCaptor.capture()).thenReturn(unusedTracker); - Class<?>[] pingCompleterDeps = new Class[] { - AgentInfoDAO.class - }; - whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), - eq(pingCompleterDeps), actionCaptor.capture()).thenReturn(unusedTracker); Activator activator = new Activator(); context.registerService(Keyring.class, mock(Keyring.class), null); @@ -282,11 +277,6 @@ }; whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), eq(agentIdCompleterDeps), actionCaptor.capture()).thenReturn(unusedTracker); - Class<?>[] pingCompleterDeps = new Class[] { - AgentInfoDAO.class - }; - whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), - eq(pingCompleterDeps), actionCaptor.capture()).thenReturn(unusedTracker); Activator activator = new Activator(); ConfigurationInfoSource configurationInfoSource = mock(ConfigurationInfoSource.class); @@ -348,16 +338,6 @@ }; whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), eq(vmIdCompleterDeps), vmCaptor.capture()).thenReturn(unusedTracker); - Class<?>[] agentIdCompleterDeps = new Class[] { - AgentInfoDAO.class - }; - whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), - eq(agentIdCompleterDeps), unusedCaptor.capture()).thenReturn(unusedTracker); - Class<?>[] pingCompleterDeps = new Class[] { - AgentInfoDAO.class - }; - whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), - eq(pingCompleterDeps), unusedCaptor.capture()).thenReturn(unusedTracker); Activator activator = new Activator(); activator.start(context); @@ -376,112 +356,6 @@ assertTrue(context.isServiceRegistered(CompleterService.class.getName(), VmIdCompleterService.class)); } - @Test - public void testAgentIdCompleterServiceAvailability() throws Exception { - StubBundleContext context = new StubBundleContext(); - MultipleServiceTracker unusedTracker = mock(MultipleServiceTracker.class); - ArgumentCaptor<Action> unusedCaptor = ArgumentCaptor.forClass(Action.class); - ArgumentCaptor<Action> agentCaptor = 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), unusedCaptor.capture()).thenReturn(unusedTracker); - Class<?>[] agentIdCompleterDeps = new Class[] { - AgentInfoDAO.class - }; - whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), - eq(agentIdCompleterDeps), agentCaptor.capture()).thenReturn(unusedTracker); - Class<?>[] pingCompleterDeps = new Class[] { - AgentInfoDAO.class - }; - whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), - eq(pingCompleterDeps), agentCaptor.capture()).thenReturn(unusedTracker); - - Activator activator = new Activator(); - activator.start(context); - - Action action = agentCaptor.getAllValues().get(1); - - Map<String, Object> services = new HashMap<>(); - services.put(AgentInfoDAO.class.getName(), mock(AgentInfoDAO.class)); - action.dependenciesAvailable(new DependencyProvider(services)); - - assertTrue(context.isServiceRegistered(CompleterService.class.getName(), AgentIdCompleterService.class)); - - action.dependenciesUnavailable(); - - assertTrue(context.isServiceRegistered(CompleterService.class.getName(), AgentIdCompleterService.class)); - } - - @Test - public void testPingCommandCompleterServiceAvailability() throws Exception { - StubBundleContext context = new StubBundleContext(); - MultipleServiceTracker unusedTracker = mock(MultipleServiceTracker.class); - ArgumentCaptor<Action> unusedCaptor = ArgumentCaptor.forClass(Action.class); - ArgumentCaptor<Action> pingCaptor = 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), unusedCaptor.capture()).thenReturn(unusedTracker); - Class<?>[] agentIdCompleterDeps = new Class[] { - AgentInfoDAO.class - }; - whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), - eq(agentIdCompleterDeps), unusedCaptor.capture()).thenReturn(unusedTracker); - Class<?>[] pingCompleterDeps = new Class[] { - AgentInfoDAO.class - }; - whenNew(MultipleServiceTracker.class).withParameterTypes(BundleContext.class, Class[].class, Action.class).withArguments(eq(context), - eq(pingCompleterDeps), pingCaptor.capture()).thenReturn(unusedTracker); - - Activator activator = new Activator(); - activator.start(context); - - Action action = pingCaptor.getValue(); - - Map<String, Object> services = new HashMap<>(); - services.put(AgentInfoDAO.class.getName(), mock(AgentInfoDAO.class)); - action.dependenciesAvailable(new DependencyProvider(services)); - - assertTrue(context.isServiceRegistered(CompleterService.class.getName(), PingCommandCompleterService.class)); - - action.dependenciesUnavailable(); - - assertTrue(context.isServiceRegistered(CompleterService.class.getName(), PingCommandCompleterService.class)); - } - private Path createStubThermostatHome() throws Exception { Path tempDir = Files.createTempDirectory("test"); tempDir.toFile().deleteOnExit();
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/AgentIdCompleterServiceTest.java Thu Nov 17 11:18:49 2016 -0500 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/AgentIdCompleterServiceTest.java Thu Nov 17 12:38:08 2016 -0500 @@ -49,14 +49,18 @@ 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 AgentIdCompleterServiceTest { private AgentIdCompleterService service; + private AgentIdsFinder finder; @Before public void setup() { service = new AgentIdCompleterService(); + finder = mock(AgentIdsFinder.class); + service.bindAgentIdsFinder(finder); } @Test @@ -78,4 +82,10 @@ assertThat(completer, is(not(equalTo(null)))); } + @Test + @SuppressWarnings("unchecked") + public void testProvidesNoSubcommandCompletions() { + assertThat(service.getSubcommandCompleters().size(), is(0)); + } + }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/AgentIdsFinderImplTest.java Thu Nov 17 12:38:08 2016 -0500 @@ -0,0 +1,95 @@ +/* + * 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.dao.AgentInfoDAO; +import com.redhat.thermostat.storage.model.AgentInformation; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class AgentIdsFinderImplTest { + + private AgentIdsFinderImpl finder; + + @Before + public void setup() { + finder = new AgentIdsFinderImpl(); + } + + @Test + public void testFindIds() { + AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); + finder.bindAgentInfoDao(agentInfoDAO); + + String id1 = "012345-56789"; + String id2 = "111111-22222"; + String id3 = "98765-543210"; + String id4 = "abcdef-01234564-848156"; + AgentInformation agentInfo1 = mock(AgentInformation.class); + agentInfo1.setAgentId(id1); + AgentInformation agentInfo2 = mock(AgentInformation.class); + agentInfo2.setAgentId(id2); + AgentInformation agentInfo3 = mock(AgentInformation.class); + agentInfo3.setAgentId(id3); + AgentInformation agentInfo4 = mock(AgentInformation.class); + agentInfo4.setAgentId(id4); + + Collection<AgentInformation> collection = new ArrayList<>(); + collection.add(agentInfo1); + collection.add(agentInfo2); + collection.add(agentInfo3); + collection.add(agentInfo4); + when(agentInfoDAO.getAllAgentInformation()).thenReturn((List<AgentInformation>) collection); + + List<CompletionInfo> result = finder.findCompletions(); + assertEquals(4, result.size()); + assertEquals(id1, result.get(0).getActualCompletion()); + assertEquals(id2, result.get(1).getActualCompletion()); + assertEquals(id3, result.get(2).getActualCompletion()); + assertEquals(id4, result.get(3).getActualCompletion()); + } + +}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/AgentIdsFinderTest.java Thu Nov 17 11:18:49 2016 -0500 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +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.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.model.AgentInformation; -import org.junit.Before; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; - -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 AgentIdsFinderTest { - - private DependencyServices dependencyServices; - private AgentIdsFinder finder; - - @Before - public void setup() { - dependencyServices = mock(DependencyServices.class); - finder = new AgentIdsFinder(dependencyServices); - } - - @Test - public void testFindIds() { - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); - when(dependencyServices.hasService(AgentInfoDAO.class)).thenReturn(true); - when(dependencyServices.getService(AgentInfoDAO.class)).thenReturn(agentInfoDAO); - - String id1 = "012345-56789"; - String id2 = "111111-22222"; - String id3 = "98765-543210"; - String id4 = "abcdef-01234564-848156"; - AgentInformation agentInfo1 = mock(AgentInformation.class); - agentInfo1.setAgentId(id1); - AgentInformation agentInfo2 = mock(AgentInformation.class); - agentInfo2.setAgentId(id2); - AgentInformation agentInfo3 = mock(AgentInformation.class); - agentInfo3.setAgentId(id3); - AgentInformation agentInfo4 = mock(AgentInformation.class); - agentInfo4.setAgentId(id4); - - Collection<AgentInformation> collection = new ArrayList<>(); - collection.add(agentInfo1); - collection.add(agentInfo2); - collection.add(agentInfo3); - collection.add(agentInfo4); - when(agentInfoDAO.getAllAgentInformation()).thenReturn((List<AgentInformation>) collection); - - List<CompletionInfo> result = finder.findCompletions(); - assertEquals(4, result.size()); - assertEquals(id1, result.get(0).getActualCompletion()); - assertEquals(id2, result.get(1).getActualCompletion()); - assertEquals(id3, result.get(2).getActualCompletion()); - assertEquals(id4, result.get(3).getActualCompletion()); - } - - @Test - public void testListDependencies() { - assertThat(finder.getRequiredDependencies(), is(equalTo(new Class[]{AgentInfoDAO.class}))); - } -}
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/PingCommandCompleterServiceTest.java Thu Nov 17 11:18:49 2016 -0500 +++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/PingCommandCompleterServiceTest.java Thu Nov 17 12:38:08 2016 -0500 @@ -38,6 +38,7 @@ import com.redhat.thermostat.common.cli.CliCommandOption; import com.redhat.thermostat.common.cli.TabCompleter; +import com.redhat.thermostat.storage.core.AgentId; import org.junit.Before; import org.junit.Test; @@ -49,18 +50,22 @@ 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 PingCommandCompleterServiceTest { private PingCommandCompleterService service; + private AgentIdsFinder finder; @Before public void setup() { service = new PingCommandCompleterService(); + finder = mock(AgentIdsFinder.class); + service.bindAgentIdsFinder(finder); } @Test - public void testCompleterAppliesToAllCommands() { + public void testCompleterAppliesToPingCommandOnly() { Set<String> commands = service.getCommands(); Set<String> expected = Collections.singleton("ping"); assertThat(commands, is(equalTo(expected))); @@ -78,4 +83,10 @@ assertThat(completer, is(not(equalTo(null)))); } + @Test + @SuppressWarnings("unchecked") + public void testProvidesNoSubcommandCompletions() { + assertThat(service.getSubcommandCompleters().size(), is(0)); + } + }