@Test public void testAutoSuggestion() throws Exception { ListRolesRequest request = new ListRolesRequest().withMaxItems(1000); Role role1 = new Role().withRoleName("foobar1"); Role role2 = new Role().withRoleName("afoobar"); Role role3 = new Role().withRoleName("foooobar"); ListRolesResult mockResult = new ListRolesResult(); mockResult.withRoles(role1, role2, role3); when(mockClient.listRoles(request)).thenReturn(mockResult); List<Principal> list = partiallyMockedPrincipalAutoSuggestion.autoSuggestion("foobar"); assertEquals(list.size(), 2); assertEquals(list.get(0).name, "foobar1"); assertEquals(list.get(1).name, "afoobar"); verify(mockClient, times(1)).listRoles(request); }
@Test public void testAutoSuggestionCaseInsensitive() throws Exception { ListRolesRequest request = new ListRolesRequest().withMaxItems(1000); Role lowercase = new Role().withRoleName("foobar"); Role uppercase = new Role().withRoleName("FOOBAR"); Role mixedCase = new Role().withRoleName("FooBar"); ListRolesResult mockResult = new ListRolesResult(); mockResult.withRoles(lowercase, uppercase, mixedCase); when(mockClient.listRoles(request)).thenReturn(mockResult); List<Principal> list = partiallyMockedPrincipalAutoSuggestion.autoSuggestion("fOOb"); assertEquals(list.size(), 3); assertEquals(list.get(0).name, "foobar"); assertEquals(list.get(1).name, "FOOBAR"); assertEquals(list.get(2).name, "FooBar"); }
@Before public void setUp() throws Exception { this.violationSinkMock = mock(ViolationSink.class); this.clientProviderMock = mock(ClientProvider.class); this.accountIdSupplierMock = mock(AccountIdSupplier.class); this.jobsPropertiesMock = mock(JobsProperties.class); this.mockAmazonIdentityManagementClient = mock(AmazonIdentityManagementClient.class); this.mockAwsApplications = mock(AwsApplications.class); mockListRolesResult = new ListRolesResult(); mockListRolesResult.setRoles(asList( createRole("aws-service-role", AWS_SERVICE_POLICY_DOCUMENT), createRole("cross-account-role", CROSS_ACCOUNT_POLICY_DOCUMENT), createRole("same-account-role", SAME_ACCOUNT_POLICY_DOCUMENT), createRole("deleted-role-reference-role", DELETED_ROLE_POLICY_DOCUMENT), createRole("management-account-role", MANAGEMENT_POLICY_DOCUMENT))); when(clientProviderMock.getClient(any(), any(String.class), any(Region.class))).thenReturn(mockAmazonIdentityManagementClient); }
public List<Principal> autoSuggestion(final String name) { if (name.length() >= 3) { String lowerCaseName = name.toLowerCase(); ListRolesRequest listRolesRequest = new ListRolesRequest(); listRolesRequest.withMaxItems(1000); ListRolesResult result = client.listRoles(listRolesRequest); List<Principal> tmp = result.getRoles().stream() .filter(p -> p.getRoleName().toLowerCase().contains(lowerCaseName)) .map(p -> new Principal(PrincipalType.ROLE, p.getRoleName())).collect(Collectors.toList()); return tmp.subList(0, Math.min(5, tmp.size())); } return new ArrayList<>(); }
@Override public List<AbstractResource<?>> listRoles(Account account, DateTime dt) { AmazonIdentityManagement iam = findClient(account); ListRolesRequest req = new ListRolesRequest(); log.debug("start list roles for account:{} via api", account.getId() + "=>" + account.getName()); ListRolesResult res = iam.listRoles(req); return converter.toIamRoles(res.getRoles(), account.getId(), dt); }