Search in sources :

Example 6 with RyaStreamsClient

use of org.apache.rya.streams.api.RyaStreamsClient in project incubator-rya by apache.

the class RyaStreamsCommandsTest method deleteQuery.

@Test
public void deleteQuery() throws Exception {
    // Mock the object that performs the rya streams operation.
    final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
    final DeleteQuery deleteQuery = mock(DeleteQuery.class);
    when(mockClient.getDeleteQuery()).thenReturn(deleteQuery);
    // Mock a shell state and connect it to a Rya instance.
    final SharedShellState state = new SharedShellState();
    state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mock(RyaClient.class));
    state.connectedToInstance("unitTest");
    state.connectedToRyaStreams(mockClient);
    // Execute the command.
    final RyaStreamsCommands commands = new RyaStreamsCommands(state, mock(SparqlPrompt.class), mock(ConsolePrinter.class));
    final UUID queryId = UUID.randomUUID();
    final String message = commands.deleteQuery(queryId.toString());
    // Verify the interactor was invoked with the provided parameters.
    verify(deleteQuery).delete(eq(queryId));
    // Verify a message is printed to the user.
    final String expected = "The query has been deleted.";
    assertEquals(expected, message);
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) ConsolePrinter(org.apache.rya.shell.util.ConsolePrinter) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) DeleteQuery(org.apache.rya.streams.api.interactor.DeleteQuery) RyaClient(org.apache.rya.api.client.RyaClient) UUID(java.util.UUID) Test(org.junit.Test)

Example 7 with RyaStreamsClient

use of org.apache.rya.streams.api.RyaStreamsClient in project incubator-rya by apache.

the class RyaStreamsCommandsTest method addQuery_doNotInsertInsertUpdate.

@Test
public void addQuery_doNotInsertInsertUpdate() throws Exception {
    // Mock the object that performs the rya streams operation.
    final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
    final AddQuery addQuery = mock(AddQuery.class);
    when(mockClient.getAddQuery()).thenReturn(addQuery);
    final String sparql = "PREFIX Sensor: <http://example.com/Equipment.owl#> " + "INSERT { " + "?subject Sensor:test2 ?newValue " + "} WHERE {" + "values (?oldValue ?newValue) {" + "('testValue1' 'newValue1')" + "('testValue2' 'newValue2')" + "}" + "?subject Sensor:test1 ?oldValue" + "}";
    final StreamsQuery addedQuery = new StreamsQuery(UUID.randomUUID(), sparql, true, false);
    when(addQuery.addQuery(eq(sparql), eq(false), eq(false))).thenReturn(addedQuery);
    // Mock a SPARQL prompt that a user entered a query through.
    final SparqlPrompt prompt = mock(SparqlPrompt.class);
    when(prompt.getSparql()).thenReturn(Optional.of(sparql));
    // Mock a shell state and connect it to a Rya instance.
    final SharedShellState state = new SharedShellState();
    state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mock(RyaClient.class));
    state.connectedToInstance("unitTest");
    state.connectedToRyaStreams(mockClient);
    // Execute the command.
    final RyaStreamsCommands commands = new RyaStreamsCommands(state, prompt, mock(ConsolePrinter.class));
    final String message = commands.addQuery(true, false);
    // Verify the interactor was invoked with the provided input.
    verify(addQuery).addQuery(sparql, false, false);
    // Verify a message is printed to the user.
    final String expected = "The added query's ID is " + addedQuery.getQueryId();
    assertEquals(expected, message);
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) ConsolePrinter(org.apache.rya.shell.util.ConsolePrinter) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) AddQuery(org.apache.rya.streams.api.interactor.AddQuery) RyaClient(org.apache.rya.api.client.RyaClient) Test(org.junit.Test)

Example 8 with RyaStreamsClient

use of org.apache.rya.streams.api.RyaStreamsClient in project incubator-rya by apache.

the class RyaStreamsCommandsTest method startQuery.

@Test
public void startQuery() throws Exception {
    // Mock the object that performs the rya streams operation.
    final RyaStreamsClient mockClient = mock(RyaStreamsClient.class);
    final StartQuery startQuery = mock(StartQuery.class);
    when(mockClient.getStartQuery()).thenReturn(startQuery);
    final GetQuery getQuery = mock(GetQuery.class);
    when(mockClient.getGetQuery()).thenReturn(getQuery);
    // Mock a shell state and connect it to a Rya instance.
    final SharedShellState state = new SharedShellState();
    state.connectedToAccumulo(mock(AccumuloConnectionDetails.class), mock(RyaClient.class));
    state.connectedToInstance("unitTest");
    state.connectedToRyaStreams(mockClient);
    // Report the query as not running.
    final UUID queryId = UUID.randomUUID();
    when(getQuery.getQuery(eq(queryId))).thenReturn(java.util.Optional.of(new StreamsQuery(queryId, "sparql", false, false)));
    // Execute the command.
    final RyaStreamsCommands commands = new RyaStreamsCommands(state, mock(SparqlPrompt.class), mock(ConsolePrinter.class));
    final String message = commands.startQuery(queryId.toString());
    // Verify the interactor was invoked with the provided parameters.
    verify(startQuery).start(queryId);
    // Verify a message is printed to the user.
    final String expected = "The query will be processed by the Rya Streams subsystem.";
    assertEquals(expected, message);
}
Also used : RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) ConsolePrinter(org.apache.rya.shell.util.ConsolePrinter) GetQuery(org.apache.rya.streams.api.interactor.GetQuery) AccumuloConnectionDetails(org.apache.rya.api.client.accumulo.AccumuloConnectionDetails) StreamsQuery(org.apache.rya.streams.api.entity.StreamsQuery) SparqlPrompt(org.apache.rya.shell.util.SparqlPrompt) RyaClient(org.apache.rya.api.client.RyaClient) UUID(java.util.UUID) StartQuery(org.apache.rya.streams.api.interactor.StartQuery) Test(org.junit.Test)

