Skip to content

Commit

Permalink
Add patch extension to DSL (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkusa authored and aklish committed Aug 16, 2019
1 parent 9c1ad5d commit 598a243
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
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.PatchOperation;
import com.yahoo.elide.contrib.testhelpers.jsonapi.elements.PatchOperationType;
import com.yahoo.elide.contrib.testhelpers.jsonapi.elements.PatchSet;
import com.yahoo.elide.contrib.testhelpers.jsonapi.elements.Relation;
import com.yahoo.elide.contrib.testhelpers.jsonapi.elements.Relationships;
import com.yahoo.elide.contrib.testhelpers.jsonapi.elements.Resource;
Expand Down Expand Up @@ -273,4 +276,22 @@ public static Relation relation(String field, boolean toOne) {
public static ResourceLinkage linkage(Type type, Id id) {
return new ResourceLinkage(id, type);
}

/**
* @param patchOperations the set of patch operation
* @return the patch set
*/
public static PatchSet patchSet(PatchOperation... patchOperations) {
return new PatchSet(patchOperations);
}

/**
* @param operation the operation type
* @param path the operation path
* @param value the operation value
* @return
*/
public static PatchOperation patchOperation(PatchOperationType operation, String path, Resource value) {
return new PatchOperation(operation, path, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019, 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 PatchOperation extends LinkedHashMap<String, Object> {

/**
* @param operation the operation type
* @param path the operation path
* @param value the operation value
*/
public PatchOperation(PatchOperationType operation, String path, Resource value) {
this.put("op", operation.name());
this.put("path", path);
this.put("value", value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.yahoo.elide.contrib.testhelpers.jsonapi.elements;

public enum PatchOperationType {
add, remove, replace
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2019, 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 com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;
import java.util.Arrays;

public class PatchSet extends ArrayList {

static private final Gson GSON_INSTANCE = new GsonBuilder()
.serializeNulls().create();

/**
* @param patchOperations the set of patch operations
*/
public PatchSet(PatchOperation... patchOperations) {
this.addAll(Arrays.asList(patchOperations));
}

/**
* To json string.
*
* @return the string
*/
public String toJSON() {
return GSON_INSTANCE.toJson(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
package com.yahoo.elide.contrib.testhelpers.jsonapi;

import static com.yahoo.elide.contrib.testhelpers.jsonapi.JsonApiDSL.*;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.elements.Relation.TO_ONE;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.elements.PatchOperationType.*;
import static com.yahoo.elide.contrib.testhelpers.jsonapi.elements.Relation.*;
import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;
Expand Down Expand Up @@ -280,4 +281,34 @@ public void verifyNoId() {

assertEquals(actual, expected);
}

@Test
public void verifyPatchOperation() {
String expected = "[{\"op\":\"add\",\"path\":\"/parent\",\"value\":{\"type\":\"parent\",\"id\":\"1\",\"relationships\":{\"children\":{\"data\":[{\"type\":\"child\",\"id\":\"2\"}]},\"spouses\":{\"data\":[{\"type\":\"parent\",\"id\":\"3\"}]}}}},{\"op\":\"add\",\"path\":\"/parent/1/children\",\"value\":{\"type\":\"child\",\"id\":\"2\"}}]";

String actual = patchSet(
patchOperation(add, "/parent",
resource(
type("parent"),
id("1"),
relationships(
relation("children",
linkage(type("child"), id("2"))
),
relation("spouses",
linkage(type("parent"), id("3"))
)
)
)
),
patchOperation(add, "/parent/1/children",
resource(
type("child"),
id("2")
)
)
).toJSON();

assertEquals(actual, expected);
}
}

0 comments on commit 598a243

Please sign in to comment.