@Before public void setup() { user = new User("test-user", "pass"); user.setEmail("test@localhost"); user = userRepository.save(user); organization = new Organization("test-organization", user); organization = organizationRepository.save(organization); category = new Category(); category.setOrganization(organization); category.setName("Category 1"); category.setDescription("Category description"); category = categoryRepository.save(category); topic = new Topic(); topic.setCategory(category); topic.setName("Topic 1"); topic.setDescription("Topic description"); topic = topicRepository.save(topic); cardDetails1 = new CardDetails(); cardDetails1.setCategory(category); cardDetails1.setCreator(user); cardDetails1.setText("Card 1"); cardDetails1 = cardDetailsRepository.save(cardDetails1); cardDetails2 = new CardDetails(); cardDetails2.setCategory(category); cardDetails2.setCreator(user); cardDetails2.setText("Card 2"); cardDetails2 = cardDetailsRepository.save(cardDetails2); cardDetails3 = new CardDetails(); cardDetails3.setCategory(category); cardDetails3.setCreator(user); cardDetails3.setText("Card 3"); cardDetails3 = cardDetailsRepository.save(cardDetails3); cardDetails4 = new CardDetails(); cardDetails4.setCategory(category); cardDetails4.setCreator(user); cardDetails4.setText("Card 4"); cardDetails4 = cardDetailsRepository.save(cardDetails4); cardDetails5 = new CardDetails(); cardDetails5.setCategory(category); cardDetails5.setCreator(user); cardDetails5.setText("Card 5"); cardDetails5 = cardDetailsRepository.save(cardDetails5); category.setCards(new HashSet<>()); category.getCards().add(cardDetails1); category.getCards().add(cardDetails2); category.getCards().add(cardDetails3); category.getCards().add(cardDetails4); category.getCards().add(cardDetails5); category = categoryRepository.save(category); }
@Override public void checkLogin(int userId, String password) { User u = userRepository.findOne(userId); if (u == null || !passwordEncoder.matches(password, u.getPassword())) { throw new UserServiceException("Username or password isn't correct"); } }
@Test(expected = SessionServiceException.class) public void addSessionAsNonOrganizerThrowsException() { User nonOrganizer = new User("test-non-organizer", "pass"); nonOrganizer.setEmail("nonorganizer@localhost"); nonOrganizer = userRepository.save(nonOrganizer); Session session = new SynchronousSession(); session.setCategory(category); session.setOrganizer(nonOrganizer); sessionService.addSession(session); }
@Override public User addUser(User user) { validateUser(user); try { String unencryptedPassword = user.getPassword(); String encryptedPassword = passwordEncoder.encode(unencryptedPassword); user.setPassword(encryptedPassword); return userRepository.save(user); } catch (Exception e) { throw new UserServiceException( String.format("Could not save user with username %s", user.getUsername())); } }
@Override public void checkEmailAvailable(String email) { User u = userRepository.findUserByEmail(email); if (u != null) throw new UserServiceException("Email is already taken."); }
@Override public void checkUsernameAvailable(String username) { User u = userRepository.findUserByUsername(username); if (u != null) throw new UserServiceException("Username is already taken."); }
@Override public User updateUser(User user) { validateUser(user); return userRepository.save(user); }
@Override public User getUserByUserId(int userId) { return userRepository.findOne(userId); }
@Override public User getUserByEmail(String email) { return userRepository.findUserByEmail(email); }
@Override public User getUserByUsername(String username) { return userRepository.findUserByUsername(username); }