UserUpdater { HierarchyTreeNode buildHierarchyNode(@NonNull LdapUserNode rootNode, @NonNull List<LdapUserNode> childNodes) { var rootNodeUser = repoUserService.getOrCreateUserById(rootNode.getId()); var rootHierarchyNode = new HierarchyTreeNode(rootNodeUser); var dnHierarchyNodeMap = new HashMap<String, HierarchyTreeNode>(); dnHierarchyNodeMap.put(rootNode.getDn(), rootHierarchyNode); childNodes.forEach(childNode -> { var userId = childNode.getId(); var user = repoUserService.getOrCreateUserById(userId); var childHierarchyNode = new HierarchyTreeNode(user); dnHierarchyNodeMap.put(childNode.getDn(), childHierarchyNode); var managerHierarchy = dnHierarchyNodeMap.get(childNode.getManagerDn()); if(Objects.isNull(managerHierarchy)) { throw new IllegalArgumentException("Child nodes not in level order!"); } managerHierarchy.addDirectStaffMember(childHierarchyNode); }); return rootHierarchyNode; } UserUpdater(HierarchyTreeNodeRepository hierarchyTreeNodeRepo,
                       RepositoryUserService repoUserService,
                       UserDataRepository userDataRepo,
                       LdapAdapter ldapAdapter,
                       List<Normalizer<UserData>> normalizers); void updateHierarchyAndUserData();  }
@Test public void buildHierarchyNodeThrowsExceptionWhenChildNodesNotInLevelOrder() { var rootUserId = "root-user"; var firstLevelUserId = "first-level-user"; var secondLevelUserId = "second-level-user"; var rootNodeDn = "root-dn"; var firstLevelDn = "first-level-dn"; var secondLevelDn = "second-level-dn"; var rootNode = new LdapUserNode() .setId(rootUserId) .setDn(rootNodeDn) .setManagerDn(rootNodeDn) .setDirectReportsDn(List.of(firstLevelDn)); var firstLevelNode = new LdapUserNode() .setId(firstLevelUserId) .setDn(firstLevelDn) .setManagerDn(rootNodeDn) .setDirectReportsDn(List.of(secondLevelDn)); var secondLevelNode = new LdapUserNode() .setId(secondLevelUserId) .setDn(secondLevelDn) .setManagerDn(firstLevelDn); given(repoUserServiceMock.getOrCreateUserById(rootUserId)).willReturn(new User(rootUserId)); given(repoUserServiceMock.getOrCreateUserById(secondLevelUserId)).willReturn(new User(secondLevelUserId)); assertThatThrownBy(() -> userUpdater.buildHierarchyNode(rootNode, List.of(secondLevelNode, firstLevelNode))) .isInstanceOf(IllegalArgumentException.class) .hasMessage("Child nodes not in level order!"); }
@Test public void buildHierarchyNodeReturnsExpectedHierarchyNode() { var rootUserId = "root-user"; var firstLevelUserId = "first-level-user"; var secondLevelUserId = "second-level-user"; var rootUser = new User(rootUserId); var firstLevelUser = new User(firstLevelUserId); var secondLevelUser = new User(secondLevelUserId); var rootNodeDn = "root-dn"; var firstLevelDn = "first-level-dn"; var secondLevelDn = "second-level-dn"; var rootNode = new LdapUserNode() .setId(rootUserId) .setDn(rootNodeDn) .setManagerDn(rootNodeDn) .setDirectReportsDn(List.of(firstLevelDn)); var firstLevelNode = new LdapUserNode() .setId(firstLevelUserId) .setDn(firstLevelDn) .setManagerDn(rootNodeDn) .setDirectReportsDn(List.of(secondLevelDn)); var secondLevelNode = new LdapUserNode() .setId(secondLevelUserId) .setDn(secondLevelDn) .setManagerDn(firstLevelDn); var firstLevelHierarchy = new HierarchyTreeNode(firstLevelUser); var secondLevelHierarchy = new HierarchyTreeNode(secondLevelUser); var expectedHierarchyNode = new HierarchyTreeNode(rootUser); expectedHierarchyNode.addDirectStaffMember(firstLevelHierarchy); firstLevelHierarchy.addDirectStaffMember(secondLevelHierarchy); given(repoUserServiceMock.getOrCreateUserById(rootUserId)).willReturn(rootUser); given(repoUserServiceMock.getOrCreateUserById(firstLevelUserId)).willReturn(firstLevelUser); given(repoUserServiceMock.getOrCreateUserById(secondLevelUserId)).willReturn(secondLevelUser); var actualHierarchyNode = userUpdater.buildHierarchyNode(rootNode, List.of(firstLevelNode, secondLevelNode)); assertThat(actualHierarchyNode).isEqualTo(expectedHierarchyNode); }