Skip to content

Commit

Permalink
Added links support to entity resource. (#1445)
Browse files Browse the repository at this point in the history
* Added links support to entity resource. Links can be customized based on usecase

* remove commented code

* remove unused variable

* comments fix

* Use fully qualified URL in links

* remove unused imports

* include baseURL in all rest method for jsonApi

* Added baseUrl support for GraphQl Request Scope

* codacy fix

* typo fix

* remove default scope attributes

* unittest for JsonApiDSL

* add relation test on JSONDSL

Co-authored-by: Chandrasekar Rajasekar <chandrasekar.rajasekar@oath.com>
  • Loading branch information
Chandrasekar-Rajasekar and Chandrasekar Rajasekar committed Jul 31, 2020
1 parent 95db95f commit 6ae9bd3
Show file tree
Hide file tree
Showing 44 changed files with 746 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.yahoo.elide.contrib.testhelpers.jsonapi.elements.Document;
import com.yahoo.elide.contrib.testhelpers.jsonapi.elements.Id;
import com.yahoo.elide.contrib.testhelpers.jsonapi.elements.Include;
import com.yahoo.elide.contrib.testhelpers.jsonapi.elements.Links;
import com.yahoo.elide.contrib.testhelpers.jsonapi.elements.PatchOperation;
import com.yahoo.elide.contrib.testhelpers.jsonapi.elements.PatchOperationType;
import com.yahoo.elide.contrib.testhelpers.jsonapi.elements.PatchSet;
Expand Down Expand Up @@ -101,7 +102,34 @@ public static Document document(Data resources, Include includes) {
* @return the resource
*/
public static Resource resource(Type type, Id id, Attributes attributes, Relationships relationships) {
return new Resource(id, type, attributes, relationships);
return new Resource(id, type, attributes, null, relationships);
}

/**
* Resource resource.
*
* @param type the type
* @param id the id
* @param attributes the attributes
* @param link the link
* @param relationships the relationships
* @return the resource
*/
public static Resource resource(Type type, Id id, Attributes attributes, Links link, Relationships relationships) {
return new Resource(id, type, attributes, link, relationships);
}

/**
* Resource resource.
*
* @param type the type
* @param id the id
* @param attributes the attributes
* @param link the link
* @return the resource
*/
public static Resource resource(Type type, Id id, Attributes attributes, Links link) {
return new Resource(id, type, attributes, link, null);
}

/**
Expand All @@ -113,7 +141,7 @@ public static Resource resource(Type type, Id id, Attributes attributes, Relatio
* @return the resource
*/
public static Resource resource(Type type, Id id, Attributes attributes) {
return new Resource(id, type, attributes, null);
return new Resource(id, type, attributes, null, null);
}

/**
Expand All @@ -125,7 +153,7 @@ public static Resource resource(Type type, Id id, Attributes attributes) {
* @return the resource
*/
public static Resource resource(Type type, Id id, Relationships relationships) {
return new Resource(id, type, null, relationships);
return new Resource(id, type, null, null, relationships);
}

/**
Expand All @@ -136,7 +164,7 @@ public static Resource resource(Type type, Id id, Relationships relationships) {
* @return the resource
*/
public static Resource resource(Type type, Id id) {
return new Resource(id, type, null, null);
return new Resource(id, type, null, null, null);
}

/**
Expand All @@ -147,7 +175,7 @@ public static Resource resource(Type type, Id id) {
* @return the resource
*/
public static Resource resource(Type type, Attributes attributes) {
return new Resource(id(null), type, attributes, null);
return new Resource(id(null), type, attributes, null, null);
}

/**
Expand All @@ -159,7 +187,7 @@ public static Resource resource(Type type, Attributes attributes) {
* @return the resource
*/
public static Resource resource(Type type, Attributes attributes, Relationships relationships) {
return new Resource(id(null), type, attributes, relationships);
return new Resource(id(null), type, attributes, null, relationships);
}

/**
Expand Down Expand Up @@ -192,6 +220,16 @@ public static Attributes attributes(Attribute... attrs) {
return new Attributes(attrs);
}

/**
* Links links.
*
* @param attrs the attrs
* @return the attributes
*/
public static Links links(Attribute... attrs) {
return new Links(attrs);
}

/**
* Attr attribute.
*
Expand Down Expand Up @@ -233,7 +271,7 @@ public static Relation relation(String field, ResourceLinkage... resourceLinkage
* @return the relation
*/
public static Relation relation(String field, boolean toOne, ResourceLinkage... resourceLinkage) {
return new Relation(field, toOne, resourceLinkage);
return new Relation(field, toOne, null, resourceLinkage);
}

/**
Expand All @@ -246,6 +284,28 @@ public static Relation relation(String field) {
return new Relation(field);
}

/**
* Relation relation.
*
* @param field the field
* @param links the links
* @return the relation
*/
public static Relation relation(String field, Links links) {
return new Relation(field, links);
}

/**
* Relation relation.
*
* @param field the field
* @param links the links
* @param resourceLinkage the resource linkage
* @return the relation
*/
public static Relation relation(String field, Links links, ResourceLinkage... resourceLinkage) {
return new Relation(field, links, resourceLinkage);
}
/**
* Relation relation.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2020, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.contrib.testhelpers.jsonapi.elements;

import java.util.LinkedHashMap;

public class Links extends LinkedHashMap<String, Object> {
/**
* Instantiates a new Attributes.
*
* @param attributes the attributes
*/
public Links(Attribute... attributes) {
for (Attribute attribute : attributes) {
this.put(attribute.key, attribute.value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import com.google.gson.annotations.Expose;

import lombok.Getter;

/**
* The type Relation.
*/
Expand All @@ -19,15 +21,20 @@ public class Relation {
/**
* The Field.
*/
final String field;
@Getter private final String field;

@Expose(serialize = false)
final boolean toOne;
@Getter private final boolean toOne;

/**
* The Links.
*/
@Getter private final Links links;

/**
* The Resource linkages.
*/
final ResourceLinkage[] resourceLinkages;
@Getter private final ResourceLinkage[] resourceLinkages;

/**
* Instantiates a new Relation.
Expand All @@ -36,7 +43,18 @@ public class Relation {
* @param resourceLinkages the resource linkages
*/
public Relation(String field, ResourceLinkage... resourceLinkages) {
this(field, TO_MANY, resourceLinkages);
this(field, TO_MANY, null, resourceLinkages);
}

/**
* Instantiates a new Relation.
*
* @param field the field
* @param links the links
* @param resourceLinkages the resource linkages
*/
public Relation(String field, Links links, ResourceLinkage... resourceLinkages) {
this(field, TO_MANY, links, resourceLinkages);
}

/**
Expand All @@ -46,9 +64,10 @@ public Relation(String field, ResourceLinkage... resourceLinkages) {
* @param toOne whether or not the relation is toOne or toMany.
* @param resourceLinkages the resource linkages
*/
public Relation(String field, boolean toOne, ResourceLinkage... resourceLinkages) {
public Relation(String field, boolean toOne, Links links, ResourceLinkage... resourceLinkages) {
this.field = field;
this.toOne = toOne;
this.links = links;
this.resourceLinkages = resourceLinkages;
}

Expand All @@ -61,6 +80,17 @@ public Relation(String field) {
this(field, TO_MANY);
}


/**
* Instantiates a new Relation.
*
* @param field the field
* @param links the links
*/
public Relation(String field, Links links) {
this(field, TO_MANY, links);
}

/**
* Instantiates a new Relation.
*
Expand All @@ -70,6 +100,7 @@ public Relation(String field) {
public Relation(String field, boolean toOne) {
this.field = field;
this.toOne = toOne;
this.links = null;
this.resourceLinkages = new ResourceLinkage[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ public class Relationships extends LinkedHashMap<String, Map<String, ?>> {
public Relationships(Relation... relations) {
for (Relation relation : relations) {
Map<String, Object> data = new LinkedHashMap<>();
if (relation.toOne) {
if (relation.resourceLinkages.length == 0) {
if (relation.getLinks() != null) {
data.put("links", relation.getLinks());
}
if (relation.isToOne()) {
if (relation.getResourceLinkages().length == 0) {
data.put("data", null);
} else {
data.put("data", relation.resourceLinkages[0]);
data.put("data", relation.getResourceLinkages()[0]);
}
} else {
data.put("data", relation.resourceLinkages);
data.put("data", relation.getResourceLinkages());
}
this.put(relation.field, data);
this.put(relation.getField(), data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ public class Resource extends ResourceLinkage {
* @param id the id
* @param type the type
* @param attributes the attributes
* @param links the links
* @param relationships the relationships
*/
public Resource(Id id, Type type, Attributes attributes, Relationships relationships) {
public Resource(Id id, Type type, Attributes attributes, Links links, Relationships relationships) {
super(id, type);
if (attributes != null) {
this.put("attributes", attributes);
}
if (relationships != null) {
this.put("relationships", relationships);
}
if (links != null) {
this.put("links", links);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.id;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.include;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.linkage;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.links;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.patchOperation;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.patchSet;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.relation;
Expand Down Expand Up @@ -324,4 +325,73 @@ public void verifyPatchOperation() {

assertEquals(expected, actual);
}

@Test
public void verifyRequestWithLinks() {
String expected = "{\"data\":{\"type\":\"blog\",\"id\":\"1\",\"attributes\":"
+ "{\"title\":\"Why You Should use Elide\",\"date\":\"2019-01-01\"},"
+ "\"links\":{\"self\":\"http://localhost:8080/json/api/v1/blog/1\"}}}";

String actual = datum(
resource(
type("blog"),
id("1"),
attributes(
attr("title", "Why You Should use Elide"),
attr("date", "2019-01-01")
),
links(
attr("self", "http://localhost:8080/json/api/v1/blog/1")
)
)
).toJSON();

assertEquals(expected, actual);
}

@Test
public void verifyRequestWithLinksRelationship() {
String expected = "{\"data\":{\"type\":\"blog\",\"id\":\"1\",\"attributes\":"
+ "{\"title\":\"Why You Should use Elide\"},"
+ "\"relationships\":{"
+ "\"author\":{"
+ "\"links\":{\"self\":\"http://localhost:8080/json/api/v1/blog/1/relationships/author\",\"related\":\"http://localhost:8080/json/api/v1/blog/1/author\"},"
+ "\"data\":[{\"type\":\"author\",\"id\":\"1\"}]"
+ "},"
+ "\"comments\":{"
+ "\"links\":{\"self\":\"http://localhost:8080/json/api/v1/blog/1/relationships/comments\",\"related\":\"http://localhost:8080/json/api/v1/blog/1/comments\"},"
+ "\"data\":[]"
+ "}},"
+ "\"links\":{\"self\":\"http://localhost:8080/json/api/v1/blog/1\"}}}";

String actual = datum(
resource(
type("blog"),
id("1"),
attributes(
attr("title", "Why You Should use Elide")
),
links(
attr("self", "http://localhost:8080/json/api/v1/blog/1")
),
relationships(
relation("author",
links(
attr("self", "http://localhost:8080/json/api/v1/blog/1/relationships/author"),
attr("related", "http://localhost:8080/json/api/v1/blog/1/author")
),
linkage(type("author"), id("1"))
),
relation("comments",
links(
attr("self", "http://localhost:8080/json/api/v1/blog/1/relationships/comments"),
attr("related", "http://localhost:8080/json/api/v1/blog/1/comments")
)
)
)
)
).toJSON();

assertEquals(expected, actual);
}
}

0 comments on commit 6ae9bd3

Please sign in to comment.