Repository: incubator-rya Updated Branches: refs/heads/develop 1b12872f9 -> 2de66c1f4
RYA-73 added timestamp to mongoDB DAO Changed the mongo storage strategy to now include timestamp in the [de]serialization of RyaStatements. The timestamp is added at insertion timein the DAO Added test for [de]serialize Added test for timestamp in the DAO Project: http://git-wip-us.apache.org/repos/asf/incubator-rya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-rya/commit/2de66c1f Tree: http://git-wip-us.apache.org/repos/asf/incubator-rya/tree/2de66c1f Diff: http://git-wip-us.apache.org/repos/asf/incubator-rya/diff/2de66c1f Branch: refs/heads/develop Commit: 2de66c1f4a8999bb9f106d5654e1e8671b695b49 Parents: 1b12872 Author: isper3at <[email protected]> Authored: Mon May 9 14:29:18 2016 -0400 Committer: pujav65 <[email protected]> Committed: Thu Jun 16 10:14:20 2016 -0400 ---------------------------------------------------------------------- dao/mongodb.rya/pom.xml | 19 +- .../dao/SimpleMongoDBStorageStrategy.java | 248 ++++++++++--------- .../java/mvm/rya/mongodb/MongoDBRyaDAOIT.java | 139 +++++++++++ .../java/mvm/rya/mongodb/MongoDBRyaDAOTest.java | 139 ----------- .../SimpleMongoDBStorageStrategyTest.java | 85 +++++++ 5 files changed, 372 insertions(+), 258 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2de66c1f/dao/mongodb.rya/pom.xml ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/pom.xml b/dao/mongodb.rya/pom.xml index b7d0c0e..2a1851b 100644 --- a/dao/mongodb.rya/pom.xml +++ b/dao/mongodb.rya/pom.xml @@ -29,6 +29,24 @@ under the License. <artifactId>mongodb.rya</artifactId> <name>Apache Rya MongoDB DAO</name> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-failsafe-plugin</artifactId> + <version>2.19.1</version> + <executions> + <execution> + <goals> + <goal>integration-test</goal> + <goal>verify</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> + <dependencies> <dependency> <groupId>org.apache.rya</groupId> @@ -49,5 +67,4 @@ under the License. <scope>test</scope> </dependency> </dependencies> - </project> http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2de66c1f/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java index 74c8366..9ca6279 100644 --- a/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java +++ b/dao/mongodb.rya/src/main/java/mvm/rya/mongodb/dao/SimpleMongoDBStorageStrategy.java @@ -1,5 +1,7 @@ package mvm.rya.mongodb.dao; +import static org.openrdf.model.vocabulary.XMLSchema.ANYURI; + /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file @@ -8,9 +10,9 @@ package mvm.rya.mongodb.dao; * 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 @@ -24,128 +26,138 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Map; -import mvm.rya.api.domain.RyaStatement; -import mvm.rya.api.domain.RyaType; -import mvm.rya.api.domain.RyaURI; -import mvm.rya.api.persist.query.RyaQuery; - import org.apache.commons.codec.binary.Hex; +import org.apache.log4j.Logger; import org.openrdf.model.impl.ValueFactoryImpl; +import org.openrdf.model.vocabulary.XMLSchema; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.DBObject; -public class SimpleMongoDBStorageStrategy implements MongoDBStorageStrategy { - - protected static final String ID = "_id"; - protected static final String OBJECT_TYPE = "objectType"; - protected static final String CONTEXT = "context"; - protected static final String PREDICATE = "predicate"; - protected static final String OBJECT = "object"; - protected static final String SUBJECT = "subject"; - protected ValueFactoryImpl factory = new ValueFactoryImpl(); - - - public SimpleMongoDBStorageStrategy() { - } - - @Override - public void createIndices(DBCollection coll){ - BasicDBObject doc = new BasicDBObject(); - doc.put(SUBJECT, 1); - doc.put(PREDICATE, 1); - coll.createIndex(doc); - doc = new BasicDBObject(PREDICATE, 1); - doc.put(OBJECT, 1); - doc.put(OBJECT_TYPE, 1); - coll.createIndex(doc); - doc = new BasicDBObject(OBJECT, 1); - doc = new BasicDBObject(OBJECT_TYPE, 1); - doc.put(SUBJECT, 1); - coll.createIndex(doc); - } - - @Override - public DBObject getQuery(RyaStatement stmt) { - RyaURI subject = stmt.getSubject(); - RyaURI predicate = stmt.getPredicate(); - RyaType object = stmt.getObject(); - RyaURI context = stmt.getContext(); - BasicDBObject query = new BasicDBObject(); - if (subject != null){ - query.append(SUBJECT, subject.getData()); - } - if (object != null){ - query.append(OBJECT, object.getData()); - query.append(OBJECT_TYPE, object.getDataType().toString()); - } - if (predicate != null){ - query.append(PREDICATE, predicate.getData()); - } - if (context != null){ - query.append(CONTEXT, context.getData()); - } - - return query; - } - - @Override - public RyaStatement deserializeDBObject(DBObject queryResult) { - Map result = queryResult.toMap(); - String subject = (String) result.get(SUBJECT); - String object = (String) result.get(OBJECT); - String objectType = (String) result.get(OBJECT_TYPE); - String predicate = (String) result.get(PREDICATE); - String context = (String) result.get(CONTEXT); - RyaType objectRya = null; - if (objectType.equalsIgnoreCase("http://www.w3.org/2001/XMLSchema#anyURI")){ - objectRya = new RyaURI(object); - } - else { - objectRya = new RyaType(factory.createURI(objectType), object); - } - - if (!context.isEmpty()){ - return new RyaStatement(new RyaURI(subject), new RyaURI(predicate), objectRya, - new RyaURI(context)); - } - return new RyaStatement(new RyaURI(subject), new RyaURI(predicate), objectRya); - } - - @Override - public DBObject serialize(RyaStatement statement){ - return serializeInternal(statement); - } - - public BasicDBObject serializeInternal(RyaStatement statement){ - String context = ""; - if (statement.getContext() != null){ - context = statement.getContext().getData(); - } - String id = statement.getSubject().getData() + " " + - statement.getPredicate().getData() + " " + statement.getObject().getData() + " " + context; - byte[] bytes = id.getBytes(); - try { - MessageDigest digest = MessageDigest.getInstance("SHA-1"); - bytes = digest.digest(bytes); - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - BasicDBObject doc = new BasicDBObject(ID, new String(Hex.encodeHex(bytes))) - .append(SUBJECT, statement.getSubject().getData()) - .append(PREDICATE, statement.getPredicate().getData()) - .append(OBJECT, statement.getObject().getData()) - .append(OBJECT_TYPE, statement.getObject().getDataType().toString()) - .append(CONTEXT, context); - return doc; - - } - - @Override - public DBObject getQuery(RyaQuery ryaQuery) { - return getQuery(ryaQuery.getQuery()); - } +import mvm.rya.api.domain.RyaStatement; +import mvm.rya.api.domain.RyaType; +import mvm.rya.api.domain.RyaURI; +import mvm.rya.api.persist.query.RyaQuery; +/** + * Defines how {@link RyaStatement}s are stored in MongoDB. + */ +public class SimpleMongoDBStorageStrategy implements MongoDBStorageStrategy { + private static final Logger LOG = Logger.getLogger(SimpleMongoDBStorageStrategy.class); + protected static final String ID = "_id"; + protected static final String OBJECT_TYPE = "objectType"; + protected static final String OBJECT_TYPE_VALUE = XMLSchema.ANYURI.stringValue(); + protected static final String CONTEXT = "context"; + protected static final String PREDICATE = "predicate"; + protected static final String OBJECT = "object"; + protected static final String SUBJECT = "subject"; + public static final String TIMESTAMP = "insertTimestamp"; + protected ValueFactoryImpl factory = new ValueFactoryImpl(); + + @Override + public void createIndices(final DBCollection coll){ + BasicDBObject doc = new BasicDBObject(); + doc.put(SUBJECT, 1); + doc.put(PREDICATE, 1); + coll.createIndex(doc); + doc = new BasicDBObject(PREDICATE, 1); + doc.put(OBJECT, 1); + doc.put(OBJECT_TYPE, 1); + coll.createIndex(doc); + doc = new BasicDBObject(OBJECT, 1); + doc = new BasicDBObject(OBJECT_TYPE, 1); + doc.put(SUBJECT, 1); + coll.createIndex(doc); + } + + @Override + public DBObject getQuery(final RyaStatement stmt) { + final RyaURI subject = stmt.getSubject(); + final RyaURI predicate = stmt.getPredicate(); + final RyaType object = stmt.getObject(); + final RyaURI context = stmt.getContext(); + final BasicDBObject query = new BasicDBObject(); + if (subject != null){ + query.append(SUBJECT, subject.getData()); + } + if (object != null){ + query.append(OBJECT, object.getData()); + query.append(OBJECT_TYPE, object.getDataType().toString()); + } + if (predicate != null){ + query.append(PREDICATE, predicate.getData()); + } + if (context != null){ + query.append(CONTEXT, context.getData()); + } + + return query; + } + + @Override + public RyaStatement deserializeDBObject(final DBObject queryResult) { + final Map result = queryResult.toMap(); + final String subject = (String) result.get(SUBJECT); + final String object = (String) result.get(OBJECT); + final String objectType = (String) result.get(OBJECT_TYPE); + final String predicate = (String) result.get(PREDICATE); + final String context = (String) result.get(CONTEXT); + final Long timestamp = (Long) result.get(TIMESTAMP); + RyaType objectRya = null; + if (objectType.equalsIgnoreCase(ANYURI.stringValue())){ + objectRya = new RyaURI(object); + } + else { + objectRya = new RyaType(factory.createURI(objectType), object); + } + + final RyaStatement statement; + if (!context.isEmpty()){ + statement = new RyaStatement(new RyaURI(subject), new RyaURI(predicate), objectRya, + new RyaURI(context)); + } else { + statement = new RyaStatement(new RyaURI(subject), new RyaURI(predicate), objectRya); + } + + if(timestamp != null) { + statement.setTimestamp(timestamp); + } + return statement; + } + + @Override + public DBObject serialize(final RyaStatement statement){ + return serializeInternal(statement); + } + + public BasicDBObject serializeInternal(final RyaStatement statement){ + String context = ""; + if (statement.getContext() != null){ + context = statement.getContext().getData(); + } + final String id = statement.getSubject().getData() + " " + + statement.getPredicate().getData() + " " + statement.getObject().getData() + " " + context; + byte[] bytes = id.getBytes(); + try { + final MessageDigest digest = MessageDigest.getInstance("SHA-1"); + bytes = digest.digest(bytes); + } catch (final NoSuchAlgorithmException e) { + LOG.error("Unable to perform SHA-1 on the ID, defaulting to raw bytes.", e); + } + final BasicDBObject doc = new BasicDBObject(ID, new String(Hex.encodeHex(bytes))) + .append(SUBJECT, statement.getSubject().getData()) + .append(PREDICATE, statement.getPredicate().getData()) + .append(OBJECT, statement.getObject().getData()) + .append(OBJECT_TYPE, statement.getObject().getDataType().toString()) + .append(CONTEXT, context) + .append(TIMESTAMP, statement.getTimestamp()); + return doc; + + } + + @Override + public DBObject getQuery(final RyaQuery ryaQuery) { + return getQuery(ryaQuery.getQuery()); + } } http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2de66c1f/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/MongoDBRyaDAOIT.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/MongoDBRyaDAOIT.java b/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/MongoDBRyaDAOIT.java new file mode 100644 index 0000000..33531b5 --- /dev/null +++ b/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/MongoDBRyaDAOIT.java @@ -0,0 +1,139 @@ +package mvm.rya.mongodb; +/* + * 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. + */ + +import static mvm.rya.mongodb.dao.SimpleMongoDBStorageStrategy.TIMESTAMP; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; + +import org.apache.hadoop.conf.Configuration; +import org.junit.Before; +import org.junit.Test; + +import com.mongodb.DB; +import com.mongodb.DBCollection; +import com.mongodb.DBObject; +import com.mongodb.MongoClient; +import com.mongodb.MongoException; + +import de.flapdoodle.embed.mongo.distribution.Version; +import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory; +import mvm.rya.api.RdfCloudTripleStoreConfiguration; +import mvm.rya.api.domain.RyaStatement; +import mvm.rya.api.domain.RyaStatement.RyaStatementBuilder; +import mvm.rya.api.domain.RyaURI; +import mvm.rya.api.persist.RyaDAOException; + +public class MongoDBRyaDAOIT { + + private MongodForTestsFactory testsFactory; + private MongoDBRyaDAO dao; + private MongoDBRdfConfiguration configuration; + private MongoClient mongoClient; + + @Before + public void setUp() throws IOException, RyaDAOException{ + testsFactory = MongodForTestsFactory.with(Version.Main.PRODUCTION); + final Configuration conf = new Configuration(); + conf.set(MongoDBRdfConfiguration.USE_TEST_MONGO, "true"); + 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); + mongoClient = testsFactory.newMongo(); + final int port = mongoClient.getServerAddressList().get(0).getPort(); + configuration.set(MongoDBRdfConfiguration.MONGO_INSTANCE_PORT, Integer.toString(port)); + 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 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()); + + dao.add(builder.build()); + + assertEquals(coll.count(),1); + + final DBObject dbo = coll.findOne(); + assertTrue(dbo.containsField(TIMESTAMP)); + } + + @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()); + + dao.add(statement); + + assertEquals(coll.count(),1); + + dao.delete(statement, configuration); + + 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(); + + final DB db = mongoClient.getDB(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); + final DBCollection coll = db.getCollection(configuration.getTriplesCollectionName()); + + 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(); + + dao.delete(query, configuration); + + assertEquals(coll.count(),1); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2de66c1f/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/MongoDBRyaDAOTest.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/MongoDBRyaDAOTest.java b/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/MongoDBRyaDAOTest.java deleted file mode 100644 index 86c01e2..0000000 --- a/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/MongoDBRyaDAOTest.java +++ /dev/null @@ -1,139 +0,0 @@ -package mvm.rya.mongodb; -/* - * 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. - */ - -import static org.junit.Assert.assertEquals; - -import java.io.IOException; - -import mvm.rya.api.RdfCloudTripleStoreConfiguration; -import mvm.rya.api.domain.RyaStatement; -import mvm.rya.api.domain.RyaStatement.RyaStatementBuilder; -import mvm.rya.api.domain.RyaURI; -import mvm.rya.api.persist.RyaDAOException; - -import org.apache.hadoop.conf.Configuration; -import org.junit.Before; -import org.junit.Test; - -import com.mongodb.DB; -import com.mongodb.DBCollection; -import com.mongodb.MongoClient; -import com.mongodb.MongoException; - -import de.flapdoodle.embed.mongo.distribution.Version; -import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory; - -public class MongoDBRyaDAOTest { - - private MongodForTestsFactory testsFactory; - private MongoDBRyaDAO dao; - private MongoDBRdfConfiguration configuration; - private MongoClient mongoClient; - - @Before - public void setUp() throws IOException, RyaDAOException{ - testsFactory = MongodForTestsFactory.with(Version.Main.PRODUCTION); - Configuration conf = new Configuration(); - conf.set(MongoDBRdfConfiguration.USE_TEST_MONGO, "true"); - 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); - mongoClient = testsFactory.newMongo(); - int port = mongoClient.getServerAddressList().get(0).getPort(); - configuration.set(MongoDBRdfConfiguration.MONGO_INSTANCE_PORT, Integer.toString(port)); - dao = new MongoDBRyaDAO(configuration, mongoClient); - - } - - @Test - public void testDeleteWildcard() throws RyaDAOException { - RyaStatementBuilder builder = new RyaStatementBuilder(); - builder.setPredicate(new RyaURI("http://temp.com")); - dao.delete(builder.build(), configuration); - } - - - @Test - public void testAdd() throws RyaDAOException, MongoException, IOException { - 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")); - - DB db = mongoClient.getDB(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); - DBCollection coll = db.getCollection(configuration.getTriplesCollectionName()); - - dao.add(builder.build()); - - assertEquals(coll.count(),1); - - } - - @Test - public void testDelete() throws RyaDAOException, MongoException, IOException { - 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")); - RyaStatement statement = builder.build(); - - DB db = mongoClient.getDB(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); - DBCollection coll = db.getCollection(configuration.getTriplesCollectionName()); - - dao.add(statement); - - assertEquals(coll.count(),1); - - dao.delete(statement, configuration); - - assertEquals(coll.count(),0); - - } - - @Test - public void testDeleteWildcardSubjectWithContext() throws RyaDAOException, MongoException, IOException { - 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")); - RyaStatement statement = builder.build(); - - DB db = mongoClient.getDB(configuration.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); - DBCollection coll = db.getCollection(configuration.getTriplesCollectionName()); - - dao.add(statement); - - assertEquals(coll.count(),1); - - 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")); - RyaStatement query = builder2.build(); - - dao.delete(query, configuration); - - assertEquals(coll.count(),1); - - } - -} http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/2de66c1f/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/SimpleMongoDBStorageStrategyTest.java ---------------------------------------------------------------------- diff --git a/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/SimpleMongoDBStorageStrategyTest.java b/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/SimpleMongoDBStorageStrategyTest.java new file mode 100644 index 0000000..be5fdb7 --- /dev/null +++ b/dao/mongodb.rya/src/test/java/mvm/rya/mongodb/SimpleMongoDBStorageStrategyTest.java @@ -0,0 +1,85 @@ +package mvm.rya.mongodb; +/* + * 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. + */ + +import static org.junit.Assert.assertEquals; +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 mvm.rya.api.domain.RyaStatement; +import mvm.rya.api.domain.RyaStatement.RyaStatementBuilder; +import mvm.rya.api.domain.RyaURI; +import mvm.rya.api.persist.RyaDAOException; +import mvm.rya.mongodb.dao.SimpleMongoDBStorageStrategy; + +public class SimpleMongoDBStorageStrategyTest { + private static final String SUBJECT = "http://subject.com"; + private static final String PREDICATE = "http://temp.com"; + private static final String OBJECT = "http://object.com"; + private static final String CONTEXT = "http://context.com"; + + private static final RyaStatement testStatement; + private static final DBObject testDBO; + private final SimpleMongoDBStorageStrategy storageStrategy = new SimpleMongoDBStorageStrategy(); + + static { + final RyaStatementBuilder builder = new RyaStatementBuilder(); + builder.setPredicate(new RyaURI(PREDICATE)); + builder.setSubject(new RyaURI(SUBJECT)); + builder.setObject(new RyaURI(OBJECT)); + builder.setContext(new RyaURI(CONTEXT)); + 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("insertTimestamp", null); + } + + @Test + public void testSerializeStatementToDBO() throws RyaDAOException, MongoException, IOException { + + final DBObject dbo = storageStrategy.serialize(testStatement); + assertEquals(testDBO, dbo); + } + + @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. + */ + statement.setTimestamp(null); + assertEquals(testStatement, statement); + } +}
