// Path: service/src/main/java/info/rolandkrueger/userservice/UserMicroserviceApplication.java
// @SpringBootApplication
// @PropertySource("userservice.properties")
// public class UserMicroserviceApplication {
// 
//     public static void main(String[] args) {
//         SpringApplication.run(
//                 new Object[]{
//                         UserMicroserviceApplication.class,
//                         DevelopmentProfileConfiguration.class}
//                 , args);
//     }
// }
// 
// Path: public-api/src/main/java/info/rolandkrueger/userservice/api/service/StaticEndpointProvider.java
// @Service
// public class StaticEndpointProvider implements UserMicroserviceEndpointProvider {
// 
//     private String endpoint;
// 
//     protected StaticEndpointProvider() {
//     }
// 
//     /**
//      * Creates a new StaticEndpointProvider for the given static endpoint URL.
//      *
//      * @param endpoint static endpoint address for the user micro-service
//      */
//     public StaticEndpointProvider(String endpoint) {
//         this.endpoint = endpoint;
//     }
// 
//     /**
//      * Returns the fixed user service endpoint address passed in through the constructor.
//      */
//     @Override
//     public String getUserMicroserviceEndpoint() {
//         return endpoint;
//     }
// }
// 
// Path: public-api/src/main/java/info/rolandkrueger/userservice/api/service/UserDetailServiceImpl.java
// @Service
// public class UserDetailServiceImpl implements UserDetailsService {
// 
//     private UserMicroserviceEndpointProvider endpointProvider;
// 
//     /**
//      * Constructor which is autowired with an instance of {@link UserMicroserviceEndpointProvider}.
//      */
//     @Autowired
//     public UserDetailServiceImpl(UserMicroserviceEndpointProvider endpointProvider) {
//         Preconditions.checkNotNull(endpointProvider);
//         this.endpointProvider = endpointProvider;
//     }
// 
//     /**
//      * Load a {@link UserDetails} object from the user micro-service for the specified username. Note that this method
//      * in addition to a {@link UsernameNotFoundException} throws an unchecked {@link RestClientException} to indicate
//      * errors that occurred during the communication process with the remote user micro-service.
//      *
//      * @param username name of the user to be loaded
//      * @return a {@link UserDetails} object
//      * @throws UsernameNotFoundException when the given username is not known to the service
//      * @throws RestClientException when some error occurred while calling the REST service
//      */
//     @Override
//     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, RestClientException {
//         UserService userService = UserServiceAPI.init(endpointProvider.getUserMicroserviceEndpoint());
//         Optional<UserApiData> loadedUser = userService.users().search().findByUsername(username).getData().stream().findFirst();
//         if (!loadedUser.isPresent()) {
//             throw new UsernameNotFoundException("User " + username + " not found");
//         }
//         return loadedUser.get().getResource().useProjection(UserProjections.FULL_DATA).read();
//     }
// }
// 
// Path: service/src/test/java/info/rolandkrueger/userservice/testsupport/AbstractRestControllerTest.java
// public abstract class AbstractRestControllerTest {
// 
//     private final static String CONTEXT_PATH = "http://localhost:";
// 
//     private static UserService service;
//     private static int port = 8080;
// 
//     protected static void setPort(int port) {
//         AbstractRestControllerTest.port = port;
//     }
// 
//     protected static UserService service() {
//         if (service == null) {
//             service = UserServiceAPI.init(CONTEXT_PATH + port);
//         }
//         return service;
//     }
// 
//     @After
//     public void tearDown(){
//         deleteAllUsers();
//         deleteAllAuthorities();
//     }
// 
//     protected static void deleteAllUsers() {
//         UsersResource users;
//         do {
//             users = service().users();
//             users.getData().stream().forEach(user -> user.getResource().delete());
//         } while (users.hasNext());
//     }
// 
//     protected static void deleteAllAuthorities() {
//         AuthoritiesResource authorities;
//         do {
//             authorities = service().authorities();
//             authorities.getData().stream().forEach(authority -> authority.getResource().delete());
//         } while (authorities.hasNext());
//     }
// 
//     protected static AuthorityApiData createAuthority(String authority) {
//         AuthorityApiData authorityApiData = new AuthorityApiData();
//         authorityApiData.setAuthority(authority);
//         authorityApiData.setDescription("The " + authority + " role");
//         return service().authorities().create(authorityApiData).getBody();
//     }
// 
//     protected static UserApiData registerUser(String username, String password, String email) {
//         ResponseEntity<UserRegistrationApiData> registrationResponse =
//                 service()
//                         .userRegistrations()
//                         .create(username, password, email);
// 
//         service()
//                 .userRegistrations()
//                 .findByToken(registrationResponse.getBody().getRegistrationConfirmationToken())
//                 .confirmRegistration();
// 
//         return service()
//                 .users()
//                 .search()
//                 .findByUsername(username).getData().stream().findFirst().get();
//     }
// 
// }


import info.rolandkrueger.userservice.UserMicroserviceApplication;
import info.rolandkrueger.userservice.api.service.StaticEndpointProvider;
import info.rolandkrueger.userservice.api.service.UserDetailServiceImpl;
import info.rolandkrueger.userservice.testsupport.AbstractRestControllerTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.notNullValue;
package info.rolandkrueger.userservice.service;



