Refactored the collection of all ACL in a specialized collector class instead of using plain List

This commit is contained in:
andrea
2015-05-20 23:34:42 +02:00
parent 1aecc3d922
commit 443bf7ab85
8 changed files with 291 additions and 212 deletions
@@ -1,54 +0,0 @@
package org.eclipse.moquette.spi.impl.security;
import org.junit.Test;
import static java.util.Arrays.*;
import static org.eclipse.moquette.spi.impl.security.Authorization.Permission.*;
import static org.junit.Assert.*;
public class ACLAuthorizatorTest {
@Test
public void testCanWriteSimpleTopic() {
ACLAuthorizator authorizator = new ACLAuthorizator(asList(new Authorization("/sensors", WRITE)));
//verify
assertTrue(authorizator.canWrite("/sensors"));
}
@Test
public void testCanReadSimpleTopic() {
ACLAuthorizator authorizator = new ACLAuthorizator(asList(new Authorization("/sensors", READ)));
//verify
assertTrue(authorizator.canRead("/sensors"));
}
@Test
public void testCanReadWriteMixedSimpleTopic() {
Authorization topicWriteACL = new Authorization("/sensors", WRITE);
Authorization topicReadACL = new Authorization("/sensors/anemometer", READ);
ACLAuthorizator authorizator = new ACLAuthorizator(asList(topicWriteACL, topicReadACL));
//verify
assertTrue(authorizator.canWrite("/sensors"));
assertFalse(authorizator.canRead("/sensors"));
}
@Test
public void testCanWriteMultiMatherTopic() {
ACLAuthorizator authorizator = new ACLAuthorizator(asList(new Authorization("/sensors/#", WRITE)));
//verify
assertTrue(authorizator.canWrite("/sensors/anemometer/wind"));
}
@Test
public void testCanWriteSingleMatherTopic() {
ACLAuthorizator authorizator = new ACLAuthorizator(asList(new Authorization("/sensors/+", WRITE)));
//verify
assertTrue(authorizator.canWrite("/sensors/anemometer"));
}
}
@@ -30,14 +30,10 @@ import static org.junit.Assert.*;
* */
public class ACLFileParserTest {
private static final Authorization RW_ANEMOMETER = new Authorization("/weather/italy/anemometer");
private static final Authorization R_ANEMOMETER = new Authorization("/weather/italy/anemometer", Permission.READ);
private static final Authorization W_ANEMOMETER = new Authorization("/weather/italy/anemometer", Permission.WRITE);
@Test
public void testParseEmpty() throws ParseException {
Reader conf = new StringReader(" ");
List<Authorization> authorizations = ACLFileParser.parse(conf);
AuthorizationsCollector authorizations = ACLFileParser.parse(conf);
//Verify
assertTrue(authorizations.isEmpty());
@@ -46,7 +42,7 @@ public class ACLFileParserTest {
@Test
public void testParseValidComment() throws ParseException {
Reader conf = new StringReader("#simple comment");
List<Authorization> authorizations = ACLFileParser.parse(conf);
AuthorizationsCollector authorizations = ACLFileParser.parse(conf);
//Verify
assertTrue(authorizations.isEmpty());
@@ -61,58 +57,11 @@ public class ACLFileParserTest {
@Test
public void testParseSingleLineACL() throws ParseException {
Reader conf = new StringReader("topic /weather/italy/anemometer");
List<Authorization> authorizations = ACLFileParser.parse(conf);
AuthorizationsCollector authorizations = ACLFileParser.parse(conf);
//Verify
assertEquals(1, authorizations.size());
Authorization anemosAuth = authorizations.iterator().next();
assertEquals(RW_ANEMOMETER, anemosAuth);
assertTrue(authorizations.canRead("/weather/italy/anemometer"));
assertTrue(authorizations.canWrite("/weather/italy/anemometer"));
}
@Test
public void testParseAuthLineValid() throws ParseException {
Authorization authorization = ACLFileParser.parseAuthLine("topic /weather/italy/anemometer");
//Verify
assertEquals(RW_ANEMOMETER, authorization);
}
@Test
public void testParseAuthLineValid_read() throws ParseException {
Authorization authorization = ACLFileParser.parseAuthLine("topic read /weather/italy/anemometer");
//Verify
assertEquals(R_ANEMOMETER, authorization);
}
@Test
public void testParseAuthLineValid_write() throws ParseException {
Authorization authorization = ACLFileParser.parseAuthLine("topic write /weather/italy/anemometer");
//Verify
assertEquals(W_ANEMOMETER, authorization);
}
@Test
public void testParseAuthLineValid_readwrite() throws ParseException {
Authorization authorization = ACLFileParser.parseAuthLine("topic readwrite /weather/italy/anemometer");
//Verify
assertEquals(RW_ANEMOMETER, authorization);
}
@Test
public void testParseAuthLineValid_topic_with_space() throws ParseException {
Authorization expected = new Authorization("/weather/eastern italy/anemometer");
Authorization authorization = ACLFileParser.parseAuthLine("topic readwrite /weather/eastern italy/anemometer");
//Verify
assertEquals(expected, authorization);
}
@Test(expected = ParseException.class)
public void testParseAuthLineValid_invalid() throws ParseException {
ACLFileParser.parseAuthLine("topic faker /weather/italy/anemometer");
}
}
@@ -0,0 +1,133 @@
/*
* Copyright (c) 2012-2014 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package org.eclipse.moquette.spi.impl.security;
import org.junit.Before;
import org.junit.Test;
import java.text.ParseException;
import static java.util.Arrays.asList;
import static org.eclipse.moquette.spi.impl.security.Authorization.Permission.READ;
import static org.eclipse.moquette.spi.impl.security.Authorization.Permission.WRITE;
import static org.eclipse.moquette.spi.impl.security.AuthorizationsCollector.*;
import static org.junit.Assert.*;
/**
* @author andrea
*/
public class AuthorizationsCollectorTest {
private static final Authorization RW_ANEMOMETER = new Authorization("/weather/italy/anemometer");
private static final Authorization R_ANEMOMETER = new Authorization("/weather/italy/anemometer", Authorization.Permission.READ);
private static final Authorization W_ANEMOMETER = new Authorization("/weather/italy/anemometer", Authorization.Permission.WRITE);
private AuthorizationsCollector authorizator;
@Before
public void setUp() {
authorizator = new AuthorizationsCollector();
}
@Test
public void testParseAuthLineValid() throws ParseException {
Authorization authorization = parseAuthLine("topic /weather/italy/anemometer");
//Verify
assertEquals(RW_ANEMOMETER, authorization);
}
@Test
public void testParseAuthLineValid_read() throws ParseException {
Authorization authorization = parseAuthLine("topic read /weather/italy/anemometer");
//Verify
assertEquals(R_ANEMOMETER, authorization);
}
@Test
public void testParseAuthLineValid_write() throws ParseException {
Authorization authorization = parseAuthLine("topic write /weather/italy/anemometer");
//Verify
assertEquals(W_ANEMOMETER, authorization);
}
@Test
public void testParseAuthLineValid_readwrite() throws ParseException {
Authorization authorization = parseAuthLine("topic readwrite /weather/italy/anemometer");
//Verify
assertEquals(RW_ANEMOMETER, authorization);
}
@Test
public void testParseAuthLineValid_topic_with_space() throws ParseException {
Authorization expected = new Authorization("/weather/eastern italy/anemometer");
Authorization authorization = parseAuthLine("topic readwrite /weather/eastern italy/anemometer");
//Verify
assertEquals(expected, authorization);
}
@Test(expected = ParseException.class)
public void testParseAuthLineValid_invalid() throws ParseException {
parseAuthLine("topic faker /weather/italy/anemometer");
}
@Test
public void testCanWriteSimpleTopic() {
authorizator.parse("topic write /sensors");
//verify
assertTrue(authorizator.canWrite("/sensors"));
}
@Test
public void testCanReadSimpleTopic() {
authorizator.parse("topic read /sensors");
//verify
assertTrue(authorizator.canRead("/sensors"));
}
@Test
public void testCanReadWriteMixedSimpleTopic() {
authorizator.parse("topic write /sensors");
authorizator.parse("topic read /sensors/anemometer");
//verify
assertTrue(authorizator.canWrite("/sensors"));
assertFalse(authorizator.canRead("/sensors"));
}
@Test
public void testCanWriteMultiMatherTopic() {
authorizator.parse("topic write /sensors/#");
//verify
assertTrue(authorizator.canWrite("/sensors/anemometer/wind"));
}
@Test
public void testCanWriteSingleMatherTopic() {
authorizator.parse("topic write /sensors/+");
//verify
assertTrue(authorizator.canWrite("/sensors/anemometer"));
}
}