Mercurial > hg > thermostat-ng > agent
changeset 2444:386c025be97a
Refactor kill-vm listeners, handle all responses, localize messages and improve tests.
Reviewed-by: neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-September/020810.html
line wrap: on
line diff
--- a/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/SwingVMKilledListener.java Tue Sep 06 11:58:43 2016 -0400 +++ b/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/SwingVMKilledListener.java Thu Sep 08 09:14:29 2016 -0400 @@ -36,48 +36,18 @@ package com.redhat.thermostat.killvm.client.internal; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; -import com.redhat.thermostat.common.command.Request; -import com.redhat.thermostat.common.command.RequestResponseListener; -import com.redhat.thermostat.common.command.Response; -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.shared.locale.Translate; - -public class SwingVMKilledListener implements RequestResponseListener { +import com.redhat.thermostat.killvm.common.VMKilledListener; - private final Translate<LocaleResources> t; - private static final Logger logger = LoggingUtils - .getLogger(SwingVMKilledListener.class); +public class SwingVMKilledListener extends VMKilledListener { - public SwingVMKilledListener() { - this.t = LocaleResources.createLocalizer(); - } - + @Override - public void fireComplete(Request request, Response response) { - switch (response.getType()) { - case ERROR: - String pid = request.getParameter("vm-pid"); - logger.log(Level.SEVERE, - "Kill request error for VM ID " - + pid); - showErrorMessage(t.localize(LocaleResources.KILL_ACTION_ERROR_RESPONSE_MSG, pid).getContents()); - break; - case OK: - logger.log(Level.INFO, - "VM with id " + request.getParameter("vm-pid") - + " killed on host " - + request.getTarget().toString()); - break; - default: - logger.log(Level.WARNING, "Unknown result from KILL VM command."); - break; - } + protected void onError(String message) { + showErrorMessage(message); } /*
--- a/killvm/command/src/main/java/com/redhat/thermostat/killvm/command/internal/ShellVMKilledListener.java Tue Sep 06 11:58:43 2016 -0400 +++ b/killvm/command/src/main/java/com/redhat/thermostat/killvm/command/internal/ShellVMKilledListener.java Thu Sep 08 09:14:29 2016 -0400 @@ -39,61 +39,58 @@ import java.io.PrintStream; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import java.util.logging.Level; -import java.util.logging.Logger; -import com.redhat.thermostat.common.command.Request; -import com.redhat.thermostat.common.command.RequestResponseListener; -import com.redhat.thermostat.common.command.Response; -import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.killvm.common.VMKilledListener; -public class ShellVMKilledListener implements RequestResponseListener { - - private static final Logger logger = LoggingUtils - .getLogger(ShellVMKilledListener.class); +public class ShellVMKilledListener extends VMKilledListener { private PrintStream out; private PrintStream err; private CountDownLatch latch = new CountDownLatch(1); - @Override - public void fireComplete(Request request, Response response) { - String message; - switch (response.getType()) { - case ERROR: - String pid = request.getParameter("vm-pid"); - message = "Kill request error for VM ID " + pid; - printMessage(err, Level.SEVERE, message); - break; - case OK: - message = "VM with id " + request.getParameter("vm-pid") + " killed."; - printMessage(out, Level.INFO, message); - break; - case NOK: - case NOOP: - message = "Request to kill VM was ignored."; - printMessage(out, Level.WARNING, message); - break; - case AUTH_FAILED: - message = "Unauthorized request was ignored."; - printMessage(out, Level.WARNING, message); - break; - default: - message = "Unknown result from KILL VM command."; - printMessage(err, Level.WARNING, message); - break; - } - latch.countDown(); - } - - private void printMessage(PrintStream printStream, Level level, String message) { - logger.log(level, message); + private void printMessage(PrintStream printStream, String message) { if (printStream != null) { printStream.println(message); } } + @Override + protected void onError(String message) { + printMessage(err, message); + } + + @Override + protected void onOk(String message) { + printMessage(out, message); + } + + @Override + protected void onNotOk(String message) { + printMessage(out, message); + } + + @Override + protected void onNoOp(String message) { + printMessage(out, message); + } + + @Override + protected void onAuthFail(String message) { + printMessage(out, message); + } + + @Override + protected void onDefault(String message) { + printMessage(out, message); + } + + @Override + protected void onComplete() { + latch.countDown(); + } + + public void setOut(PrintStream out) { this.out = out; }
--- a/killvm/command/src/test/java/com/redhat/thermostat/killvm/command/internal/ShellVMKilledListenerTest.java Tue Sep 06 11:58:43 2016 -0400 +++ b/killvm/command/src/test/java/com/redhat/thermostat/killvm/command/internal/ShellVMKilledListenerTest.java Thu Sep 08 09:14:29 2016 -0400 @@ -38,14 +38,17 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; +import java.io.PrintStream; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.LogRecord; import java.util.logging.Logger; +import org.junit.Before; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -53,34 +56,33 @@ import com.redhat.thermostat.common.command.Request; import com.redhat.thermostat.common.command.Response; import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.killvm.common.VMKilledListener; public class ShellVMKilledListenerTest { private final ShellVMKilledListener listener = new ShellVMKilledListener(); - private static final Logger logger = LoggingUtils - .getLogger(ShellVMKilledListener.class); + private final PrintStream printStream = mock(PrintStream.class); + private final Request request = mock(Request.class); + @Before + public void setup() { + listener.setErr(printStream); + listener.setOut(printStream); + } @Test - public void testSuccessfulKillResponse() { - Request request = mock(Request.class); - + public void testOkayResponse() { Response resp = new Response(Response.ResponseType.OK); - Handler handler = mock(Handler.class); - logger.addHandler(handler); + final boolean[] complete = {false}; - final boolean[] complete = {false}; doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - LogRecord log = (LogRecord)invocation.getArguments()[0]; - if (log.getLevel().equals(Level.INFO)) { - complete[0] = true; - } + complete[0] = true; return null; } - }).when(handler).publish(any(LogRecord.class)); + }).when(printStream).println(new String("VM with id null killed successfully.")); listener.fireComplete(request, resp); @@ -89,23 +91,36 @@ @Test public void testErrorResponse() { - Request request = mock(Request.class); Response resp = new Response(Response.ResponseType.ERROR); - Handler handler = mock(Handler.class); - logger.addHandler(handler); + final boolean[] complete = {false}; - final boolean[] complete = {false}; doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - LogRecord log = (LogRecord)invocation.getArguments()[0]; - if (log.getLevel().equals(Level.SEVERE)) { - complete[0] = true; - } + complete[0] = true; return null; } - }).when(handler).publish(any(LogRecord.class)); + }).when(printStream).println(new String("Kill request error for VM ID null")); + + listener.fireComplete(request, resp); + + assertTrue(complete[0]); + } + + @Test + public void testNotOkayResponse() { + Response resp = new Response(Response.ResponseType.NOK); + + final boolean[] complete = {false}; + + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + complete[0] = true; + return null; + } + }).when(printStream).println(new String("Kill request acknowledged and refused.")); listener.fireComplete(request, resp); @@ -113,28 +128,41 @@ } @Test - public void testDefaultResponse() { - Request request = mock(Request.class); - Response resp = new Response(Response.ResponseType.NOK); - - Handler handler = mock(Handler.class); - logger.addHandler(handler); + public void testNoOpResponse() { + Response resp = new Response(Response.ResponseType.NOOP); final boolean[] complete = {false}; + doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { - LogRecord log = (LogRecord)invocation.getArguments()[0]; - if (log.getLevel().equals(Level.WARNING)) { - complete[0] = true; - } + complete[0] = true; return null; } - }).when(handler).publish(any(LogRecord.class)); + }).when(printStream).println(new String("Kill request acknowledged and no action taken.")); listener.fireComplete(request, resp); assertTrue(complete[0]); } + @Test + public void testAuthFailedResponse() { + + Response resp = new Response(Response.ResponseType.AUTH_FAILED); + + final boolean[] complete = {false}; + + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + complete[0] = true; + return null; + } + }).when(printStream).println(new String("Unauthorized kill request ignored.")); + + listener.fireComplete(request, resp); + + assertTrue(complete[0]); + } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/killvm/common/src/main/java/com/redhat/thermostat/killvm/common/VMKilledListener.java Thu Sep 08 09:14:29 2016 -0400 @@ -0,0 +1,117 @@ +/* + * 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.killvm.common; + + +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.redhat.thermostat.common.command.Request; +import com.redhat.thermostat.common.command.RequestResponseListener; +import com.redhat.thermostat.common.command.Response; +import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.killvm.common.internal.LocaleResources; +import com.redhat.thermostat.shared.locale.Translate; + +public class VMKilledListener implements RequestResponseListener { + + private static final Translate<LocaleResources> translator = LocaleResources.createLocalizer(); + + private static final Logger logger = LoggingUtils + .getLogger(VMKilledListener.class); + + @Override + public void fireComplete(Request request, Response response) { + String pid = request.getParameter("vm-pid"); + String message; + switch (response.getType()) { + case ERROR: + message = translator.localize(LocaleResources.ERROR_RESPONSE, pid).getContents(); + logger.log(Level.SEVERE, message); + onError(message); + break; + case OK: + message = translator.localize(LocaleResources.OK_RESPONSE, pid).getContents(); + logger.log(Level.INFO, message); + onOk(message); + break; + case NOK: + message = translator.localize(LocaleResources.NOK_RESPONSE).getContents(); + logger.log(Level.WARNING, message); + onNotOk(message); + break; + case NOOP: + message = translator.localize(LocaleResources.NOOP_RESPONSE).getContents(); + logger.log(Level.WARNING, message); + onNoOp(message); + break; + case AUTH_FAILED: + message = translator.localize(LocaleResources.AUTH_FAILED_RESPONSE).getContents(); + logger.log(Level.WARNING, message); + onAuthFail(message); + break; + default: + message = translator.localize(LocaleResources.DEFAULT_RESPONSE).getContents(); + logger.log(Level.WARNING, message); + onDefault(message); + break; + } + + onComplete(); + } + + protected void onError(String message) { + } + + protected void onOk(String message) { + } + + protected void onNotOk(String message) { + } + + protected void onNoOp(String message) { + } + + protected void onAuthFail(String message) { + } + + protected void onDefault(String message) { + } + + protected void onComplete() { + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/killvm/common/src/main/java/com/redhat/thermostat/killvm/common/internal/LocaleResources.java Thu Sep 08 09:14:29 2016 -0400 @@ -0,0 +1,57 @@ +/* + * 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.killvm.common.internal; + +import com.redhat.thermostat.shared.locale.Translate; + +public enum LocaleResources { + + ERROR_RESPONSE, + OK_RESPONSE, + NOK_RESPONSE, + NOOP_RESPONSE, + AUTH_FAILED_RESPONSE, + DEFAULT_RESPONSE + ; + + public static final String RESOURCE_BUNDLE = + "com.redhat.thermostat.killvm.common.locale.strings"; + + public static Translate<LocaleResources> createLocalizer() { + return new Translate<>(RESOURCE_BUNDLE, LocaleResources.class); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/killvm/common/src/main/resources/com/redhat/thermostat/killvm/common/locale/strings.properties Thu Sep 08 09:14:29 2016 -0400 @@ -0,0 +1,6 @@ +ERROR_RESPONSE=Kill request error for VM ID {0} +OK_RESPONSE=VM with id {0} killed successfully. +NOK_RESPONSE=Kill request acknowledged and refused. +NOOP_RESPONSE=Kill request acknowledged and no action taken. +AUTH_FAILED_RESPONSE=Unauthorized kill request ignored. +DEFAULT_RESPONSE=Unknown result from kill request. \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/killvm/common/src/test/java/com/redhat/thermostat/killvm/common/VMKilledListenerTest.java Thu Sep 08 09:14:29 2016 -0400 @@ -0,0 +1,175 @@ +/* + * 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.killvm.common; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; + +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + +import com.redhat.thermostat.common.command.Request; +import com.redhat.thermostat.common.command.Response; +import com.redhat.thermostat.common.utils.LoggingUtils; + +public class VMKilledListenerTest { + + private final VMKilledListener listener = new VMKilledListener(); + private final Request request = mock(Request.class); + private static final Handler handler = mock(Handler.class); + private static final Logger logger = LoggingUtils + .getLogger(VMKilledListener.class); + + @BeforeClass + public static void setupClass() { + logger.addHandler(handler); + } + + @Test + public void testSuccessfulKillResponse() { + Response resp = new Response(Response.ResponseType.OK); + + final boolean[] complete = {false}; + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + LogRecord log = (LogRecord)invocation.getArguments()[0]; + if (log.getLevel().equals(Level.INFO)) { + complete[0] = true; + } + return null; + } + }).when(handler).publish(any(LogRecord.class)); + + listener.fireComplete(request, resp); + + assertTrue(complete[0]); + } + + @Test + public void testErrorResponse() { + Response resp = new Response(Response.ResponseType.ERROR); + + final boolean[] complete = {false}; + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + LogRecord log = (LogRecord)invocation.getArguments()[0]; + if (log.getLevel().equals(Level.SEVERE)) { + complete[0] = true; + } + return null; + } + }).when(handler).publish(any(LogRecord.class)); + + listener.fireComplete(request, resp); + + assertTrue(complete[0]); + } + + @Test + public void testNotOkayResponse() { + Response resp = new Response(Response.ResponseType.NOK); + + final boolean[] complete = {false}; + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + LogRecord log = (LogRecord)invocation.getArguments()[0]; + if (log.getLevel().equals(Level.WARNING)) { + complete[0] = true; + } + return null; + } + }).when(handler).publish(any(LogRecord.class)); + + listener.fireComplete(request, resp); + + assertTrue(complete[0]); + } + + @Test + public void testNoOpResfonse() { + Response resp = new Response(Response.ResponseType.NOOP); + + final boolean[] complete = {false}; + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + LogRecord log = (LogRecord)invocation.getArguments()[0]; + if (log.getLevel().equals(Level.WARNING)) { + complete[0] = true; + } + return null; + } + }).when(handler).publish(any(LogRecord.class)); + + listener.fireComplete(request, resp); + + assertTrue(complete[0]); + } + + @Test + public void testAuthFailedResponse() { + Response resp = new Response(Response.ResponseType.AUTH_FAILED); + + final boolean[] complete = {false}; + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) throws Throwable { + LogRecord log = (LogRecord)invocation.getArguments()[0]; + if (log.getLevel().equals(Level.WARNING)) { + complete[0] = true; + } + return null; + } + }).when(handler).publish(any(LogRecord.class)); + + listener.fireComplete(request, resp); + + assertTrue(complete[0]); + } +}