Search in sources :

Example 21 with ShellState

use of org.apache.rya.shell.SharedShellState.ShellState in project incubator-rya by apache.

the class RyaCommands method sparqlQuery.

@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) {
    // Fetch the command that is connected to the store.
    final ShellState shellState = state.getShellState();
    final RyaClient commands = shellState.getConnectedCommands().get();
    final Optional<String> ryaInstanceName = shellState.getRyaInstanceName();
    try {
        // file option specified
        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 {
            // No Options specified. Show the user the SPARQL Prompt
            final Optional<String> sparqlQueryOpt = sparqlPrompt.getSparql();
            if (sparqlQueryOpt.isPresent()) {
                sparqlQuery = sparqlQueryOpt.get();
            } else {
                // user aborted the SPARQL prompt.
                return "";
            }
        }
        consolePrinter.println("Executing Query...");
        consolePrinter.flush();
        return commands.getExecuteSparqlQuery().executeSparqlQuery(ryaInstanceName.get(), sparqlQuery);
    } catch (final RyaClientException | IOException e) {
        log.error("Error", e);
        throw new RuntimeException("Can not execute the SPARQL Query. Reason: " + e.getMessage(), e);
    }
}
Also used : RyaClientException(org.apache.rya.api.client.RyaClientException) ShellState(org.apache.rya.shell.SharedShellState.ShellState) RyaClient(org.apache.rya.api.client.RyaClient) IOException(java.io.IOException) File(java.io.File) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 22 with ShellState

use of org.apache.rya.shell.SharedShellState.ShellState in project incubator-rya by apache.

the class SharedShellStateTest method connectedToInstance.

@Test
public void connectedToInstance() {
    final SharedShellState state = new SharedShellState();
    // Connect to Accumulo.
    final AccumuloConnectionDetails connectionDetails = mock(AccumuloConnectionDetails.class);
    final RyaClient connectedCommands = mock(RyaClient.class);
    state.connectedToAccumulo(connectionDetails, connectedCommands);
    // Connect to an Instance.
    state.connectedToInstance("instance");
    // Verify the state.
    final ShellState expected = ShellState.builder().setConnectionState(ConnectionState.CONNECTED_TO_INSTANCE).setAccumuloDetails(connectionDetails).setConnectedCommands(connectedCommands).setRyaInstanceName("instance").build();
    assertEquals(expected, state.getShellState());
}
Also used : AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) ShellState(org.apache.rya.shell.SharedShellState.ShellState) RyaClient(org.apache.rya.api.client.RyaClient) Test(org.junit.Test)

Example 23 with ShellState

use of org.apache.rya.shell.SharedShellState.ShellState in project incubator-rya by apache.

the class SharedShellStateTest method disconnected.

@Test
public void disconnected() {
    final SharedShellState state = new SharedShellState();
    // Connect to Accumulo and an instance.
    final AccumuloConnectionDetails connectionDetails = mock(AccumuloConnectionDetails.class);
    final RyaClient connectedCommands = mock(RyaClient.class);
    state.connectedToAccumulo(connectionDetails, connectedCommands);
    state.connectedToInstance("instance");
    // Disconnect.
    state.disconnected();
    // Verify the state.
    final ShellState expected = ShellState.builder().setConnectionState(ConnectionState.DISCONNECTED).build();
    assertEquals(expected, state.getShellState());
}
Also used : AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) ShellState(org.apache.rya.shell.SharedShellState.ShellState) RyaClient(org.apache.rya.api.client.RyaClient) Test(org.junit.Test)

Example 24 with ShellState

use of org.apache.rya.shell.SharedShellState.ShellState in project incubator-rya by apache.

the class SharedShellStateTest method disconnectedToConnectedToStorage.

@Test
public void disconnectedToConnectedToStorage() {
    final SharedShellState state = new SharedShellState();
    // Connect to Accumulo.
    final AccumuloConnectionDetails connectionDetails = mock(AccumuloConnectionDetails.class);
    final RyaClient connectedCommands = mock(RyaClient.class);
    state.connectedToAccumulo(connectionDetails, connectedCommands);
    // Verify the state.
    final ShellState expected = ShellState.builder().setConnectionState(ConnectionState.CONNECTED_TO_STORAGE).setAccumuloDetails(connectionDetails).setConnectedCommands(connectedCommands).build();
    assertEquals(expected, state.getShellState());
}
Also used : AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) ShellState(org.apache.rya.shell.SharedShellState.ShellState) RyaClient(org.apache.rya.api.client.RyaClient) Test(org.junit.Test)

Aggregations

ShellState (org.apache.rya.shell.SharedShellState.ShellState)24 RyaClient (org.apache.rya.api.client.RyaClient)16 RyaClientException (org.apache.rya.api.client.RyaClientException)15 CliCommand (org.springframework.shell.core.annotation.CliCommand)13 InstanceDoesNotExistException (org.apache.rya.api.client.InstanceDoesNotExistException)9 Test (org.junit.Test)8 IOException (java.io.IOException)6 AccumuloConnectionDetails (org.apache.rya.api.client.accumulo.AccumuloConnectionDetails)4 RyaDetails (org.apache.rya.api.instance.RyaDetails)3 GetInstanceDetails (org.apache.rya.api.client.GetInstanceDetails)2 InstallConfiguration (org.apache.rya.api.client.Install.InstallConfiguration)2 InstallPrompt (org.apache.rya.shell.util.InstallPrompt)2 ApplicationContext (org.springframework.context.ApplicationContext)2 Bootstrap (org.springframework.shell.Bootstrap)2 CommandResult (org.springframework.shell.core.CommandResult)2 JLineShellComponent (org.springframework.shell.core.JLineShellComponent)2 CliAvailabilityIndicator (org.springframework.shell.core.annotation.CliAvailabilityIndicator)2 MongoClient (com.mongodb.MongoClient)1 MongoException (com.mongodb.MongoException)1 File (java.io.File)1