RyaCommands implements CommandMarker { @CliCommand(value = LOAD_DATA_CMD, help = "Loads RDF Statement data from a local file to the connected Rya instance.") public 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 ) { final ShellState shellState = state.getShellState(); final RyaClient commands = shellState.getConnectedCommands().get(); final Optional<String> ryaInstanceName = shellState.getRyaInstanceName(); try { final long start = System.currentTimeMillis(); final String userHome = FilenameUtils.separatorsToUnix(System.getProperty("user.home")); final Path rootedFile = Paths.get( file.replaceFirst("^~", userHome) ); RDFFormat rdfFormat = null; if (format != null) { rdfFormat = RdfFormatUtils.getRdfFormatFromName(format); if (rdfFormat == null) { throw new RuntimeException("Unsupported RDF Statement data input format: " + format); } } else if (rdfFormat == null) { rdfFormat = Rio.getParserFormatForFileName(rootedFile.getFileName().toString()).get(); if (rdfFormat == null) { throw new RuntimeException("Unable to detect RDF Statement data input format for file: " + rootedFile); } else { consolePrinter.println("Detected RDF Format: " + rdfFormat); consolePrinter.flush(); } } commands.getLoadStatementsFile().loadStatements(ryaInstanceName.get(), rootedFile, rdfFormat); final String seconds = new DecimalFormat("0.0##").format((System.currentTimeMillis() - start) / 1000.0); return "Loaded the file: '" + file + "' successfully in " + seconds + " seconds."; } catch (final RyaClientException | IOException e) { log.error("Error", e); throw new RuntimeException("Can not load the RDF Statement data. Reason: " + e.getMessage(), 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 testLoadData() throws InstanceDoesNotExistException, RyaClientException, IOException { final String instanceName = "unitTest"; final String statementsFile = "/path/to/statements.nt"; final String format = null; final LoadStatementsFile mockLoadStatementsFile = mock(LoadStatementsFile.class); final RyaClient mockCommands = mock(RyaClient.class); when(mockCommands.getLoadStatementsFile()).thenReturn(mockLoadStatementsFile); 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.loadData(statementsFile, format); verify(mockLoadStatementsFile).loadStatements(instanceName, Paths.get(statementsFile), RDFFormat.NTRIPLES); assertTrue(message.startsWith("Loaded the file: '" + statementsFile +"' successfully in ")); assertTrue(message.endsWith(" seconds.")); }
@Test public void loadData_relativePath() throws Exception { final String instanceName = "unitTest"; final String statementsFile = "~/statements.nt"; final String format = null; final LoadStatementsFile mockLoadStatementsFile = mock(LoadStatementsFile.class); final RyaClient mockCommands = mock(RyaClient.class); when(mockCommands.getLoadStatementsFile()).thenReturn(mockLoadStatementsFile); 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.loadData(statementsFile, format); final String rootedFile = System.getProperty("user.home") + "/statements.nt"; verify(mockLoadStatementsFile).loadStatements(instanceName, Paths.get(rootedFile), RDFFormat.NTRIPLES); assertTrue(message.startsWith("Loaded the file: '" + statementsFile +"' successfully in ")); assertTrue(message.endsWith(" seconds.")); }
@Test public void testLoadData_specifyFormat() throws InstanceDoesNotExistException, RyaClientException, IOException { final String instanceName = "unitTest"; final String statementsFile = "/path/to/statements.nt"; final String format = "N-TRIPLES"; final LoadStatementsFile mockLoadStatementsFile = mock(LoadStatementsFile.class); final RyaClient mockCommands = mock(RyaClient.class); when(mockCommands.getLoadStatementsFile()).thenReturn(mockLoadStatementsFile); 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.loadData(statementsFile, format); verify(mockLoadStatementsFile).loadStatements(instanceName, Paths.get(statementsFile), RDFFormat.NTRIPLES); assertTrue(message.startsWith("Loaded the file: '" + statementsFile +"' successfully in ")); assertTrue(message.endsWith(" seconds.")); }
@Test(expected = RuntimeException.class) public void testLoadData_specifyInvalidFormat() throws InstanceDoesNotExistException, RyaClientException, IOException { final String instanceName = "unitTest"; final String statementsFile = "/path/to/statements.nt"; final String format = "INVALID_FORMAT_NAME"; final LoadStatementsFile mockLoadStatementsFile = mock(LoadStatementsFile.class); final RyaClient mockCommands = mock(RyaClient.class); when(mockCommands.getLoadStatementsFile()).thenReturn(mockLoadStatementsFile); 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.loadData(statementsFile, format); }
@Test(expected = RuntimeException.class) public void testLoadData_specifyInvalidFilenameFormat() throws InstanceDoesNotExistException, RyaClientException, IOException { final String instanceName = "unitTest"; final String statementsFile = "/path/to/statements.invalidFormat"; final String format = null; final LoadStatementsFile mockLoadStatementsFile = mock(LoadStatementsFile.class); final RyaClient mockCommands = mock(RyaClient.class); when(mockCommands.getLoadStatementsFile()).thenReturn(mockLoadStatementsFile); 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.loadData(statementsFile, format); }