Search in sources :

Example 1 with UserInfo

use of com.artezio.arttime.services.integration.spi.UserInfo in project ART-TIME by Artezio.

the class KeycloakClientTest method testLoadUserGroups.

@Test
public void testLoadUserGroups() throws NoSuchFieldException {
    keycloakClient = createMockBuilder(KeycloakClient.class).addMockedMethod("listGroups", UserRepresentation.class, RealmResource.class).createMock();
    setField(keycloakClient, "settings", settings);
    String departmentAttribute = "department";
    settings.setKeycloakUserDepartmentAttribute(departmentAttribute);
    String username1 = "user1";
    String username2 = "user2";
    String username3 = "user3";
    String group1 = "grp1";
    String group2 = "grp2";
    String group3 = "grp3";
    Map<String, List<String>> user1attributes = new HashMap<>();
    Map<String, List<String>> user2attributes = new HashMap<>();
    Map<String, List<String>> user3attributes = new HashMap<>();
    List<String> user1groups = Arrays.asList(group1, group2);
    List<String> user2groups = Arrays.asList(group1, group3);
    List<String> user3groups = Arrays.asList(group3);
    user1attributes.put(departmentAttribute, user1groups);
    user2attributes.put(departmentAttribute, user2groups);
    user3attributes.put(departmentAttribute, user3groups);
    UserRepresentation user1 = new UserRepresentation();
    UserRepresentation user2 = new UserRepresentation();
    UserRepresentation user3 = new UserRepresentation();
    user1.setUsername(username1);
    user2.setUsername(username2);
    user3.setUsername(username3);
    user1.setAttributes(user1attributes);
    user2.setAttributes(user2attributes);
    user3.setAttributes(user3attributes);
    RealmResource mockRealm = mock(RealmResource.class);
    expect(keycloakClient.listGroups(user1, mockRealm)).andReturn(new HashSet<>(user1groups));
    expect(keycloakClient.listGroups(user2, mockRealm)).andReturn(new HashSet<>(user2groups));
    expect(keycloakClient.listGroups(user3, mockRealm)).andReturn(new HashSet<>(user3groups));
    replay(keycloakClient);
    Map<String, List<UserInfo>> actual = keycloakClient.loadUserGroups(Arrays.asList(user1, user2, user3), mockRealm);
    verify(keycloakClient);
    assertTrue(actual.containsKey(group1));
    assertTrue(actual.containsKey(group2));
    assertTrue(actual.containsKey(group3));
    List<UserInfo> userInfosGroup1 = actual.get(group1);
    List<UserInfo> userInfosGroup2 = actual.get(group2);
    List<UserInfo> userInfosGroup3 = actual.get(group3);
    assertTrue(userInfosGroup1.stream().anyMatch(u -> u.getUsername().equals(username1)));
    assertTrue(userInfosGroup1.stream().anyMatch(u -> u.getUsername().equals(username2)));
    assertFalse(userInfosGroup1.stream().anyMatch(u -> u.getUsername().equals(username3)));
    assertTrue(userInfosGroup2.stream().anyMatch(u -> u.getUsername().equals(username1)));
    assertFalse(userInfosGroup2.stream().anyMatch(u -> u.getUsername().equals(username2)));
    assertFalse(userInfosGroup2.stream().anyMatch(u -> u.getUsername().equals(username3)));
    assertFalse(userInfosGroup3.stream().anyMatch(u -> u.getUsername().equals(username1)));
    assertTrue(userInfosGroup3.stream().anyMatch(u -> u.getUsername().equals(username2)));
    assertTrue(userInfosGroup3.stream().anyMatch(u -> u.getUsername().equals(username3)));
}
Also used : TimerService(javax.ejb.TimerService) java.util(java.util) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) Settings(com.artezio.arttime.config.Settings) RunWith(org.junit.runner.RunWith) TimerConfig(javax.ejb.TimerConfig) UsersResource(org.keycloak.admin.client.resource.UsersResource) PrivateAccessor.setField(junitx.util.PrivateAccessor.setField) TestSubject(org.easymock.TestSubject) Test(org.junit.Test) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) EasyMock(org.easymock.EasyMock) Setting(com.artezio.arttime.config.Setting) UserInfo(com.artezio.arttime.services.integration.spi.UserInfo) EnvironmentVariables(org.junit.contrib.java.lang.system.EnvironmentVariables) Timer(javax.ejb.Timer) Rule(org.junit.Rule) Duration(java.time.Duration) EasyMockRunner(org.easymock.EasyMockRunner) UserResource(org.keycloak.admin.client.resource.UserResource) Assert(org.junit.Assert) Before(org.junit.Before) RealmResource(org.keycloak.admin.client.resource.RealmResource) UserInfo(com.artezio.arttime.services.integration.spi.UserInfo) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 2 with UserInfo

use of com.artezio.arttime.services.integration.spi.UserInfo in project ART-TIME by Artezio.

the class KeycloakClientTest method testPopulateCache.

@Test
public void testPopulateCache() {
    keycloakClient = createMockBuilder(KeycloakClient.class).addMockedMethod("loadUsers", RealmResource.class).addMockedMethod("loadDepartments", List.class).addMockedMethod("loadUserGroups", List.class, RealmResource.class).addMockedMethod("toUserInfo", List.class).addMockedMethod("getRealm").createMock();
    RealmResource mockRealm = createMock(RealmResource.class);
    List<UserRepresentation> usersList = new ArrayList<>();
    Set<String> departments = new HashSet<>();
    expect(keycloakClient.getRealm()).andReturn(mockRealm);
    expect(keycloakClient.loadUsers(mockRealm)).andReturn(usersList);
    expect(keycloakClient.loadDepartments(usersList)).andReturn(departments);
    expect(keycloakClient.loadUserGroups(usersList, mockRealm)).andReturn(Collections.emptyMap());
    UserInfo userInfo = new UserInfo("username", null, null, null, null);
    expect(keycloakClient.toUserInfo(anyObject(List.class))).andReturn(Arrays.asList(userInfo)).anyTimes();
    replay(keycloakClient);
    keycloakClient.populateCache();
    verify(keycloakClient);
}
Also used : RealmResource(org.keycloak.admin.client.resource.RealmResource) UserInfo(com.artezio.arttime.services.integration.spi.UserInfo) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) Test(org.junit.Test)

