changeset 1826:1240d8f621f3

Disable JMX Notifications tab controls when selected VM is not running Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-October/016677.html PR2666
author Andrew Azores <aazores@redhat.com>
date Thu, 08 Oct 2015 10:54:59 -0400
parents 2ae8438ee9d2
children b1ec95a6f5b6
files vm-jmx/client-core/src/main/java/com/redhat/thermostat/vm/jmx/client/core/JmxNotificationsView.java vm-jmx/client-core/src/main/java/com/redhat/thermostat/vm/jmx/client/core/internal/Activator.java vm-jmx/client-core/src/main/java/com/redhat/thermostat/vm/jmx/client/core/internal/JmxNotificationsViewController.java vm-jmx/client-core/src/main/java/com/redhat/thermostat/vm/jmx/client/core/internal/JmxNotificationsViewServiceImpl.java vm-jmx/client-core/src/test/java/com/redhat/thermostat/vm/jmx/client/core/internal/ActivatorTest.java vm-jmx/client-core/src/test/java/com/redhat/thermostat/vm/jmx/client/core/internal/JmxNotificationsViewControllerTest.java vm-jmx/client-core/src/test/java/com/redhat/thermostat/vm/jmx/client/core/internal/JmxNotificationsViewServiceImplTest.java vm-jmx/client-swing/src/main/java/com/redhat/thermostat/vm/jmx/client/swing/internal/JmxNotificationsSwingView.java
diffstat 8 files changed, 96 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/vm-jmx/client-core/src/main/java/com/redhat/thermostat/vm/jmx/client/core/JmxNotificationsView.java	Mon Oct 05 11:58:01 2015 -0400
+++ b/vm-jmx/client-core/src/main/java/com/redhat/thermostat/vm/jmx/client/core/JmxNotificationsView.java	Thu Oct 08 10:54:59 2015 -0400
@@ -51,6 +51,7 @@
     public abstract void addNotificationActionListener(ActionListener<NotificationAction> listener);
     public abstract void removeNotificationActionListener(ActionListener<NotificationAction> listener);
 
+    public abstract void setViewControlsEnabled(boolean enabled);
     public abstract void setNotificationsEnabled(boolean enabled);
     public abstract void clearNotifications();
     public abstract void addNotification(JmxNotification data);
--- a/vm-jmx/client-core/src/main/java/com/redhat/thermostat/vm/jmx/client/core/internal/Activator.java	Mon Oct 05 11:58:01 2015 -0400
+++ b/vm-jmx/client-core/src/main/java/com/redhat/thermostat/vm/jmx/client/core/internal/Activator.java	Thu Oct 08 10:54:59 2015 -0400
@@ -40,6 +40,7 @@
 import java.util.Hashtable;
 import java.util.Map;
 
