From 31e4f1dff91d22d4979bcffb63bd6c6214fcbac6 Mon Sep 17 00:00:00 2001 From: Stefano Mazzocchi Date: Fri, 2 Jul 2010 09:30:53 +0000 Subject: [PATCH] more solid broker path analysis and more tests git-svn-id: http://google-refine.googlecode.com/svn/trunk@1068 7d457c2a-affb-35e4-300a-418c747d4874 --- .../gridworks/broker/GridworksBroker.java | 39 ++++++++------- .../broker/tests/GridworksBrokerTests.java | 48 +++++++++++++++++-- broker/core/tests/src/tests.log4j.properties | 6 +++ 3 files changed, 70 insertions(+), 23 deletions(-) create mode 100644 broker/core/tests/src/tests.log4j.properties diff --git a/broker/core/src/com/metaweb/gridworks/broker/GridworksBroker.java b/broker/core/src/com/metaweb/gridworks/broker/GridworksBroker.java index 452135f00..1b9562ac3 100644 --- a/broker/core/src/com/metaweb/gridworks/broker/GridworksBroker.java +++ b/broker/core/src/com/metaweb/gridworks/broker/GridworksBroker.java @@ -96,39 +96,42 @@ public abstract class GridworksBroker extends ButterflyModuleImpl { logger.info("process '{}'", path); } - response.setCharacterEncoding("UTF-8"); - response.setHeader("Content-Type", "application/json"); - try { - String uid = getUserId(request); - logger.debug("uid: {}", uid); - String pid = getParameter(request, "pid"); - logger.debug("pid: {}", pid); - - // NOTE: conditionals should be ordered by call frequency estimate to (slightly) improve performance - // we could be using a hashtable and some classes that implement the commands, but the complexity overhead - // doesn't seem to justify the marginal benefit. if ("get_state".equals(path)) { - getState(response, pid, uid, getInteger(request, "rev")); + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + getState(response, getParameter(request, "pid"), getUserId(request), getInteger(request, "rev")); } else if ("expire".equals(path)) { + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); expire(response); } else if ("obtain_lock".equals(path)) { - obtainLock(response, pid, uid, getInteger(request, "locktype"), getParameter(request, "lockvalue")); + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + obtainLock(response, getParameter(request, "pid"), getUserId(request), getInteger(request, "locktype"), getParameter(request, "lockvalue")); } else if ("release_lock".equals(path)) { - releaseLock(response, pid, uid, getParameter(request, "lock")); + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + releaseLock(response, getParameter(request, "pid"), getUserId(request), getParameter(request, "lock")); } else if ("transform".equals(path)) { - addTransformations(response, pid, uid, getParameter(request, "lock"), getList(request, "transformations")); + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + addTransformations(response, getParameter(request, "pid"), getUserId(request), getParameter(request, "lock"), getList(request, "transformations")); } else if ("start".equals(path)) { - startProject(response, pid, uid, getParameter(request, "lock"), getData(request), getParameter(request, "metadata"), getInteger(request, "rev")); + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + startProject(response, getParameter(request, "pid"), getUserId(request), getParameter(request, "lock"), getData(request), getParameter(request, "metadata"), getInteger(request, "rev")); } else if ("open".equals(path)) { - openProject(response, pid); + response.setCharacterEncoding("UTF-8"); + response.setHeader("Content-Type", "application/json"); + openProject(response, getParameter(request, "pid")); } else { boolean value = super.process(path, request, response); if (logger.isDebugEnabled()) logger.debug("< process '{}'", path); return value; } - + } catch (RuntimeException e) { logger.error("runtime error", e.getMessage()); respondError(response, e.getMessage()); diff --git a/broker/core/tests/src/com/metaweb/gridworks/broker/tests/GridworksBrokerTests.java b/broker/core/tests/src/com/metaweb/gridworks/broker/tests/GridworksBrokerTests.java index ea48793c2..37b26c40b 100644 --- a/broker/core/tests/src/com/metaweb/gridworks/broker/tests/GridworksBrokerTests.java +++ b/broker/core/tests/src/com/metaweb/gridworks/broker/tests/GridworksBrokerTests.java @@ -1,9 +1,15 @@ package com.metaweb.gridworks.broker.tests; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.File; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Map; +import java.util.Map.Entry; import javax.servlet.ServletConfig; import javax.servlet.http.HttpServletRequest; @@ -30,7 +36,7 @@ public class GridworksBrokerTests { @BeforeSuite public void suite_init() { - //System.setProperty("log4j.configuration", "tests.log4j.properties"); + System.setProperty("log4j.configuration", "tests.log4j.properties"); data = new File("data"); if (!data.exists()) data.mkdirs(); } @@ -51,25 +57,30 @@ public class GridworksBrokerTests { HttpServletRequest request = null; HttpServletResponse response = null; ServletConfig config = null; + StringWriter writer = null; @BeforeMethod public void setup() throws Exception { + config = mock(ServletConfig.class); request = mock(HttpServletRequest.class); response = mock(HttpServletResponse.class); - config = mock(ServletConfig.class); + writer = new StringWriter(); when(config.getInitParameter("gridworks.data")).thenReturn(data.getAbsolutePath()); + when(response.getWriter()).thenReturn(new PrintWriter(writer)); SUT = new GridworksBrokerImpl(); SUT.init(config); } @AfterMethod - public void teardown() { + public void teardown() throws Exception { + SUT.destroy(); SUT = null; - - request = null; + + writer = null; response = null; + request = null; config = null; } @@ -77,8 +88,35 @@ public class GridworksBrokerTests { @Test public void testLifeCycle() { + logger.info("testing lifecycle"); Assert.assertTrue(true); } + @Test + public void testService() { + try { + call(SUT, request, response, "expire", null); + logger.info(writer.toString()); + } catch (Exception e) { + Assert.fail(); + } + } + + // ------------------------------------------------------------------------------------ + private void call(GridworksBroker broker, HttpServletRequest request, HttpServletResponse response, String service, Map params) throws Exception { + if (params != null) { + for (Entry e : params.entrySet()) { + when(request.getParameter(e.getKey())).thenReturn(e.getValue()); + } + } + + broker.process(service, request, response); + + if (params != null) { + for (Entry e : params.entrySet()) { + verify(request,times(1)).getParameter(e.getKey()); + } + } + } } diff --git a/broker/core/tests/src/tests.log4j.properties b/broker/core/tests/src/tests.log4j.properties new file mode 100644 index 000000000..63c3978ef --- /dev/null +++ b/broker/core/tests/src/tests.log4j.properties @@ -0,0 +1,6 @@ +log4j.rootLogger=DEBUG, console + +log4j.appender.console=org.apache.log4j.ConsoleAppender + +log4j.appender.console.layout=org.apache.log4j.PatternLayout +log4j.appender.console.layout.ConversionPattern=%-4r %-5p [%t] %m%n \ No newline at end of file