Example 3 with UserInfo

use of com.artezio.arttime.services.integration.spi.UserInfo in project ART-TIME by Artezio.

the class LdapAdapterTest method testGetTeamByDepartment.

@Test
public void testGetTeamByDepartment() {
    String department = "dep1";
    String dummyDepartment = "someOtherDepartment";
    UserInfo user1 = new UserInfo("a_user", "a", "a", null, department);
    UserInfo user2 = new UserInfo("b_user", "b", "b", null, dummyDepartment);
    UserInfo user3 = new UserInfo("c_user", "c", "c", null, department);
    List<UserInfo> users = Arrays.asList(user2, user3, user1);
    expect(ldapClient.listUsers()).andReturn(users);
    expect(workdaysCalendarRepository.findDefaultCalendar(anyObject())).andReturn(null).anyTimes();
    EasyMock.replay(ldapClient, workdaysCalendarRepository);
    List<Employee> actual = ldapAdapter.getTeamByDepartment(department);
    verify(ldapClient);
    assertEquals(2, actual.size());
    assertEquals(user1.getUsername(), actual.get(0).getUserName());
    assertEquals(user3.getUsername(), actual.get(1).getUserName());
}
Also used : Employee(com.artezio.arttime.datamodel.Employee) UserInfo(com.artezio.arttime.services.integration.spi.UserInfo) Test(org.junit.Test)

Example 4 with UserInfo

use of com.artezio.arttime.services.integration.spi.UserInfo in project ART-TIME by Artezio.

the class KeycloakAdapterTest method testGetTeamByGroupCode.

@Test
public void testGetTeamByGroupCode() {
    String code = "code";
    UserInfo user1 = new UserInfo("2user", "b", "b", null, null);
    UserInfo user2 = new UserInfo("1user", "a", "a", null, null);
    List<UserInfo> users = Arrays.asList(user2, user1);
    expect(keycloakClient.listUsers(code)).andReturn(users);
    expect(workdaysCalendarRepository.findDefaultCalendar(anyObject())).andReturn(null).anyTimes();
    replay(keycloakClient, workdaysCalendarRepository);
    List<Employee> actual = keycloak.getTeamByGroupCode(code);
    verify(keycloakClient);
    assertEquals(2, actual.size());
    assertEquals(user2.getUsername(), actual.get(0).getUserName());
    assertEquals(user1.getUsername(), actual.get(1).getUserName());
}
Also used : Employee(com.artezio.arttime.datamodel.Employee) UserInfo(com.artezio.arttime.services.integration.spi.UserInfo) Test(org.junit.Test)

Example 5 with UserInfo

use of com.artezio.arttime.services.integration.spi.UserInfo in project ART-TIME by Artezio.

the class KeycloakAdapterTest method testFindEmployeesByFullName.

@Test
public void testFindEmployeesByFullName() {
    UserInfo userInfo1 = new UserInfo("uname1", "a_fname", "a_lname", "em1", "dep1");
    UserInfo userInfo2 = new UserInfo("uname2", "b_fname", "b_lname", "em2", "dep2");
    UserInfo userInfo3 = new UserInfo("uname3", "c_fname", "a_lname", "em2", "dep2");
    expect(keycloakClient.listUsers()).andReturn(Arrays.asList(userInfo3, userInfo1, userInfo2));
    expect(workdaysCalendarRepository.findDefaultCalendar(anyString())).andReturn(null).times(3);
    replay(keycloakClient, workdaysCalendarRepository);
    List<Employee> actual = keycloak.findEmployeesByFullName("a_lname");
    verify(keycloakClient, workdaysCalendarRepository);
    assertFalse(actual.isEmpty());
    assertEquals("uname1", actual.get(0).getUserName());
    assertEquals("uname3", actual.get(1).getUserName());
}
Also used : Employee(com.artezio.arttime.datamodel.Employee) UserInfo(com.artezio.arttime.services.integration.spi.UserInfo) Test(org.junit.Test)

Aggregations

UserInfo (com.artezio.arttime.services.integration.spi.UserInfo)19 Test (org.junit.Test)18 Employee (com.artezio.arttime.datamodel.Employee)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)3 SearchControls (javax.naming.directory.SearchControls)2 SearchResult (javax.naming.directory.SearchResult)2 InitialLdapContext (javax.naming.ldap.InitialLdapContext)2 RealmResource (org.keycloak.admin.client.resource.RealmResource)2 Setting (com.artezio.arttime.config.Setting)1 Settings (com.artezio.arttime.config.Settings)1 Duration (java.time.Duration)1 java.util (java.util)1 Timer (javax.ejb.Timer)1 TimerConfig (javax.ejb.TimerConfig)1 TimerService (javax.ejb.TimerService)1 NamingEnumeration (javax.naming.NamingEnumeration)1 NamingException (javax.naming.NamingException)1 Attributes (javax.naming.directory.Attributes)1 PrivateAccessor.setField (junitx.util.PrivateAccessor.setField)1