+import com.redhat.thermostat.storage.dao.VmInfoDAO;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
@@ -67,6 +68,7 @@
         final Class<?>[] deps = new Class<?>[] {
                 ApplicationService.class,
                 AgentInfoDAO.class,
+                VmInfoDAO.class,
                 JmxNotificationDAO.class,
                 ApplicationService.class,
                 RequestQueue.class,
@@ -78,12 +80,13 @@
             public void dependenciesAvailable(Map<String, Object> services) {
                 ApplicationService appSvc = (ApplicationService) services.get(ApplicationService.class.getName());
                 AgentInfoDAO agentDao = (AgentInfoDAO) services.get(AgentInfoDAO.class.getName());
+                VmInfoDAO vmInfoDAO = (VmInfoDAO) services.get(VmInfoDAO.class.getName());
                 JmxNotificationDAO notificationDao = (JmxNotificationDAO) services.get(JmxNotificationDAO.class.getName());
                 JmxNotificationsViewProvider viewProvider = (JmxNotificationsViewProvider) services.get(JmxNotificationsViewProvider.class.getName());
                 TimerFactory tf = ((ApplicationService) services.get(ApplicationService.class.getName())).getTimerFactory();
                 RequestQueue queue = (RequestQueue) services.get(RequestQueue.class.getName());
 
-                JmxNotificationsViewServiceImpl notificationsView = new JmxNotificationsViewServiceImpl(appSvc, agentDao, notificationDao, queue, tf, viewProvider);
+                JmxNotificationsViewServiceImpl notificationsView = new JmxNotificationsViewServiceImpl(appSvc, agentDao, vmInfoDAO, notificationDao, queue, tf, viewProvider);
 
                 Dictionary props = new Hashtable();
                 props.put(Constants.GENERIC_SERVICE_CLASSNAME, VmRef.class.getName());
--- a/vm-jmx/client-core/src/main/java/com/redhat/thermostat/vm/jmx/client/core/internal/JmxNotificationsViewController.java	Mon Oct 05 11:58:01 2015 -0400
+++ b/vm-jmx/client-core/src/main/java/com/redhat/thermostat/vm/jmx/client/core/internal/JmxNotificationsViewController.java	Thu Oct 08 10:54:59 2015 -0400
@@ -52,8 +52,12 @@
 import com.redhat.thermostat.common.TimerFactory;
 import com.redhat.thermostat.shared.locale.LocalizedString;
 import com.redhat.thermostat.shared.locale.Translate;
+import com.redhat.thermostat.storage.core.HostRef;
 import com.redhat.thermostat.storage.core.VmRef;
 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 com.redhat.thermostat.vm.jmx.client.core.JmxNotificationsView;
 import com.redhat.thermostat.vm.jmx.client.core.JmxNotificationsView.NotificationAction;
 import com.redhat.thermostat.vm.jmx.client.core.JmxNotificationsViewProvider;
@@ -67,26 +71,30 @@
     private final JmxNotificationsView view;
     private final Timer timer;
     private final JmxNotificationDAO dao;
+    private final VmInfoDAO vmInfo;
+    private final AgentInfoDAO agentInfo;
     private final VmRef vm;
+
     private final Translate<LocaleResources> t = LocaleResources.createLocalizer();
     private final JmxToggleNotificationRequest toggleReq;
+    private final AtomicBoolean notificationsEnabled = new AtomicBoolean(false);
 
-    private final AtomicBoolean notificationsEnabled = new AtomicBoolean(false);
-    
     public JmxNotificationsViewController(ApplicationService appSvc,
-            AgentInfoDAO agent, JmxNotificationDAO notification,
+            AgentInfoDAO agent, VmInfoDAO vmInfo, JmxNotificationDAO notification,
             TimerFactory timerFactory, RequestQueue queue,
             JmxNotificationsViewProvider viewProvider, VmRef vmId) {
-        this(appSvc, agent, notification, timerFactory, queue, viewProvider, vmId,
+        this(appSvc, agent, vmInfo, notification, timerFactory, queue, viewProvider, vmId,
                 new JmxToggleNotificationRequestFactory());
     }
     
     JmxNotificationsViewController(final ApplicationService appSvc,
-            AgentInfoDAO agent, JmxNotificationDAO notification,
+            AgentInfoDAO agent, VmInfoDAO vmInfoDAO, JmxNotificationDAO notification,
             TimerFactory timerFactory, RequestQueue queue,
             JmxNotificationsViewProvider viewProvider, VmRef vmId,
             JmxToggleNotificationRequestFactory reqFactory) {
         this.dao = notification;
+        this.agentInfo = agent;
+        this.vmInfo = vmInfoDAO;
         this.view = viewProvider.createView();
         this.timer = timerFactory.createTimer();
         this.vm = vmId;
@@ -130,6 +138,7 @@
                     stopUpdatingView();
                     break;
                 case VISIBLE:
+                    view.setViewControlsEnabled(isVmAlive());
                     startUpdatingView();
                     break;
                 }
@@ -151,6 +160,7 @@
             }
         });
 
+        view.setViewControlsEnabled(isVmAlive());
     }
 
     private void initializeTimer() {
@@ -182,6 +192,13 @@
 
     }
 
