// Path: src/main/java/com/thoughtworks/go/strongauth/authentication/AuthenticationRequest.java
// @Value
// public class AuthenticationRequest {
//     private final String username;
//     private final String password;
// }
// 
// Path: src/main/java/com/thoughtworks/go/strongauth/authentication/Authenticator.java
// public class Authenticator {
// 
//     public final String pluginId = Constants.PLUGIN_ID;
//     private static final Logger LOGGER = Logger.getLoggerFor(Authenticator.class);
//     private final List<? extends HashProvider> hashProviders;
// 
//     public Authenticator(
//             final PrincipalDetailSource principalDetailSource,
//             final List<? extends HashProvider> hashProviders
//     ) {
//         this.principalDetailSource = principalDetailSource;
//         this.hashProviders = hashProviders;
//     }
// 
//     private PrincipalDetailSource principalDetailSource;
// 
//     public Optional<Principal> authenticate(final String username, final String password) {
//         LOGGER.info(format("Login attempt for user %s", username));
//         Optional<Principal> maybePrincipal = flatMap(principalDetailSource.byUsername(username), new Function<PrincipalDetail, Optional<Principal>>() {
//             @Override
//             public Optional<Principal> apply(PrincipalDetail principalDetail) {
//                 return toPrincipal(principalDetail, password);
//             }
//         });
// 
//         if (!maybePrincipal.isPresent()) {
//             LOGGER.warn(format("Login attempt for user %s failed.", username));
//         }
//         return maybePrincipal;
//     }
// 
//     private Optional<Principal> toPrincipal(final PrincipalDetail principalDetail, final String password) {
//         Optional<? extends HashProvider> maybeHashProvider = getHashProvider(principalDetail.getHashConfiguration());
// 
//         return flatMap(
//                 maybeHashProvider,
//                 new Function<HashProvider, Optional<Principal>>() {
//                     @Override
//                     public Optional<Principal> apply(HashProvider hashProvider) {
//                         return hashProvider.validateHash(password, principalDetail) ?
//                                 Optional.of(new Principal(principalDetail.getUsername())) :
//                                 Optional.<Principal>absent();
//                     }
//                 }
//         );
//     }
// 
// 
//     private Optional<? extends HashProvider> getHashProvider(final String hashConfig) {
//         for (HashProvider provider : hashProviders) {
//             if (provider.canHandle(hashConfig)) {
//                 return Optional.of(provider);
//             }
//         }
//         return Optional.absent();
//     }
// 
// 
// }
// 
// Path: src/main/java/com/thoughtworks/go/strongauth/authentication/Principal.java
// @Value
// public class Principal {
//     private final String id;
// }
// 
// Path: src/main/java/com/thoughtworks/go/strongauth/wire/GoAuthenticationRequestDecoder.java
// public class GoAuthenticationRequestDecoder {
// 
//     public final String pluginId = Constants.PLUGIN_ID;
//     private static final Logger LOGGER = Logger.getLoggerFor(GoAuthenticationRequestDecoder.class);
// 
//     private static Gson gson = new GsonBuilder().registerTypeAdapter(
//             AuthenticationRequest.class, new JsonDeserializer<AuthenticationRequest>() {
//                 @Override
//                 public AuthenticationRequest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
//                         throws JsonParseException {
//                     if (!json.isJsonObject()) {
//                         throw new JsonParseException("Malformed json for AuthenticationRequest");
//                     }
// 
//                     return new AuthenticationRequest(
//                             getAsStringOrFail(json.getAsJsonObject(), "username"),
//                             getAsStringOrFail(json.getAsJsonObject(), "password")
//                     );
// 
//                 }
//             }).create();
// 
//     public static String getAsStringOrFail(JsonObject object, String fieldName) throws JsonParseException {
//         JsonElement username = object.get(fieldName);
//         if (username != null && username.isJsonPrimitive()) {
//             return username.getAsString();
//         } else {
//             throw new JsonParseException(format("Expected a string value for field %s", fieldName));
//         }
//     }
// 
//     public Optional<AuthenticationRequest> decode(GoPluginApiRequest goRequest) {
//         try {
//             AuthenticationRequest request = gson.getAdapter(AuthenticationRequest.class).fromJson(goRequest.requestBody());
//             return hasRequiredFields(request) ? Optional.of(request) : Optional.<AuthenticationRequest>absent();
//         } catch (IOException e) {
//             LOGGER.warn("IOException when parsing incoming authentication request.", e);
//             return Optional.absent();
//         } catch (JsonParseException e) {
//             LOGGER.warn("Failure when parsing incoming authentication request.", e);
//             return Optional.absent();
//         }
//     }
// 
//     private boolean hasRequiredFields(AuthenticationRequest request) {
//         return request.getPassword() != null && request.getUsername() != null;
//     }
// 
// }
// 
// Path: src/main/java/com/thoughtworks/go/strongauth/wire/GoUserEncoder.java
// public class GoUserEncoder {
// 
//     public GoPluginApiResponse encode(final String username) {
//         final DefaultGoPluginApiResponse response = new DefaultGoPluginApiResponse(200);
//         final ImmutableMap<String, ImmutableMap<String, String>> map =
//                 ImmutableMap.of("user", ImmutableMap.of("username", username));
//         response.setResponseBody(Json.toJson(map));
//         return response;
//     }
// 
//     public GoPluginApiResponse noUser() {
//         return new DefaultGoPluginApiResponse(200);
//     }
// 
// }


