// Path: src/main/java/com/jeanchampemont/notedown/NoteDownApplication.java
// @Configuration
// @ComponentScan
// @EnableAutoConfiguration
// @EnableScheduling
// public class NoteDownApplication {
// 
//     public static void main(String[] args) {
//         SpringApplication.run(NoteDownApplication.class, args);
//     }
// 
// }
// 
// Path: src/main/java/com/jeanchampemont/notedown/security/AuthenticationService.java
// @Service
// public class AuthenticationService {
//     private UserRepository userRepository;
// 
//     private AuthenticationManager authenticationManager;
// 
//     @Autowired
//     public AuthenticationService(UserRepository userRepository, AuthenticationManager authenticationManager) {
//         this.userRepository = userRepository;
//         this.authenticationManager = authenticationManager;
//     }
// 
//     public User getCurrentUser() {
//         String email = SecurityContextHolder.getContext().getAuthentication().getName();
//         return userRepository.findByEmailIgnoreCase(email);
//     }
// 
//     public boolean isAuthenticated() {
//         return (SecurityContextHolder.getContext().getAuthentication() != null);
//     }
// 
//     public void newAuthentication(String email, String password) {
//         Authentication request = new UsernamePasswordAuthenticationToken(email, password);
//         Authentication result = authenticationManager.authenticate(request);
//         SecurityContextHolder.getContext().setAuthentication(result);
//     }
// }
// 
// Path: src/main/java/com/jeanchampemont/notedown/user/persistence/User.java
// @Entity
// @Table(name = "user_account")
// public class User {
//     @Id
//     @GeneratedValue(strategy = GenerationType.TABLE)
//     private long id;
// 
//     @Column(name = "email", unique = true, length = 64)
//     private String email;
// 
//     @Column(name = "password", length = 60)
//     private String password;
// 
//     @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
//     private Set<Note> notes = new HashSet<>();
// 
//     @Column(name = "locale", length = 2)
//     private String locale;
// 
//     @Column(name = "display_name", length = 64)
//     private String displayName;
// 
//     public long getId() {
//         return id;
//     }
// 
//     public void setId(long id) {
//         this.id = id;
//     }
// 
//     public String getEmail() {
//         return email;
//     }
// 
//     public void setEmail(String email) {
//         this.email = email;
//     }
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public void setPassword(String password) {
//         this.password = password;
//     }
// 
//     public Set<Note> getNotes() {
//         return notes;
//     }
// 
//     public void setNotes(Set<Note> notes) {
//         this.notes = notes;
//     }
// 
//     public String getLocale() {
//         return locale;
//     }
// 
//     public void setLocale(String locale) {
//         this.locale = locale;
//     }
// 
//     public String getDisplayName() {
//         return displayName;
//     }
// 
//     public void setDisplayName(String displayName) {
//         this.displayName = displayName;
//     }
// }
// 
// Path: src/main/java/com/jeanchampemont/notedown/user/persistence/repository/UserRepository.java
// public interface UserRepository extends CrudRepository<User, Long> {
//     User findByEmailIgnoreCase(String email);
// }
// 
// Path: src/main/java/com/jeanchampemont/notedown/utils/exception/OperationNotAllowedException.java
// public class OperationNotAllowedException extends RuntimeException {
// 
// }


import java.util.Optional;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
import com.jeanchampemont.notedown.NoteDownApplication;
import com.jeanchampemont.notedown.security.AuthenticationService;
import com.jeanchampemont.notedown.user.persistence.User;
import com.jeanchampemont.notedown.user.persistence.repository.UserRepository;
import com.jeanchampemont.notedown.utils.exception.OperationNotAllowedException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
        when(repoMock.findByEmailIgnoreCase(email)).thenReturn(user);
        when(authenticationServiceMock.getCurrentUser()).thenReturn(user);
        when(encoderMock.matches("wrongPassword", password)).thenReturn(false);

        boolean success = sut.changePassword(user, "wrongPassword", newPassword);

        verify(repoMock).findByEmailIgnoreCase(email);
        verify(encoderMock).matches(anyString(), eq(password));

        assertFalse(success);
    }

    @Test
    public void testChangePasswordNotAllowed() {
        String email = "toto@tata.fr";
        String password = "password";
        String newPassword = "superSafePassword";

        User user = new User();
        user.setEmail(email);
        user.setPassword(password);

        User notAuthorizedUser = new User();
        notAuthorizedUser.setId(42);

        when(repoMock.findByEmailIgnoreCase(email)).thenReturn(user);
        when(authenticationServiceMock.getCurrentUser()).thenReturn(notAuthorizedUser);

        boolean success = sut.changePassword(user, password, newPassword);

// Path: src/main/java/com/jeanchampemont/notedown/NoteDownApplication.java
// @Configuration
// @ComponentScan
// @EnableAutoConfiguration
// @EnableScheduling
// public class NoteDownApplication {
// 
//     public static void main(String[] args) {
//         SpringApplication.run(NoteDownApplication.class, args);
//     }
// 
// }
// 
// Path: src/main/java/com/jeanchampemont/notedown/security/AuthenticationService.java
// @Service
// public class AuthenticationService {
//     private UserRepository userRepository;
// 
//     private AuthenticationManager authenticationManager;
// 
//     @Autowired
//     public AuthenticationService(UserRepository userRepository, AuthenticationManager authenticationManager) {
//         this.userRepository = userRepository;
//         this.authenticationManager = authenticationManager;
//     }
// 
//     public User getCurrentUser() {
//         String email = SecurityContextHolder.getContext().getAuthentication().getName();
//         return userRepository.findByEmailIgnoreCase(email);
//     }
// 
//     public boolean isAuthenticated() {
//         return (SecurityContextHolder.getContext().getAuthentication() != null);
//     }
// 
//     public void newAuthentication(String email, String password) {
//         Authentication request = new UsernamePasswordAuthenticationToken(email, password);
//         Authentication result = authenticationManager.authenticate(request);
//         SecurityContextHolder.getContext().setAuthentication(result);
//     }
// }
// 
// Path: src/main/java/com/jeanchampemont/notedown/user/persistence/User.java
// @Entity
// @Table(name = "user_account")
// public class User {
//     @Id
//     @GeneratedValue(strategy = GenerationType.TABLE)
//     private long id;
// 
//     @Column(name = "email", unique = true, length = 64)
//     private String email;
// 
//     @Column(name = "password", length = 60)
//     private String password;
// 
//     @OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
//     private Set<Note> notes = new HashSet<>();
// 
//     @Column(name = "locale", length = 2)
//     private String locale;
// 
//     @Column(name = "display_name", length = 64)
//     private String displayName;
// 
//     public long getId() {
//         return id;
//     }
// 
//     public void setId(long id) {
//         this.id = id;
//     }
// 
//     public String getEmail() {
//         return email;
//     }
// 
//     public void setEmail(String email) {
//         this.email = email;
//     }
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public void setPassword(String password) {
//         this.password = password;
//     }
// 
//     public Set<Note> getNotes() {
//         return notes;
//     }
// 
//     public void setNotes(Set<Note> notes) {
//         this.notes = notes;
//     }
// 
//     public String getLocale() {
//         return locale;
//     }
// 
//     public void setLocale(String locale) {
//         this.locale = locale;
//     }
// 
//     public String getDisplayName() {
//         return displayName;
//     }
// 
//     public void setDisplayName(String displayName) {
//         this.displayName = displayName;
//     }
// }
// 
// Path: src/main/java/com/jeanchampemont/notedown/user/persistence/repository/UserRepository.java
// public interface UserRepository extends CrudRepository<User, Long> {
//     User findByEmailIgnoreCase(String email);
// }
// 
// Path: src/main/java/com/jeanchampemont/notedown/utils/exception/OperationNotAllowedException.java
// public class OperationNotAllowedException extends RuntimeException {
// 
// }


// Path: src/test/java/com/jeanchampemont/notedown/user/UserServiceTest.java
import java.util.Optional;
import static org.junit.Assert.*;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
import com.jeanchampemont.notedown.NoteDownApplication;
import com.jeanchampemont.notedown.security.AuthenticationService;
import com.jeanchampemont.notedown.user.persistence.User;
import com.jeanchampemont.notedown.user.persistence.repository.UserRepository;
import com.jeanchampemont.notedown.utils.exception.OperationNotAllowedException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

        when(repoMock.findByEmailIgnoreCase(email)).thenReturn(user);
        when(authenticationServiceMock.getCurrentUser()).thenReturn(user);
        when(encoderMock.matches("wrongPassword", password)).thenReturn(false);

        boolean success = sut.changePassword(user, "wrongPassword", newPassword);

        verify(repoMock).findByEmailIgnoreCase(email);
        verify(encoderMock).matches(anyString(), eq(password));

        assertFalse(success);
    }

    @Test
    public void testChangePasswordNotAllowed() {
        String email = "toto@tata.fr";
        String password = "password";
        String newPassword = "superSafePassword";

        User user = new User();
        user.setEmail(email);
        user.setPassword(password);

        User notAuthorizedUser = new User();
        notAuthorizedUser.setId(42);

        when(repoMock.findByEmailIgnoreCase(email)).thenReturn(user);
        when(authenticationServiceMock.getCurrentUser()).thenReturn(notAuthorizedUser);

        boolean success = sut.changePassword(user, password, newPassword);

        verify(repoMock).findByEmailIgnoreCase(email);