Skip to content

Commit

Permalink
Merge pull request #21 from nfl/update-base64-encoder
Browse files Browse the repository at this point in the history
update encoder to java utils
  • Loading branch information
vaant authored Sep 25, 2017
2 parents e924d44 + 1c70b81 commit 3bc1b86
Show file tree
Hide file tree
Showing 21 changed files with 52 additions and 32 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/nfl/glitr/Glitr.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/nfl/glitr/relay/PageInfoWithTotal.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
45 changes: 22 additions & 23 deletions src/main/java/com/nfl/glitr/relay/RelayHelper.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.nfl.glitr.data.additionalScalars;

public class CustomScalar {

private String value;


public String getValue() {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.nfl.glitr.data.additionalScalars;

public class Root {

private String aString;
private CustomScalar scalar;


public String getaString() {
return aString;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class Novel extends AbstractRead {
private String title;
private boolean reviewed;


public String getTitle() {
return title;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nfl.glitr.data.circularReference;

public interface Readable {

Book getSynopsis();
}
1 change: 1 addition & 0 deletions src/test/groovy/com/nfl/glitr/data/mutation/Bitrate.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Bitrate implements Playable {
private LocalDate createdDate;
private Instant modifiedDateTime;


public Integer getKbps() {
return kbps;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class VideoMutationIn {
private String url;
private List<Bitrate> bitrateList;


public List<Bitrate> getBitrateList() {
return bitrateList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class VideoMutationInput extends RelayMutationType {

private VideoMutationIn videoMutation;


@GlitrDescription("Info meta data needed")
@GlitrNonNull
public VideoMutationIn getVideoMutation() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class VideoMutationOut {
private String url;
private String bitrateList;


public String getBitrateList() {
return bitrateList;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class VideoMutationPayload extends RelayMutationType {

private VideoMutationOut videoMutationPayload;


public VideoMutationOut getVideoMutationPayload() {
return videoMutationPayload;
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/groovy/com/nfl/glitr/data/query/AbstractContent.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.nfl.glitr.data.query;

public abstract class AbstractContent extends AbstractTimestamped {

private String title;


public String getTitle() {
return title;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.nfl.glitr.data.query;

public abstract class AbstractTimestamped {

private Long lastModifiedDate;


public Long getLastModifiedDate() {
return lastModifiedDate;
}
Expand Down
1 change: 1 addition & 0 deletions src/test/groovy/com/nfl/glitr/data/query/Identifiable.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

@GlitrDescription("Identifiable interface needed for Relay")
public interface Identifiable {

String getId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

@GlitrDescription("Identifiable interface needed for Relay")
public abstract class IdentifiableAbstractClass {

public abstract String getId();
}
1 change: 1 addition & 0 deletions src/test/groovy/com/nfl/glitr/data/query/Video.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class Video extends AbstractContent implements Playable {
private String url;
private List<Bitrate> bitrateList;


public List<Bitrate> getBitrateList() {
return bitrateList;
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/groovy/com/nfl/glitr/relay/RelayHelperTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
})
}
}

0 comments on commit 3bc1b86

Please sign in to comment.