Search in sources :

Example 16 with UserInfo

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

the class KeycloakAdapterTest 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(keycloakClient.listUsers()).andReturn(users);
    expect(workdaysCalendarRepository.findDefaultCalendar(anyObject())).andReturn(null).anyTimes();
    replay(keycloakClient, workdaysCalendarRepository);
    List<Employee> actual = keycloak.getTeamByDepartment(department);
    verify(keycloakClient);
    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 17 with UserInfo

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

the class KeycloakAdapterTest method testGetEmployees.

@Test
public void testGetEmployees() {
    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.getEmployees();
    verify(keycloakClient, workdaysCalendarRepository);
    assertEquals(3, actual.size());
    assertEquals("uname1", actual.get(0).getUserName());
    assertEquals("uname3", actual.get(1).getUserName());
    assertEquals("uname2", actual.get(2).getUserName());
}
Also used : Employee(com.artezio.arttime.datamodel.Employee) UserInfo(com.artezio.arttime.services.integration.spi.UserInfo) Test(org.junit.Test)

Example 18 with UserInfo

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

the class LdapClientTest method testListUsers.

@Test
public void testListUsers() throws Exception {
    final String userContextDN = "user-cdn";
    final String filterExpression = "filter-expr";
    final Object[] filterArgs = new Object[] {};
    LdapClient.Filter mockFilter = createMock(LdapClient.Filter.class);
    expect(mockFilter.getExpression()).andReturn(filterExpression).anyTimes();
    expect(mockFilter.getArgs()).andReturn(filterArgs).anyTimes();
    SearchControls mockControls = createMock(SearchControls.class);
    InitialLdapContext mockContext = createMock(InitialLdapContext.class);
    ldapClient = createMockBuilder(LdapClient.class).addMockedMethod("initializeContext", new Class[] {}).addMockedMethod("makeSearchControls").createMock();
    settings.setLdapUserContextDN(userContextDN);
    setField(ldapClient, "settings", settings);
    setAttributeMapping();
    NamingEnumeration enumeration = createMock(NamingEnumeration.class);
    SearchResult result1 = createMock(SearchResult.class);
    SearchResult result2 = createMock(SearchResult.class);
    expect(enumeration.hasMore()).andReturn(true).times(2);
    expect(enumeration.hasMore()).andReturn(false).once();
    expect(enumeration.next()).andReturn(result1).once();
    expect(enumeration.next()).andReturn(result2).once();
    enumeration.close();
    expectLastCall().andVoid();
    Employee expectedEmployee1 = new Employee("uname1", "fname1", "lname1", "email1", "dep1");
    Employee expectedEmployee2 = new Employee("uname2", "fname2", "lname2", "email2", "dep2");
    expect(result1.getAttributes()).andReturn(getAttributesFor(expectedEmployee1));
    expect(result2.getAttributes()).andReturn(getAttributesFor(expectedEmployee2));
    expect(ldapClient.makeSearchControls()).andReturn(mockControls);
    expect(ldapClient.initializeContext()).andReturn(mockContext);
    mockContext.close();
    expectLastCall().once().andVoid();
    expect(mockContext.search(eq(userContextDN), eq(filterExpression), eq(filterArgs), eq(mockControls))).andReturn(enumeration);
    replay(ldapClient, mockContext, mockControls, mockFilter, enumeration, result1, result2);
    List<UserInfo> actualList = ldapClient.listUsers(mockFilter);
    assertEquals(2, actualList.size());
    UserInfo actualUser1 = actualList.stream().filter(e -> e.getUsername().equals(expectedEmployee1.getUserName())).findFirst().get();
    UserInfo actualUser2 = actualList.stream().filter(e -> e.getUsername().equals(expectedEmployee2.getUserName())).findFirst().get();
    assertTrue(isEmployeeFullyEqualToUserInfo(expectedEmployee1, actualUser1));
    assertTrue(isEmployeeFullyEqualToUserInfo(expectedEmployee2, actualUser2));
}
Also used : NamingEnumeration(javax.naming.NamingEnumeration) SearchResult(javax.naming.directory.SearchResult) UserInfo(com.artezio.arttime.services.integration.spi.UserInfo) Employee(com.artezio.arttime.datamodel.Employee) InitialLdapContext(javax.naming.ldap.InitialLdapContext) SearchControls(javax.naming.directory.SearchControls) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with UserInfo

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

the class LdapClient method listUsers.

protected List<UserInfo> listUsers(Filter filter) {
    List<UserInfo> users = new ArrayList<>();
    InitialLdapContext ctx = null;
    try {
        ctx = initializeContext();
        SearchControls controls = makeSearchControls();
        NamingEnumeration<SearchResult> answer = ctx.search(settings.getLdapUserContextDN(), filter.getExpression(), filter.getArgs(), controls);
        while (answer.hasMore()) {
            SearchResult sr = answer.next();
            Attributes attrs = sr.getAttributes();
            UserInfo user = createUserInfo(attrs);
            users.add(user);
        }
        answer.close();
    } catch (NamingException ex) {
        throw new RuntimeException("Error listing users ", ex);
    } finally {
        closeContext(ctx);
    }
    return users;
}
Also used : InitialLdapContext(javax.naming.ldap.InitialLdapContext) Attributes(javax.naming.directory.Attributes) UserInfo(com.artezio.arttime.services.integration.spi.UserInfo) SearchControls(javax.naming.directory.SearchControls) SearchResult(javax.naming.directory.SearchResult) NamingException(javax.naming.NamingException)

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