changeset 1397:c406ad5b66f1

Backport clean-data command fix to Thermostat 1.0 Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-May/009825.html PR1782
author Severin Gehwolf <sgehwolf@redhat.com>
date Tue, 22 Apr 2014 17:01:48 +0200
parents 4a8b8abc4498
children 044868dbf9ad
files client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/CleanDataCommand.java client/cli/src/test/java/com/redhat/thermostat/client/cli/internal/CleanDataCommandTest.java distribution/config/thermostat-roles.properties
diffstat 3 files changed, 39 insertions(+), 95 deletions(-) [+]
line wrap: on
line diff
--- a/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/CleanDataCommand.java	Wed Apr 23 19:09:59 2014 +0200
+++ b/client/cli/src/main/java/com/redhat/thermostat/client/cli/internal/CleanDataCommand.java	Tue Apr 22 17:01:48 2014 +0200
@@ -37,13 +37,7 @@
 package com.redhat.thermostat.client.cli.internal;
 
 import java.io.PrintStream;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.HashSet;
 import java.util.List;
-import java.util.Set;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -52,25 +46,15 @@
 import com.redhat.thermostat.common.cli.Arguments;
 import com.redhat.thermostat.common.cli.CommandContext;
 import com.redhat.thermostat.common.cli.CommandException;
-import com.redhat.thermostat.common.utils.LoggingUtils;
 import com.redhat.thermostat.shared.locale.Translate;
-import com.redhat.thermostat.storage.core.Categories;
-import com.redhat.thermostat.storage.core.Category;
-import com.redhat.thermostat.storage.core.Cursor;
-import com.redhat.thermostat.storage.core.DescriptorParsingException;
 import com.redhat.thermostat.storage.core.HostRef;
-import com.redhat.thermostat.storage.core.PreparedStatement;
-import com.redhat.thermostat.storage.core.StatementDescriptor;
-import com.redhat.thermostat.storage.core.StatementExecutionException;
 import com.redhat.thermostat.storage.core.Storage;
 import com.redhat.thermostat.storage.dao.AgentInfoDAO;
 import com.redhat.thermostat.storage.model.AgentInformation;
