RyaCommands implements CommandMarker { @CliCommand(value = SPARQL_QUERY_CMD, help = "Executes the provided SPARQL Query on the connected Rya instance.") public String sparqlQuery( @CliOption(key = { "file" }, mandatory = false, help = "A local file containing the SPARQL Query that is to be read and executed.") final String file) { final ShellState shellState = state.getShellState(); final RyaClient commands = shellState.getConnectedCommands().get(); final Optional<String> ryaInstanceName = shellState.getRyaInstanceName(); final ExecuteSparqlQuery queryCommand = commands.getExecuteSparqlQuery(); try { String sparqlQuery; if (file != null) { sparqlQuery = new String(Files.readAllBytes(new File(file).toPath()), StandardCharsets.UTF_8); consolePrinter.println("Loaded Query:"); consolePrinter.println(sparqlQuery); } else { final Optional<String> sparqlQueryOpt = sparqlPrompt.getSparql(); if (sparqlQueryOpt.isPresent()) { sparqlQuery = sparqlQueryOpt.get(); } else { return ""; } } consolePrinter.println("Executing Query..."); consolePrinter.flush(); final TupleQueryResult rezIter = queryCommand.executeSparqlQuery(ryaInstanceName.get(), sparqlQuery); final List<String> bindings = new ArrayList<>(); if(rezIter.hasNext()) { consolePrinter.println("Query Results:"); final BindingSet bs = rezIter.next(); for(final String name : bs.getBindingNames()) { bindings.add(name); } consolePrinter.println(Strings.join(bindings, ",")); consolePrinter.println(formatLine(bs, bindings)); } else { consolePrinter.println("No Results Found."); } int count = 0; while(rezIter.hasNext()) { final BindingSet bs = rezIter.next(); consolePrinter.println(formatLine(bs, bindings)); count++; if(count == 20) { final Optional<String> rez = sparqlPrompt.getSparqlWithResults(); if(rez.isPresent()) { break; } } } rezIter.close(); return "Done."; } catch (final RyaClientException | IOException e) { log.error("Error", e); throw new RuntimeException("Can not execute the SPARQL Query. Reason: " + e.getMessage(), e); } catch(final Exception e) { log.error("Failed to close the results iterator.", e); return ""; } finally { try { queryCommand.close(); } catch (final IOException e) { log.error("Failed to close the sail resources used.", e); } } } @Autowired  RyaCommands(final SharedShellState state, final SparqlPrompt sparqlPrompt,
            final ConsolePrinter consolePrinter); @CliAvailabilityIndicator({ LOAD_DATA_CMD, SPARQL_QUERY_CMD }) boolean areInstanceCommandsAvailable(); @CliCommand(value = LOAD_DATA_CMD, help = "Loads RDF Statement data from a local file to the connected Rya instance.") String loadData(
            @CliOption(key = { "file" }, mandatory = true, help = "A local file containing RDF Statements that is to be loaded.")
            final String file,
            @CliOption(key = { "format" }, mandatory = false, help = "The format of the supplied RDF Statements file. [RDF/XML, N-Triples, Turtle, N3, TriX, TriG, BinaryRDF, N-Quads, JSON-LD, RDF/JSON, RDFa]")
            final String format
            ); @CliCommand(value = SPARQL_QUERY_CMD, help = "Executes the provided SPARQL Query on the connected Rya instance.") String sparqlQuery(
            @CliOption(key = { "file" }, mandatory = false, help = "A local file containing the SPARQL Query that is to be read and executed.")
            final String file); static final String LOAD_DATA_CMD; static final String SPARQL_QUERY_CMD; }
@Test public void testSparqlQuery() throws InstanceDoesNotExistException, RyaClientException, IOException { final String instanceName = "unitTest"; final String queryFile = "src/test/resources/Query1.sparql"; final String queryContent = FileUtils.readFileToString(new File(queryFile), StandardCharsets.UTF_8); final TupleQueryResult expectedResult = mock(TupleQueryResult.class); final ExecuteSparqlQuery mockExecuteSparqlQuery = mock(ExecuteSparqlQuery.class); when(mockExecuteSparqlQuery.executeSparqlQuery(instanceName, queryContent)).thenReturn(expectedResult); 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); final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter); final String message = commands.sparqlQuery(queryFile); verify(mockExecuteSparqlQuery).executeSparqlQuery(instanceName, queryContent); assertEquals("Done.", message); }
@Test(expected = RuntimeException.class) public void testSparqlQuery_nonexistentFile() throws InstanceDoesNotExistException, RyaClientException, IOException { final String instanceName = "unitTest"; final String queryFile = "src/test/resources/Nonexistent.sparql"; final RyaClient mockCommands = mock(RyaClient.class); 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); final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter); commands.sparqlQuery(queryFile); }
@Test public void testSparqlQuery_fromPrompt() throws InstanceDoesNotExistException, RyaClientException, IOException { final String instanceName = "unitTest"; final String queryContent = "SELECT * WHERE { ?person <http: final String queryFile = null; final TupleQueryResult expectedResult = mock(TupleQueryResult.class); final ExecuteSparqlQuery mockExecuteSparqlQuery = mock(ExecuteSparqlQuery.class); when(mockExecuteSparqlQuery.executeSparqlQuery(instanceName, queryContent)).thenReturn(expectedResult); 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); final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter); final String message = commands.sparqlQuery(queryFile); verify(mockExecuteSparqlQuery).executeSparqlQuery(instanceName, queryContent); assertEquals("Done.", message); }
@Test public void testSparqlQuery_fromPrompt_cancelled() throws InstanceDoesNotExistException, RyaClientException, IOException { final String instanceName = "unitTest"; final String queryFile = null; final String expectedMessage = ""; final ExecuteSparqlQuery mockQuery = mock(ExecuteSparqlQuery.class); final RyaClient mockCommands = mock(RyaClient.class); when(mockCommands.getExecuteSparqlQuery()).thenReturn(mockQuery); 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.absent()); final ConsolePrinter mockConsolePrinter = mock(ConsolePrinter.class); final RyaCommands commands = new RyaCommands(state, mockSparqlPrompt, mockConsolePrinter); final String message = commands.sparqlQuery(queryFile); assertEquals(expectedMessage, message); }