Extract JSON helper methods to JsonUtils Change-Id: I31d758e359d9c11ad6c84125600e661e8d869703 Signed-off-by: Michael Ochmann <michael.ochmann@sap.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cfoauth/JsonUtils.java b/src/main/java/com/googlesource/gerrit/plugins/cfoauth/JsonUtils.java new file mode 100644 index 0000000..0227eed --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/cfoauth/JsonUtils.java
@@ -0,0 +1,51 @@ +// Copyright (C) 2015 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.cfoauth; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.JsonPrimitive; + +public class JsonUtils { + + public static String getAttribute(JsonObject json, String name) { + JsonPrimitive prim = getAsJsonPrimitive(json, name); + return prim != null && prim.isString() ? prim.getAsString() : null; + } + + public static long getLongAttribute(JsonObject json, String name, + long defaultValue) { + JsonPrimitive prim = getAsJsonPrimitive(json, name); + return prim != null && prim.isNumber() ? prim.getAsLong() : defaultValue; + } + + public static JsonPrimitive getAsJsonPrimitive(JsonObject json, String name) { + JsonElement attr = json.get(name); + if (attr == null || !attr.isJsonPrimitive()) { + return null; + } + return attr.getAsJsonPrimitive(); + } + + public static JsonObject getAsJsonObject(String s) { + JsonElement json = new JsonParser().parse(s); + if (!json.isJsonObject()) { + return new JsonObject(); + } + return json.getAsJsonObject(); + } + +}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/cfoauth/UAAClient.java b/src/main/java/com/googlesource/gerrit/plugins/cfoauth/UAAClient.java index e2ccc02..259339d 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/cfoauth/UAAClient.java +++ b/src/main/java/com/googlesource/gerrit/plugins/cfoauth/UAAClient.java
@@ -14,6 +14,8 @@ package com.googlesource.gerrit.plugins.cfoauth; +import static com.googlesource.gerrit.plugins.cfoauth.JsonUtils.*; + import static java.net.HttpURLConnection.HTTP_OK; import static java.nio.charset.StandardCharsets.US_ASCII; import static java.nio.charset.StandardCharsets.UTF_8; @@ -25,10 +27,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; -import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonPrimitive; import org.apache.commons.codec.binary.Base64; import org.scribe.model.OAuthRequest; @@ -273,12 +272,6 @@ "Unsupported signature algorithm ''{0}''", alg)); } - @VisibleForTesting - String getAttribute(JsonObject json, String name) { - JsonPrimitive prim = getAsJsonPrimitive(json, name); - return prim != null && prim.isString() ? prim.getAsString() : null; - } - private String getAccessTokenAttribute(String tokenResponse) throws UAAClientException { JsonObject json = getAsJsonObject(tokenResponse); @@ -290,28 +283,6 @@ return accessToken; } - @VisibleForTesting - long getLongAttribute(JsonObject json, String name, long defaultValue) { - JsonPrimitive prim = getAsJsonPrimitive(json, name); - return prim != null && prim.isNumber() ? prim.getAsLong() : defaultValue; - } - - private JsonPrimitive getAsJsonPrimitive(JsonObject json, String name) { - JsonElement attr = json.get(name); - if (attr == null || !attr.isJsonPrimitive()) { - return null; - } - return attr.getAsJsonPrimitive(); - } - - private JsonObject getAsJsonObject(String s) { - JsonElement json = new JsonParser().parse(s); - if (!json.isJsonObject()) { - return new JsonObject(); - } - return json.getAsJsonObject(); - } - private String decodeBase64(String s) { return new String(Base64.decodeBase64(s), UTF_8); }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/cfoauth/UAAClientTest.java b/src/test/java/com/googlesource/gerrit/plugins/cfoauth/UAAClientTest.java index 2616784..244286c 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/cfoauth/UAAClientTest.java +++ b/src/test/java/com/googlesource/gerrit/plugins/cfoauth/UAAClientTest.java
@@ -15,15 +15,11 @@ package com.googlesource.gerrit.plugins.cfoauth; import static org.junit.Assert.*; +import static com.googlesource.gerrit.plugins.cfoauth.JsonUtils.getAttribute; +import static com.googlesource.gerrit.plugins.cfoauth.JsonUtils.getLongAttribute; import com.google.gson.JsonObject; -import com.googlesource.gerrit.plugins.cfoauth.AccessToken; -import com.googlesource.gerrit.plugins.cfoauth.HMACSHA256SignatureVerifier; -import com.googlesource.gerrit.plugins.cfoauth.SignatureVerifier; -import com.googlesource.gerrit.plugins.cfoauth.UAAClient; -import com.googlesource.gerrit.plugins.cfoauth.UAAClientException; - import org.junit.Before; import org.junit.Test; @@ -110,9 +106,9 @@ @Test public void testToJsonWebToken() throws Exception { JsonObject jsonWebToken = client.toJsonWebToken(HS256_TEST_TOKEN); - assertEquals("marissa", client.getAttribute(jsonWebToken, "user_name")); - assertEquals("marissa@test.org", client.getAttribute(jsonWebToken, "email")); - assertEquals(1436232932L, client.getLongAttribute(jsonWebToken, "exp", 0)); + assertEquals("marissa", getAttribute(jsonWebToken, "user_name")); + assertEquals("marissa@test.org", getAttribute(jsonWebToken, "email")); + assertEquals(1436232932L, getLongAttribute(jsonWebToken, "exp", 0)); } @Test(expected = UAAClientException.class)