/**
 * @author Roland Krüger
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = UserMicroserviceApplication.class)
@WebIntegrationTest(randomPort = true)
public class UserDetailServiceImplTest extends AbstractRestControllerTest {

// Path: service/src/main/java/info/rolandkrueger/userservice/UserMicroserviceApplication.java
// @SpringBootApplication
// @PropertySource("userservice.properties")
// public class UserMicroserviceApplication {
// 
//     public static void main(String[] args) {
//         SpringApplication.run(
//                 new Object[]{
//                         UserMicroserviceApplication.class,
//                         DevelopmentProfileConfiguration.class}
//                 , args);
//     }
// }
// 
// Path: public-api/src/main/java/info/rolandkrueger/userservice/api/service/StaticEndpointProvider.java
// @Service
// public class StaticEndpointProvider implements UserMicroserviceEndpointProvider {
// 
//     private String endpoint;
// 
//     protected StaticEndpointProvider() {
//     }
// 
//     /**
//      * Creates a new StaticEndpointProvider for the given static endpoint URL.
//      *
//      * @param endpoint static endpoint address for the user micro-service
//      */
//     public StaticEndpointProvider(String endpoint) {
//         this.endpoint = endpoint;
//     }
// 
//     /**
//      * Returns the fixed user service endpoint address passed in through the constructor.
//      */
//     @Override
//     public String getUserMicroserviceEndpoint() {
//         return endpoint;
//     }
// }
// 
// Path: public-api/src/main/java/info/rolandkrueger/userservice/api/service/UserDetailServiceImpl.java
// @Service
// public class UserDetailServiceImpl implements UserDetailsService {
// 
//     private UserMicroserviceEndpointProvider endpointProvider;
// 
//     /**
//      * Constructor which is autowired with an instance of {@link UserMicroserviceEndpointProvider}.
//      */
//     @Autowired
//     public UserDetailServiceImpl(UserMicroserviceEndpointProvider endpointProvider) {
//         Preconditions.checkNotNull(endpointProvider);
//         this.endpointProvider = endpointProvider;
//     }
// 
//     /**
//      * Load a {@link UserDetails} object from the user micro-service for the specified username. Note that this method
//      * in addition to a {@link UsernameNotFoundException} throws an unchecked {@link RestClientException} to indicate
//      * errors that occurred during the communication process with the remote user micro-service.
//      *
//      * @param username name of the user to be loaded
//      * @return a {@link UserDetails} object
//      * @throws UsernameNotFoundException when the given username is not known to the service
//      * @throws RestClientException when some error occurred while calling the REST service
//      */
//     @Override
//     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, RestClientException {
//         UserService userService = UserServiceAPI.init(endpointProvider.getUserMicroserviceEndpoint());
//         Optional<UserApiData> loadedUser = userService.users().search().findByUsername(username).getData().stream().findFirst();
//         if (!loadedUser.isPresent()) {
//             throw new UsernameNotFoundException("User " + username + " not found");
//         }
//         return loadedUser.get().getResource().useProjection(UserProjections.FULL_DATA).read();
//     }
// }
// 
// Path: service/src/test/java/info/rolandkrueger/userservice/testsupport/AbstractRestControllerTest.java
// public abstract class AbstractRestControllerTest {
// 
//     private final static String CONTEXT_PATH = "http://localhost:";
// 
//     private static UserService service;
//     private static int port = 8080;
// 
//     protected static void setPort(int port) {
//         AbstractRestControllerTest.port = port;
//     }
// 
//     protected static UserService service() {
//         if (service == null) {
//             service = UserServiceAPI.init(CONTEXT_PATH + port);
//         }
//         return service;
//     }
// 
//     @After
//     public void tearDown(){
//         deleteAllUsers();
//         deleteAllAuthorities();
//     }
// 
//     protected static void deleteAllUsers() {
//         UsersResource users;
//         do {
//             users = service().users();
//             users.getData().stream().forEach(user -> user.getResource().delete());
//         } while (users.hasNext());
//     }
// 
//     protected static void deleteAllAuthorities() {
//         AuthoritiesResource authorities;
//         do {
//             authorities = service().authorities();
//             authorities.getData().stream().forEach(authority -> authority.getResource().delete());
//         } while (authorities.hasNext());
//     }
// 
//     protected static AuthorityApiData createAuthority(String authority) {
//         AuthorityApiData authorityApiData = new AuthorityApiData();
//         authorityApiData.setAuthority(authority);
//         authorityApiData.setDescription("The " + authority + " role");
//         return service().authorities().create(authorityApiData).getBody();
//     }
// 
//     protected static UserApiData registerUser(String username, String password, String email) {
//         ResponseEntity<UserRegistrationApiData> registrationResponse =
//                 service()
//                         .userRegistrations()
//                         .create(username, password, email);
// 
//         service()
//                 .userRegistrations()
//                 .findByToken(registrationResponse.getBody().getRegistrationConfirmationToken())
//                 .confirmRegistration();
// 
//         return service()
//                 .users()
//                 .search()
//                 .findByUsername(username).getData().stream().findFirst().get();
//     }
// 
// }


// Path: service/src/test/java/info/rolandkrueger/userservice/service/UserDetailServiceImplTest.java
import info.rolandkrueger.userservice.UserMicroserviceApplication;
import info.rolandkrueger.userservice.api.service.StaticEndpointProvider;
import info.rolandkrueger.userservice.api.service.UserDetailServiceImpl;
import info.rolandkrueger.userservice.testsupport.AbstractRestControllerTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.notNullValue;

package info.rolandkrueger.userservice.service;



/**
 * @author Roland Krüger
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = UserMicroserviceApplication.class)
@WebIntegrationTest(randomPort = true)
public class UserDetailServiceImplTest extends AbstractRestControllerTest {

    private UserDetailServiceImpl userDetailService;