Guest User

Untitled

a guest
Oct 5th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.35 KB | None | 0 0
  1. import java.io.Serializable;
  2. import java.util.HashSet;
  3. import java.util.Set;
  4.  
  5. import javax.persistence.CascadeType;
  6. import javax.persistence.Column;
  7. import javax.persistence.Entity;
  8. import javax.persistence.GeneratedValue;
  9. import javax.persistence.GenerationType;
  10. import javax.persistence.Id;
  11. import javax.persistence.OneToMany;
  12. import javax.persistence.OneToOne;
  13. import javax.persistence.Table;
  14.  
  15. @Entity
  16. @Table(name = "USERS")
  17. public class User implements Serializable {
  18.  
  19. /**
  20. *
  21. */
  22. private static final long serialVersionUID = 1L;
  23.  
  24. @Id
  25. @GeneratedValue(strategy= GenerationType.IDENTITY)
  26. @Column(name = "USERNAME")
  27. private String username;
  28.  
  29. @Column(name = "PASSWORD", nullable = false)
  30. private String password;
  31.  
  32. @Column(name = "ENABLED", nullable = false)
  33. private boolean enabled;
  34.  
  35. public User() {
  36. super();
  37. // TODO Auto-generated constructor stub
  38. }
  39.  
  40. public User(String username, String password, boolean enabled, Set<Authorities> authorities, Customer customer) {
  41. super();
  42. this.username = username;
  43. this.password = password;
  44. this.enabled = enabled;
  45. this.authorities = authorities;
  46. this.customer = customer;
  47. }
  48.  
  49. @OneToMany(cascade = CascadeType.ALL, mappedBy = "user")
  50. private Set<Authorities> authorities = new HashSet<>();
  51.  
  52. @OneToOne(cascade = CascadeType.ALL)
  53. private Customer customer;
  54. //Getter and Setter methods
  55.  
  56. public String getUsername() {
  57. return username;
  58. }
  59.  
  60. public void setUsername(String username) {
  61. this.username = username;
  62. }
  63.  
  64. public String getPassword() {
  65. return password;
  66. }
  67.  
  68. public void setPassword(String password) {
  69. this.password = password;
  70. }
  71.  
  72. public boolean isEnabled() {
  73. return enabled;
  74. }
  75.  
  76. public void setEnabled(boolean enabled) {
  77. this.enabled = enabled;
  78. }
  79.  
  80. public Set<Authorities> getAuthorities() {
  81. return authorities;
  82. }
  83.  
  84. public void setAuthorities(Set<Authorities> authorities) {
  85. this.authorities = authorities;
  86. }
  87.  
  88. public Customer getCustomer() {
  89. return customer;
  90. }
  91.  
  92. public void setCustomer(Customer customer) {
  93. this.customer = customer;
  94. }
  95.  
  96. }
  97.  
  98. package com.vishal.project.repos;
  99. import com.vishal.project.entities.User;
  100. public interface UserRepository extends CrudRepository<User,Long> {
  101.  
  102. User findUserByUsername(String username);
  103.  
  104. }
  105.  
  106. package com.vishal.project.services;
  107.  
  108. import org.springframework.beans.factory.annotation.Autowired;
  109. import org.springframework.security.core.userdetails.User.UserBuilder;
  110. import org.springframework.security.core.userdetails.UserDetails;
  111. import org.springframework.security.core.userdetails.UserDetailsService;
  112. import org.springframework.security.core.userdetails.UsernameNotFoundException;
  113. import org.springframework.stereotype.Component;
  114. import org.springframework.stereotype.Service;
  115. import org.springframework.transaction.annotation.Transactional;
  116.  
  117. import com.vishal.project.entities.User;
  118.  
  119. import com.vishal.project.repos.UserRepository;
  120.  
  121.  
  122.  
  123. @Service("UserService")
  124. public class UserDetailsServiceImpl implements UserDetailsService {
  125.  
  126. @Autowired
  127. private UserRepository userRepo;
  128.  
  129. @Transactional(readOnly = true)
  130. @Override
  131. public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  132.  
  133. User user = userRepo.findUserByUsername(username);
  134. UserBuilder builder = null;
  135. if (user != null) {
  136.  
  137. builder = org.springframework.security.core.userdetails.User.withUsername(username);
  138. builder.disabled(!user.isEnabled());
  139. builder.password(user.getPassword());
  140. String[] authorities = user.getAuthorities()
  141. .stream().map(a -> a.getAuthority()).toArray(String[]::new);
  142.  
  143. builder.authorities(authorities);
  144. } else {
  145. throw new UsernameNotFoundException("User not found.");
  146. }
  147. return builder.build();
  148. }
  149.  
  150. public User save(User user) {
  151. return userRepo.save(user);
  152. }
  153.  
  154.  
  155. }
  156.  
  157. package com.vishal.project;
  158.  
  159. import static org.junit.Assert.assertNotNull;
  160.  
  161. import org.junit.After;
  162. import org.junit.Before;
  163. import org.junit.Test;
  164. import org.slf4j.Logger;
  165. import org.slf4j.LoggerFactory;
  166. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  167. import org.springframework.context.support.GenericApplicationContext;
  168.  
  169. import org.springframework.security.core.userdetails.UserDetailsService;
  170.  
  171. import com.vishal.project.entities.User;
  172. import com.vishal.project.services.UserService;
  173.  
  174.  
  175.  
  176. public class UserDetailsTest {
  177.  
  178. private static Logger logger = LoggerFactory.getLogger(UserDetailsTest.class);
  179.  
  180. private GenericApplicationContext ctx;
  181. private UserDetailsService userDetailsService;
  182. private UserService userService;
  183.  
  184. @Before
  185. public void SetUp() {
  186. ctx = new AnnotationConfigApplicationContext(OnlineBookShoppeApplication.class);
  187. userDetailsService = ctx.getBean(UserDetailsService.class);
  188. logger.info("starting to run tests now............");
  189. assertNotNull(userDetailsService);
  190. }
  191. @Test
  192. public void testfindByUsername() {
  193. User user = userService.findByUsername("admin");
  194. String Username = user.getUsername();
  195. logger.info("Username=" + Username);
  196.  
  197. }
  198.  
  199. @After
  200. public void teardown() {
  201. ctx.close();
  202. }
  203.  
  204. }
  205.  
  206. java.lang.NullPointerException
  207. at com.vishal.project.UserDetailsTest.testfindByUsername(UserDetailsTest.java:37)
  208. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  209. at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
  210. at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
  211. at java.lang.reflect.Method.invoke(Unknown Source)
  212. at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
  213. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  214. at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
  215. at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  216. at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
  217. at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
  218. at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
  219. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
  220. at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
  221. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  222. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  223. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  224. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  225. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  226. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  227. at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
  228. at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  229. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
  230. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
  231. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
  232. at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
  233.  
  234. userDetailsService = ctx.getBean(UserDetailsService.class);
  235.  
  236. userService = ctx.getBean(UserService.class);
Add Comment
Please, Sign In to add comment