+    private boolean isVmAlive() {
+        HostRef agent = vm.getHostRef();
+        AgentInformation agentInformation = agentInfo.getAgentInformation(agent);
+        VmInfo.AliveStatus status = vmInfo.getVmInfo(vm).isAlive(agentInformation);
+        return status.equals(VmInfo.AliveStatus.RUNNING);
+    }
+
     private void startUpdatingView() {
         timer.start();
     }
--- a/vm-jmx/client-core/src/main/java/com/redhat/thermostat/vm/jmx/client/core/internal/JmxNotificationsViewServiceImpl.java	Mon Oct 05 11:58:01 2015 -0400
+++ b/vm-jmx/client-core/src/main/java/com/redhat/thermostat/vm/jmx/client/core/internal/JmxNotificationsViewServiceImpl.java	Thu Oct 08 10:54:59 2015 -0400
@@ -45,6 +45,7 @@
 import com.redhat.thermostat.common.TimerFactory;
 import com.redhat.thermostat.storage.core.VmRef;
 import com.redhat.thermostat.storage.dao.AgentInfoDAO;
+import com.redhat.thermostat.storage.dao.VmInfoDAO;
 import com.redhat.thermostat.vm.jmx.client.core.JmxNotificationsViewProvider;
 import com.redhat.thermostat.vm.jmx.client.core.JmxNotificationsViewService;
 import com.redhat.thermostat.vm.jmx.common.JmxNotificationDAO;
@@ -55,17 +56,19 @@
 
     private final ApplicationService appSvc;
     private final JmxNotificationsViewProvider viewProvider;
+    private final AgentInfoDAO agentDao;
+    private final VmInfoDAO vmInfoDao;
     private final JmxNotificationDAO notificationDao;
