From 1c70b81c17f0238018faee43539ee2af6beae7e4 Mon Sep 17 00:00:00 2001 From: "arash.shokoufandeh" Date: Mon, 25 Sep 2017 11:55:17 -0700 Subject: [PATCH] updating base64 encoder to the one bundled in Java 8, like graphql-java along with some minor cleanup --- src/main/java/com/nfl/glitr/Glitr.java | 3 +- .../nfl/glitr/relay/PageInfoWithTotal.java | 1 + .../java/com/nfl/glitr/relay/RelayHelper.java | 45 +++++++++---------- .../data/additionalScalars/CustomScalar.java | 2 + .../glitr/data/additionalScalars/Root.java | 2 + .../data/circularReference/AbstractRead.java | 2 + .../glitr/data/circularReference/Book.java | 2 + .../CircularReferenceTest.groovy | 1 - .../glitr/data/circularReference/Novel.java | 1 + .../data/circularReference/Readable.java | 1 + .../com/nfl/glitr/data/mutation/Bitrate.java | 1 + .../glitr/data/mutation/VideoMutationIn.java | 1 + .../data/mutation/VideoMutationInput.java | 1 + .../glitr/data/mutation/VideoMutationOut.java | 1 + .../data/mutation/VideoMutationPayload.java | 1 + .../nfl/glitr/data/query/AbstractContent.java | 2 + .../glitr/data/query/AbstractTimestamped.java | 2 + .../nfl/glitr/data/query/Identifiable.java | 1 + .../data/query/IdentifiableAbstractClass.java | 1 + .../com/nfl/glitr/data/query/Video.java | 1 + .../nfl/glitr/relay/RelayHelperTest.groovy | 12 ++--- 21 files changed, 52 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/nfl/glitr/Glitr.java b/src/main/java/com/nfl/glitr/Glitr.java index cca8eba..8eb88c1 100644 --- a/src/main/java/com/nfl/glitr/Glitr.java +++ b/src/main/java/com/nfl/glitr/Glitr.java @@ -59,11 +59,10 @@ private GraphQLSchema buildSchema(Class queryRoot, Class mutationRoot) { } GraphQLObjectType queryType = (GraphQLObjectType) typeRegistry.lookup(queryRoot); - GraphQLSchema schema = GraphQLSchema.newSchema() + return GraphQLSchema.newSchema() .query(queryType) .mutation(mutationType) .build(typeRegistry.getTypeDictionary()); - return schema; } public GraphQLSchema reloadSchema(Class queryRoot, Class mutationRoot) { diff --git a/src/main/java/com/nfl/glitr/relay/PageInfoWithTotal.java b/src/main/java/com/nfl/glitr/relay/PageInfoWithTotal.java index 15c45e3..8a4bce8 100644 --- a/src/main/java/com/nfl/glitr/relay/PageInfoWithTotal.java +++ b/src/main/java/com/nfl/glitr/relay/PageInfoWithTotal.java @@ -7,6 +7,7 @@ public class PageInfoWithTotal extends DefaultPageInfo { private int total; + public PageInfoWithTotal(ConnectionCursor startCursor, ConnectionCursor endCursor, boolean hasPreviousPage, boolean hasNextPage) { super(startCursor, endCursor, hasPreviousPage, hasNextPage); diff --git a/src/main/java/com/nfl/glitr/relay/RelayHelper.java b/src/main/java/com/nfl/glitr/relay/RelayHelper.java index 0166620..1abf872 100644 --- a/src/main/java/com/nfl/glitr/relay/RelayHelper.java +++ b/src/main/java/com/nfl/glitr/relay/RelayHelper.java @@ -1,5 +1,6 @@ package com.nfl.glitr.relay; +import com.nfl.glitr.exception.GlitrException; import com.nfl.glitr.registry.TypeRegistry; import graphql.relay.ConnectionCursor; import graphql.relay.DefaultConnection; @@ -12,13 +13,14 @@ import graphql.schema.GraphQLObjectType; import graphql.schema.GraphQLOutputType; -import javax.xml.bind.DatatypeConverter; -import java.io.UnsupportedEncodingException; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; import static graphql.Assert.assertNotNull; +import static java.lang.String.format; +import static java.util.Base64.getDecoder; +import static java.util.Base64.getEncoder; public class RelayHelper { @@ -80,31 +82,28 @@ public static graphql.relay.Connection buildConnection(Iterable col, int skip } public static String createCursor(int offset) { - return Base64.toBase64(DUMMY_CURSOR_PREFIX + Integer.toString(offset)); + byte[] bytes = (DUMMY_CURSOR_PREFIX + Integer.toString(offset)).getBytes(StandardCharsets.UTF_8); + return getEncoder().encodeToString(bytes); } public static int getOffsetFromCursor(String cursor, int defaultValue) { - if (cursor == null) return defaultValue; - String string = Base64.fromBase64(cursor); - return Integer.parseInt(string.substring(DUMMY_CURSOR_PREFIX.length())); - } - - - static public class Base64 { - - private Base64() { + if (cursor == null) { + return defaultValue; } - - public static String toBase64(String string) { - try { - return DatatypeConverter.printBase64Binary(string.getBytes("utf-8")); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException(e); - } + byte[] decode; + try { + decode = getDecoder().decode(cursor); + } catch (IllegalArgumentException e) { + throw new GlitrException(format("The cursor is not in base64 format : '%s'", cursor), e); } - - public static String fromBase64(String string) { - return new String(DatatypeConverter.parseBase64Binary(string), Charset.forName("UTF-8")); + String string = new String(decode, StandardCharsets.UTF_8); + if (DUMMY_CURSOR_PREFIX.length() > string.length()) { + throw new GlitrException(format("The cursor prefix is missing from the cursor : '%s'", cursor)); + } + try { + return Integer.parseInt(string.substring(DUMMY_CURSOR_PREFIX.length())); + } catch (NumberFormatException nfe) { + throw new GlitrException(format("The cursor was not created by this class : '%s'", cursor), nfe); } } } diff --git a/src/test/groovy/com/nfl/glitr/data/additionalScalars/CustomScalar.java b/src/test/groovy/com/nfl/glitr/data/additionalScalars/CustomScalar.java index 40a0773..aa8a433 100644 --- a/src/test/groovy/com/nfl/glitr/data/additionalScalars/CustomScalar.java +++ b/src/test/groovy/com/nfl/glitr/data/additionalScalars/CustomScalar.java @@ -1,8 +1,10 @@ package com.nfl.glitr.data.additionalScalars; public class CustomScalar { + private String value; + public String getValue() { return value; } diff --git a/src/test/groovy/com/nfl/glitr/data/additionalScalars/Root.java b/src/test/groovy/com/nfl/glitr/data/additionalScalars/Root.java index 4e738a2..0032407 100644 --- a/src/test/groovy/com/nfl/glitr/data/additionalScalars/Root.java +++ b/src/test/groovy/com/nfl/glitr/data/additionalScalars/Root.java @@ -1,9 +1,11 @@ package com.nfl.glitr.data.additionalScalars; public class Root { + private String aString; private CustomScalar scalar; + public String getaString() { return aString; } diff --git a/src/test/groovy/com/nfl/glitr/data/circularReference/AbstractRead.java b/src/test/groovy/com/nfl/glitr/data/circularReference/AbstractRead.java index 09a1eec..2a0731b 100644 --- a/src/test/groovy/com/nfl/glitr/data/circularReference/AbstractRead.java +++ b/src/test/groovy/com/nfl/glitr/data/circularReference/AbstractRead.java @@ -1,9 +1,11 @@ package com.nfl.glitr.data.circularReference; public abstract class AbstractRead { + private int pageCount; private Novel novel; + public int getPageCount() { return pageCount; } diff --git a/src/test/groovy/com/nfl/glitr/data/circularReference/Book.java b/src/test/groovy/com/nfl/glitr/data/circularReference/Book.java index 92744af..79cbba9 100644 --- a/src/test/groovy/com/nfl/glitr/data/circularReference/Book.java +++ b/src/test/groovy/com/nfl/glitr/data/circularReference/Book.java @@ -1,9 +1,11 @@ package com.nfl.glitr.data.circularReference; public class Book implements Readable { + private String title; private Book synopsis; + public String getTitle() { return title; } diff --git a/src/test/groovy/com/nfl/glitr/data/circularReference/CircularReferenceTest.groovy b/src/test/groovy/com/nfl/glitr/data/circularReference/CircularReferenceTest.groovy index 33f678c..e4559d1 100644 --- a/src/test/groovy/com/nfl/glitr/data/circularReference/CircularReferenceTest.groovy +++ b/src/test/groovy/com/nfl/glitr/data/circularReference/CircularReferenceTest.groovy @@ -11,7 +11,6 @@ import spock.lang.Specification class CircularReferenceTest extends Specification { - def "Inspect AbstractClass that has a field circular reference"() { setup: Glitr glitr = GlitrBuilder.newGlitr().withQueryRoot(new QueryType()).build() diff --git a/src/test/groovy/com/nfl/glitr/data/circularReference/Novel.java b/src/test/groovy/com/nfl/glitr/data/circularReference/Novel.java index 0e77b2d..2d12604 100644 --- a/src/test/groovy/com/nfl/glitr/data/circularReference/Novel.java +++ b/src/test/groovy/com/nfl/glitr/data/circularReference/Novel.java @@ -5,6 +5,7 @@ public class Novel extends AbstractRead { private String title; private boolean reviewed; + public String getTitle() { return title; } diff --git a/src/test/groovy/com/nfl/glitr/data/circularReference/Readable.java b/src/test/groovy/com/nfl/glitr/data/circularReference/Readable.java index cccc3cb..7373315 100644 --- a/src/test/groovy/com/nfl/glitr/data/circularReference/Readable.java +++ b/src/test/groovy/com/nfl/glitr/data/circularReference/Readable.java @@ -1,5 +1,6 @@ package com.nfl.glitr.data.circularReference; public interface Readable { + Book getSynopsis(); } diff --git a/src/test/groovy/com/nfl/glitr/data/mutation/Bitrate.java b/src/test/groovy/com/nfl/glitr/data/mutation/Bitrate.java index dc2885d..3ff951a 100644 --- a/src/test/groovy/com/nfl/glitr/data/mutation/Bitrate.java +++ b/src/test/groovy/com/nfl/glitr/data/mutation/Bitrate.java @@ -18,6 +18,7 @@ public class Bitrate implements Playable { private LocalDate createdDate; private Instant modifiedDateTime; + public Integer getKbps() { return kbps; } diff --git a/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationIn.java b/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationIn.java index 1c054b1..caff4b7 100644 --- a/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationIn.java +++ b/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationIn.java @@ -8,6 +8,7 @@ public class VideoMutationIn { private String url; private List bitrateList; + public List getBitrateList() { return bitrateList; } diff --git a/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationInput.java b/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationInput.java index 07da2d5..73fda97 100644 --- a/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationInput.java +++ b/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationInput.java @@ -9,6 +9,7 @@ public class VideoMutationInput extends RelayMutationType { private VideoMutationIn videoMutation; + @GlitrDescription("Info meta data needed") @GlitrNonNull public VideoMutationIn getVideoMutation() { diff --git a/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationOut.java b/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationOut.java index 40a77e1..5d0d11c 100644 --- a/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationOut.java +++ b/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationOut.java @@ -7,6 +7,7 @@ public class VideoMutationOut { private String url; private String bitrateList; + public String getBitrateList() { return bitrateList; } diff --git a/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationPayload.java b/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationPayload.java index 89e015d..3d72ba8 100644 --- a/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationPayload.java +++ b/src/test/groovy/com/nfl/glitr/data/mutation/VideoMutationPayload.java @@ -6,6 +6,7 @@ public class VideoMutationPayload extends RelayMutationType { private VideoMutationOut videoMutationPayload; + public VideoMutationOut getVideoMutationPayload() { return videoMutationPayload; } diff --git a/src/test/groovy/com/nfl/glitr/data/query/AbstractContent.java b/src/test/groovy/com/nfl/glitr/data/query/AbstractContent.java index 11bc1f6..7305387 100644 --- a/src/test/groovy/com/nfl/glitr/data/query/AbstractContent.java +++ b/src/test/groovy/com/nfl/glitr/data/query/AbstractContent.java @@ -1,8 +1,10 @@ package com.nfl.glitr.data.query; public abstract class AbstractContent extends AbstractTimestamped { + private String title; + public String getTitle() { return title; } diff --git a/src/test/groovy/com/nfl/glitr/data/query/AbstractTimestamped.java b/src/test/groovy/com/nfl/glitr/data/query/AbstractTimestamped.java index e1acce3..50e4b2f 100644 --- a/src/test/groovy/com/nfl/glitr/data/query/AbstractTimestamped.java +++ b/src/test/groovy/com/nfl/glitr/data/query/AbstractTimestamped.java @@ -1,8 +1,10 @@ package com.nfl.glitr.data.query; public abstract class AbstractTimestamped { + private Long lastModifiedDate; + public Long getLastModifiedDate() { return lastModifiedDate; } diff --git a/src/test/groovy/com/nfl/glitr/data/query/Identifiable.java b/src/test/groovy/com/nfl/glitr/data/query/Identifiable.java index 44a317c..d5b47b7 100644 --- a/src/test/groovy/com/nfl/glitr/data/query/Identifiable.java +++ b/src/test/groovy/com/nfl/glitr/data/query/Identifiable.java @@ -4,5 +4,6 @@ @GlitrDescription("Identifiable interface needed for Relay") public interface Identifiable { + String getId(); } diff --git a/src/test/groovy/com/nfl/glitr/data/query/IdentifiableAbstractClass.java b/src/test/groovy/com/nfl/glitr/data/query/IdentifiableAbstractClass.java index 7079b17..d1842a2 100644 --- a/src/test/groovy/com/nfl/glitr/data/query/IdentifiableAbstractClass.java +++ b/src/test/groovy/com/nfl/glitr/data/query/IdentifiableAbstractClass.java @@ -4,5 +4,6 @@ @GlitrDescription("Identifiable interface needed for Relay") public abstract class IdentifiableAbstractClass { + public abstract String getId(); } diff --git a/src/test/groovy/com/nfl/glitr/data/query/Video.java b/src/test/groovy/com/nfl/glitr/data/query/Video.java index 2c60795..8c01cfc 100644 --- a/src/test/groovy/com/nfl/glitr/data/query/Video.java +++ b/src/test/groovy/com/nfl/glitr/data/query/Video.java @@ -10,6 +10,7 @@ public class Video extends AbstractContent implements Playable { private String url; private List bitrateList; + public List getBitrateList() { return bitrateList; } diff --git a/src/test/groovy/com/nfl/glitr/relay/RelayHelperTest.groovy b/src/test/groovy/com/nfl/glitr/relay/RelayHelperTest.groovy index a82a785..e3b1c8e 100644 --- a/src/test/groovy/com/nfl/glitr/relay/RelayHelperTest.groovy +++ b/src/test/groovy/com/nfl/glitr/relay/RelayHelperTest.groovy @@ -40,11 +40,11 @@ class RelayHelperTest extends Specification { def items = [] for (def i = 0; i < 10; i++) { if ((i + toSkip) < totalCount) { - items.add(i + toSkip); + items.add(i + toSkip) } } - def connection = RelayHelper.buildConnection(items, toSkip, totalCount); + def connection = RelayHelper.buildConnection(items, toSkip, totalCount) assert (connection.pageInfo.hasNextPage == hasNext) assert (connection.pageInfo.hasPreviousPage == hasPrev) assert (connection.edges.size() == resultSize) @@ -53,12 +53,12 @@ class RelayHelperTest extends Specification { assert pageInfoWithTotal.total == totalCount if (resultSize > 0) { - assert (connection.pageInfo.startCursor?.value == RelayHelper.Base64.toBase64("simple-cursor${toSkip}")) - assert (connection.pageInfo.endCursor?.value == RelayHelper.Base64.toBase64("simple-cursor${toSkip + resultSize - 1}")) + assert (connection.pageInfo.startCursor?.value == RelayHelper.createCursor(toSkip)) + assert (connection.pageInfo.endCursor?.value == RelayHelper.createCursor(toSkip + resultSize - 1)) } connection.edges.forEach({ - assert (it.cursor.value == RelayHelper.Base64.toBase64("simple-cursor${it.node}")); - }); + assert (it.cursor.value == RelayHelper.createCursor(it.node)) + }) } }