import com.google.common.base.Optional;
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import com.thoughtworks.go.strongauth.authentication.AuthenticationRequest;
import com.thoughtworks.go.strongauth.authentication.Authenticator;
import com.thoughtworks.go.strongauth.authentication.Principal;
import com.thoughtworks.go.strongauth.wire.GoAuthenticationRequestDecoder;
import com.thoughtworks.go.strongauth.wire.GoUserEncoder;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
package com.thoughtworks.go.strongauth.handlers;



public class AuthenticationHandlerTest {

    private GoAuthenticationRequestDecoder requestDecoder = mock(GoAuthenticationRequestDecoder.class);
// Path: src/main/java/com/thoughtworks/go/strongauth/authentication/AuthenticationRequest.java
// @Value
// public class AuthenticationRequest {
//     private final String username;
//     private final String password;
// }
// 
// Path: src/main/java/com/thoughtworks/go/strongauth/authentication/Authenticator.java
// public class Authenticator {
// 
//     public final String pluginId = Constants.PLUGIN_ID;
//     private static final Logger LOGGER = Logger.getLoggerFor(Authenticator.class);
//     private final List<? extends HashProvider> hashProviders;
// 
//     public Authenticator(
//             final PrincipalDetailSource principalDetailSource,
//             final List<? extends HashProvider> hashProviders
//     ) {
//         this.principalDetailSource = principalDetailSource;
//         this.hashProviders = hashProviders;
//     }
// 
//     private PrincipalDetailSource principalDetailSource;
// 
//     public Optional<Principal> authenticate(final String username, final String password) {
//         LOGGER.info(format("Login attempt for user %s", username));
//         Optional<Principal> maybePrincipal = flatMap(principalDetailSource.byUsername(username), new Function<PrincipalDetail, Optional<Principal>>() {
//             @Override
//             public Optional<Principal> apply(PrincipalDetail principalDetail) {
//                 return toPrincipal(principalDetail, password);
//             }
//         });
// 
//         if (!maybePrincipal.isPresent()) {
//             LOGGER.warn(format("Login attempt for user %s failed.", username));
//         }
//         return maybePrincipal;
//     }
// 
//     private Optional<Principal> toPrincipal(final PrincipalDetail principalDetail, final String password) {
//         Optional<? extends HashProvider> maybeHashProvider = getHashProvider(principalDetail.getHashConfiguration());
// 
//         return flatMap(
//                 maybeHashProvider,
//                 new Function<HashProvider, Optional<Principal>>() {
//                     @Override
//                     public Optional<Principal> apply(HashProvider hashProvider) {
//                         return hashProvider.validateHash(password, principalDetail) ?
//                                 Optional.of(new Principal(principalDetail.getUsername())) :
//                                 Optional.<Principal>absent();
//                     }
//                 }
//         );
//     }
// 
// 
//     private Optional<? extends HashProvider> getHashProvider(final String hashConfig) {
//         for (HashProvider provider : hashProviders) {
//             if (provider.canHandle(hashConfig)) {
//                 return Optional.of(provider);
//             }
//         }
//         return Optional.absent();
//     }
// 
// 
// }
// 
// Path: src/main/java/com/thoughtworks/go/strongauth/authentication/Principal.java
// @Value
// public class Principal {
//     private final String id;
// }
// 
// Path: src/main/java/com/thoughtworks/go/strongauth/wire/GoAuthenticationRequestDecoder.java
// public class GoAuthenticationRequestDecoder {
// 
//     public final String pluginId = Constants.PLUGIN_ID;
//     private static final Logger LOGGER = Logger.getLoggerFor(GoAuthenticationRequestDecoder.class);
// 
//     private static Gson gson = new GsonBuilder().registerTypeAdapter(
//             AuthenticationRequest.class, new JsonDeserializer<AuthenticationRequest>() {
//                 @Override
//                 public AuthenticationRequest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
//                         throws JsonParseException {
//                     if (!json.isJsonObject()) {
//                         throw new JsonParseException("Malformed json for AuthenticationRequest");
//                     }
// 
//                     return new AuthenticationRequest(
//                             getAsStringOrFail(json.getAsJsonObject(), "username"),
//                             getAsStringOrFail(json.getAsJsonObject(), "password")
//                     );
// 
//                 }
//             }).create();
// 
//     public static String getAsStringOrFail(JsonObject object, String fieldName) throws JsonParseException {
//         JsonElement username = object.get(fieldName);
//         if (username != null && username.isJsonPrimitive()) {
//             return username.getAsString();
//         } else {
//             throw new JsonParseException(format("Expected a string value for field %s", fieldName));
//         }
//     }
// 
//     public Optional<AuthenticationRequest> decode(GoPluginApiRequest goRequest) {
//         try {
//             AuthenticationRequest request = gson.getAdapter(AuthenticationRequest.class).fromJson(goRequest.requestBody());
//             return hasRequiredFields(request) ? Optional.of(request) : Optional.<AuthenticationRequest>absent();
//         } catch (IOException e) {
//             LOGGER.warn("IOException when parsing incoming authentication request.", e);
//             return Optional.absent();
//         } catch (JsonParseException e) {
//             LOGGER.warn("Failure when parsing incoming authentication request.", e);
//             return Optional.absent();
//         }
//     }
// 
//     private boolean hasRequiredFields(AuthenticationRequest request) {
//         return request.getPassword() != null && request.getUsername() != null;
//     }
// 
// }
// 
// Path: src/main/java/com/thoughtworks/go/strongauth/wire/GoUserEncoder.java
// public class GoUserEncoder {
// 
//     public GoPluginApiResponse encode(final String username) {
//         final DefaultGoPluginApiResponse response = new DefaultGoPluginApiResponse(200);
//         final ImmutableMap<String, ImmutableMap<String, String>> map =
//                 ImmutableMap.of("user", ImmutableMap.of("username", username));
//         response.setResponseBody(Json.toJson(map));
//         return response;
//     }
// 
//     public GoPluginApiResponse noUser() {
//         return new DefaultGoPluginApiResponse(200);
//     }
// 
// }


// Path: src/test/java/com/thoughtworks/go/strongauth/handlers/AuthenticationHandlerTest.java
import com.google.common.base.Optional;
import com.thoughtworks.go.plugin.api.request.GoPluginApiRequest;
import com.thoughtworks.go.plugin.api.response.GoPluginApiResponse;
import com.thoughtworks.go.strongauth.authentication.AuthenticationRequest;
import com.thoughtworks.go.strongauth.authentication.Authenticator;
import com.thoughtworks.go.strongauth.authentication.Principal;
import com.thoughtworks.go.strongauth.wire.GoAuthenticationRequestDecoder;
import com.thoughtworks.go.strongauth.wire.GoUserEncoder;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

package com.thoughtworks.go.strongauth.handlers;



public class AuthenticationHandlerTest {

    private GoAuthenticationRequestDecoder requestDecoder = mock(GoAuthenticationRequestDecoder.class);
    private Authenticator authenticator = mock(Authenticator.class);