ListVersionsAction implements Runnable { @Override public void run() { if (listVersionsActionConfig.name == null || !validName(listVersionsActionConfig.name)) { throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN)); } try { SanitizedSecret sanitizedSecret = keywhizClient.getSanitizedSecretByName(listVersionsActionConfig.name); List<SanitizedSecret> versions = keywhizClient.listSecretVersions(sanitizedSecret.name(), listVersionsActionConfig.idx, listVersionsActionConfig.number); printing.printSecretVersions(versions, sanitizedSecret.version()); } catch (NotFoundException e) { throw new AssertionError("Secret does not exist: " + listVersionsActionConfig.name); } catch (IOException e) { throw Throwables.propagate(e); } } ListVersionsAction(ListVersionsActionConfig listVersionsActionConfig, KeywhizClient client, Printing printing); @Override void run();  }
@Test public void listVersionsCallsPrint() throws Exception { listVersionsActionConfig.name = secretV2.getDisplayName(); listVersionsActionConfig.idx = 5; listVersionsActionConfig.number = 15; when(keywhizClient.getSanitizedSecretByName(secretV2.getDisplayName())).thenReturn( sanitizedSecretV2); when(keywhizClient.listSecretVersions(eq(secretV2.getName()), anyInt(), anyInt())).thenReturn( secretVersions); listVersionsAction.run(); verify(keywhizClient).listSecretVersions(secretV2.getName(), 5, 15); verify(printing).printSecretVersions(secretVersions, Optional.of(1L)); }
@Test public void listVersionsUsesDefaults() throws Exception { listVersionsActionConfig.name = secretV2.getDisplayName(); when(keywhizClient.getSanitizedSecretByName(secretV2.getDisplayName())).thenReturn( sanitizedSecretV2); when(keywhizClient.listSecretVersions(eq(secretV2.getName()), anyInt(), anyInt())).thenReturn( secretVersions); listVersionsAction.run(); verify(keywhizClient).listSecretVersions(secretV2.getName(), 0, 10); verify(printing).printSecretVersions(secretVersions, Optional.of(1L)); }
@Test(expected = AssertionError.class) public void listVersionsThrowsIfSecretDoesNotExist() throws Exception { listVersionsActionConfig.name = secretV2.getDisplayName(); when(keywhizClient.getSanitizedSecretByName(secretV2.getDisplayName())).thenThrow( new KeywhizClient.NotFoundException()); listVersionsAction.run(); }
@Test(expected = IllegalArgumentException.class) public void listVersionsThrowsIfNoSecretSpecified() { listVersionsActionConfig.name = null; listVersionsAction.run(); }
@Test(expected = IllegalArgumentException.class) public void listVersionsValidatesSecretName() { listVersionsActionConfig.name = "Invalid Name"; listVersionsAction.run(); }