Repository: incubator-rya Updated Branches: refs/heads/master 2396ebb87 -> ea91e26a5
RYA-494 Closes #295, Fixed a bug where the shell was not loading or displaying all Statements. Project: http://git-wip-us.apache.org/repos/asf/incubator-rya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-rya/commit/ea91e26a Tree: http://git-wip-us.apache.org/repos/asf/incubator-rya/tree/ea91e26a Diff: http://git-wip-us.apache.org/repos/asf/incubator-rya/diff/ea91e26a Branch: refs/heads/master Commit: ea91e26a51bc8bff601d49117f99c8608059181b Parents: 2396ebb Author: kchilton2 <[email protected]> Authored: Tue May 8 18:33:24 2018 -0400 Committer: Valiyil <[email protected]> Committed: Wed May 16 11:51:11 2018 -0400 ---------------------------------------------------------------------- .../accumulo/AccumuloExecuteSparqlQuery.java | 2 +- .../accumulo/AccumuloExecuteSparqlQueryIT.java | 144 +++++++++++++++++++ .../src/test/resources/test-statements.nt | 22 +++ .../java/org/apache/rya/shell/RyaCommands.java | 2 +- .../apache/rya/shell/AccumuloRyaCommandsIT.java | 91 ++++++++++++ .../shell/src/test/resources/test-statements.nt | 22 +++ 6 files changed, 281 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/ea91e26a/extras/indexing/src/main/java/org/apache/rya/api/client/accumulo/AccumuloExecuteSparqlQuery.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/main/java/org/apache/rya/api/client/accumulo/AccumuloExecuteSparqlQuery.java b/extras/indexing/src/main/java/org/apache/rya/api/client/accumulo/AccumuloExecuteSparqlQuery.java index b97ae8a..e226260 100644 --- a/extras/indexing/src/main/java/org/apache/rya/api/client/accumulo/AccumuloExecuteSparqlQuery.java +++ b/extras/indexing/src/main/java/org/apache/rya/api/client/accumulo/AccumuloExecuteSparqlQuery.java @@ -93,7 +93,7 @@ public class AccumuloExecuteSparqlQuery extends AccumuloCommand implements Execu sailRepo = new SailRepository(sail); sailRepoConn = sailRepo.getConnection(); - // Execute the query. + // Execute the query. final TupleQuery tupleQuery = sailRepoConn.prepareTupleQuery(QueryLanguage.SPARQL, sparqlQuery); return tupleQuery.evaluate(); } catch (final SailException | AccumuloException | AccumuloSecurityException | RyaDAOException | InferenceEngineException e) { http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/ea91e26a/extras/indexing/src/test/java/org/apache/rya/api/client/accumulo/AccumuloExecuteSparqlQueryIT.java ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/java/org/apache/rya/api/client/accumulo/AccumuloExecuteSparqlQueryIT.java b/extras/indexing/src/test/java/org/apache/rya/api/client/accumulo/AccumuloExecuteSparqlQueryIT.java new file mode 100644 index 0000000..8c9d67a --- /dev/null +++ b/extras/indexing/src/test/java/org/apache/rya/api/client/accumulo/AccumuloExecuteSparqlQueryIT.java @@ -0,0 +1,144 @@ +/** + * 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.api.client.accumulo; + +import static org.junit.Assert.assertEquals; + +import java.nio.file.Paths; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +import org.apache.rya.api.RdfCloudTripleStoreConstants; +import org.apache.rya.api.client.Install.InstallConfiguration; +import org.apache.rya.api.client.RyaClient; +import org.apache.rya.test.accumulo.AccumuloITBase; +import org.eclipse.rdf4j.model.IRI; +import org.eclipse.rdf4j.model.Resource; +import org.eclipse.rdf4j.model.Statement; +import org.eclipse.rdf4j.model.ValueFactory; +import org.eclipse.rdf4j.model.impl.SimpleValueFactory; +import org.eclipse.rdf4j.query.BindingSet; +import org.eclipse.rdf4j.query.TupleQueryResult; +import org.eclipse.rdf4j.rio.RDFFormat; +import org.junit.Test; + +import com.google.common.collect.Sets; + +/** + * Integration tests for the methods of {@link AccumuloExecuteSparqlQueryIT}. + */ +public class AccumuloExecuteSparqlQueryIT extends AccumuloITBase { + + @Test + public void queryFindsAllLoadedStatements_fromSet() throws Exception { + // Using the Rya Client, install an instance of Rya for the test. + final AccumuloConnectionDetails connectionDetails = new AccumuloConnectionDetails( + getUsername(), + getPassword().toCharArray(), + getInstanceName(), + getZookeepers()); + + final RyaClient client = AccumuloRyaClientFactory.build(connectionDetails, super.getConnector()); + + final String ryaInstance = UUID.randomUUID().toString().replace('-', '_'); + client.getInstall().install(ryaInstance, InstallConfiguration.builder().build()); + + // Load some data into the instance. + final ValueFactory vf = SimpleValueFactory.getInstance(); + final Set<Statement> statements = Sets.newHashSet( + vf.createStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob")), + vf.createStatement(vf.createIRI("urn:Bob"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Alice")), + vf.createStatement(vf.createIRI("urn:Bob"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Charlie")), + vf.createStatement(vf.createIRI("urn:Charlie"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Alice")), + vf.createStatement(vf.createIRI("urn:David"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Eve")), + vf.createStatement(vf.createIRI("urn:Eve"), vf.createIRI("urn:listensTo"), vf.createIRI("urn:Bob"))); + client.getLoadStatements().loadStatements(ryaInstance, statements); + + // Execute a query. + final Set<Statement> fetched = new HashSet<>(); + try(final TupleQueryResult result = client.getExecuteSparqlQuery().executeSparqlQuery(ryaInstance, "SELECT * WHERE { ?s ?p ?o }")) { + while(result.hasNext()) { + final BindingSet bs = result.next(); + + // If this is the statement that indicates the Rya version. + if(RdfCloudTripleStoreConstants.RTS_VERSION_PREDICATE.equals(bs.getBinding("p").getValue())) { + continue; + } + + // Otherwise add it to the list of fetched statements. + fetched.add( vf.createStatement( + (Resource)bs.getBinding("s").getValue(), + (IRI)bs.getBinding("p").getValue(), + bs.getBinding("o").getValue()) ); + } + } + + // Show it resulted in the expected results. + assertEquals(statements, fetched); + } + + @Test + public void queryFindsAllLoadedStatements_fromFile() throws Exception { + // Using the Rya Client, install an instance of Rya for the test. + final AccumuloConnectionDetails connectionDetails = new AccumuloConnectionDetails( + getUsername(), + getPassword().toCharArray(), + getInstanceName(), + getZookeepers()); + + final RyaClient client = AccumuloRyaClientFactory.build(connectionDetails, super.getConnector()); + + final String ryaInstance = UUID.randomUUID().toString().replace('-', '_'); + client.getInstall().install(ryaInstance, InstallConfiguration.builder().build()); + + // Load some data into the instance from a file. + client.getLoadStatementsFile().loadStatements(ryaInstance, Paths.get("src/test/resources/test-statements.nt"), RDFFormat.NTRIPLES); + + // Execute a query. + final ValueFactory vf = SimpleValueFactory.getInstance(); + final Set<Statement> fetched = new HashSet<>(); + try(final TupleQueryResult result = client.getExecuteSparqlQuery().executeSparqlQuery(ryaInstance, "SELECT * WHERE { ?s ?p ?o }")) { + while(result.hasNext()) { + final BindingSet bs = result.next(); + + // If this is the statement that indicates the Rya version + if(RdfCloudTripleStoreConstants.RTS_VERSION_PREDICATE.equals(bs.getBinding("p").getValue())) { + continue; + } + + // Otherwise add it to the list of fetched statements. + fetched.add( vf.createStatement( + (Resource)bs.getBinding("s").getValue(), + (IRI)bs.getBinding("p").getValue(), + bs.getBinding("o").getValue()) ); + } + } + + // Show it resulted in the expected results. + final Set<Statement> expected = Sets.newHashSet( + vf.createStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob")), + vf.createStatement(vf.createIRI("urn:Bob"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Alice")), + vf.createStatement(vf.createIRI("urn:Bob"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Charlie")), + vf.createStatement(vf.createIRI("urn:Charlie"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Alice")), + vf.createStatement(vf.createIRI("urn:David"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Eve")), + vf.createStatement(vf.createIRI("urn:Eve"), vf.createIRI("urn:listensTo"), vf.createIRI("urn:Bob"))); + assertEquals(expected, fetched); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/ea91e26a/extras/indexing/src/test/resources/test-statements.nt ---------------------------------------------------------------------- diff --git a/extras/indexing/src/test/resources/test-statements.nt b/extras/indexing/src/test/resources/test-statements.nt new file mode 100644 index 0000000..db0d51f --- /dev/null +++ b/extras/indexing/src/test/resources/test-statements.nt @@ -0,0 +1,22 @@ +# 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. +<urn:Alice> <urn:talksTo> <urn:Bob>. +<urn:Bob> <urn:talksTo> <urn:Alice>. +<urn:Bob> <urn:talksTo> <urn:Charlie>. +<urn:Charlie> <urn:talksTo> <urn:Alice>. +<urn:David> <urn:talksTo> <urn:Eve>. +<urn:Eve> <urn:listensTo> <urn:Bob>. \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/ea91e26a/extras/shell/src/main/java/org/apache/rya/shell/RyaCommands.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/main/java/org/apache/rya/shell/RyaCommands.java b/extras/shell/src/main/java/org/apache/rya/shell/RyaCommands.java index c257860..5004be3 100644 --- a/extras/shell/src/main/java/org/apache/rya/shell/RyaCommands.java +++ b/extras/shell/src/main/java/org/apache/rya/shell/RyaCommands.java @@ -185,7 +185,7 @@ public class RyaCommands implements CommandMarker { if(rezIter.hasNext()) { consolePrinter.println("Query Results:"); final BindingSet bs = rezIter.next(); - for(final String name : rezIter.next().getBindingNames()) { + for(final String name : bs.getBindingNames()) { bindings.add(name); } consolePrinter.println(Strings.join(bindings, ",")); http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/ea91e26a/extras/shell/src/test/java/org/apache/rya/shell/AccumuloRyaCommandsIT.java ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/java/org/apache/rya/shell/AccumuloRyaCommandsIT.java b/extras/shell/src/test/java/org/apache/rya/shell/AccumuloRyaCommandsIT.java new file mode 100644 index 0000000..8036125 --- /dev/null +++ b/extras/shell/src/test/java/org/apache/rya/shell/AccumuloRyaCommandsIT.java @@ -0,0 +1,91 @@ +/** + * 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.shell; + +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import org.apache.accumulo.minicluster.MiniAccumuloCluster; +import org.apache.rya.api.client.Install.InstallConfiguration; +import org.apache.rya.shell.util.InstallPrompt; +import org.apache.rya.shell.util.PasswordPrompt; +import org.apache.rya.shell.util.SparqlPrompt; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.shell.Bootstrap; +import org.springframework.shell.core.CommandResult; +import org.springframework.shell.core.JLineShellComponent; + +import com.google.common.base.Optional; + +/** + * Integration tests for the methods of {@link RyaCommands}. + */ +public class AccumuloRyaCommandsIT extends RyaShellAccumuloITBase { + + @Test + public void loadsAndQueryData() throws Exception { + final MiniAccumuloCluster cluster = getCluster(); + final Bootstrap bootstrap = getTestBootstrap(); + final JLineShellComponent shell = getTestShell(); + + // Mock the user entering the correct password. + final ApplicationContext context = bootstrap.getApplicationContext(); + final PasswordPrompt mockPrompt = context.getBean( PasswordPrompt.class ); + when(mockPrompt.getPassword()).thenReturn("password".toCharArray()); + + // Connect to the mini accumulo instance. + String cmd = + RyaConnectionCommands.CONNECT_ACCUMULO_CMD + " " + + "--username root " + + "--instanceName " + cluster.getInstanceName() + " "+ + "--zookeepers " + cluster.getZooKeepers(); + CommandResult result = shell.executeCommand(cmd); + + // Install an instance of rya. + final String instanceName = "testInstance"; + final InstallConfiguration installConf = InstallConfiguration.builder().build(); + + final InstallPrompt installPrompt = context.getBean( InstallPrompt.class ); + when(installPrompt.promptInstanceName()).thenReturn("testInstance"); + when(installPrompt.promptInstallConfiguration("testInstance")).thenReturn( installConf ); + when(installPrompt.promptVerified(instanceName, installConf)).thenReturn(true); + + result = shell.executeCommand( RyaAdminCommands.INSTALL_CMD ); + assertTrue( result.isSuccess() ); + + // Connect to the instance that was just installed. + cmd = RyaConnectionCommands.CONNECT_INSTANCE_CMD + " --instance " + instanceName; + result = shell.executeCommand(cmd); + assertTrue( result.isSuccess() ); + + // Load a statements file into the instance. + cmd = RyaCommands.LOAD_DATA_CMD + " --file src/test/resources/test-statements.nt"; + result = shell.executeCommand(cmd); + assertTrue( result.isSuccess() ); + + // Query for all of the statements that were loaded. + final SparqlPrompt sparqlPrompt = context.getBean(SparqlPrompt.class); + when(sparqlPrompt.getSparql()).thenReturn(Optional.of("select * where { ?s ?p ?o .}")); + + cmd = RyaCommands.SPARQL_QUERY_CMD; + result = shell.executeCommand(cmd); + assertTrue( result.isSuccess() ); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rya/blob/ea91e26a/extras/shell/src/test/resources/test-statements.nt ---------------------------------------------------------------------- diff --git a/extras/shell/src/test/resources/test-statements.nt b/extras/shell/src/test/resources/test-statements.nt new file mode 100644 index 0000000..db0d51f --- /dev/null +++ b/extras/shell/src/test/resources/test-statements.nt @@ -0,0 +1,22 @@ +# 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. +<urn:Alice> <urn:talksTo> <urn:Bob>. +<urn:Bob> <urn:talksTo> <urn:Alice>. +<urn:Bob> <urn:talksTo> <urn:Charlie>. +<urn:Charlie> <urn:talksTo> <urn:Alice>. +<urn:David> <urn:talksTo> <urn:Eve>. +<urn:Eve> <urn:listensTo> <urn:Bob>. \ No newline at end of file
