Gerrit repo validation fails without credentials 

Bug: 378898
Change-Id: Idd642adc2d9210fa097d76807b3bc75c544b3af6
diff --git a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java
index ecc35bb..63db2c6 100644
--- a/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java
+++ b/org.eclipse.mylyn.gerrit.core/src/org/eclipse/mylyn/internal/gerrit/core/client/GerritHttpClient.java
@@ -261,22 +261,15 @@
 	private void authenticate(String openIdProvider, IProgressMonitor monitor) throws GerritException, IOException {
 		while (true) {
 			AuthenticationCredentials credentials = location.getCredentials(AuthenticationType.REPOSITORY);
-			if (openIdProvider == null && credentials == null) {
-				throw new GerritLoginException();
-			}
 
-			// try form based authentication first
 			int code;
 			if (openIdProvider != null) {
 				code = authenticateOpenIdService(openIdProvider, credentials, monitor);
 				if (code == -1) {
 					continue;
 				}
-			} else {
-				code = HttpStatus.SC_NOT_FOUND;
-			}
-
-			if (code == HttpStatus.SC_NOT_FOUND) {
+			} else if (credentials != null) {
+				// try form based authentication first
 				code = authenticateForm(credentials, monitor);
 				if (code == -1) {
 					continue;
@@ -291,6 +284,8 @@
 						}
 					}
 				}
+			} else {
+				throw new GerritLoginException();
 			}
 
 			// Location: http://egit.eclipse.org/r/#SignInFailure,SIGN_IN,Session cookie not available
diff --git a/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/AllGerritTests.java b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/AllGerritTests.java
index 1d725cd..1678842 100644
--- a/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/AllGerritTests.java
+++ b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/AllGerritTests.java
@@ -18,6 +18,7 @@
 import org.eclipse.mylyn.commons.sdk.util.TestConfiguration;
 import org.eclipse.mylyn.gerrit.tests.core.GerritConnectorTest;
 import org.eclipse.mylyn.gerrit.tests.core.client.GerritClientTest;
+import org.eclipse.mylyn.gerrit.tests.core.client.OpenIdAuthenticationTest;
 import org.eclipse.mylyn.gerrit.tests.support.GerritFixture;
 import org.eclipse.mylyn.gerrit.tests.ui.GerritUrlHandlerTest;
 
@@ -42,6 +43,7 @@
 		if (!configuration.isLocalOnly()) {
 			// network tests
 			suite.addTestSuite(GerritUrlHandlerTest.class);
+			suite.addTestSuite(OpenIdAuthenticationTest.class);
 			if (configuration.isDefaultOnly()) {
 				addTests(suite, GerritFixture.DEFAULT);
 			} else {
diff --git a/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/OpenIdAuthenticationTest.java b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/OpenIdAuthenticationTest.java
new file mode 100644
index 0000000..6a12a3b
--- /dev/null
+++ b/org.eclipse.mylyn.gerrit.tests/src/org/eclipse/mylyn/gerrit/tests/core/client/OpenIdAuthenticationTest.java
@@ -0,0 +1,94 @@
+/*******************************************************************************
+ * Copyright (c) 2012 Tasktop Technologies and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     Tasktop Technologies - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.mylyn.gerrit.tests.core.client;
+
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.HttpMethodBase;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.mylyn.commons.net.UnsupportedRequestException;
+import org.eclipse.mylyn.commons.net.WebLocation;
+import org.eclipse.mylyn.gerrit.tests.support.GerritFixture;
+import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient;
+import org.eclipse.mylyn.internal.gerrit.core.client.GerritHttpClient.Request;
+import org.eclipse.mylyn.internal.gerrit.core.client.GerritLoginException;
+import org.eclipse.mylyn.internal.gerrit.core.client.IOpenIdLocation;
+import org.eclipse.mylyn.internal.gerrit.core.client.OpenIdAuthenticationRequest;
+import org.eclipse.mylyn.internal.gerrit.core.client.OpenIdAuthenticationResponse;
+
+/**
+ * @author Steffen Pingel
+ */
+public class OpenIdAuthenticationTest extends TestCase {
+
+	private class StubRepositoryLocation extends WebLocation implements IOpenIdLocation {
+
+		String providerUrl;
+
+		public StubRepositoryLocation(String url) {
+			super(url);
+		}
+
+		@Override
+		public String getProviderUrl() {
+			return providerUrl;
+		}
+
+		@Override
+		public OpenIdAuthenticationResponse requestAuthentication(OpenIdAuthenticationRequest request,
+				IProgressMonitor monitor) throws UnsupportedRequestException {
+			return new OpenIdAuthenticationResponse(null, null);
+		}
+
+	}
+
+	private static String PROVIDER_URL = "https://www.google.com/accounts/o8/id"; //$NON-NLS-1$
+
+	StubRepositoryLocation location = new StubRepositoryLocation(GerritFixture.current()
+			.repository()
+			.getRepositoryUrl());
+
+	GerritHttpClient client = new GerritHttpClient(location);
+
+	public void testExecuteNullOpenIdProviderNullCredentials() throws Exception {
+		client.execute(createRequest(), null);
+	}
+
+	public void testExecuteOpenIdProviderNullCredentials() throws Exception {
+		location.providerUrl = PROVIDER_URL;
+		try {
+			client.execute(createRequest(), null);
+			fail("Expected GerritLoginException");
+		} catch (GerritLoginException expected) {
+			// ignore
+		}
+	}
+
+	private Request<Object> createRequest() {
+		return new Request<Object>() {
+			@Override
+			public HttpMethodBase createMethod() throws IOException {
+				return new GetMethod();
+			}
+
+			@Override
+			public Object process(HttpMethodBase method) throws IOException {
+				// ignore
+				return null;
+			}
+		};
+	}
+
+}