Search in sources :

Example 1 with ExecuteSparqlQuery

use of org.apache.rya.api.client.ExecuteSparqlQuery in project incubator-rya by apache.

the class RyaCommandsTest method testSparqlQuery_fromPrompt.

@Test
public void testSparqlQuery_fromPrompt() throws InstanceDoesNotExistException, RyaClientException, IOException {
    // Mock the object that performs the create operation.
    final String instanceName = "unitTest";
    final String queryContent = "SELECT * WHERE { ?person <http://isA> ?noun }";
    final String queryFile = null;
    final String expectedMessage = "MockAnswer";
    final ExecuteSparqlQuery mockExecuteSparqlQuery = mock(ExecuteSparqlQuery.class);
    when(mockExecuteSparqlQuery.executeSparqlQuery(instanceName, queryContent)).thenReturn(expectedMessage);
    final RyaClient mockCommands = mock(RyaClient.class);
    when(mockCommands.getExecuteSparqlQuery()).thenReturn(mockExecuteSparqlQuery);
    final SharedShellState state = new SharedShellState();
    state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands);
    state.connectedToInstance(instanceName);
    final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class);
    when(mockSparqlPrompt.getSparql()).thenReturn(Optional.of(queryContent));
    final ConsolePrinter mockConsolePrinter = mock(ConsolePrinter.class);
    // Execute the command.
    final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter);
    final String message = commands.sparqlQuery(queryFile);
    // Verify the values that were provided to the command were passed through to LoadStatementsFile.
    verify(mockExecuteSparqlQuery).executeSparqlQuery(instanceName, queryContent);
    assertEquals(expectedMessage, message);
// Verify a message is returned that explains what was created.
}
Also used : ConsolePrinter(org.apache.rya.shell.util.ConsolePrinter) ExecuteSparqlQuery(org.apache.rya.api.client.ExecuteSparqlQuery) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) RyaClient(org.apache.rya.api.client.RyaClient) Test(org.junit.Test)

Example 2 with ExecuteSparqlQuery

use of org.apache.rya.api.client.ExecuteSparqlQuery in project incubator-rya by apache.

the class RyaCommandsTest method testSparqlQuery.

@Test
public void testSparqlQuery() throws InstanceDoesNotExistException, RyaClientException, IOException {
    // Mock the object that performs the create operation.
    final String instanceName = "unitTest";
    final String queryFile = "src/test/resources/Query1.sparql";
    final String queryContent = FileUtils.readFileToString(new File(queryFile), StandardCharsets.UTF_8);
    final String expectedMessage = "MockAnswer";
    final ExecuteSparqlQuery mockExecuteSparqlQuery = mock(ExecuteSparqlQuery.class);
    when(mockExecuteSparqlQuery.executeSparqlQuery(instanceName, queryContent)).thenReturn(expectedMessage);
    final RyaClient mockCommands = mock(RyaClient.class);
    when(mockCommands.getExecuteSparqlQuery()).thenReturn(mockExecuteSparqlQuery);
    final SharedShellState state = new SharedShellState();
    state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mockCommands);
    state.connectedToInstance(instanceName);
    final SparqlPrompt mockSparqlPrompt = mock(SparqlPrompt.class);
    final ConsolePrinter mockConsolePrinter = mock(ConsolePrinter.class);
    // Execute the command.
    final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter);
    final String message = commands.sparqlQuery(queryFile);
    // Verify the values that were provided to the command were passed through to LoadStatementsFile.
    verify(mockExecuteSparqlQuery).executeSparqlQuery(instanceName, queryContent);
    assertEquals(expectedMessage, message);
// Verify a message is returned that explains what was created.
}
Also used : ConsolePrinter(org.apache.rya.shell.util.ConsolePrinter) ExecuteSparqlQuery(org.apache.rya.api.client.ExecuteSparqlQuery) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) RyaClient(org.apache.rya.api.client.RyaClient) File(java.io.File) LoadStatementsFile(org.apache.rya.api.client.LoadStatementsFile) Test(org.junit.Test)

Example 3 with ExecuteSparqlQuery

use of org.apache.rya.api.client.ExecuteSparqlQuery in project incubator-rya by apache.

the class MongoExecuteSparqlQueryIT method ExecuteSparqlQuery_exec.

@Test
public void ExecuteSparqlQuery_exec() throws MongoException, DuplicateInstanceNameException, RyaClientException {
    // Install an instance of Rya.
    final MongoConnectionDetails connectionDetails = getConnectionDetails();
    final RyaClient ryaClient = MongoRyaClientFactory.build(connectionDetails, getMongoClient());
    final InstallConfiguration installConfig = InstallConfiguration.builder().setEnableTableHashPrefix(false).setEnableEntityCentricIndex(false).setEnableFreeTextIndex(false).setEnableTemporalIndex(false).setEnablePcjIndex(false).setEnableGeoIndex(false).build();
    ryaClient.getInstall().install(conf.getRyaInstanceName(), installConfig);
    // Load some statements into that instance.
    final List<Statement> statements = makeTestStatements();
    ryaClient.getLoadStatements().loadStatements(conf.getRyaInstanceName(), statements);
    // Execute the SPARQL against the Rya instance.
    final ExecuteSparqlQuery executeSparql = ryaClient.getExecuteSparqlQuery();
    final String sparql = "SELECT * where { ?a ?b ?c }";
    final String results = executeSparql.executeSparqlQuery(conf.getRyaInstanceName(), sparql);
    // Show the result matches what is expected.
    assertTrue("result has header.", results.startsWith("Query Result:"));
    assertTrue("result has column headings.", results.contains("a,b,c"));
    assertTrue("result has footer.", results.contains("Retrieved 3 results in"));
    for (final Statement expect : statements) {
        assertTrue("All results should contain expected subjects:", results.contains(expect.getSubject().stringValue()));
        assertTrue("All results should contain expected predicates:", results.contains(expect.getPredicate().stringValue()));
        assertTrue("All results should contain expected objects:", results.contains(expect.getObject().stringValue()));
    }
}
Also used : ExecuteSparqlQuery(org.apache.rya.api.client.ExecuteSparqlQuery) Statement(org.openrdf.model.Statement) RyaClient(org.apache.rya.api.client.RyaClient) InstallConfiguration(org.apache.rya.api.client.Install.InstallConfiguration) Test(org.junit.Test)

Aggregations

ExecuteSparqlQuery (org.apache.rya.api.client.ExecuteSparqlQuery)3 RyaClient (org.apache.rya.api.client.RyaClient)3 Test (org.junit.Test)3 AccumuloConnectionDetails (org.apache.rya.api.client.accumulo.AccumuloConnectionDetails)2 ConsolePrinter (org.apache.rya.shell.util.ConsolePrinter)2 SparqlPrompt (org.apache.rya.shell.util.SparqlPrompt)2 File (java.io.File)1 InstallConfiguration (org.apache.rya.api.client.Install.InstallConfiguration)1 LoadStatementsFile (org.apache.rya.api.client.LoadStatementsFile)1 Statement (org.openrdf.model.Statement)1