From 0e5024f1e4f2d12509e87b292c1fc899ef3c9ad4 Mon Sep 17 00:00:00 2001 From: "eric.white" Date: Fri, 5 Jan 2018 11:38:48 -0500 Subject: [PATCH] RYA-413 Fixed how MongoDBRyaDAO closed its internal mongoClient. Updated AbstractMongoDBRdfConfigurationBuilder config tags. --- ...bstractMongoDBRdfConfigurationBuilder.java | 26 +-- .../org/apache/rya/mongodb/MongoDBRyaDAO.java | 3 - .../apache/rya/mongodb/MongoDBRyaDAO2IT.java | 65 ++++++++ .../apache/rya/mongodb/MongoDBRyaDAOIT.java | 61 +++++++ .../config/RyaMongoDbSailFactoryTest.java | 149 ++++++++++++++++++ 5 files changed, 288 insertions(+), 16 deletions(-) create mode 100644 extras/indexing/src/test/java/org/apache/rya/sail/config/RyaMongoDbSailFactoryTest.java diff --git a/dao/mongodb.rya/src/main/java/org/apache/rya/mongodb/AbstractMongoDBRdfConfigurationBuilder.java b/dao/mongodb.rya/src/main/java/org/apache/rya/mongodb/AbstractMongoDBRdfConfigurationBuilder.java index 369f7a0d8..8afcb426f 100644 --- a/dao/mongodb.rya/src/main/java/org/apache/rya/mongodb/AbstractMongoDBRdfConfigurationBuilder.java +++ b/dao/mongodb.rya/src/main/java/org/apache/rya/mongodb/AbstractMongoDBRdfConfigurationBuilder.java @@ -40,23 +40,23 @@ public abstract class AbstractMongoDBRdfConfigurationBuilder coll = db.getCollection(conf.getTriplesCollectionName()); + + dao.add(builder.build()); + + assertEquals(coll.count(), 1); + + final Document dbo = coll.find().first(); + assertTrue(dbo.containsKey(DOCUMENT_VISIBILITY)); + assertTrue(dbo.containsKey(TIMESTAMP)); + } finally { + dao.destroy(); + } + + // Test reinitializing the same instance + try { + dao.init(); + } finally { + dao.destroy(); + } + + // Reconstruct new DAO and try again + dao = new MongoDBRyaDAO(); + try { + dao.setConf(conf); + dao.init(); + + 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.setColumnVisibility(new DocumentVisibility("B").flatten()); + + final MongoDatabase db = conf.getMongoClient().getDatabase(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); + final MongoCollection coll = db.getCollection(conf.getTriplesCollectionName()); + + dao.add(builder.build()); + + assertEquals(coll.count(), 1); + + final Document dbo = coll.find().first(); + assertTrue(dbo.containsKey(DOCUMENT_VISIBILITY)); + assertTrue(dbo.containsKey(TIMESTAMP)); + } finally { + dao.destroy(); + } + } } \ No newline at end of file diff --git a/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/MongoDBRyaDAOIT.java b/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/MongoDBRyaDAOIT.java index a05393ced..c5ff223cc 100644 --- a/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/MongoDBRyaDAOIT.java +++ b/dao/mongodb.rya/src/test/java/org/apache/rya/mongodb/MongoDBRyaDAOIT.java @@ -153,6 +153,67 @@ public void testDeleteWildcardSubjectWithContext() throws RyaDAOException, Mongo } } + @Test + public void testReconstructDao() throws RyaDAOException, IOException { + MongoDBRyaDAO dao = new MongoDBRyaDAO(); + try { + dao.setConf(conf); + dao.init(); + + 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.setColumnVisibility(new DocumentVisibility("B").flatten()); + + final MongoDatabase db = conf.getMongoClient().getDatabase(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); + final MongoCollection coll = db.getCollection(conf.getTriplesCollectionName()); + + dao.add(builder.build()); + + assertEquals(coll.count(), 1); + + final Document dbo = coll.find().first(); + assertTrue(dbo.containsKey(DOCUMENT_VISIBILITY)); + assertTrue(dbo.containsKey(TIMESTAMP)); + } finally { + dao.destroy(); + } + + // Test reinitializing the same instance + try { + dao.init(); + } finally { + dao.destroy(); + } + + // Reconstruct new DAO and try again + dao = new MongoDBRyaDAO(); + try { + dao.setConf(conf); + dao.init(); + + 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.setColumnVisibility(new DocumentVisibility("B").flatten()); + + final MongoDatabase db = conf.getMongoClient().getDatabase(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME)); + final MongoCollection coll = db.getCollection(conf.getTriplesCollectionName()); + + dao.add(builder.build()); + + assertEquals(coll.count(), 1); + + final Document dbo = coll.find().first(); + assertTrue(dbo.containsKey(DOCUMENT_VISIBILITY)); + assertTrue(dbo.containsKey(TIMESTAMP)); + } finally { + dao.destroy(); + } + } + @Test public void testVisibility() throws RyaDAOException, MongoException, IOException { final MongoDBRyaDAO dao = new MongoDBRyaDAO(); diff --git a/extras/indexing/src/test/java/org/apache/rya/sail/config/RyaMongoDbSailFactoryTest.java b/extras/indexing/src/test/java/org/apache/rya/sail/config/RyaMongoDbSailFactoryTest.java new file mode 100644 index 000000000..6cfd4ea28 --- /dev/null +++ b/extras/indexing/src/test/java/org/apache/rya/sail/config/RyaMongoDbSailFactoryTest.java @@ -0,0 +1,149 @@ +/* + * 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.sail.config; + +import static org.junit.Assert.assertFalse; + +import org.apache.rya.mongodb.MongoITBase; +import org.junit.Assert; +import org.junit.Test; +import org.openrdf.model.Statement; +import org.openrdf.model.ValueFactory; +import org.openrdf.repository.sail.SailRepository; +import org.openrdf.repository.sail.SailRepositoryConnection; +import org.openrdf.sail.Sail; + +/** + * Tests {@link RyaSailFactory} with a MongoDB backend. + */ +public class RyaMongoDbSailFactoryTest extends MongoITBase { + @Test + public void testCreateMongoDbSail() throws Exception { + Sail sail = null; + SailRepository repo = null; + SailRepositoryConnection conn = null; + try { + sail = RyaSailFactory.getInstance(conf); + repo = new SailRepository(sail); + conn = repo.getConnection(); + } finally { + if (conn != null) { + conn.close(); + } + if (repo != null) { + repo.shutDown(); + } + if (sail != null) { + sail.shutDown(); + } + } + } + + @Test + public void testAddStatement() throws Exception { + Sail sail = null; + SailRepository repo = null; + SailRepositoryConnection conn = null; + try { + sail = RyaSailFactory.getInstance(conf); + repo = new SailRepository(sail); + conn = repo.getConnection(); + + final ValueFactory vf = conn.getValueFactory(); + final Statement s = vf.createStatement(vf.createURI("u:a"), vf.createURI("u:b"), vf.createURI("u:c")); + + assertFalse(conn.hasStatement(s, false)); + + conn.add(s); + + Assert.assertTrue(conn.hasStatement(s, false)); + } finally { + if (conn != null) { + conn.close(); + } + if (repo != null) { + repo.shutDown(); + } + if (sail != null) { + sail.shutDown(); + } + } + } + + @Test + public void testReuseSail() throws Exception { + Sail sail = null; + SailRepository repo = null; + SailRepositoryConnection conn = null; + try { + sail = RyaSailFactory.getInstance(conf); + repo = new SailRepository(sail); + conn = repo.getConnection(); + + final ValueFactory vf = conn.getValueFactory(); + final Statement s = vf.createStatement(vf.createURI("u:a"), vf.createURI("u:b"), vf.createURI("u:c")); + + assertFalse(conn.hasStatement(s, false)); + + conn.add(s); + + Assert.assertTrue(conn.hasStatement(s, false)); + + conn.remove(s); + + Assert.assertFalse(conn.hasStatement(s, false)); + } finally { + if (conn != null) { + conn.close(); + } + if (repo != null) { + repo.shutDown(); + } + if (sail != null) { + sail.shutDown(); + } + } + + // Reuse Sail after shutdown + try { + sail = RyaSailFactory.getInstance(conf); + repo = new SailRepository(sail); + conn = repo.getConnection(); + + final ValueFactory vf = conn.getValueFactory(); + final Statement s = vf.createStatement(vf.createURI("u:a"), vf.createURI("u:b"), vf.createURI("u:c")); + + assertFalse(conn.hasStatement(s, false)); + + conn.add(s); + + Assert.assertTrue(conn.hasStatement(s, false)); + } finally { + if (conn != null) { + conn.close(); + } + if (repo != null) { + repo.shutDown(); + } + if (sail != null) { + sail.shutDown(); + } + } + } +} \ No newline at end of file