Example 9 with RyaStreamsClient

use of org.apache.rya.streams.api.RyaStreamsClient in project incubator-rya by apache.

the class RyaConnectionCommands method connectToInstance.

@CliCommand(value = CONNECT_INSTANCE_CMD, help = "Connect to a specific Rya instance")
public void connectToInstance(@CliOption(key = { "instance" }, mandatory = true, help = "The name of the Rya instance the shell will interact with.") final String ryaInstance) {
    final RyaClient ryaClient = sharedState.getShellState().getConnectedCommands().get();
    try {
        final InstanceExists instanceExists = ryaClient.getInstanceExists();
        // Make sure the requested instance exists.
        if (!instanceExists.exists(ryaInstance)) {
            throw new RuntimeException(String.format("'%s' does not match an existing Rya instance.", ryaInstance));
        }
        // Store the instance name in the shared state.
        sharedState.connectedToInstance(ryaInstance);
        // If the Rya instance is configured to interact with Rya Streams, then connect the
        // Rya Streams client to the shared state.
        final com.google.common.base.Optional<RyaDetails> ryaDetails = ryaClient.getGetInstanceDetails().getDetails(ryaInstance);
        if (ryaDetails.isPresent()) {
            final com.google.common.base.Optional<RyaStreamsDetails> streamsDetails = ryaDetails.get().getRyaStreamsDetails();
            if (streamsDetails.isPresent()) {
                final String kafkaHostname = streamsDetails.get().getHostname();
                final int kafkaPort = streamsDetails.get().getPort();
                final RyaStreamsClient streamsClient = KafkaRyaStreamsClientFactory.make(ryaInstance, kafkaHostname, kafkaPort);
                sharedState.connectedToRyaStreams(streamsClient);
            }
        }
    } catch (final RyaClientException e) {
        throw new RuntimeException("Could not connect to Rya instance. Reason: " + e.getMessage(), e);
    }
}
Also used : RyaStreamsDetails(org.apache.rya.api.instance.RyaDetails.RyaStreamsDetails) RyaClientException(org.apache.rya.api.client.RyaClientException) RyaDetails(org.apache.rya.api.instance.RyaDetails) RyaClient(org.apache.rya.api.client.RyaClient) RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) InstanceExists(org.apache.rya.api.client.InstanceExists) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Example 10 with RyaStreamsClient

use of org.apache.rya.streams.api.RyaStreamsClient in project incubator-rya by apache.

the class RyaConnectionCommands method disconnect.

@CliCommand(value = DISCONNECT_COMMAND_NAME_CMD, help = "Disconnect the shell's Rya storage connection (Accumulo).")
public void disconnect() {
    final ShellState shellState = sharedState.getShellState();
    // If connected to Mongo, there is a client that needs to be closed.
    final com.google.common.base.Optional<MongoClient> mongoAdminClient = shellState.getMongoAdminClient();
    if (mongoAdminClient.isPresent()) {
        mongoAdminClient.get().close();
    }
    // If connected to Rya Streams, then close the associated resources.
    final com.google.common.base.Optional<RyaStreamsClient> streamsClient = shellState.getRyaStreamsCommands();
    if (streamsClient.isPresent()) {
        try {
            streamsClient.get().close();
        } catch (final Exception e) {
            System.err.print("Could not close the RyaStreamsClient.");
            e.printStackTrace();
        }
    }
    // Update the shared state to disconnected.
    sharedState.disconnected();
}
Also used : MongoClient(com.mongodb.MongoClient) RyaStreamsClient(org.apache.rya.streams.api.RyaStreamsClient) ShellState(org.apache.rya.shell.SharedShellState.ShellState) RyaClientException(org.apache.rya.api.client.RyaClientException) AccumuloSecurityException(org.apache.accumulo.core.client.AccumuloSecurityException) MongoException(com.mongodb.MongoException) IOException(java.io.IOException) AccumuloException(org.apache.accumulo.core.client.AccumuloException) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Aggregations

RyaStreamsClient (org.apache.rya.streams.api.RyaStreamsClient)22 RyaClient (org.apache.rya.api.client.RyaClient)15 StreamsQuery (org.apache.rya.streams.api.entity.StreamsQuery)14 AccumuloConnectionDetails (org.apache.rya.api.client.accumulo.AccumuloConnectionDetails)13 Test (org.junit.Test)13 ConsolePrinter (org.apache.rya.shell.util.ConsolePrinter)12 SparqlPrompt (org.apache.rya.shell.util.SparqlPrompt)12 UUID (java.util.UUID)10 CliCommand (org.springframework.shell.core.annotation.CliCommand)8 RyaStreamsException (org.apache.rya.streams.api.exception.RyaStreamsException)6 AddQuery (org.apache.rya.streams.api.interactor.AddQuery)5 GetQuery (org.apache.rya.streams.api.interactor.GetQuery)5 IOException (java.io.IOException)4 RyaClientException (org.apache.rya.api.client.RyaClientException)4 MalformedQueryException (org.openrdf.query.MalformedQueryException)3 RyaStreamsDetails (org.apache.rya.api.instance.RyaDetails.RyaStreamsDetails)2 StartQuery (org.apache.rya.streams.api.interactor.StartQuery)2 StopQuery (org.apache.rya.streams.api.interactor.StopQuery)2 MongoClient (com.mongodb.MongoClient)1 MongoException (com.mongodb.MongoException)1