Created
October 15, 2013 11:18
-
-
Save golonzovsky/8b565084673c725498ae to your computer and use it in GitHub Desktop.
authority
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.springapp.notes.entities; | |
import javax.persistence.CascadeType; | |
import javax.persistence.Entity; | |
import javax.persistence.FetchType; | |
import javax.persistence.Id; | |
import javax.persistence.OneToMany; | |
import javax.persistence.Table; | |
import java.util.LinkedHashSet; | |
import java.util.LinkedList; | |
import java.util.List; | |
import com.sun.istack.internal.NotNull; | |
@Entity | |
@Table(name = "users") | |
public class User { | |
@Id | |
@NotNull | |
private String username; | |
private String password; | |
private Integer enabled; | |
@OneToMany(mappedBy = "user", fetch = FetchType.EAGER, | |
cascade = CascadeType.ALL, orphanRemoval = true) | |
private final List<Authority> authorities = new LinkedList<Authority>(); | |
public String getUsername() { | |
return username; | |
} | |
public void setUsername(String username) { | |
this.username = username; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
public Integer getEnabled() { | |
return enabled; | |
} | |
public void setEnabled(Integer enabled) { | |
this.enabled = enabled; | |
} | |
public List<Authority> getAuthorities() { | |
return authorities; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.springapp.notes.repository; | |
import static junit.framework.Assert.assertTrue; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertNotNull; | |
import junit.framework.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.transaction.annotation.Transactional; | |
import com.springapp.notes.entities.Authority; | |
import com.springapp.notes.entities.User; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration("classpath:applicationContext.xml") | |
@Transactional | |
public class UserRepositoryTest { | |
private static final String PASS = "dummy-pass"; | |
private static final String USERNAME = "testUser"; | |
private User user; | |
@Autowired | |
private UserRepository repo; | |
@Before | |
public void setUp() throws Exception { | |
user = new User(); | |
user.setUsername(USERNAME); | |
user.setPassword(PASS); | |
user.setEnabled(1); | |
} | |
@Test | |
public void testCreateAndGet() throws Exception { | |
repo.save(user); | |
final User found = repo.findOne(USERNAME); | |
assertNotNull(found); | |
assertEquals(PASS, found.getPassword()); | |
} | |
/** data dependent */ | |
@Test | |
public void testGetRolesForAdmin() throws Exception { | |
final User found = repo.findOne("admin"); | |
assertNotNull(found); | |
assertTrue(found.getAuthorities().size() > 0); | |
Assert.assertEquals("ROLE_ADMIN", found.getAuthorities().get(0).getAuthority()); | |
} | |
@Test | |
public void testUserWithAuth() throws Exception { | |
user.getAuthorities().add(new Authority("dummy_role_01", user)); | |
user.getAuthorities().add(new Authority("dummy_role_02", user)); | |
repo.save(user); | |
final User found = repo.findOne(USERNAME); | |
assertTrue(found.getAuthorities().size() == 2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment