use of com.sequenceiq.freeipa.service.freeipa.user.conversion.UserMetadataConverter in project cloudbreak by hortonworks.
the class FreeIpaUsersStateProviderTest method testGetFilteredFreeIpaState.
@Test
void testGetFilteredFreeIpaState() throws Exception {
List<String> user1GroupNames = List.of("group1", "group2");
List<String> user2GroupNames = List.of("group2", "group3", IPA_UNMANAGED_GROUPS.get(0));
List<String> groupsWithoutMembers = List.of("group4");
com.sequenceiq.freeipa.client.model.User user1 = createIpaUser("user1", user1GroupNames);
String userNotFound = "userNotFound";
Set<com.sequenceiq.freeipa.client.model.Group> groupsFindAll = Stream.of(user1GroupNames.stream(), user2GroupNames.stream(), groupsWithoutMembers.stream(), IPA_UNMANAGED_GROUPS.stream()).flatMap(groupName -> groupName).map(this::createIpaGroup).collect(Collectors.toSet());
JsonRpcClientException jsonRpcException = new JsonRpcClientException(FreeIpaErrorCodes.NOT_FOUND.getValue(), "group not found", null);
FreeIpaClientException notFoundException = new FreeIpaClientException("Invoke FreeIPA failed", jsonRpcException);
when(freeIpaClient.userShow(user1.getUid())).thenReturn(user1);
when(freeIpaClient.userShow(userNotFound)).thenThrow(notFoundException);
when(freeIpaClient.groupFindAll()).thenReturn(groupsFindAll);
Set<String> expectedUsers = Sets.newHashSet(user1.getUid());
Set<String> expectedGroups = groupsFindAll.stream().map(com.sequenceiq.freeipa.client.model.Group::getCn).filter(groupName -> !IPA_UNMANAGED_GROUPS.contains(groupName)).collect(Collectors.toSet());
UserMetadata user1Metadata = new UserMetadata("user1-crn", 1L);
doReturn(Optional.of(user1Metadata)).when(userMetadataConverter).toUserMetadata(argThat(arg -> user1.getUid().equals(arg.getUid())));
Map<String, UserMetadata> expectedUserMetadata = Map.of(user1.getUid(), user1Metadata);
UsersState ipaState = underTest.getFilteredFreeIpaState(freeIpaClient, Set.of(user1.getUid(), userNotFound));
for (FmsUser fmsUser : ipaState.getUsers()) {
assertTrue(expectedUsers.contains(fmsUser.getName()));
expectedUsers.remove(fmsUser.getName());
}
assertTrue(expectedUsers.isEmpty());
for (FmsGroup fmsGroup : ipaState.getGroups()) {
assertTrue(expectedGroups.contains(fmsGroup.getName()));
expectedGroups.remove(fmsGroup.getName());
}
assertTrue(expectedGroups.isEmpty());
assertEquals(expectedUserMetadata, ipaState.getUserMetadataMap());
}
use of com.sequenceiq.freeipa.service.freeipa.user.conversion.UserMetadataConverter in project cloudbreak by hortonworks.
the class FreeIpaUsersStateProviderTest method testGetUserState.
@Test
void testGetUserState() throws Exception {
List<String> user1GroupNames = List.of("group1", "group2");
List<String> user2GroupNames = List.of("group2", "group3", IPA_UNMANAGED_GROUPS.get(0));
List<String> ipaOnlyUserGroupNames = List.of("dont_include");
List<String> groupsWithoutMembers = List.of("group4");
Map<String, Pair<List<String>, Optional<UserMetadata>>> users = Map.of("user1", Pair.of(user1GroupNames, Optional.empty()), "user2", Pair.of(user2GroupNames, Optional.of(new UserMetadata("user2-crn", 1L))), IPA_PROTECTED_USERS.get(0), Pair.of(ipaOnlyUserGroupNames, Optional.empty()));
Set<com.sequenceiq.freeipa.client.model.User> usersFindAll = users.entrySet().stream().map(entry -> createIpaUser(entry.getKey(), entry.getValue().getLeft())).collect(Collectors.toSet());
Set<com.sequenceiq.freeipa.client.model.Group> groupsFindAll = Stream.of(user1GroupNames.stream(), user2GroupNames.stream(), groupsWithoutMembers.stream(), IPA_UNMANAGED_GROUPS.stream()).flatMap(groupName -> groupName).map(this::createIpaGroup).collect(Collectors.toSet());
when(freeIpaClient.userFindAll()).thenReturn(usersFindAll);
when(freeIpaClient.groupFindAll()).thenReturn(groupsFindAll);
Set<String> expectedUsers = users.keySet().stream().filter(user -> !IPA_PROTECTED_USERS.contains(user)).collect(Collectors.toSet());
Set<String> expectedGroups = groupsFindAll.stream().map(com.sequenceiq.freeipa.client.model.Group::getCn).filter(groupName -> !IPA_UNMANAGED_GROUPS.contains(groupName)).collect(Collectors.toSet());
Map<String, UserMetadata> expectedUserMetadata = Maps.newHashMap();
expectedUsers.forEach(username -> {
Optional<UserMetadata> userMetadata = users.get(username).getRight();
doReturn(userMetadata).when(userMetadataConverter).toUserMetadata(argThat(arg -> username.equals(arg.getUid())));
userMetadata.ifPresent(meta -> expectedUserMetadata.put(username, meta));
});
UsersState ipaState = underTest.getUsersState(freeIpaClient);
for (FmsUser fmsUser : ipaState.getUsers()) {
assertTrue(expectedUsers.contains(fmsUser.getName()));
expectedUsers.remove(fmsUser.getName());
}
assertTrue(expectedUsers.isEmpty());
for (FmsGroup fmsGroup : ipaState.getGroups()) {
assertTrue(expectedGroups.contains(fmsGroup.getName()));
expectedGroups.remove(fmsGroup.getName());
}
assertTrue(expectedGroups.isEmpty());
assertEquals(expectedUserMetadata, ipaState.getUserMetadataMap());
}
Aggregations