more solid broker path analysis and more tests

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1068 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Stefano Mazzocchi 2010-07-02 09:30:53 +00:00
parent 689d743d0b
commit 31e4f1dff9
3 changed files with 70 additions and 23 deletions

View File

@ -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());

View File

@ -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<String,String> params) throws Exception {
if (params != null) {
for (Entry<String,String> e : params.entrySet()) {
when(request.getParameter(e.getKey())).thenReturn(e.getValue());
}
}
broker.process(service, request, response);
if (params != null) {
for (Entry<String,String> e : params.entrySet()) {
verify(request,times(1)).getParameter(e.getKey());
}
}
}
}

View File

@ -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