Search in sources :

Example 1 with User

use of org.nextprot.api.user.domain.User in project nextprot-api by calipho-sib.

the class UsersExtractorTest method testExtractData.

@Test
public void testExtractData() throws Exception {
    UsersExtractor extractor = new UsersExtractor();
    List<Map<String, ?>> records = new ArrayList<Map<String, ?>>();
    records.add(ImmutableMap.of("user_id", 24L, "user_name", "tahitibob", "first_name", "tahiti", "last_name", "bob", "role_name", ""));
    records.add(ImmutableMap.of("user_id", 23L, "user_name", "spongebob", "first_name", "", "last_name", "", "role_name", "ROLE_USER"));
    records.add(ImmutableMap.of("user_id", 23L, "user_name", "spongebob", "first_name", "", "last_name", "", "role_name", "ROLE_ADMIN"));
    List<User> users = extractor.extractData(mockResultSet(records));
    Assert.assertEquals(2, users.size());
    User user = users.get(0);
    Assert.assertEquals(24, user.getId());
    Assert.assertEquals("tahitibob", user.getUsername());
    Assert.assertEquals("tahiti", user.getFirstName());
    Assert.assertEquals("bob", user.getLastName());
    Assert.assertTrue(user.getAuthorities().isEmpty());
    user = users.get(1);
    Assert.assertEquals(23, user.getId());
    Assert.assertEquals("spongebob", user.getUsername());
    Assert.assertNull(user.getFirstName());
    Assert.assertNull(user.getLastName());
    Assert.assertTrue(!user.getAuthorities().isEmpty());
    Assert.assertTrue(user.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_USER")));
    Assert.assertTrue(user.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_ADMIN")));
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.nextprot.api.user.domain.User) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Example 2 with User

use of org.nextprot.api.user.domain.User in project nextprot-api by calipho-sib.

the class UserDaoTest method testCreateAndGetUser.

@Test
public void testCreateAndGetUser() {
    User user = new User();
    user.setUsername("freud");
    long id = userDao.createUser(user);
    Assert.assertTrue(id > 0);
    user = userDao.getUserByUsername(user.getUsername());
    Assert.assertNull(user.getFirstName());
    Assert.assertNull(user.getLastName());
    Assert.assertTrue(user.getAuthorities().isEmpty());
}
Also used : User(org.nextprot.api.user.domain.User) UserResourceBaseTest(org.nextprot.api.user.dao.test.base.UserResourceBaseTest) Test(org.junit.Test)

Example 3 with User

use of org.nextprot.api.user.domain.User in project nextprot-api by calipho-sib.

the class UserDaoTest method testReadUser2.

@Test
public void testReadUser2() {
    User user = userDao.getUserByUsername("tahitibob");
    Assert.assertEquals(24, user.getId());
    Assert.assertEquals("tahitibob", user.getUsername());
    Assert.assertEquals("tahiti", user.getFirstName());
    Assert.assertEquals("bob", user.getLastName());
    Assert.assertTrue(user.getAuthorities().isEmpty());
}
Also used : User(org.nextprot.api.user.domain.User) UserResourceBaseTest(org.nextprot.api.user.dao.test.base.UserResourceBaseTest) Test(org.junit.Test)

Example 4 with User

use of org.nextprot.api.user.domain.User in project nextprot-api by calipho-sib.

the class UserResourceAuthorizationServiceTest method mockUser.

private static User mockUser(String name) {
    User user = Mockito.mock(User.class);
    Mockito.when(user.getUsername()).thenReturn(name);
    return user;
}
Also used : User(org.nextprot.api.user.domain.User)

Example 5 with User

use of org.nextprot.api.user.domain.User in project nextprot-api by calipho-sib.

the class UsersExtractor method extractData.

public List<User> extractData(ResultSet resultSet) throws SQLException, DataAccessException {
    LinkedHashMap<Long, User> usersById = new LinkedHashMap<Long, User>();
    Set<GrantedAuthority> roles;
    while (resultSet.next()) {
        long id = resultSet.getLong("user_id");
        String roleName = resultSet.getString("role_name");
        if (!usersById.containsKey(id)) {
            User user = new User();
            roles = new HashSet<GrantedAuthority>();
            user.setId(id);
            user.setUsername(resultSet.getString("user_name"));
            user.setFirstName(resultSet.getString("first_name"));
            user.setLastName(resultSet.getString("last_name"));
            user.setAuthorities(roles);
            usersById.put(id, user);
        } else {
            roles = usersById.get(id).getAuthorities();
        }
        if (roleName != null)
            roles.add(new SimpleGrantedAuthority(roleName));
    }
    List<User> users = new ArrayList<User>(usersById.values());
    if (sort)
        Collections.sort(users, userComparator);
    return users;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.nextprot.api.user.domain.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

User (org.nextprot.api.user.domain.User)12 Test (org.junit.Test)9 UserResourceBaseTest (org.nextprot.api.user.dao.test.base.UserResourceBaseTest)7 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)7 GrantedAuthority (org.springframework.security.core.GrantedAuthority)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 HashSet (java.util.HashSet)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)1 NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)1