DriverRemoteAcceptor implements RemoteAcceptor { @Override public Object configure(final List<String> args) throws RemoteException { final String option = args.size() == 0 ? "" : args.get(0); if (!POSSIBLE_TOKENS.contains(option)) throw new RemoteException(String.format("The 'config' option expects one of ['%s'] as an argument", String.join(",", POSSIBLE_TOKENS))); final List<String> arguments = args.subList(1, args.size()); if (option.equals(TOKEN_HELP)) { return ":remote config [timeout [<ms>|none]|alias [reset|show|<alias> <actual>]|help]"; } else if (option.equals(TOKEN_TIMEOUT)) { final String errorMessage = "The timeout option expects a positive integer representing milliseconds or 'none' as an argument"; if (arguments.size() != 1) throw new RemoteException(errorMessage); try { timeout = arguments.get(0).equals(TOKEN_NONE) ? NO_TIMEOUT : Integer.parseInt(arguments.get(0)); if (timeout < NO_TIMEOUT) throw new RemoteException("The value for the timeout cannot be less than " + NO_TIMEOUT); return timeout == NO_TIMEOUT ? "Remote timeout is disabled" : "Set remote timeout to " + timeout + "ms"; } catch (Exception ignored) { throw new RemoteException(errorMessage); } } else if (option.equals(TOKEN_ALIAS)) { if (arguments.size() == 1 && arguments.get(0).equals(TOKEN_RESET)) { aliases.clear(); return "Aliases cleared"; } if (arguments.size() == 1 && arguments.get(0).equals(TOKEN_SHOW)) { return aliases; } if (arguments.size() % 2 != 0) throw new RemoteException("Arguments to alias must be 'reset' to clear any existing alias settings or key/value alias/binding pairs"); final Map<String,Object> aliasMap = ElementHelper.asMap(arguments.toArray()); aliases.clear(); aliasMap.forEach((k,v) -> aliases.put(k, v.toString())); return aliases; } return this.toString(); } DriverRemoteAcceptor(final GremlinShellEnvironment shellEnvironment); @Override Object connect(final List<String> args); @Override Object configure(final List<String> args); @Override Object submit(final List<String> args); @Override void close(); int getTimeout(); @Override boolean allowRemoteConsole(); @Override String toString(); static String getScript(final String submittedScript, final GremlinShellEnvironment shellEnvironment); static final int NO_TIMEOUT; static final String USER_AGENT; }
@Test(expected = RemoteException.class) public void shouldNotConfigureWithBadCommand() throws Exception { acceptor.configure(Arrays.asList("test")); }
@Test(expected = RemoteException.class) public void shouldNotConfigureWithUnevenPairsOfAliases() throws Exception { acceptor.configure(Arrays.asList("alias g social x")); }
@Test public void shouldResetAliases() throws Exception { final Map<String,String> resetAliases = (Map<String,String>) acceptor.configure(Arrays.asList("alias", "g", "main.social")); assertEquals(1, resetAliases.size()); assertEquals("main.social", resetAliases.get("g")); assertEquals("Aliases cleared", acceptor.configure(Arrays.asList("alias", "reset"))); final Map<String,String> shownAliases = (Map<String,String>) acceptor.configure(Arrays.asList("alias", "show")); assertEquals(0, shownAliases.size()); }
@Test public void shouldAddOverwriteAndShowAliases() throws Exception { final Map<String,String> aliases = (Map<String,String>) acceptor.configure(Arrays.asList("alias", "g", "social", "graph", "main")); assertEquals(2, aliases.size()); assertEquals("social", aliases.get("g")); assertEquals("main", aliases.get("graph")); final Map<String,String> resetAliases = (Map<String,String>) acceptor.configure(Arrays.asList("alias", "g", "main.social")); assertEquals(1, resetAliases.size()); assertEquals("main.social", resetAliases.get("g")); final Map<String,String> shownAliases = (Map<String,String>) acceptor.configure(Arrays.asList("alias", "show")); assertEquals(1, shownAliases.size()); assertEquals("main.social", shownAliases.get("g")); }
@Test(expected = RemoteException.class) public void shouldConfigureTimeoutNotLessThanNoTimeout() throws Exception { acceptor.configure(Arrays.asList("timeout", "-1")); }