DescribeAction implements Runnable { @Override public void run() { List<String> describeType = describeActionConfig.describeType; if (describeType == null || describeType.isEmpty()) { throw new IllegalArgumentException("Must specify a single type to describe."); } if (describeActionConfig.name == null || !validName(describeActionConfig.name)) { throw new IllegalArgumentException(format("Invalid name, must match %s", VALID_NAME_PATTERN)); } String firstType = describeType.get(0).toLowerCase().trim(); String name = describeActionConfig.name; switch (firstType) { case "group": try { Group group = keywhizClient.getGroupByName(name); printing.printGroupWithDetails(group); } catch (NotFoundException e) { throw new AssertionError("Group not found."); } catch (IOException e) { throw Throwables.propagate(e); } break; case "client": try { Client client = keywhizClient.getClientByName(name); printing.printClientWithDetails(client); } catch (NotFoundException e) { throw new AssertionError("Client not found."); } catch (IOException e) { throw Throwables.propagate(e); } break; case "secret": SanitizedSecret sanitizedSecret; try { sanitizedSecret = keywhizClient.getSanitizedSecretByName(name); printing.printSanitizedSecretWithDetails(sanitizedSecret); } catch (NotFoundException e) { throw new AssertionError("Secret not found."); } catch (IOException e) { throw Throwables.propagate(e); } break; default: throw new IllegalArgumentException("Invalid describe type specified: " + firstType); } } DescribeAction(DescribeActionConfig describeActionConfig, KeywhizClient client,
      Printing printing); @Override void run();  }
@Test public void describeCallsPrintForGroup() throws Exception { describeActionConfig.describeType = Arrays.asList("group"); describeActionConfig.name = "Web"; Group group = new Group(0, describeActionConfig.name, null, null, null, null, null, null); when(keywhizClient.getGroupByName(anyString())).thenReturn(group); describeAction.run(); verify(printing).printGroupWithDetails(group); }
@Test public void describeCallsPrintForClient() throws Exception { describeActionConfig.describeType = Arrays.asList("client"); describeActionConfig.name = "client-name"; Client client = new Client(0, describeActionConfig.name, null, null, null, null, null, null, null, null, false, false); when(keywhizClient.getClientByName(describeActionConfig.name)).thenReturn(client); describeAction.run(); verify(printing).printClientWithDetails(client); }
@Test public void describeCallsPrintForSecret() throws Exception { describeActionConfig.describeType = Arrays.asList("secret"); describeActionConfig.name = "General_Password"; when(keywhizClient.getSanitizedSecretByName(anyString())).thenReturn(sanitizedSecret); describeAction.run(); verify(printing).printSanitizedSecretWithDetails(sanitizedSecret); }
@Test(expected = AssertionError.class) public void describeThrowsIfGroupDoesNotExist() throws Exception { describeActionConfig.describeType = Arrays.asList("group"); describeActionConfig.name = "nonexistent-group-name"; when(keywhizClient.getGroupByName(anyString())).thenThrow(new NotFoundException()); describeAction.run(); }
@Test(expected = AssertionError.class) public void describeThrowsIfClientDoesNotExist() throws Exception { describeActionConfig.describeType = Arrays.asList("client"); describeActionConfig.name = "nonexistent-client-name"; when(keywhizClient.getClientByName("nonexistent-client-name")).thenThrow( new NotFoundException()); when(keywhizClient.allClients()).thenReturn(Arrays.asList()); describeAction.run(); }
@Test(expected = AssertionError.class) public void describeThrowsIfSecretDoesNotExist() throws Exception { describeActionConfig.describeType = Arrays.asList("secret"); describeActionConfig.name = "nonexistent-secret-name"; when(keywhizClient.getSanitizedSecretByName(anyString())).thenThrow(new NotFoundException()); describeAction.run(); }
@Test(expected = IllegalArgumentException.class) public void describeThrowsIfNoTypeSpecified() throws Exception { describeActionConfig.describeType = null; describeAction.run(); }
@Test(expected = IllegalArgumentException.class) public void describeThrowsIfTooManyArguments() throws Exception { describeActionConfig.describeType = Arrays.asList("group", "client"); describeAction.run(); }
@Test(expected = IllegalArgumentException.class) public void describeThrowsIfInvalidType() throws Exception { describeActionConfig.describeType = Arrays.asList("invalid_type"); describeActionConfig.name = "any-name"; describeAction.run(); }
@Test(expected = IllegalArgumentException.class) public void describeValidatesGroupName() throws Exception { describeActionConfig.describeType = Arrays.asList("group"); describeActionConfig.name = "Invalid Name"; describeAction.run(); }
@Test(expected = IllegalArgumentException.class) public void describeValidatesSecretName() throws Exception { describeActionConfig.describeType = Arrays.asList("secret"); describeActionConfig.name = "Invalid Name"; describeAction.run(); }