Repository: incubator-rya Updated Branches: refs/heads/master 0e613334f -> cc1cc712d
http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/cc1cc712/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/MongoDBRyaDAOTest.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/MongoDBRyaDAOTest.java b/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/MongoDBRyaDAOTest.java index 5be4940..980a2ff 100644 --- a/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/MongoDBRyaDAOTest.java +++ b/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/MongoDBRyaDAOTest.java @@ -28,67 +28,68 @@ import org.apache.rya.api.domain.RyaStatement; import org.apache.rya.api.domain.RyaStatement.RyaStatementBuilder; import org.apache.rya.api.domain.RyaURI; import org.apache.rya.api.persist.RyaDAOException; +import org.bson.Document; import org.junit.Before; import org.junit.Test; -import com.mongodb.DB; -import com.mongodb.DBCollection; import com.mongodb.MongoException; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; public class MongoDBRyaDAOTest extends MongoRyaTestBase { - private MongoDBRyaDAO dao; - private MongoDBRdfConfiguration configuration; + private MongoDBRyaDAO dao; + private MongoDBRdfConfiguration configuration; - @Before - public void setUp() throws IOException, RyaDAOException{ - final Configuration conf = new Configuration(); + @Before + public void setUp() throws IOException, RyaDAOException{ + final Configuration conf = new Configuration(); conf.set(MongoDBRdfConfiguration.MONGO_DB_NAME, "test"); conf.set(MongoDBRdfConfiguration.MONGO_COLLECTION_PREFIX, "rya_"); conf.set(RdfCloudTripleStoreConfiguration.CONF_TBL_PREFIX, "rya_"); configuration = new MongoDBRdfConfiguration(conf); final int port = mongoClient.getServerAddressList().get(0).getPort(); configuration.set(MongoDBRdfConfiguration.MONGO_INSTANCE_PORT, ""+port); - dao = new MongoDBRyaDAO(configuration, mongoClient); - } + dao = new MongoDBRyaDAO(configuration, mongoClient); + } - @Test - public void testDeleteWildcard() throws RyaDAOException { - final RyaStatementBuilder builder = new RyaStatementBuilder(); - builder.setPredicate(new RyaURI("http://temp.com")); - dao.delete(builder.build(), configuration); - } + @Test + public void testDeleteWildcard() throws RyaDAOException { + final RyaStatementBuilder builder = new RyaStatementBuilder(); + builder.setPredicate(new RyaURI("http://temp.com")); + dao.delete(builder.build(), configuration); + } - @Test - public void testAdd() throws RyaDAOException, MongoException, IOException { - final RyaStatementBuilder builder = new RyaStatementBuilder(); - builder.setPredicate(new RyaURI("http://temp.com")); - builder.setSubject(new RyaURI("http://subject.com")); - builder.setObject(new RyaURI("http://object.com")); + @Test + public void testAdd() throws RyaDAOException, MongoException, IOException { + final RyaStatementBuilder builder = new RyaStatementBuilder(); + builder.setPredicate(new RyaURI("http://temp.com")); + builder.setSubject(new RyaURI("http://subject.com")); + builder.setObject(new RyaURI("http://object.com")); - final DB db = mongoClient.getDB(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); - final DBCollection coll = db.getCollection(configuration.getTriplesCollectionName()); + final MongoDatabase db = mongoClient.getDatabase(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); + final MongoCollection<Document> coll = db.getCollection(configuration.getTriplesCollectionName()); - dao.add(builder.build()); + dao.add(builder.build()); assertEquals(coll.count(),1); - } + } - @Test - public void testDelete() throws RyaDAOException, MongoException, IOException { - final RyaStatementBuilder builder = new RyaStatementBuilder(); - builder.setPredicate(new RyaURI("http://temp.com")); - builder.setSubject(new RyaURI("http://subject.com")); - builder.setObject(new RyaURI("http://object.com")); - final RyaStatement statement = builder.build(); + @Test + public void testDelete() throws RyaDAOException, MongoException, IOException { + final RyaStatementBuilder builder = new RyaStatementBuilder(); + builder.setPredicate(new RyaURI("http://temp.com")); + builder.setSubject(new RyaURI("http://subject.com")); + builder.setObject(new RyaURI("http://object.com")); + final RyaStatement statement = builder.build(); - final DB db = mongoClient.getDB(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); - final DBCollection coll = db.getCollection(configuration.getTriplesCollectionName()); + final MongoDatabase db = mongoClient.getDatabase(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); + final MongoCollection<Document> coll = db.getCollection(configuration.getTriplesCollectionName()); - dao.add(statement); + dao.add(statement); assertEquals(coll.count(),1); @@ -96,34 +97,32 @@ public class MongoDBRyaDAOTest extends MongoRyaTestBase { assertEquals(coll.count(),0); - } + } - @Test - public void testDeleteWildcardSubjectWithContext() throws RyaDAOException, MongoException, IOException { - final RyaStatementBuilder builder = new RyaStatementBuilder(); - builder.setPredicate(new RyaURI("http://temp.com")); - builder.setSubject(new RyaURI("http://subject.com")); - builder.setObject(new RyaURI("http://object.com")); - builder.setContext(new RyaURI("http://context.com")); - final RyaStatement statement = builder.build(); + @Test + public void testDeleteWildcardSubjectWithContext() throws RyaDAOException, MongoException, IOException { + final RyaStatementBuilder builder = new RyaStatementBuilder(); + builder.setPredicate(new RyaURI("http://temp.com")); + builder.setSubject(new RyaURI("http://subject.com")); + builder.setObject(new RyaURI("http://object.com")); + builder.setContext(new RyaURI("http://context.com")); + final RyaStatement statement = builder.build(); - final DB db = mongoClient.getDB(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); - final DBCollection coll = db.getCollection(configuration.getTriplesCollectionName()); + final MongoDatabase db = mongoClient.getDatabase(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); + final MongoCollection<Document> coll = db.getCollection(configuration.getTriplesCollectionName()); - dao.add(statement); + dao.add(statement); assertEquals(coll.count(),1); - final RyaStatementBuilder builder2 = new RyaStatementBuilder(); - builder2.setPredicate(new RyaURI("http://temp.com")); - builder2.setObject(new RyaURI("http://object.com")); - builder2.setContext(new RyaURI("http://context3.com")); - final RyaStatement query = builder2.build(); + final RyaStatementBuilder builder2 = new RyaStatementBuilder(); + builder2.setPredicate(new RyaURI("http://temp.com")); + builder2.setObject(new RyaURI("http://object.com")); + builder2.setContext(new RyaURI("http://context3.com")); + final RyaStatement query = builder2.build(); dao.delete(query, configuration); assertEquals(coll.count(),1); - - } - + } } http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/cc1cc712/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/SimpleMongoDBStorageStrategyTest.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/SimpleMongoDBStorageStrategyTest.java b/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/SimpleMongoDBStorageStrategyTest.java index bec403c..2af6da9 100644 --- a/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/SimpleMongoDBStorageStrategyTest.java +++ b/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/SimpleMongoDBStorageStrategyTest.java @@ -23,17 +23,19 @@ import static org.openrdf.model.vocabulary.XMLSchema.ANYURI; import java.io.IOException; -import org.junit.Test; - -import com.mongodb.BasicDBObject; -import com.mongodb.DBObject; -import com.mongodb.MongoException; - import org.apache.rya.api.domain.RyaStatement; import org.apache.rya.api.domain.RyaStatement.RyaStatementBuilder; import org.apache.rya.api.domain.RyaURI; import org.apache.rya.api.persist.RyaDAOException; import org.apache.rya.mongodb.dao.SimpleMongoDBStorageStrategy; +import org.apache.rya.mongodb.document.util.DocumentVisibilityConversionException; +import org.apache.rya.mongodb.document.util.DocumentVisibilityUtil; +import org.apache.rya.mongodb.document.visibility.DocumentVisibility; +import org.junit.Test; + +import com.mongodb.BasicDBObject; +import com.mongodb.DBObject; +import com.mongodb.MongoException; public class SimpleMongoDBStorageStrategyTest { private static final String SUBJECT = "http://subject.com"; @@ -41,6 +43,7 @@ public class SimpleMongoDBStorageStrategyTest { private static final String OBJECT = "http://object.com"; private static final String CONTEXT = "http://context.com"; private static final String STATEMENT_METADATA = "{}"; + private static final DocumentVisibility DOCUMENT_VISIBILITY = new DocumentVisibility("A&B"); private static final RyaStatement testStatement; private static final DBObject testDBO; @@ -52,18 +55,24 @@ public class SimpleMongoDBStorageStrategyTest { builder.setSubject(new RyaURI(SUBJECT)); builder.setObject(new RyaURI(OBJECT)); builder.setContext(new RyaURI(CONTEXT)); + builder.setColumnVisibility(DOCUMENT_VISIBILITY.flatten()); builder.setTimestamp(null); testStatement = builder.build(); testDBO = new BasicDBObject(); - testDBO.put("_id", "d5f8fea0e85300478da2c9b4e132c69502e21221"); - testDBO.put("subject", SUBJECT); - testDBO.put("predicate", PREDICATE); - testDBO.put("object", OBJECT); - testDBO.put("objectType", ANYURI.stringValue()); - testDBO.put("context", CONTEXT); - testDBO.put("statementMetadata", STATEMENT_METADATA); - testDBO.put("insertTimestamp", null); + testDBO.put(SimpleMongoDBStorageStrategy.ID, "d5f8fea0e85300478da2c9b4e132c69502e21221"); + testDBO.put(SimpleMongoDBStorageStrategy.SUBJECT, SUBJECT); + testDBO.put(SimpleMongoDBStorageStrategy.PREDICATE, PREDICATE); + testDBO.put(SimpleMongoDBStorageStrategy.OBJECT, OBJECT); + testDBO.put(SimpleMongoDBStorageStrategy.OBJECT_TYPE, ANYURI.stringValue()); + testDBO.put(SimpleMongoDBStorageStrategy.CONTEXT, CONTEXT); + testDBO.put(SimpleMongoDBStorageStrategy.STATEMENT_METADATA, STATEMENT_METADATA); + try { + testDBO.put(SimpleMongoDBStorageStrategy.DOCUMENT_VISIBILITY, DocumentVisibilityUtil.toMultidimensionalArray(DOCUMENT_VISIBILITY)); + } catch (final DocumentVisibilityConversionException e) { + e.printStackTrace(); + } + testDBO.put(SimpleMongoDBStorageStrategy.TIMESTAMP, null); } @Test @@ -76,7 +85,7 @@ public class SimpleMongoDBStorageStrategyTest { @Test public void testDeSerializeStatementToDBO() throws RyaDAOException, MongoException, IOException { final RyaStatement statement = storageStrategy.deserializeDBObject(testDBO); - /** + /* * Since RyaStatement creates a timestamp using JVM time if the timestamp is null, we want to re-null it * for this test. Timestamp is created at insert time by the Server, this test * can be found in the RyaDAO. http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/cc1cc712/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/document/util/DisjunctiveNormalFormConverterTest.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/document/util/DisjunctiveNormalFormConverterTest.java b/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/document/util/DisjunctiveNormalFormConverterTest.java new file mode 100644 index 0000000..cc6a16e --- /dev/null +++ b/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/document/util/DisjunctiveNormalFormConverterTest.java @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.mongodb.document.util; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.apache.accumulo.core.security.ColumnVisibility.Node; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.rya.mongodb.document.visibility.DocumentVisibility; +import org.junit.Test; + +import com.google.common.collect.ImmutableList; + +/** + * Tests the methods of {@link DisjunctiveNormalFormConverter}. + */ +public class DisjunctiveNormalFormConverterTest { + private static final ImmutableList<Pair<String, String>> INPUT_AND_EXPECTED_BOOLEAN_EXPRESSIONS = + ImmutableList.of( + Pair.of("A", "A"), + Pair.of("A&B", "A&B"), + Pair.of("A|B", "A|B"), + Pair.of("A&(B|C)", "(A&B)|(A&C)"), + Pair.of("A|(B&C)", "A|(B&C)"), + Pair.of("HCS|(FDW&TGE&(TS|BX))", "HCS|(BX&FDW&TGE)|(FDW&TGE&TS)") + ); + + /** + * Test truth table with a {@code null} {@link List}. + */ + @Test (expected=NullPointerException.class) + public void testTruthTableNullList() { + final List<String> list = null; + DisjunctiveNormalFormConverter.createTruthTableInputs(list); + } + + /** + * Test truth table with a {@code null} {@link Node}. + */ + @Test (expected=NullPointerException.class) + public void testTruthTableNullNode() { + final Node node = null; + final byte[] expression = new DocumentVisibility("A").getExpression(); + DisjunctiveNormalFormConverter.createTruthTableInputs(node, expression); + } + + /** + * Test truth table with a {@code null} expression. + */ + @Test (expected=NullPointerException.class) + public void testTruthTableNullExpression() { + final Node node = new DocumentVisibility("A").getParseTree(); + final byte[] expression = null; + DisjunctiveNormalFormConverter.createTruthTableInputs(node, expression); + } + + /** + * Test truth table with 0 inputs. + */ + @Test + public void testTruthTableSize() { + final byte[][] truthTable = DisjunctiveNormalFormConverter.createTruthTableInputs(0); + final byte[][] expected = + { + {} + }; + assertArrayEquals(expected, truthTable); + } + + /** + * Test truth table with 1 inputs + */ + @Test + public void testTruthTableSize1() { + final byte[][] truthTable = DisjunctiveNormalFormConverter.createTruthTableInputs(1); + final byte[][] expected= + { + {0}, + {1} + }; + assertArrayEquals(expected, truthTable); + } + + /** + * Test truth table with 2 inputs from a list.. + */ + @Test + public void testTruthTableSize2FromList() { + final byte[][] truthTable = DisjunctiveNormalFormConverter.createTruthTableInputs(ImmutableList.of("A", "B")); + final byte[][] expected = + { + {0, 0}, + {0, 1}, + {1, 0}, + {1, 1} + }; + assertArrayEquals(expected, truthTable); + } + + /** + * Test truth table with 3 inputs from a node. + */ + @Test + public void testTruthTableSize3FromNode() { + final DocumentVisibility dv = new DocumentVisibility("(A&B)|(A&C)"); + final byte[][] truthTable = DisjunctiveNormalFormConverter.createTruthTableInputs(dv.getParseTree(), dv.getExpression()); + final byte[][] expected = + { + {0, 0, 0}, + {0, 0, 1}, + {0, 1, 0}, + {0, 1, 1}, + {1, 0, 0}, + {1, 0, 1}, + {1, 1, 0}, + {1, 1, 1} + }; + assertArrayEquals(expected, truthTable); + } + + /** + * Test ability to convert expressions into Disjunctive Normal Form. + */ + @Test + public void testConvertToDnf() { + for (final Pair<String, String> pair : INPUT_AND_EXPECTED_BOOLEAN_EXPRESSIONS) { + final String input = pair.getLeft(); + final String expected = pair.getRight(); + + final DocumentVisibility inputDv = new DocumentVisibility(input); + final DocumentVisibility resultDv = DisjunctiveNormalFormConverter.convertToDisjunctiveNormalForm(inputDv); + + assertEquals(expected, new String(resultDv.flatten())); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/cc1cc712/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/document/util/DocumentVisibilityUtilTest.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/document/util/DocumentVisibilityUtilTest.java b/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/document/util/DocumentVisibilityUtilTest.java new file mode 100644 index 0000000..1e6817f --- /dev/null +++ b/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/document/util/DocumentVisibilityUtilTest.java @@ -0,0 +1,139 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.mongodb.document.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.util.Arrays; + +import org.apache.commons.lang3.tuple.Pair; +import org.apache.log4j.Logger; +import org.apache.rya.mongodb.document.visibility.DocumentVisibility; +import org.junit.Test; + +import com.google.common.base.Charsets; +import com.google.common.collect.ImmutableSet; + +/** + * Tests the methods of {@link DocumentVisibilityUtil}. + */ +public class DocumentVisibilityUtilTest { + private static final Logger log = Logger.getLogger(DocumentVisibilityUtilTest.class); + + private static final ImmutableSet<Pair<String, String>> INPUT_AND_EXPECTED_BOOLEAN_EXPRESSIONS = + ImmutableSet.copyOf(Arrays.asList( + Pair.of("", ""), + Pair.of("A", "A"), + Pair.of("\"A\"", "\"A\""), + Pair.of("(A)", "A"), + Pair.of("A|B", "A|B"), + Pair.of("(A)|(B)|(C)", "A|B|C"), + Pair.of("(A|B)", "A|B"), + Pair.of("A|B|C", "A|B|C"), + Pair.of("(A|B|C)", "A|B|C"), + Pair.of("A&B", "A&B"), + Pair.of("(A&B)", "A&B"), + Pair.of("A&B&C", "A&B&C"), + Pair.of("(A&B&C)", "A&B&C"), + Pair.of("(A&B)|(C&D)", "(A&B)|(C&D)"), + Pair.of("A&(B|C)", "(A&B)|(A&C)"), + Pair.of("(A&B)|C", "C|(A&B)"), + Pair.of("A|(B&C)", "A|(B&C)"), + Pair.of("(A|B)&C", "(A&C)|(B&C)"), + Pair.of("(A&B)&(C|D)", "(A&B&C)|(A&B&D)"), + Pair.of("(A|B)|(C&D)", "A|B|(C&D)"), + Pair.of("(A|B)&(C|D)", "(A&C)|(A&D)|(B&C)|(B&D)"), + Pair.of("(A|B)&(C|(D&E))", "(A&C)|(B&C)|(A&D&E)|(B&D&E)"), + Pair.of("G|(FDW&TGE)", "G|(FDW&TGE)"), + Pair.of("HCS|(FDW&TGE&(TS|BX))", "HCS|(BX&FDW&TGE)|(FDW&TGE&TS)"), + Pair.of("\"A&B\"", "\"A&B\""), + Pair.of("\"A\"&\"B\"", "\"A\"&\"B\""), + Pair.of("\"A\"|\"B\"", "\"A\"|\"B\""), + Pair.of("\"A#C\"&B", "\"A#C\"&B"), + Pair.of("\"A#C\"&\"B+D\"", "\"A#C\"&\"B+D\""), + Pair.of("(A|C)&(B|C)", "C|(A&B)"), + Pair.of("(A&B)|(A&C)", "(A&B)|(A&C)") + + )); + + private static final ImmutableSet<String> INVALID_BOOLEAN_EXPRESSIONS = + ImmutableSet.of( + "A|B&C", + "A=B", + "A|B|", + "A&|B", + "()", + ")", + "dog|!cat" + ); + + @Test + public void testGoodExpressions() throws DocumentVisibilityConversionException { + int count = 1; + for (final Pair<String, String> pair : INPUT_AND_EXPECTED_BOOLEAN_EXPRESSIONS) { + final String booleanExpression = pair.getLeft(); + final String expected = pair.getRight(); + log.info("Valid Test: " + count); + log.info("Original: " + booleanExpression); + + // Convert to multidimensional array + final DocumentVisibility dv = new DocumentVisibility(booleanExpression); + final Object[] multidimensionalArray = DocumentVisibilityUtil.toMultidimensionalArray(dv); + log.info("Array : " + Arrays.deepToString(multidimensionalArray)); + + // Convert multidimensional array back to string + final String booleanStringResult = DocumentVisibilityUtil.multidimensionalArrayToBooleanString(multidimensionalArray); + log.info("Result : " + booleanStringResult); + + // Compare results + assertEquals(expected, booleanStringResult); + log.info("==========================="); + count++; + } + } + + @Test + public void testBadExpressions() { + int count = 1; + for (final String booleanExpression : INVALID_BOOLEAN_EXPRESSIONS) { + log.info("Invalid Test: " + count); + try { + log.info("Original: " + booleanExpression); + // Convert to multidimensional array + final DocumentVisibility dv = new DocumentVisibility(booleanExpression); + final Object[] multidimensionalArray = DocumentVisibilityUtil.toMultidimensionalArray(dv); + log.info("Array : " + Arrays.deepToString(multidimensionalArray)); + + // Convert multidimensional array back to string + final String booleanString = DocumentVisibilityUtil.multidimensionalArrayToBooleanString(multidimensionalArray); + log.info("Result : " + booleanString); + + // Compare results + final String expected = new String(dv.flatten(), Charsets.UTF_8); + assertEquals(expected, booleanString); + fail("Bad expression passed."); + } catch (final Exception e) { + // Expected + } + log.info("==========================="); + count++; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/cc1cc712/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/document/visibility/DocumentVisibilityAdapterTest.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/document/visibility/DocumentVisibilityAdapterTest.java b/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/document/visibility/DocumentVisibilityAdapterTest.java new file mode 100644 index 0000000..3038a58 --- /dev/null +++ b/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/document/visibility/DocumentVisibilityAdapterTest.java @@ -0,0 +1,167 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rya.mongodb.document.visibility; + +import static org.junit.Assert.assertEquals; + +import org.apache.rya.mongodb.MongoDbRdfConstants; +import org.apache.rya.mongodb.document.visibility.DocumentVisibilityAdapter.MalformedDocumentVisibilityException; +import org.junit.Test; + +import com.mongodb.BasicDBObject; +import com.mongodb.util.JSON; + +/** + * Tests the methods of {@link DocumentVisibilityAdapter}. + */ +public class DocumentVisibilityAdapterTest { + @Test + public void testToDBObject() { + final DocumentVisibility dv = new DocumentVisibility("A"); + final BasicDBObject dbObject = DocumentVisibilityAdapter.toDBObject(dv); + final BasicDBObject expected = (BasicDBObject) JSON.parse( + "{" + + "documentVisibility : [[\"A\"]]" + + "}" + ); + assertEquals(expected, dbObject); + } + + @Test + public void testToDBObject_and() { + final DocumentVisibility dv = new DocumentVisibility("A&B&C"); + final BasicDBObject dbObject = DocumentVisibilityAdapter.toDBObject(dv); + final BasicDBObject expected = (BasicDBObject) JSON.parse( + "{" + + "documentVisibility : [[\"A\", \"B\", \"C\"]]" + + "}" + ); + assertEquals(expected, dbObject); + } + + @Test + public void testToDBObject_or() { + final DocumentVisibility dv = new DocumentVisibility("A|B|C"); + final BasicDBObject dbObject = DocumentVisibilityAdapter.toDBObject(dv); + final BasicDBObject expected = (BasicDBObject) JSON.parse( + "{" + + "documentVisibility : [[\"C\"], [\"B\"], [\"A\"]]" + + "}" + ); + assertEquals(expected, dbObject); + } + + @Test + public void testToDBObject_Expression() { + final DocumentVisibility dv = new DocumentVisibility("A&B&C"); + final BasicDBObject dbObject = DocumentVisibilityAdapter.toDBObject(dv.getExpression()); + final BasicDBObject expected = (BasicDBObject) JSON.parse( + "{" + + "documentVisibility : [[\"A\", \"B\", \"C\"]]" + + "}" + ); + assertEquals(expected, dbObject); + } + + @Test + public void testToDBObject_nullExpression() { + final BasicDBObject dbObject = DocumentVisibilityAdapter.toDBObject((byte[])null); + final BasicDBObject expected = (BasicDBObject) JSON.parse( + "{" + + "documentVisibility : []" + + "}" + ); + assertEquals(expected, dbObject); + } + + @Test + public void testToDBObject_nullDocumentVisibility() { + final BasicDBObject dbObject = DocumentVisibilityAdapter.toDBObject((DocumentVisibility)null); + final BasicDBObject expected = (BasicDBObject) JSON.parse( + "{" + + "documentVisibility : []" + + "}" + ); + assertEquals(expected, dbObject); + } + + @Test + public void testToDBObject_emptyDocumentVisibility() { + final BasicDBObject dbObject = DocumentVisibilityAdapter.toDBObject(MongoDbRdfConstants.EMPTY_DV); + final BasicDBObject expected = (BasicDBObject) JSON.parse( + "{" + + "documentVisibility : []" + + "}" + ); + assertEquals(expected, dbObject); + } + + @Test + public void testToDocumentVisibility() throws MalformedDocumentVisibilityException { + final BasicDBObject dbObject = (BasicDBObject) JSON.parse( + "{" + + "documentVisibility : [\"A\"]" + + "}" + ); + final DocumentVisibility dv = DocumentVisibilityAdapter.toDocumentVisibility(dbObject); + final DocumentVisibility expected = new DocumentVisibility("A"); + assertEquals(expected, dv); + } + + @Test + public void testToDocumentVisibility_and() throws MalformedDocumentVisibilityException { + final BasicDBObject dbObject = (BasicDBObject) JSON.parse( + "{" + + "documentVisibility : [\"A\", \"B\", \"C\"]" + + "}" + ); + final DocumentVisibility dv = DocumentVisibilityAdapter.toDocumentVisibility(dbObject); + final DocumentVisibility expected = new DocumentVisibility("A&B&C"); + assertEquals(expected, dv); + } + + @Test + public void testToDocumentVisibility_or() throws MalformedDocumentVisibilityException { + final BasicDBObject dbObject = (BasicDBObject) JSON.parse( + "{" + + "documentVisibility : [[\"A\"], [\"B\"], [\"C\"]]" + + "}" + ); + final DocumentVisibility dv = DocumentVisibilityAdapter.toDocumentVisibility(dbObject); + final DocumentVisibility expected = new DocumentVisibility("A|B|C"); + assertEquals(expected, dv); + } + + @Test + public void testToDocumentVisibility_empty() throws MalformedDocumentVisibilityException { + final BasicDBObject dbObject = (BasicDBObject) JSON.parse( + "{" + + "documentVisibility : []" + + "}" + ); + final DocumentVisibility dv = DocumentVisibilityAdapter.toDocumentVisibility(dbObject); + final DocumentVisibility expected = MongoDbRdfConstants.EMPTY_DV; + assertEquals(expected, dv); + } + + @Test (expected=MalformedDocumentVisibilityException.class) + public void testToDocumentVisibility_null() throws MalformedDocumentVisibilityException { + DocumentVisibilityAdapter.toDocumentVisibility(null); + } +} \ No newline at end of file
