Skip to content

Commit

Permalink
temporary fix for NullPointerException in SymmetricFilterTest
Browse files Browse the repository at this point in the history
  • Loading branch information
knaas committed Mar 18, 2008
1 parent 755a066 commit c27b809
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 109 deletions.
2 changes: 2 additions & 0 deletions symmetric/src/main/resources/symmetric-web.xml
Expand Up @@ -6,6 +6,8 @@
<bean id="authenticationFilter"
class="org.jumpmind.symmetric.web.AuthenticationFilter">
<property name="regexPattern" value="^/(ack|pull|push)(/*)|$" />
<property name="nodeService" ref="nodeService"/>
<property name="registrationService" ref="registrationService"/>
</bean>

<bean id="nodeConcurrencyFilter"
Expand Down
@@ -0,0 +1,18 @@
/**
* Copyright (C) 2005 Big Lots Inc.
*/

package org.jumpmind.symmetric.web;

public class MockNodeService extends org.jumpmind.symmetric.service.mock.MockNodeService
{
static final String GOOD_SECURITY_TOKEN = "1";

static final String GOOD_NODE_ID = "1";

@Override
public boolean isNodeAuthorized(String nodeId, String password)
{
return GOOD_NODE_ID.equals(nodeId) && GOOD_SECURITY_TOKEN.equals(password);
}
}
@@ -0,0 +1,13 @@
/**
* Copyright (C) 2005 Big Lots Inc.
*/

package org.jumpmind.symmetric.web;

