diff --git a/src/test/java/de/muenchen/zammad/ldap/service/CreateGroupAndUserTest.java b/src/test/java/de/muenchen/zammad/ldap/service/CreateGroupAndUserTest.java index a030f1f..2a6370f 100644 --- a/src/test/java/de/muenchen/zammad/ldap/service/CreateGroupAndUserTest.java +++ b/src/test/java/de/muenchen/zammad/ldap/service/CreateGroupAndUserTest.java @@ -1,12 +1,14 @@ package de.muenchen.zammad.ldap.service; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.util.List; +import java.util.TreeMap; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -18,7 +20,10 @@ import de.muenchen.zammad.ldap.domain.ZammadGroupDTO; import de.muenchen.zammad.ldap.domain.ZammadUserDTO; +import de.muenchen.zammad.ldap.tree.LdapOuNode; +import lombok.extern.log4j.Log4j2; +@Log4j2 @ExtendWith(MockitoExtension.class) @MockitoSettings(strictness = Strictness.LENIENT) class CreateGroupAndUserTest extends PrepareTestEnvironment { @@ -26,6 +31,9 @@ class CreateGroupAndUserTest extends PrepareTestEnvironment { @Captor private ArgumentCaptor createGroupCaptor; + @Captor + private ArgumentCaptor updateGroupCaptor; + @Captor private ArgumentCaptor userUserCaptor; @@ -53,4 +61,55 @@ void createTest() { } + + @Test + void createParentNodeTest() { + + var zammadService = mock(ZammadService.class); + when(zammadService.getZammadGroups()).thenReturn(List.of( new ZammadGroupDTO("1", "1", "shortname_2_1", true, true, "lhmobjectId_2_1", null))); + when(zammadService.getZammadUsers()).thenReturn(List.of()); + + assertEquals(1, zammadService.getZammadGroups().size()); + assertEquals(0, zammadService.getZammadUsers().size()); + + groupMocksCreateParentNodeTest(zammadService); + + var zammadSyncService = new ZammadSyncServiceSubtree(zammadService, createZammadProperties()); + + var childTree_level_2 = new TreeMap(); + var number = 1; + var level = 2; + var dn = String.format("dn_level_%d_no_%d", level, number); + var child_level_2 = new LdapOuNode(dn, createEnhancedLdapOuSearchResultDTO(level,number), new TreeMap(), null); + childTree_level_2.put(dn, child_level_2); + + var childTree_level_1 = new TreeMap(); + level = 1; + dn = String.format("dn_level_%d_no_%d", level, number); + var child_level_1 = new LdapOuNode(dn, createEnhancedLdapOuSearchResultDTO(level,number), new TreeMap(), null); + child_level_1.setChildNodes(childTree_level_2); + childTree_level_1.put(dn, child_level_1); + + var childTree_level_0 = new TreeMap(); + level = 0; + dn = String.format("dn_level_%d_no_%d", level, number); + var child_level_0 = new LdapOuNode(dn, createEnhancedLdapOuSearchResultDTO(level,number), new TreeMap(), null); + child_level_0.setChildNodes(childTree_level_1); + childTree_level_0.put(dn, child_level_0); + + log.info(child_level_0.toString()); + zammadSyncService.updateZammadGroupsWithUsers(childTree_level_0); + + verify(zammadService, times(2)).createZammadGroup(createGroupCaptor.capture()); + assertNull(createGroupCaptor.getAllValues().get(0).getParentId()); + assertEquals("shortname_0_1", createGroupCaptor.getAllValues().get(0).getName()); + assertEquals("2", createGroupCaptor.getAllValues().get(1).getParentId()); + assertEquals("shortname_0_1::shortname_1_1", createGroupCaptor.getAllValues().get(1).getName()); + + verify(zammadService, times(1)).updateZammadGroup(updateGroupCaptor.capture()); + assertEquals("shortname_0_1::shortname_1_1::shortname_2_1", updateGroupCaptor.getAllValues().get(0).getName()); + assertEquals("3", updateGroupCaptor.getAllValues().get(0).getParentId()); + + } + } diff --git a/src/test/java/de/muenchen/zammad/ldap/service/PrepareTestEnvironment.java b/src/test/java/de/muenchen/zammad/ldap/service/PrepareTestEnvironment.java index fa34ac7..8a77884 100644 --- a/src/test/java/de/muenchen/zammad/ldap/service/PrepareTestEnvironment.java +++ b/src/test/java/de/muenchen/zammad/ldap/service/PrepareTestEnvironment.java @@ -65,6 +65,19 @@ protected void userAndGroupMocks(ZammadService zammadService) { when(zammadService.createZammadUser(new ZammadUserDTO(null, "vorname_2_3_1", "nachname_2_3_1", "lhmobjectId_2_3_1", true, null, null, "lhmobjectId_2_3_1", List.of(0, 1), Map.of("3", List.of("full")), null, true, null))).thenReturn(new ZammadUserDTO("6", "vorname_2_3_1", "nachname_2_3_1", "lhmobjectId_2_3_1", true, null, null, "lhmobjectId_2_3_1", List.of(0, 1), Map.of("3", List.of("full")), null, true, null)); when(zammadService.createZammadUser(new ZammadUserDTO(null, "vorname_2_3_2", "nachname_2_3_2", "lhmobjectId_2_3_2", true, null, null, "lhmobjectId_2_3_2", List.of(0, 1), Map.of("3", List.of("full")), null, true, null))).thenReturn(new ZammadUserDTO("6", "vorname_2_3_2", "nachname_2_3_2", "lhmobjectId_2_3_2", true, null, null, "lhmobjectId_2_3_2", List.of(0, 1), Map.of("3", List.of("full")), null, true, null)); when(zammadService.createZammadUser(new ZammadUserDTO(null, "vorname_2_3_3", "nachname_2_3_3", "lhmobjectId_2_3_3", true, null, null, "lhmobjectId_2_3_3", List.of(0, 1), Map.of("3", List.of("full")), null, true, null))).thenReturn(new ZammadUserDTO("6", "vorname_2_3_3", "nachname_2_3_3", "lhmobjectId_2_3_3", true, null, null, "lhmobjectId_2_3_3", List.of(0, 1), Map.of("3", List.of("full")), null, true, null)); + + } + + protected void groupMocksCreateParentNodeTest(ZammadService zammadService) { + + when(zammadService.createZammadGroup(new ZammadGroupDTO(null, null, "shortname_2_1", true, true, "lhmobjectId_2_1", null))).thenReturn(new ZammadGroupDTO("1", null, "shortname_2_1", true, true, "lhmobjectId_2_1", null)); + when(zammadService.createZammadUser(new ZammadUserDTO(null, "vorname_2_3_1", "nachname_2_3_1", "lhmobjectId_2_3_1", true, null, null, "lhmobjectId_2_3_1", List.of(0, 1), Map.of("1", List.of("full")), null, true, null))).thenReturn(new ZammadUserDTO("1", "vorname_2_3_1", "nachname_2_3_1", "lhmobjectId_2_3_1", true, null, null, "lhmobjectId_2_3_1", List.of(0, 1), Map.of("1", List.of("full")), null, true, null)); + when(zammadService.createZammadUser(new ZammadUserDTO(null, "vorname_2_3_2", "nachname_2_3_2", "lhmobjectId_2_3_2", true, null, null, "lhmobjectId_2_3_2", List.of(0, 1), Map.of("1", List.of("full")), null, true, null))).thenReturn(new ZammadUserDTO("2", "vorname_2_3_2", "nachname_2_3_2", "lhmobjectId_2_3_2", true, null, null, "lhmobjectId_2_3_2", List.of(0, 1), Map.of("1", List.of("full")), null, true, null)); + when(zammadService.createZammadUser(new ZammadUserDTO(null, "vorname_2_3_3", "nachname_2_3_3", "lhmobjectId_2_3_3", true, null, null, "lhmobjectId_2_3_3", List.of(0, 1), Map.of("1", List.of("full")), null, true, null))).thenReturn(new ZammadUserDTO("2", "vorname_2_3_3", "nachname_2_3_3", "lhmobjectId_2_3_3", true, null, null, "lhmobjectId_2_3_3", List.of(0, 1), Map.of("1", List.of("full")), null, true, null)); + + when(zammadService.createZammadGroup(new ZammadGroupDTO(null, null, "shortname_0_1", true, true, "lhmobjectId_0_1", null))).thenReturn(new ZammadGroupDTO("2", null, "shortname_0_1", true, true, "lhmobjectId_0_1", null)); + when(zammadService.createZammadGroup(new ZammadGroupDTO(null, "2", "shortname_0_1::shortname_1_1", true, true, "lhmobjectId_1_1", null))).thenReturn(new ZammadGroupDTO("3", "2", "shortname_0_1::shortname_1_1", true, true, "lhmobjectId_1_1", null)); + } @@ -189,7 +202,7 @@ private Map createNextDnLevel(Integer level) { } - private List createLdapOuUser(Integer level, Integer no) { + protected List createLdapOuUser(Integer level, Integer no) { var userNo = 0; var user = new ArrayList(); @@ -215,7 +228,7 @@ private List createLdapOuUser(Integer level, Integer no) { return user; } - private EnhancedLdapOuSearchResultDTO createEnhancedLdapOuSearchResultDTO(Integer level, Integer no) { + protected EnhancedLdapOuSearchResultDTO createEnhancedLdapOuSearchResultDTO(Integer level, Integer no) { var ou = new EnhancedLdapOuSearchResultDTO(); @@ -253,4 +266,19 @@ protected ZammadProperties createZammadProperties() { return zammadProperties; } + protected Map createChildLdapTree() { + + var dn = "dn_level_0_no_1"; + var rootNode = new LdapOuNode(dn, createEnhancedLdapOuSearchResultDTO(0,1), createNextDnLevel(1), createLdapOuUser(0, 0) ); + + var root = new HashMap(); + root.put(dn, rootNode); + + log.info("Test groups created: " + rootNode.flatListLdapOuDTO().size()); + log.info("Test user created: " + rootNode.flatListLdapUserDTO().size()); + log.info(rootNode.toString()); + + return root; + } + }