UserUpdater { Collection<HierarchyTreeNode> buildHierarchyTrees(@NonNull Collection<LdapUserNode> rootNodes, @NonNull Collection<LdapUserNode> allNodes) { var dnNodeMap = allNodes.stream() .collect(Collectors.toMap(LdapUserNode::getDn, Function.identity())); return rootNodes.stream() .map(rootNode -> { var childNodesInLevelOrder = getChildNodesInLevelOrder(rootNode, dnNodeMap); return buildHierarchyNode(rootNode, childNodesInLevelOrder); }) .collect(Collectors.toList()); } UserUpdater(HierarchyTreeNodeRepository hierarchyTreeNodeRepo,
                       RepositoryUserService repoUserService,
                       UserDataRepository userDataRepo,
                       LdapAdapter ldapAdapter,
                       List<Normalizer<UserData>> normalizers); void updateHierarchyAndUserData();  }
@Test public void buildHierarchyNodesReturnsExpectedNodes() { var firstRootUserId = "first-root-user"; var secondRootUserId = "second-root-user"; var firstRootChildUserId = "first-root-child-user"; var secondRootChildUserId = "second-root-child-user"; var firstRootUser = new User(firstRootUserId); var secondRootUser = new User(secondRootUserId); var firstRootChildUser = new User(firstRootChildUserId); var secondRootChildUser = new User(secondRootChildUserId); var firstRootDn = "first-root-dn"; var secondRootDn = "second-root-dn"; var firstRootChildDn = "first-root-child-dn"; var secondRootChildDn = "second-root-child-dn"; var firstRootNode = new LdapUserNode() .setId(firstRootUserId) .setDn(firstRootDn) .setManagerDn(firstRootDn) .setDirectReportsDn(List.of(firstRootChildDn)); var firstRootChildNode = new LdapUserNode() .setId(firstRootChildUserId) .setDn(firstRootChildDn) .setManagerDn(firstRootDn); var secondRootNode = new LdapUserNode() .setId(secondRootUserId) .setDn(secondRootDn) .setManagerDn(secondRootDn) .setDirectReportsDn(List.of(secondRootChildDn)); var secondRootChildNode = new LdapUserNode() .setId(secondRootChildUserId) .setDn(secondRootChildDn) .setManagerDn(secondRootDn); var firstRootChildHierarchyNode = new HierarchyTreeNode(firstRootChildUser); var expectedFirstRootHierarchyNode = new HierarchyTreeNode(firstRootUser); expectedFirstRootHierarchyNode.addDirectStaffMember(firstRootChildHierarchyNode); var secondRootChildHierarchyNode = new HierarchyTreeNode(secondRootChildUser); var expectedSecondRootHierarchyNode = new HierarchyTreeNode(secondRootUser); expectedSecondRootHierarchyNode.addDirectStaffMember(secondRootChildHierarchyNode); given(repoUserServiceMock.getOrCreateUserById(firstRootUserId)).willReturn(firstRootUser); given(repoUserServiceMock.getOrCreateUserById(firstRootChildUserId)).willReturn(firstRootChildUser); given(repoUserServiceMock.getOrCreateUserById(secondRootUserId)).willReturn(secondRootUser); given(repoUserServiceMock.getOrCreateUserById(secondRootChildUserId)).willReturn(secondRootChildUser); var actualRootHierarchyNodes = userUpdater.buildHierarchyTrees(List.of(firstRootNode, secondRootNode), List.of(firstRootNode, secondRootNode, firstRootChildNode, secondRootChildNode)); assertThat(actualRootHierarchyNodes).containsExactly(expectedFirstRootHierarchyNode, expectedSecondRootHierarchyNode); }
@Test public void buildHierarchyNodesThrowsExceptionWhenNodesMissing() { var rootUserId = "root-user"; var rootNodeDn = "root-dn"; var missingNodeDn = "mssing-node-dn"; var rootNode = new LdapUserNode() .setId(rootUserId) .setDn(rootNodeDn) .setManagerDn(rootNodeDn) .setDirectReportsDn(List.of(missingNodeDn)); var expectedMessage = String.format("Child node with DN '%s' not found!", missingNodeDn); assertThatThrownBy(() -> userUpdater.buildHierarchyTrees(List.of(rootNode), List.of(rootNode))) .isInstanceOf(IllegalArgumentException.class) .hasMessage(expectedMessage); }