public class MockRegistrationService extends org.jumpmind.symmetric.service.mock.MockRegistrationService
{
public boolean isAutoRegistration()
{
return true;
}
}
Expand Up @@ -27,9 +27,6 @@
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.jumpmind.symmetric.common.Constants;
import org.jumpmind.symmetric.service.mock.MockNodeService;
import org.jumpmind.symmetric.service.mock.MockRegistrationService;
import org.springframework.mock.web.MockFilterChain;
import org.springframework.mock.web.MockFilterConfig;
import org.springframework.mock.web.MockHttpServletRequest;
Expand All @@ -43,171 +40,120 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class SymmetricFilterTest {
public class SymmetricFilterTest
{

private static final String GOOD_SECURITY_TOKEN = "1";
private static final String GOOD_NODE_ID = "1";
private static final String BAD_SECURITY_TOKEN = "2";

private static final String BAD_NODE_ID = "2";

protected ConfigurableWebApplicationContext applicationContext;

protected ServletContext servletContext;

@BeforeMethod(alwaysRun = true)
protected void springTestContextBeforeTestMethod(Method method)
throws Exception {
protected void springTestContextBeforeTestMethod(Method method) throws Exception
{
servletContext = new MockServletContext();
applicationContext = new XmlWebApplicationContext();
applicationContext.setServletContext(servletContext);
applicationContext.setConfigLocations(new String[] {
"/symmetric-properties.xml", "/symmetric-web.xml" });
applicationContext.setConfigLocation(SymmetricFilterTest.class.getPackage().getName().replace(".", "/")
+ "/symmetric-web.xml");
applicationContext.refresh();
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
applicationContext);

applicationContext.getBeanFactory().registerSingleton(
Constants.NODE_SERVICE, new MockNodeService() {
@Override
public boolean isNodeAuthorized(String nodeId,
String password) {
return GOOD_NODE_ID.equals(nodeId)
&& GOOD_SECURITY_TOKEN.equals(password);
}
});
applicationContext.getBeanFactory().registerSingleton(
Constants.REGISTRATION_SERVICE, new MockRegistrationService() {
public boolean isAutoRegistration() {
return true;
}
});
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, applicationContext);
}

@DataProvider(name = "authenticationFilterForbiddenParams")
public Object[][] authenticationFilterForbiddenParams() {
public Object[][] authenticationFilterForbiddenParams()
{
final Map<String, String> emptyAuthentication = new HashMap<String, String>();
emptyAuthentication.put(WebConstants.SECURITY_TOKEN, "");
emptyAuthentication.put(WebConstants.NODE_ID, "");

return new Object[][] {
{ "GET", "/ack", null },
{ "GET", "/ack/", null },
{ "GET", "/ack/more", null },
{ "GET", "/ack?name=value", null },
{ "GET", "/ack?name=value&name=value", null },
{
"GET",
String.format("/ack?%s=1&%s=2",
WebConstants.SECURITY_TOKEN,
WebConstants.NODE_ID), null },
{ "GET", "/ack", emptyAuthentication },
{ "PUT", "/ack", null },
{ "POST", "/ack", null },
{ "DELETE", "/ack", null },
{ "TRACE", "/ack", null },
{ "OPTIONS", "/ack", null },
{ "HEAD", "/ack", null },
{ "GET", "/pull", null },
{ "GET", "/pull/", null },
{ "GET", "/pull/more", null },
{ "GET", "/pull?name=value", null },
{ "GET", "/pull?name=value&name=value", null },
{
"GET",
String.format("/pull?%s=1&%s=2",
WebConstants.SECURITY_TOKEN,
WebConstants.NODE_ID), null },
{ "GET", "/pull", emptyAuthentication },
{ "PUT", "/pull", null },
{ "POST", "/pull", null },
{ "DELETE", "/pull", null },
{ "TRACE", "/pull", null },
{ "OPTIONS", "/pull", null },
{ "HEAD", "/pull", null },
{ "GET", "/push", null },
{ "GET", "/push/", null },
{ "GET", "/push/more", null },
{ "GET", "/push?name=value", null },
{ "GET", "/push?name=value&name=value", null },
{
"GET",
String.format("/push?%s=1&%s=2",
WebConstants.SECURITY_TOKEN,
WebConstants.NODE_ID), null },
{ "GET", "/push", emptyAuthentication },
{ "PUT", "/push", null }, { "POST", "/push", null },
{ "DELETE", "/push", null }, { "TRACE", "/push", null },
{ "OPTIONS", "/push", null }, { "HEAD", "/push", null }, };
return new Object[][] { {"GET", "/ack", null}, {"GET", "/ack/", null}, {"GET", "/ack/more", null},
{"GET", "/ack?name=value", null}, {"GET", "/ack?name=value&name=value", null},
{"GET", String.format("/ack?%s=1&%s=2", WebConstants.SECURITY_TOKEN, WebConstants.NODE_ID), null},
{"GET", "/ack", emptyAuthentication}, {"PUT", "/ack", null}, {"POST", "/ack", null},
{"DELETE", "/ack", null}, {"TRACE", "/ack", null}, {"OPTIONS", "/ack", null}, {"HEAD", "/ack", null},
{"GET", "/pull", null}, {"GET", "/pull/", null}, {"GET", "/pull/more", null},
{"GET", "/pull?name=value", null}, {"GET", "/pull?name=value&name=value", null},
{"GET", String.format("/pull?%s=1&%s=2", WebConstants.SECURITY_TOKEN, WebConstants.NODE_ID), null},
{"GET", "/pull", emptyAuthentication}, {"PUT", "/pull", null}, {"POST", "/pull", null},
{"DELETE", "/pull", null}, {"TRACE", "/pull", null}, {"OPTIONS", "/pull", null}, {"HEAD", "/pull", null},
{"GET", "/push", null}, {"GET", "/push/", null}, {"GET", "/push/more", null},
{"GET", "/push?name=value", null}, {"GET", "/push?name=value&name=value", null},
{"GET", String.format("/push?%s=1&%s=2", WebConstants.SECURITY_TOKEN, WebConstants.NODE_ID), null},
{"GET", "/push", emptyAuthentication}, {"PUT", "/push", null}, {"POST", "/push", null},
{"DELETE", "/push", null}, {"TRACE", "/push", null}, {"OPTIONS", "/push", null}, {"HEAD", "/push", null},};
}

@Test(groups = "continuous", dataProvider = "authenticationFilterForbiddenParams")
public void testAuthenticationFilterForbidden(String method, String uri,
Map<String, String> parameters) throws Exception {
public void testAuthenticationFilterForbidden(String method, String uri, Map<String, String> parameters)
throws Exception
{

final SymmetricFilter filter = new SymmetricFilter();
filter.init(new MockFilterConfig(servletContext));
final MockHttpServletRequest request = new MockHttpServletRequest(
servletContext, method, uri);
if (parameters != null) {
final MockHttpServletRequest request = new MockHttpServletRequest(servletContext, method, uri);
if (parameters != null)
{
request.setParameters(parameters);
}
final MockHttpServletResponse response = new MockHttpServletResponse();
filter.doFilter(request, response, new MockFilterChain());
Assert.assertEquals(response.getStatus(),
HttpServletResponse.SC_FORBIDDEN);
Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_FORBIDDEN);
filter.destroy();
}

@DataProvider(name = "authenticationFilterRegistrationRequiredParams")
public Object[][] authenticationFilterRegistrationRequiredParams() {
public Object[][] authenticationFilterRegistrationRequiredParams()
{
final Map<String, String> badAuthentication = new HashMap<String, String>();
badAuthentication.put(WebConstants.SECURITY_TOKEN, BAD_SECURITY_TOKEN);
badAuthentication.put(WebConstants.NODE_ID, BAD_NODE_ID);
return new Object[][] { { "GET", "/ack", badAuthentication },
{ "GET", "/pull", badAuthentication },
{ "GET", "/push", badAuthentication }, };
return new Object[][] { {"GET", "/ack", badAuthentication}, {"GET", "/pull", badAuthentication},
{"GET", "/push", badAuthentication},};
}

@Test(groups = "continuous", dataProvider = "authenticationFilterRegistrationRequiredParams")
public void testAuthenticationFilterRegistrationRequired(String method,
String uri, Map<String, String> parameters) throws Exception {
public void testAuthenticationFilterRegistrationRequired(String method, String uri, Map<String, String> parameters)
throws Exception
{

final SymmetricFilter filter = new SymmetricFilter();
filter.init(new MockFilterConfig(servletContext));

final MockHttpServletRequest request = new MockHttpServletRequest(
servletContext, method, uri);
if (parameters != null) {
final MockHttpServletRequest request = new MockHttpServletRequest(servletContext, method, uri);
if (parameters != null)
{
request.setParameters(parameters);
}
final MockHttpServletResponse response = new MockHttpServletResponse();
filter.doFilter(request, response, new MockFilterChain());
Assert.assertEquals(response.getStatus(),
WebConstants.REGISTRATION_REQUIRED);
Assert.assertEquals(response.getStatus(), WebConstants.REGISTRATION_REQUIRED);
filter.destroy();
}

@DataProvider(name = "authenticationFilterParams")
public Object[][] authenticationFilterParams() {
public Object[][] authenticationFilterParams()
{
final Map<String, String> goodAuthentication = new HashMap<String, String>();
goodAuthentication
.put(WebConstants.SECURITY_TOKEN, GOOD_SECURITY_TOKEN);
goodAuthentication.put(WebConstants.NODE_ID, GOOD_NODE_ID);
return new Object[][] { { "GET", "/ack", goodAuthentication },
{ "GET", "/pull", goodAuthentication },
{ "GET", "/push", goodAuthentication }, };
goodAuthentication.put(WebConstants.SECURITY_TOKEN, MockNodeService.GOOD_SECURITY_TOKEN);
goodAuthentication.put(WebConstants.NODE_ID, MockNodeService.GOOD_NODE_ID);
return new Object[][] { {"GET", "/ack", goodAuthentication}, {"GET", "/pull", goodAuthentication},
{"GET", "/push", goodAuthentication},};
}

@Test(groups = "continuous", dataProvider = "authenticationFilterParams")
public void testAuthenticationFilter(String method, String uri,
Map<String, String> parameters) throws Exception {
public void testAuthenticationFilter(String method, String uri, Map<String, String> parameters) throws Exception
{

final SymmetricFilter filter = new SymmetricFilter();
filter.init(new MockFilterConfig(servletContext));

final MockHttpServletRequest request = new MockHttpServletRequest(
servletContext, method, uri);
if (parameters != null) {
final MockHttpServletRequest request = new MockHttpServletRequest(servletContext, method, uri);
if (parameters != null)
{
request.setParameters(parameters);
}
final MockHttpServletResponse response = new MockHttpServletResponse();
Expand Down
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<import resource="classpath:/symmetric-properties.xml" />
<bean name="nodeService" class="org.jumpmind.symmetric.web.MockNodeService" />
<bean name="registrationService" class="org.jumpmind.symmetric.web.MockRegistrationService" />
<import resource="classpath:/symmetric-web.xml" />
</beans>

0 comments on commit c27b809

Please sign in to comment.