Add tests for access token json extractor

Change-Id: I6391c0276abbb0184ea6b8d4cd1760dd1135324b
diff --git a/BUILD b/BUILD
index 966c97b..ad23b6d 100644
--- a/BUILD
+++ b/BUILD
@@ -1,4 +1,9 @@
-load("//tools/bzl:plugin.bzl", "gerrit_plugin")
+load("//tools/bzl:junit.bzl", "junit_tests")
+load(
+    "//tools/bzl:plugin.bzl",
+    "gerrit_plugin",
+    "PLUGIN_DEPS",
+)
 
 gerrit_plugin(
     name = "oauth",
@@ -17,3 +22,13 @@
         "@scribe//jar",
     ],
 )
+
+junit_tests(
+    name = "oauth_tests",
+    srcs = glob(["src/test/java/**/*.java"]),
+    tags = ["oauth"],
+    deps = PLUGIN_DEPS + [
+        ":oauth__plugin",
+        "@scribe//jar",
+    ],
+)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/oauth/OAuth2AccessTokenJsonExtractor.java b/src/main/java/com/googlesource/gerrit/plugins/oauth/OAuth2AccessTokenJsonExtractor.java
index 3fad710..6c2f1a0 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/oauth/OAuth2AccessTokenJsonExtractor.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/oauth/OAuth2AccessTokenJsonExtractor.java
@@ -16,6 +16,7 @@
 
 import static org.scribe.model.OAuthConstants.ACCESS_TOKEN;
 
+import com.google.common.annotations.VisibleForTesting;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.scribe.exceptions.OAuthException;
@@ -35,6 +36,7 @@
     return INSTANCE;
   }
 
+  @VisibleForTesting
   @Override
   public Token extract(String response) {
     Preconditions.checkEmptyString(response, "Cannot extract a token from a null or empty String");
diff --git a/src/test/java/com/googlesource/gerrit/plugins/oauth/OAuth2AccessTokenJsonExtractorTest.java b/src/test/java/com/googlesource/gerrit/plugins/oauth/OAuth2AccessTokenJsonExtractorTest.java
new file mode 100644
index 0000000..09df7ee
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/oauth/OAuth2AccessTokenJsonExtractorTest.java
@@ -0,0 +1,70 @@
+// Copyright (C) 2018 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.oauth;
+
+import static org.junit.Assert.assertEquals;
+import static org.scribe.model.OAuthConstants.ACCESS_TOKEN;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.scribe.exceptions.OAuthException;
+import org.scribe.extractors.AccessTokenExtractor;
+import org.scribe.model.Token;
+
+public class OAuth2AccessTokenJsonExtractorTest {
+  private static final AccessTokenExtractor extractor = OAuth2AccessTokenJsonExtractor.instance();
+  private static final String TOKEN = "I0122HHJKLEM21F3WLPYHDKGKZULAUO4SGMV3ABKFTDT3T3X";
+  private static final String RESPONSE = "{\"" + ACCESS_TOKEN + "\":\"" + TOKEN + "\"}'";
+  private static final String RESPONSE_NON_JSON = ACCESS_TOKEN + "=" + TOKEN;
+  private static final String RESPONSE_WITH_BLANKS =
+      "{ \"" + ACCESS_TOKEN + "\" : \"" + TOKEN + "\"}'";
+  private static final String MESSAGE = "Cannot extract a token from a null or empty String";
+
+  @Rule public ExpectedException exception = ExpectedException.none();
+
+  @Test
+  public void parseResponse() throws Exception {
+    Token token = extractor.extract(RESPONSE);
+    assertEquals(token.getToken(), TOKEN);
+  }
+
+  @Test
+  public void parseResponseWithBlanks() throws Exception {
+    Token token = extractor.extract(RESPONSE_WITH_BLANKS);
+    assertEquals(token.getToken(), TOKEN);
+  }
+
+  @Test
+  public void failParseNonJsonResponse() throws Exception {
+    exception.expect(OAuthException.class);
+    exception.expectMessage("Cannot extract an access token. Response was: " + RESPONSE_NON_JSON);
+    extractor.extract(RESPONSE_NON_JSON);
+  }
+
+  @Test
+  public void shouldThrowExceptionIfForNullParameter() throws Exception {
+    exception.expect(IllegalArgumentException.class);
+    exception.expectMessage(MESSAGE);
+    extractor.extract(null);
+  }
+
+  @Test
+  public void shouldThrowExceptionIfForEmptyString() throws Exception {
+    exception.expect(IllegalArgumentException.class);
+    exception.expectMessage(MESSAGE);
+    extractor.extract("");
+  }
+}
diff --git a/tools/bzl/junit.bzl b/tools/bzl/junit.bzl
new file mode 100644
index 0000000..3af7e58
--- /dev/null
+++ b/tools/bzl/junit.bzl
@@ -0,0 +1,4 @@
+load(
+    "@com_googlesource_gerrit_bazlets//tools:junit.bzl",
+    "junit_tests",
+)