-import com.redhat.thermostat.storage.model.BasePojo;
 
 public class CleanDataCommand extends AbstractCommand {
 
     private static final Translate<LocaleResources> translator = LocaleResources.createLocalizer();
-    private static final Logger logger = LoggingUtils.getLogger(CleanDataCommand.class);
     private BundleContext bundleContext;
     private boolean removeLiveAgent = false;
 
@@ -110,14 +94,10 @@
         AgentInfoDAO agentInfoDAO = (AgentInfoDAO) bundleContext.getService(agentServiceRef);
         
         try {
-            Set<String> storedAgentIdList = getAllRegisteredAgents(storage);
-            
             for (String agentId : agentIdList) {
-                AgentInformation agentInfo = agentInfoDAO.getAgentInformation(new HostRef(agentId, agentId));
+                AgentInformation agentInfo = agentInfoDAO.getAgentInformation(new HostRef(agentId, "unused"));
                 if (agentInfo != null) {
                     removeAgentDataIfSane(storage, agentId, !agentInfo.isAlive(), output);
-                } else if (storedAgentIdList.contains(agentId)) {
-                    removeAgentDataIfSane(storage, agentId, true, output);
                 } else {
                     output.println(translator.localize(LocaleResources.AGENT_NOT_FOUND, agentId).getContents());
                 }
@@ -135,16 +115,10 @@
         AgentInfoDAO agentInfoDAO = (AgentInfoDAO) bundleContext.getService(agentServiceRef);
         
         try {
-            Set<String> storedAgentIdList = getAllRegisteredAgents(storage);
-            List<String> aliveAgentsId = new ArrayList<String>();
-            List<AgentInformation> allAliveAgentsInfo = agentInfoDAO.getAliveAgents(); 
-            for (AgentInformation aliveAgent : allAliveAgentsInfo) {
-                aliveAgentsId.add(aliveAgent.getAgentId());
-            }
-            
-            for (String agentId : storedAgentIdList) {
-                boolean isDead = !aliveAgentsId.contains(agentId);
-                removeAgentDataIfSane(storage, agentId, isDead, output);
+            List<AgentInformation> allAgents = agentInfoDAO.getAllAgentInformation();
+            for (AgentInformation agentInfo : allAgents) {
+                boolean isDead = !agentInfo.isAlive();
+                removeAgentDataIfSane(storage, agentInfo.getAgentId(), isDead, output);
             }
         } finally {
             bundleContext.ungetService(agentServiceRef);
@@ -172,29 +146,4 @@
         }
     }
 
-    private Set<String> getAllRegisteredAgents(Storage storage) {
-        List<Category<?>> categories = Categories.getAllCategories();
-        Set<String> agents = new HashSet<>();
-        PreparedStatement<BasePojo> prepared = null;
-        Cursor<BasePojo> agentCursor = null;
-        for (Category category : categories) {
-            String query = "QUERY " + category.getName();
-            StatementDescriptor<BasePojo> desc = new StatementDescriptor<>(category, query);
-            try {
-                prepared = storage.prepareStatement(desc);
-                agentCursor = prepared.executeQuery();
-            } catch (DescriptorParsingException e) {
-                logger.log(Level.SEVERE, "Preparing query '" + desc + "' failed!", e);
-                return Collections.emptySet();
-            } catch (StatementExecutionException e) {
-                logger.log(Level.SEVERE, "Executing query '" + desc + "' failed!", e);
-                return Collections.emptySet();
-            }
-            while (agentCursor.hasNext()) {
-                agents.add(agentCursor.next().getAgentId());
-            }
-        }
-        return agents;
-    }
-
 }
--- a/client/cli/src/test/java/com/redhat/thermostat/client/cli/internal/CleanDataCommandTest.java	Wed Apr 23 19:09:59 2014 +0200
+++ b/client/cli/src/test/java/com/redhat/thermostat/client/cli/internal/CleanDataCommandTest.java	Tue Apr 22 17:01:48 2014 +0200
@@ -36,44 +36,33 @@
 
 package com.redhat.thermostat.client.cli.internal;
 
-import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.isA;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
-import static org.mockito.Mockito.times;
 
 import java.io.PrintStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 
 import org.junit.Before;
 import org.junit.Test;
-import org.junit.runner.RunWith;
 
 import com.redhat.thermostat.common.cli.Arguments;
 import com.redhat.thermostat.common.cli.CommandContext;
 import com.redhat.thermostat.common.cli.CommandException;
 import com.redhat.thermostat.common.cli.Console;
-import com.redhat.thermostat.storage.core.Cursor;
 import com.redhat.thermostat.storage.core.DescriptorParsingException;
 import com.redhat.thermostat.storage.core.HostRef;
-import com.redhat.thermostat.storage.core.PreparedStatement;
-import com.redhat.thermostat.storage.core.StatementDescriptor;
 import com.redhat.thermostat.storage.core.StatementExecutionException;
 import com.redhat.thermostat.storage.core.Storage;
 import com.redhat.thermostat.storage.dao.AgentInfoDAO;
 import com.redhat.thermostat.storage.model.AgentInformation;
-import com.redhat.thermostat.storage.model.BasePojo;
 import com.redhat.thermostat.testutils.StubBundleContext;
 
-import org.powermock.api.mockito.PowerMockito;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-import org.powermock.modules.junit4.PowerMockRunner;
-
-@RunWith(PowerMockRunner.class)
-@PrepareForTest(AgentInformation.class)
 public class CleanDataCommandTest {
 
     private CleanDataCommand cleanDataCommand;
@@ -82,6 +71,8 @@
     private PrintStream mockOutput;
     private AgentInfoDAO mockAgentInfoDAO;
     private Arguments mockArguments;
+    private List<AgentInformation> liveAgentInfoList;
+    private List<AgentInformation> allAgentInfoList;
 
     @Before
     public void setUp() throws DescriptorParsingException, StatementExecutionException {
@@ -104,36 +95,31 @@
         when(mockCommandContext.getConsole()).thenReturn(mockConsole);
         when(mockConsole.getOutput()).thenReturn(mockOutput);
         
-        List<AgentInformation> liveAgentInfoList = new ArrayList<AgentInformation>();
-        AgentInformation mockAgent1 = PowerMockito.mock(AgentInformation.class);
-        AgentInformation mockAgent2 = PowerMockito.mock(AgentInformation.class);
-        AgentInformation mockAgent3 = PowerMockito.mock(AgentInformation.class);
-        AgentInformation mockAgent4 = PowerMockito.mock(AgentInformation.class);
-        AgentInformation mockAgent5 = PowerMockito.mock(AgentInformation.class);
-        when(mockAgent1.getAgentId()).thenReturn("agentId1");
-        when(mockAgent2.getAgentId()).thenReturn("agentId2");
-        when(mockAgent3.getAgentId()).thenReturn("agentId3");
-        when(mockAgent4.getAgentId()).thenReturn("agentId4");
-        when(mockAgent5.getAgentId()).thenReturn("agentId5");
-        when(mockAgent4.isAlive()).thenReturn(true);
-        when(mockAgent5.isAlive()).thenReturn(true);
+        liveAgentInfoList = new ArrayList<AgentInformation>();
+        AgentInformation mockAgent1 = new AgentInformation("agentId1");
+        AgentInformation mockAgent2 = new AgentInformation("agentId2");
+        AgentInformation mockAgent3 = new AgentInformation("agentId3");
+        AgentInformation mockAgent4 = new AgentInformation("agentId4");
+        AgentInformation mockAgent5 = new AgentInformation("agentId5");
+        mockAgent4.setAlive(true);
+        mockAgent5.setAlive(true);
         
         liveAgentInfoList.add(mockAgent4);
         liveAgentInfoList.add(mockAgent5);
-        when(mockAgentInfoDAO.getAliveAgents()).thenReturn(liveAgentInfoList);
-        when(mockAgentInfoDAO.getAgentInformation(new HostRef("agentId1", "agentId1"))).thenReturn(mockAgent1);
-        when(mockAgentInfoDAO.getAgentInformation(new HostRef("agentId2", "agentId2"))).thenReturn(mockAgent2);
-        when(mockAgentInfoDAO.getAgentInformation(new HostRef("agentId3", "agentId3"))).thenReturn(mockAgent3);
-        when(mockAgentInfoDAO.getAgentInformation(new HostRef("agentId4", "agentId4"))).thenReturn(mockAgent4);
-        when(mockAgentInfoDAO.getAgentInformation(new HostRef("agentId5", "agentId5"))).thenReturn(mockAgent5);
+        allAgentInfoList = new ArrayList<>(5);
+        allAgentInfoList.addAll(Arrays.asList(
+           mockAgent1,
+           mockAgent2,
+           mockAgent3,
+           mockAgent4,
+           mockAgent5
+        ));
         
-        Cursor<BasePojo> agentCursor = (Cursor<BasePojo>) mock(Cursor.class);
-        when(agentCursor.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
-        when(agentCursor.next()).thenReturn(mockAgent1).thenReturn(mockAgent2).thenReturn(mockAgent3).thenReturn(mockAgent4).thenReturn(mockAgent5).thenReturn(null);
-        
-        PreparedStatement<BasePojo> prepared = (PreparedStatement<BasePojo>) mock(PreparedStatement.class);
-        when(mockStorage.prepareStatement((StatementDescriptor<BasePojo>) any(StatementDescriptor.class))).thenReturn(prepared);
-        when(prepared.executeQuery()).thenReturn(agentCursor);
+        when(mockAgentInfoDAO.getAgentInformation(new HostRef("agentId1", "unused"))).thenReturn(mockAgent1);
+        when(mockAgentInfoDAO.getAgentInformation(new HostRef("agentId2", "unused"))).thenReturn(mockAgent2);
+        when(mockAgentInfoDAO.getAgentInformation(new HostRef("agentId3", "unused"))).thenReturn(mockAgent3);
+        when(mockAgentInfoDAO.getAgentInformation(new HostRef("agentId4", "unused"))).thenReturn(mockAgent4);
+        when(mockAgentInfoDAO.getAgentInformation(new HostRef("agentId5", "unused"))).thenReturn(mockAgent5);
     }
 
     @Test
@@ -209,6 +195,7 @@
     @Test
     public void testRemoveAllDeadAgents() throws CommandException {
         when(mockArguments.hasArgument("all")).thenReturn(true);
+        when(mockAgentInfoDAO.getAllAgentInformation()).thenReturn(allAgentInfoList);
         
         cleanDataCommand.run(mockCommandContext);
         
@@ -222,6 +209,7 @@
     public void testRemoveAllLiveAgents() throws CommandException {
         when(mockArguments.hasArgument("all")).thenReturn(true);
         when(mockArguments.hasArgument("alive")).thenReturn(true);
+        when(mockAgentInfoDAO.getAllAgentInformation()).thenReturn(allAgentInfoList);
       
         cleanDataCommand.run(mockCommandContext);
       
--- a/distribution/config/thermostat-roles.properties	Wed Apr 23 19:09:59 2014 +0200
+++ b/distribution/config/thermostat-roles.properties	Tue Apr 22 17:01:48 2014 +0200
@@ -53,6 +53,13 @@
 #                    thermostat-vms-grant-read-username-ALL, \
 #                    thermostat-vms-grant-read-vmId-ALL
 #
+# Example recursive role definition which allows thermostat users to
+# use the clean-data command, which may perform global delete operations.
+# Consider assigning this role to client users if they need to use the
+# clean-data command. Note that other roles for thermostat client users
+# grant read-only access - at various levels - only.
+#thermostat-cmd-clean-data = thermostat-purge
+#
 # Example recursive role definition that grants all command channel privileges.
 # You may uncomment the following lines and assign your client users this
 # "thermostat-cmdc" role.