|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.rya.indexing; |
| 20 | + |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertNotEquals; |
| 23 | +import static org.junit.Assert.assertNotNull; |
| 24 | +import static org.junit.Assert.assertNull; |
| 25 | + |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import org.apache.accumulo.core.client.AccumuloException; |
| 30 | +import org.apache.accumulo.core.client.AccumuloSecurityException; |
| 31 | +import org.apache.accumulo.core.client.Connector; |
| 32 | +import org.apache.rya.accumulo.AccumuloRdfConfiguration; |
| 33 | +import org.apache.rya.accumulo.AccumuloRyaDAO; |
| 34 | +import org.apache.rya.api.RdfCloudTripleStoreConfiguration; |
| 35 | +import org.apache.rya.api.persist.RyaDAOException; |
| 36 | +import org.apache.rya.indexing.accumulo.ConfigUtils; |
| 37 | +import org.apache.rya.rdftriplestore.inference.InferenceEngineException; |
| 38 | +import org.apache.rya.sail.config.RyaSailFactory; |
| 39 | +import org.eclipse.rdf4j.model.Statement; |
| 40 | +import org.eclipse.rdf4j.model.ValueFactory; |
| 41 | +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; |
| 42 | +import org.eclipse.rdf4j.model.vocabulary.RDFS; |
| 43 | +import org.eclipse.rdf4j.query.BindingSet; |
| 44 | +import org.eclipse.rdf4j.query.QueryLanguage; |
| 45 | +import org.eclipse.rdf4j.query.TupleQueryResult; |
| 46 | +import org.eclipse.rdf4j.repository.Repository; |
| 47 | +import org.eclipse.rdf4j.repository.RepositoryConnection; |
| 48 | +import org.eclipse.rdf4j.repository.sail.SailRepository; |
| 49 | +import org.eclipse.rdf4j.sail.Sail; |
| 50 | +import org.junit.Test; |
| 51 | + |
| 52 | +public class IndexingFunctionRegistryFreeTextTest { |
| 53 | + |
| 54 | + private final static String STRING_LENGTH_QUERY = "select (strlen(?o) AS ?o_len) WHERE { ?s <http://worksAt> ?o } LIMIT 1"; |
| 55 | + |
| 56 | + private Sail accumuloSail; |
| 57 | + private Repository accumuloRepo; |
| 58 | + private RepositoryConnection accumuloConn; |
| 59 | + private AccumuloRyaDAO accumuloDao; |
| 60 | + |
| 61 | + @Test |
| 62 | + public void accumuloConnTest1() throws Exception { |
| 63 | + this.init(getAccumuloConf(true)); |
| 64 | + this.sparqlConnTest(this.accumuloConn, STRING_LENGTH_QUERY); |
| 65 | + this.close(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void accumuloConnTest2() throws Exception { |
| 70 | + this.init(getAccumuloConf(false)); |
| 71 | + this.sparqlConnTest(this.accumuloConn, STRING_LENGTH_QUERY); |
| 72 | + this.close(); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + private void init(RdfCloudTripleStoreConfiguration conf) throws AccumuloSecurityException, InferenceEngineException, AccumuloException, RyaDAOException { |
| 77 | + |
| 78 | + this.accumuloSail = RyaSailFactory.getInstance(conf); |
| 79 | + this.accumuloRepo = new SailRepository(accumuloSail); |
| 80 | + this.accumuloConn = this.accumuloRepo.getConnection(); |
| 81 | + |
| 82 | + Connector conn = ConfigUtils.getConnector(conf); |
| 83 | + this.accumuloDao = new AccumuloRyaDAO(); |
| 84 | + this.accumuloDao.setConnector(conn); |
| 85 | + this.accumuloDao.init(); |
| 86 | + } |
| 87 | + |
| 88 | + private void close() throws RyaDAOException { |
| 89 | + this.accumuloConn.close(); |
| 90 | + this.accumuloRepo.shutDown(); |
| 91 | + this.accumuloSail.shutDown(); |
| 92 | + this.accumuloDao.destroy(); |
| 93 | + } |
| 94 | + |
| 95 | + private RdfCloudTripleStoreConfiguration getAccumuloConf(boolean useFreeText) { |
| 96 | + RdfCloudTripleStoreConfiguration conf; |
| 97 | + conf = new AccumuloRdfConfiguration(); |
| 98 | + conf.setBoolean(ConfigUtils.USE_MOCK_INSTANCE, true); |
| 99 | + conf.set(RdfCloudTripleStoreConfiguration.CONF_TBL_PREFIX, "rya_"); |
| 100 | + conf.set(AccumuloRdfConfiguration.CLOUDBASE_USER, "root"); |
| 101 | + conf.set(AccumuloRdfConfiguration.CLOUDBASE_PASSWORD, ""); |
| 102 | + conf.set(AccumuloRdfConfiguration.CLOUDBASE_INSTANCE, "instance"); |
| 103 | + conf.set(RdfCloudTripleStoreConfiguration.CONF_QUERY_AUTH, ""); |
| 104 | + conf.setBoolean(ConfigUtils.DISPLAY_QUERY_PLAN, true); |
| 105 | + |
| 106 | + conf.setBoolean(ConfigUtils.USE_FREETEXT, useFreeText); |
| 107 | + |
| 108 | + if (useFreeText) { |
| 109 | + conf.setStrings(ConfigUtils.FREETEXT_PREDICATES_LIST, RDFS.LABEL.stringValue()); |
| 110 | + } |
| 111 | + |
| 112 | + return conf; |
| 113 | + } |
| 114 | + |
| 115 | + private void sparqlConnTest(RepositoryConnection conn, String query) throws Exception { |
| 116 | + ValueFactory vf = SimpleValueFactory.getInstance(); |
| 117 | + |
| 118 | + Statement s = vf.createStatement(vf.createIRI("http://Joe"), vf.createIRI("http://worksAt"), vf.createLiteral("CoffeeShop")); |
| 119 | + conn.add(s); |
| 120 | + |
| 121 | + TupleQueryResult result = null; |
| 122 | + Exception e = null; |
| 123 | + List<BindingSet> results = new ArrayList<>(); |
| 124 | + String o_len = ""; |
| 125 | + |
| 126 | + try { |
| 127 | + result = conn.prepareTupleQuery(QueryLanguage.SPARQL, query).evaluate(); |
| 128 | + |
| 129 | + while (result.hasNext()) { |
| 130 | + BindingSet bSet = result.next(); |
| 131 | + o_len = bSet.getValue("o_len").stringValue(); |
| 132 | + results.add(bSet); |
| 133 | + } |
| 134 | + |
| 135 | + } catch (Exception ex) { |
| 136 | + e = ex; |
| 137 | + } |
| 138 | + |
| 139 | + assertNull(e); |
| 140 | + assertNotNull(result); |
| 141 | + assertNotEquals(0, results.size()); |
| 142 | + assertEquals("10", o_len); |
| 143 | + |
| 144 | + conn.remove(s); |
| 145 | + } |
| 146 | +} |
0 commit comments