-    private final AgentInfoDAO agentDao;
     private final RequestQueue requestQueue;
     private final TimerFactory timerFactory;
 
     public JmxNotificationsViewServiceImpl(ApplicationService appSvc,
-            AgentInfoDAO agentDao, JmxNotificationDAO notificationDao,
+            AgentInfoDAO agentDao, VmInfoDAO vmInfoDao, JmxNotificationDAO notificationDao,
             RequestQueue requestQueue, TimerFactory timerFactory,
             JmxNotificationsViewProvider viewProvider) {
         this.appSvc = appSvc;
         this.agentDao = agentDao;
+        this.vmInfoDao = vmInfoDao;
         this.notificationDao = notificationDao;
         this.timerFactory = timerFactory;
         this.requestQueue = requestQueue;
@@ -79,7 +82,7 @@
 
     @Override
     public InformationServiceController<VmRef> getInformationServiceController(VmRef ref) {
-        return new JmxNotificationsViewController(appSvc, agentDao, notificationDao, timerFactory,
+        return new JmxNotificationsViewController(appSvc, agentDao, vmInfoDao, notificationDao, timerFactory,
                 requestQueue, viewProvider, ref);
     }
 
--- a/vm-jmx/client-core/src/test/java/com/redhat/thermostat/vm/jmx/client/core/internal/ActivatorTest.java	Mon Oct 05 11:58:01 2015 -0400
+++ b/vm-jmx/client-core/src/test/java/com/redhat/thermostat/vm/jmx/client/core/internal/ActivatorTest.java	Thu Oct 08 10:54:59 2015 -0400
@@ -40,6 +40,7 @@
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
 
+import com.redhat.thermostat.storage.dao.VmInfoDAO;
 import org.junit.Test;
 
 import com.redhat.thermostat.client.command.RequestQueue;
@@ -58,6 +59,7 @@
 
         bundleContext.registerService(ApplicationService.class, mock(ApplicationService.class), null);
         bundleContext.registerService(AgentInfoDAO.class, mock(AgentInfoDAO.class), null);
+        bundleContext.registerService(VmInfoDAO.class, mock(VmInfoDAO.class), null);
         bundleContext.registerService(JmxNotificationDAO.class, mock(JmxNotificationDAO.class), null);
         bundleContext.registerService(ApplicationService.class, mock(ApplicationService.class), null);
         bundleContext.registerService(RequestQueue.class, mock(RequestQueue.class), null);
--- a/vm-jmx/client-core/src/test/java/com/redhat/thermostat/vm/jmx/client/core/internal/JmxNotificationsViewControllerTest.java	Mon Oct 05 11:58:01 2015 -0400
+++ b/vm-jmx/client-core/src/test/java/com/redhat/thermostat/vm/jmx/client/core/internal/JmxNotificationsViewControllerTest.java	Thu Oct 08 10:54:59 2015 -0400
@@ -41,6 +41,7 @@
 import static org.mockito.Matchers.anyBoolean;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Matchers.isA;
+import static org.mockito.Mockito.atLeastOnce;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.never;
@@ -53,6 +54,9 @@
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
 
+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 org.mockito.ArgumentCaptor;
@@ -91,13 +95,14 @@
     private Timer timer;
     private TimerFactory timerFactory;
     private VmRef vm;
+    private VmInfo vmInfo;
     private RequestQueue queue;
     private JmxNotificationsViewController controller;
     private HostRef host;
+
     private JmxToggleNotificationRequest toggleReq;
 
     private Runnable successAction;
-
     private Runnable failureAction;
 
     @Before
@@ -117,6 +122,7 @@
         
         AgentInfoDAO agentDao = mock(AgentInfoDAO.class);
         notificationDao = mock(JmxNotificationDAO.class);
+        VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class);
         queue = mock(RequestQueue.class);
         view = mock(JmxNotificationsView.class);
         viewProvider = mock(JmxNotificationsViewProvider.class);
@@ -125,16 +131,21 @@
         timerFactory = mock(TimerFactory.class);
         when(timerFactory.createTimer()).thenReturn(timer);
 
+        vmInfo = mock(VmInfo.class);
+        when(vmInfo.isAlive(any(AgentInformation.class))).thenReturn(VmInfo.AliveStatus.RUNNING);
+        when(vmInfoDAO.getVmInfo(any(VmRef.class))).thenReturn(vmInfo);
+
         host = mock(HostRef.class);
         vm = mock(VmRef.class);
         when(vm.getHostRef()).thenReturn(host);
+        when(host.getAgentId()).thenReturn("123");
         
         JmxToggleNotificationRequestFactory reqFactory = mock(JmxToggleNotificationRequestFactory.class);
         toggleReq = mock(JmxToggleNotificationRequest.class);
         when(reqFactory.createRequest(eq(queue), eq(agentDao), any(Runnable.class), 
                 any(Runnable.class))).thenReturn(toggleReq);
 
-        controller = new JmxNotificationsViewController(appSvc, agentDao, notificationDao, timerFactory, 
+        controller = new JmxNotificationsViewController(appSvc, agentDao, vmInfoDAO, notificationDao, timerFactory,
                 queue, viewProvider, vm, reqFactory);
         ArgumentCaptor<Runnable> successCaptor = ArgumentCaptor.forClass(Runnable.class);
         ArgumentCaptor<Runnable> failureCaptor = ArgumentCaptor.forClass(Runnable.class);
@@ -252,7 +263,7 @@
         
         ArgumentCaptor<LocalizedString> warningCaptor = ArgumentCaptor.forClass(LocalizedString.class);
         verify(view).displayWarning(warningCaptor.capture());
-        assertEquals(translator.localize(LocaleResources.NOTIFICATIONS_CANNOT_ENABLE).getContents(), 
+        assertEquals(translator.localize(LocaleResources.NOTIFICATIONS_CANNOT_ENABLE).getContents(),
                 warningCaptor.getValue().getContents());
     }
     
@@ -272,6 +283,28 @@
         verify(view).setNotificationsEnabled(false);
     }
 
+    @Test
+    public void verifyControlsEnabled() {
+        ArgumentCaptor<ActionListener> listenerCaptor = ArgumentCaptor.forClass(ActionListener.class);
+        verify(view).addActionListener(listenerCaptor.capture());
+
+        listenerCaptor.getValue().actionPerformed(new ActionEvent<>(view, Action.VISIBLE));
+
+        verify(view, atLeastOnce()).setViewControlsEnabled(true);
+    }
+    
+    @Test
+    public void verifyControlsDisabledWhenVmDead() {
+        when(vmInfo.isAlive(any(AgentInformation.class))).thenReturn(VmInfo.AliveStatus.EXITED);
+
+        ArgumentCaptor<ActionListener> listenerCaptor = ArgumentCaptor.forClass(ActionListener.class);
+        verify(view).addActionListener(listenerCaptor.capture());
+
+        listenerCaptor.getValue().actionPerformed(new ActionEvent<>(view, Action.VISIBLE));
+
+        verify(view, atLeastOnce()).setViewControlsEnabled(false);
+    }
+
     private void answerSuccess(boolean enable) {
         doAnswer(new Answer<Void>() {
 
--- a/vm-jmx/client-core/src/test/java/com/redhat/thermostat/vm/jmx/client/core/internal/JmxNotificationsViewServiceImplTest.java	Mon Oct 05 11:58:01 2015 -0400
+++ b/vm-jmx/client-core/src/test/java/com/redhat/thermostat/vm/jmx/client/core/internal/JmxNotificationsViewServiceImplTest.java	Thu Oct 08 10:54:59 2015 -0400
@@ -38,9 +38,15 @@
 
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import com.redhat.thermostat.storage.core.HostRef;
+import com.redhat.thermostat.storage.core.VmId;
+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;
 
@@ -58,6 +64,7 @@
 public class JmxNotificationsViewServiceImplTest {
 
     private AgentInfoDAO agentDao;
+    private VmInfoDAO vmInfoDao;
     private JmxNotificationDAO notificationDao;
     private Timer timer;
     private TimerFactory timerFactory;
@@ -70,6 +77,7 @@
     public void setUp() {
         ApplicationService appSvc = mock(ApplicationService.class);
         agentDao = mock(AgentInfoDAO.class);
+        vmInfoDao = mock(VmInfoDAO.class);
         notificationDao = mock(JmxNotificationDAO.class);
         timer = mock(Timer.class);
         timerFactory = mock(TimerFactory.class);
@@ -81,7 +89,7 @@
         viewProvider = mock(JmxNotificationsViewProvider.class);
         when(viewProvider.createView()).thenReturn(view);
 
-        service = new JmxNotificationsViewServiceImpl(appSvc, agentDao, notificationDao, 
+        service = new JmxNotificationsViewServiceImpl(appSvc, agentDao, vmInfoDao, notificationDao,
                 queue, timerFactory, viewProvider);
     }
 
@@ -93,6 +101,12 @@
     @Test
     public void verifyGetInformationServiceController() {
         VmRef vm = mock(VmRef.class);
+        HostRef hostRef = mock(HostRef.class);
+        VmInfo vmInfo = mock(VmInfo.class);
+        when(hostRef.getAgentId()).thenReturn("agentId");
+        when(vm.getHostRef()).thenReturn(hostRef);
+        when(vmInfoDao.getVmInfo(any(VmRef.class))).thenReturn(vmInfo);
+        when(vmInfo.isAlive(any(AgentInformation.class))).thenReturn(VmInfo.AliveStatus.RUNNING);
         assertNotNull(service.getInformationServiceController(vm));
     }
 
--- a/vm-jmx/client-swing/src/main/java/com/redhat/thermostat/vm/jmx/client/swing/internal/JmxNotificationsSwingView.java	Mon Oct 05 11:58:01 2015 -0400
+++ b/vm-jmx/client-swing/src/main/java/com/redhat/thermostat/vm/jmx/client/swing/internal/JmxNotificationsSwingView.java	Thu Oct 08 10:54:59 2015 -0400
@@ -206,6 +206,16 @@
     }
 
     @Override
+    public void setViewControlsEnabled(final boolean enabled) {
+        SwingUtilities.invokeLater(new Runnable() {
+            @Override
+            public void run() {
+                toolbarButton.setEnabled(enabled);
+            }
+        });
+    }
+
+    @Override
     public void setNotificationsEnabled(final boolean enabled) {
         SwingUtilities.invokeLater(new Runnable() {
             @Override