Skip to content

Commit

Permalink
CreateParentNodeTest added.
Browse files Browse the repository at this point in the history
  • Loading branch information
sfi2022 committed Sep 26, 2024
1 parent 49ad288 commit 8d7a9ea
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,14 +20,20 @@

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 {

@Captor
private ArgumentCaptor<ZammadGroupDTO> createGroupCaptor;

@Captor
private ArgumentCaptor<ZammadGroupDTO> updateGroupCaptor;

@Captor
private ArgumentCaptor<ZammadUserDTO> userUserCaptor;

Expand Down Expand Up @@ -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<String, LdapOuNode>();
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<String, LdapOuNode>(), null);
childTree_level_2.put(dn, child_level_2);

var childTree_level_1 = new TreeMap<String, LdapOuNode>();
level = 1;
dn = String.format("dn_level_%d_no_%d", level, number);
var child_level_1 = new LdapOuNode(dn, createEnhancedLdapOuSearchResultDTO(level,number), new TreeMap<String, LdapOuNode>(), null);
child_level_1.setChildNodes(childTree_level_2);
childTree_level_1.put(dn, child_level_1);

var childTree_level_0 = new TreeMap<String, LdapOuNode>();
level = 0;
dn = String.format("dn_level_%d_no_%d", level, number);
var child_level_0 = new LdapOuNode(dn, createEnhancedLdapOuSearchResultDTO(level,number), new TreeMap<String, LdapOuNode>(), 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());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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));

}


Expand Down Expand Up @@ -189,7 +202,7 @@ private Map<String, LdapOuNode> createNextDnLevel(Integer level) {
}


private List<EnhancedLdapUserDto> createLdapOuUser(Integer level, Integer no) {
protected List<EnhancedLdapUserDto> createLdapOuUser(Integer level, Integer no) {

var userNo = 0;
var user = new ArrayList<EnhancedLdapUserDto>();
Expand All @@ -215,7 +228,7 @@ private List<EnhancedLdapUserDto> 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();

Expand Down Expand Up @@ -253,4 +266,19 @@ protected ZammadProperties createZammadProperties() {
return zammadProperties;
}

protected Map<String, LdapOuNode> 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<String, LdapOuNode>();
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;
}

}

0 comments on commit 8d7a9ea

Please sign in to comment.