ListAction implements Runnable { @Override public void run() { List<String> listOptions = listActionConfig.listType; if (listOptions == null) { try { printing.printAllSanitizedSecrets(keywhizClient.allSecrets()); } catch (IOException e) { throw Throwables.propagate(e); } return; } List<String> options = Arrays.asList(listOptions.get(0).split(",")); String firstOption = options.get(0).toLowerCase().trim(); try { switch (firstOption) { case "groups": printing.printAllGroups(keywhizClient.allGroups()); break; case "clients": printing.printAllClients(keywhizClient.allClients()); break; case "secrets": if (listActionConfig.idx == null && listActionConfig.num == null) { printing.printAllSanitizedSecrets(keywhizClient.allSecrets()); } else if (listActionConfig.idx != null && listActionConfig.num != null) { checkArgument(listActionConfig.idx >= 0); checkArgument(listActionConfig.num >= 0); if (listActionConfig.newestFirst == null) { printing.printAllSanitizedSecrets(keywhizClient.allSecretsBatched(listActionConfig.idx, listActionConfig.num, true)); } else { printing.printAllSanitizedSecrets(keywhizClient.allSecretsBatched(listActionConfig.idx, listActionConfig.num, listActionConfig.newestFirst)); } } else { throw new AssertionError("Both idx and num must be provided for batched secret queries"); } break; default: throw new AssertionError("Invalid list option: " + firstOption); } } catch(IOException e) { throw Throwables.propagate(e); } } ListAction(ListActionConfig listActionConfig, KeywhizClient client, Printing printing); @Override void run();  }
@Test public void listCallsPrintForListAll() throws Exception { listActionConfig.listType = null; listAction.run(); verify(printing).printAllSanitizedSecrets(keywhizClient.allSecrets()); }
@Test public void listCallsPrintForListGroups() throws Exception { listActionConfig.listType = Arrays.asList("groups"); listAction.run(); verify(printing).printAllGroups(keywhizClient.allGroups()); }
@Test public void listCallsPrintForListClients() throws Exception { listActionConfig.listType = Arrays.asList("clients"); listAction.run(); verify(printing).printAllClients(keywhizClient.allClients()); }
@Test public void listCallsPrintForListSecrets() throws Exception { listActionConfig.listType = Arrays.asList("secrets"); listAction.run(); verify(printing).printAllSanitizedSecrets(keywhizClient.allSecrets()); }
@Test public void listCallsPrintForListSecretsBatched() throws Exception { listActionConfig.listType = Arrays.asList("secrets"); listActionConfig.idx = 0; listActionConfig.num = 10; listActionConfig.newestFirst = false; listAction.run(); verify(printing).printAllSanitizedSecrets(keywhizClient.allSecretsBatched(0, 10, false)); }
@Test public void listCallsPrintForListSecretsBatchedWithDefault() throws Exception { listActionConfig.listType = Arrays.asList("secrets"); listActionConfig.idx = 5; listActionConfig.num = 10; listAction.run(); verify(printing).printAllSanitizedSecrets(keywhizClient.allSecretsBatched(5, 10, true)); }
@Test public void listCallsErrorsCorrectly() throws Exception { listActionConfig.listType = Arrays.asList("secrets"); listActionConfig.idx = 5; boolean error = false; try { listAction.run(); } catch (AssertionError e) { error = true; } assert(error); listActionConfig.listType = Arrays.asList("secrets"); listActionConfig.idx = 5; listActionConfig.num = -5; error = false; try { listAction.run(); } catch (IllegalArgumentException e) { error = true; } assert(error); }
@Test(expected = AssertionError.class) public void listThrowsIfInvalidType() throws Exception { listActionConfig.listType = Arrays.asList("invalid_type"); listAction.run(); }