Search in sources :

Example 6 with User

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

the class UserDaoImpl method getUserByUsername.

@Override
public User getUserByUsername(String username) throws DataAccessException {
    Map<String, String> namedParams = new HashMap<String, String>();
    namedParams.put("user_name", username);
    String sql = sqlDictionary.getSQLQuery("read-user-by-name");
    NamedParameterJdbcTemplate template = new NamedParameterJdbcTemplate(dsLocator.getUserDataSource());
    List<User> users = template.query(sql, namedParams, new UsersExtractor());
    if (users.isEmpty())
        throw new EmptyResultDataAccessException(1);
    return users.get(0);
}
Also used : User(org.nextprot.api.user.domain.User) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 7 with User

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

the class UsersExtractorTest method testExtractDataSortedByUsername.

@Test
public void testExtractDataSortedByUsername() throws Exception {
    UsersExtractor extractor = new UsersExtractor(true);
    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(1);
    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(0);
    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_USER")));
}
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 8 with User

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

the class UserDaoTest method testUpdateUser.

@Test
public void testUpdateUser() {
    User updated = new User();
    updated.setId(23L);
    updated.setUsername("spongebob");
    updated.setFirstName("sponge");
    updated.setLastName("bob");
    userDao.updateUser(updated);
    User user = userDao.getUserByUsername("spongebob");
    Assert.assertEquals(23, user.getId());
    Assert.assertEquals("spongebob", user.getUsername());
    Assert.assertEquals("sponge", user.getFirstName());
    Assert.assertEquals("bob", user.getLastName());
    Assert.assertTrue(!user.getAuthorities().isEmpty());
    Assert.assertEquals(2, user.getAuthorities().size());
    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) UserResourceBaseTest(org.nextprot.api.user.dao.test.base.UserResourceBaseTest) Test(org.junit.Test)

Example 9 with User

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

the class UserDaoTest method testDeleteUser.

@Test
public void testDeleteUser() {
    User toDelete = new User();
    toDelete.setId(23);
    Assert.assertEquals(2, userDao.getUserList().size());
    userDao.deleteUser(toDelete);
    Assert.assertEquals(1, userDao.getUserList().size());
}
Also used : User(org.nextprot.api.user.domain.User) UserResourceBaseTest(org.nextprot.api.user.dao.test.base.UserResourceBaseTest) Test(org.junit.Test)

Example 10 with User

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

the class UserDaoTest method testCreateAndGetUserWithRole.

@Test
public void testCreateAndGetUserWithRole() {
    User user = new User();
    user.setUsername("freud");
    user.setFirstName("huber");
    user.setLastName("freud");
    Set<GrantedAuthority> hs = new HashSet<GrantedAuthority>();
    hs.add(new SimpleGrantedAuthority("ROLE_USER"));
    user.setAuthorities(hs);
    long id = userDao.createUser(user);
    Assert.assertTrue(id > 0);
    user = userDao.getUserByUsername(user.getUsername());
    Assert.assertEquals("huber", user.getFirstName());
    Assert.assertEquals("freud", user.getLastName());
    Assert.assertTrue(!user.getAuthorities().isEmpty());
    Assert.assertEquals(1, user.getAuthorities().size());
    Assert.assertTrue(user.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_USER")));
}
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) HashSet(java.util.HashSet) UserResourceBaseTest(org.nextprot.api.user.dao.test.base.UserResourceBaseTest) Test(org.junit.Test)

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