Mercurial > hg > thermostat
changeset 2321:ae02dcfdaab6
Refactor BytemanRequestResponseListener.
Reviewed-by: neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-May/018936.html
line wrap: on
line diff
--- a/vm-byteman/client-cli/src/main/java/com/redhat/thermostat/vm/byteman/client/cli/internal/BytemanControlCommand.java Mon May 30 11:22:19 2016 -0400 +++ b/vm-byteman/client-cli/src/main/java/com/redhat/thermostat/vm/byteman/client/cli/internal/BytemanControlCommand.java Mon May 30 18:46:07 2016 +0200 @@ -68,6 +68,7 @@ import com.redhat.thermostat.vm.byteman.common.VmBytemanDAO; import com.redhat.thermostat.vm.byteman.common.VmBytemanStatus; import com.redhat.thermostat.vm.byteman.common.command.BytemanRequest; +import com.redhat.thermostat.vm.byteman.common.command.BytemanRequestResponseListener; import com.redhat.thermostat.vm.byteman.common.command.BytemanRequest.RequestAction; public class BytemanControlCommand extends AbstractCommand { @@ -206,9 +207,22 @@ private void submitRequest(CommandContext ctx, RequestQueue requestQueue, Request request) { CountDownLatch latch = new CountDownLatch(1); - request.addListener(new BytemanRequestResponseListener(latch, ctx)); + BytemanRequestResponseListener listener = new BytemanRequestResponseListener(latch); + request.addListener(listener); requestQueue.putRequest(request); waitWithTimeout(latch); + printResponse(listener, ctx); + } + + private void printResponse(BytemanRequestResponseListener listener, CommandContext ctx) { + if (listener.isError()) { + PrintStream err = ctx.getConsole().getError(); + err.println(listener.getErrorMessage()); + } else { + PrintStream out = ctx.getConsole().getOutput(); + out.println(translator.localize(LocaleResources.REQUEST_SUCCESS) + .getContents()); + } } // package-private for testing
--- a/vm-byteman/client-cli/src/main/java/com/redhat/thermostat/vm/byteman/client/cli/internal/BytemanRequestResponseListener.java Mon May 30 11:22:19 2016 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +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.vm.byteman.client.cli.internal; - -import java.io.PrintStream; -import java.util.concurrent.CountDownLatch; - -import com.redhat.thermostat.common.cli.CommandContext; -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.shared.locale.Translate; - -class BytemanRequestResponseListener implements RequestResponseListener { - - private static final Translate<LocaleResources> t = LocaleResources.createLocalizer(); - private final CountDownLatch latch; - private final CommandContext ctx; - - BytemanRequestResponseListener(CountDownLatch latch, CommandContext ctx) { - this.latch = latch; - this.ctx = ctx; - } - - @Override - public void fireComplete(Request request, Response response) { - boolean isError = false; - String errorMsg = ""; - switch(response.getType()) { - case AUTH_FAILED: - isError = true; - errorMsg = t.localize(LocaleResources.REQUEST_FAILED_AUTH_ISSUE).getContents(); - break; - case ERROR: - isError = true; - errorMsg = t.localize(LocaleResources.REQUEST_FAILED_UNKNOWN_ISSUE).getContents(); - break; - case OK: - break; - default: - isError = true; - errorMsg = t.localize(LocaleResources.ERROR_UNKNOWN_RESPONSE, response.getType().toString()).getContents(); - } - latch.countDown(); - if (isError) { - PrintStream err = ctx.getConsole().getError(); - err.println(errorMsg); - } else { - PrintStream out = ctx.getConsole().getOutput(); - out.println(t.localize(LocaleResources.REQUEST_SUCCESS).getContents()); - } - } - -}
--- a/vm-byteman/client-cli/src/main/java/com/redhat/thermostat/vm/byteman/client/cli/internal/LocaleResources.java Mon May 30 11:22:19 2016 -0400 +++ b/vm-byteman/client-cli/src/main/java/com/redhat/thermostat/vm/byteman/client/cli/internal/LocaleResources.java Mon May 30 18:46:07 2016 +0200 @@ -54,10 +54,7 @@ NO_METRICS_AVAILABLE, ERROR_NO_STATUS, BYTEMAN_STATUS_MSG, - REQUEST_FAILED_AUTH_ISSUE, - REQUEST_FAILED_UNKNOWN_ISSUE, REQUEST_SUCCESS, - ERROR_UNKNOWN_RESPONSE, ; static final String RESOURCE_BUNDLE = LocaleResources.class.getPackage().getName() + ".strings";
--- a/vm-byteman/client-cli/src/main/resources/com/redhat/thermostat/vm/byteman/client/cli/internal/strings.properties Mon May 30 11:22:19 2016 -0400 +++ b/vm-byteman/client-cli/src/main/resources/com/redhat/thermostat/vm/byteman/client/cli/internal/strings.properties Mon May 30 18:46:07 2016 +0200 @@ -17,7 +17,4 @@ --------------- \n\ {2} \n\ --------------- -REQUEST_FAILED_AUTH_ISSUE = Request failed due to authentication/authorization issues. -REQUEST_FAILED_UNKNOWN_ISSUE = Request failed for some unknown reason. REQUEST_SUCCESS = Request submitted successfully. -ERROR_UNKNOWN_RESPONSE = Unknown response: {0}
--- a/vm-byteman/client-cli/src/test/java/com/redhat/thermostat/vm/byteman/client/cli/internal/BytemanControlCommandTest.java Mon May 30 11:22:19 2016 -0400 +++ b/vm-byteman/client-cli/src/test/java/com/redhat/thermostat/vm/byteman/client/cli/internal/BytemanControlCommandTest.java Mon May 30 18:46:07 2016 +0200 @@ -80,6 +80,7 @@ import com.redhat.thermostat.vm.byteman.common.VmBytemanDAO; import com.redhat.thermostat.vm.byteman.common.VmBytemanStatus; import com.redhat.thermostat.vm.byteman.common.command.BytemanRequest; +import com.redhat.thermostat.vm.byteman.common.command.BytemanRequestResponseListener; import com.redhat.thermostat.vm.byteman.common.command.BytemanRequest.RequestAction; public class BytemanControlCommandTest { @@ -350,6 +351,8 @@ String expectedRule = new String(StreamUtils.readAll(new FileInputStream(new File(file)))); assertEquals(expectedRule, submittedRequest.getParameter(BytemanRequest.RULE_PARAM_NAME)); assertSame(REQUEST_QUEUE_ADDRESS, submittedRequest.getTarget()); + String out = ctxFactory.getOutput(); + assertEquals("Request submitted successfully.\n", out); } @SuppressWarnings("unchecked")
--- a/vm-byteman/client-cli/src/test/java/com/redhat/thermostat/vm/byteman/client/cli/internal/BytemanRequestResponseListenerTest.java Mon May 30 11:22:19 2016 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,130 +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.vm.byteman.client.cli.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; -import java.util.concurrent.CountDownLatch; - -import org.junit.Before; -import org.junit.Test; - -import com.redhat.thermostat.common.cli.CommandContext; -import com.redhat.thermostat.common.cli.Console; -import com.redhat.thermostat.common.command.Request; -import com.redhat.thermostat.common.command.Response; -import com.redhat.thermostat.common.command.Response.ResponseType; - -public class BytemanRequestResponseListenerTest { - - private static final String EMPTY_STRING = ""; - private BytemanRequestResponseListener listener; - private ByteArrayOutputStream bout; - private ByteArrayOutputStream berr; - private CountDownLatch latch; - - @Before - public void setup() { - bout = new ByteArrayOutputStream(); - berr = new ByteArrayOutputStream(); - CommandContext ctx = mock(CommandContext.class); - Console console = mock(Console.class); - when(ctx.getConsole()).thenReturn(console); - PrintStream outStream = new PrintStream(bout); - PrintStream errStream = new PrintStream(berr); - when(console.getError()).thenReturn(errStream); - when(console.getOutput()).thenReturn(outStream); - latch = mock(CountDownLatch.class); - listener = new BytemanRequestResponseListener(latch, ctx); - } - - @Test - public void testAuthIssue() { - listener.fireComplete(mock(Request.class), new Response(ResponseType.AUTH_FAILED)); - verify(latch).countDown(); - String stdOut = getOutAsString(); - String errOut = getErrAsString(); - assertEquals(EMPTY_STRING, stdOut); - assertTrue(errOut.contains("authentication")); - assertTrue(errOut.contains("issue")); - } - - private String getOutAsString() { - return new String(bout.toByteArray()); - } - - private String getErrAsString() { - return new String(berr.toByteArray()); - } - - @Test - public void testSuccess() { - listener.fireComplete(mock(Request.class), new Response(ResponseType.OK)); - verify(latch).countDown(); - String stdOut = getOutAsString(); - String errOut = getErrAsString(); - assertEquals("Request submitted successfully.\n", stdOut); - assertEquals(EMPTY_STRING, errOut); - } - - @Test - public void testUnknownError() { - listener.fireComplete(mock(Request.class), new Response(ResponseType.ERROR)); - verify(latch).countDown(); - String stdOut = getOutAsString(); - String errOut = getErrAsString(); - assertEquals(EMPTY_STRING, stdOut); - assertTrue(errOut.contains("unknown")); - assertTrue(errOut.contains("reason")); - } - - @Test - public void testUnknownType() { - listener.fireComplete(mock(Request.class), new Response(ResponseType.NOK)); - verify(latch).countDown(); - String stdOut = getOutAsString(); - String errOut = getErrAsString(); - assertEquals(EMPTY_STRING, stdOut); - assertEquals("Unknown response: NOK\n", errOut); - } -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm-byteman/common/src/main/java/com/redhat/thermostat/vm/byteman/common/command/BytemanRequestResponseListener.java Mon May 30 18:46:07 2016 +0200 @@ -0,0 +1,86 @@ +/* + * 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.vm.byteman.common.command; + +import java.util.concurrent.CountDownLatch; + +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.shared.locale.Translate; +import com.redhat.thermostat.vm.byteman.common.internal.LocaleResources; + +public class BytemanRequestResponseListener implements RequestResponseListener { + + private static final Translate<LocaleResources> t = LocaleResources.createLocalizer(); + private final CountDownLatch latch; + private String errorMsg = ""; + private boolean isError = false; + + public BytemanRequestResponseListener(CountDownLatch latch) { + this.latch = latch; + } + + @Override + public void fireComplete(Request request, Response response) { + switch(response.getType()) { + case AUTH_FAILED: + isError = true; + errorMsg = t.localize(LocaleResources.REQUEST_FAILED_AUTH_ISSUE).getContents(); + break; + case ERROR: + isError = true; + errorMsg = t.localize(LocaleResources.REQUEST_FAILED_UNKNOWN_ISSUE).getContents(); + break; + case OK: + break; + default: + isError = true; + errorMsg = t.localize(LocaleResources.ERROR_UNKNOWN_RESPONSE, response.getType().toString()).getContents(); + } + latch.countDown(); + } + + public String getErrorMessage() { + return errorMsg; + } + + public boolean isError() { + return isError; + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm-byteman/common/src/main/java/com/redhat/thermostat/vm/byteman/common/internal/LocaleResources.java Mon May 30 18:46:07 2016 +0200 @@ -0,0 +1,53 @@ +/* + * 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.vm.byteman.common.internal; + +import com.redhat.thermostat.shared.locale.Translate; + +public enum LocaleResources { + + REQUEST_FAILED_AUTH_ISSUE, + REQUEST_FAILED_UNKNOWN_ISSUE, + ERROR_UNKNOWN_RESPONSE, + ; + + static final String RESOURCE_BUNDLE = LocaleResources.class.getPackage().getName() + ".strings"; + + public static Translate<LocaleResources> createLocalizer() { + return new Translate<>(RESOURCE_BUNDLE, LocaleResources.class); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm-byteman/common/src/main/resources/com/redhat/thermostat/vm/byteman/common/internal/strings.properties Mon May 30 18:46:07 2016 +0200 @@ -0,0 +1,3 @@ +REQUEST_FAILED_AUTH_ISSUE = Request failed due to authentication/authorization issues. +REQUEST_FAILED_UNKNOWN_ISSUE = Request failed for some unknown reason. +ERROR_UNKNOWN_RESPONSE = Unknown response: {0}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm-byteman/common/src/test/java/com/redhat/thermostat/vm/byteman/common/command/BytemanRequestResponseListenerTest.java Mon May 30 18:46:07 2016 +0200 @@ -0,0 +1,101 @@ +/* + * 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.vm.byteman.common.command; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.util.concurrent.CountDownLatch; + +import org.junit.Before; +import org.junit.Test; + +import com.redhat.thermostat.common.command.Request; +import com.redhat.thermostat.common.command.Response; +import com.redhat.thermostat.common.command.Response.ResponseType; +import com.redhat.thermostat.vm.byteman.common.command.BytemanRequestResponseListener; + +public class BytemanRequestResponseListenerTest { + + private BytemanRequestResponseListener listener; + private CountDownLatch latch; + + @Before + public void setup() { + latch = mock(CountDownLatch.class); + listener = new BytemanRequestResponseListener(latch); + } + + @Test + public void testAuthIssue() { + listener.fireComplete(mock(Request.class), new Response(ResponseType.AUTH_FAILED)); + verify(latch).countDown(); + assertTrue(listener.isError()); + String errOut = listener.getErrorMessage(); + assertTrue(errOut.contains("authentication")); + assertTrue(errOut.contains("issue")); + } + + @Test + public void testSuccess() { + listener.fireComplete(mock(Request.class), new Response(ResponseType.OK)); + verify(latch).countDown(); + assertFalse(listener.isError()); + } + + @Test + public void testUnknownError() { + listener.fireComplete(mock(Request.class), new Response(ResponseType.ERROR)); + verify(latch).countDown(); + assertTrue(listener.isError()); + String errOut = listener.getErrorMessage(); + assertTrue(errOut.contains("unknown")); + assertTrue(errOut.contains("reason")); + } + + @Test + public void testUnknownType() { + listener.fireComplete(mock(Request.class), new Response(ResponseType.NOK)); + verify(latch).countDown(); + assertTrue(listener.isError()); + String errOut = listener.getErrorMessage(); + assertEquals("Unknown response: NOK", errOut); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vm-byteman/common/src/test/java/com/redhat/thermostat/vm/byteman/common/internal/LocaleResourcesTest.java Mon May 30 18:46:07 2016 +0200 @@ -0,0 +1,53 @@ +/* + * 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.vm.byteman.common.internal; + +import com.redhat.thermostat.testutils.AbstractLocaleResourcesTest; + +public class LocaleResourcesTest extends AbstractLocaleResourcesTest<LocaleResources> { + + @Override + protected Class<LocaleResources> getEnumClass() { + return LocaleResources.class; + } + + @Override + protected String getResourceBundle() { + return LocaleResources.RESOURCE_BUNDLE; + } + +}