Skip to content

Commit

Permalink
Add workflow, node, and edge tests
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Sep 22, 2023
1 parent f594249 commit 9d94bce
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
*/
package org.opensearch.flowframework.template;

import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.flowframework.workflow.Workflow;
import org.opensearch.test.OpenSearchTestCase;
Expand All @@ -20,10 +17,9 @@
import java.util.List;
import java.util.stream.Collectors;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.flowframework.template.GraphJsonUtil.edge;
import static org.opensearch.flowframework.template.GraphJsonUtil.node;
import static org.opensearch.flowframework.template.GraphJsonUtil.workflow;
import static org.opensearch.flowframework.template.TemplateTestJsonUtil.edge;
import static org.opensearch.flowframework.template.TemplateTestJsonUtil.node;
import static org.opensearch.flowframework.template.TemplateTestJsonUtil.workflow;

public class TemplateParserTests extends OpenSearchTestCase {

Expand All @@ -33,12 +29,7 @@ public class TemplateParserTests extends OpenSearchTestCase {

// Wrap parser into string list
private static List<String> parse(String json) throws IOException {
XContentParser parser = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
json
);
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
XContentParser parser = TemplateTestJsonUtil.jsonToParser(json);
Workflow w = Workflow.parse(parser);
return TemplateParser.parseWorkflowToSequence(w).stream().map(ProcessNode::id).collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@
*/
package org.opensearch.flowframework.template;

import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.json.JsonXContent;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.flowframework.workflow.Workflow;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;

/**
* Utility methods to create a JSON string useful for testing nodes and edges
* Utility methods for tests of template JSON
*/
public class GraphJsonUtil {
public class TemplateTestJsonUtil {

public static String node(String id) {
return "{\"" + WorkflowNode.ID_FIELD + "\": \"" + id + "\", \"" + WorkflowNode.TYPE_FIELD + "\": \"" + "placeholder" + "\"}";
Expand All @@ -34,4 +43,17 @@ private static String arrayField(String fieldName, List<String> objects) {
return "\"" + fieldName + "\": [" + objects.stream().collect(Collectors.joining(", ")) + "]";
}

public static String parseToJson(ToXContentObject object) throws IOException {
return object.toXContent(JsonXContent.contentBuilder(), ToXContent.EMPTY_PARAMS).toString();
}

public static XContentParser jsonToParser(String json) throws IOException {
XContentParser parser = JsonXContent.jsonXContent.createParser(
NamedXContentRegistry.EMPTY,
LoggingDeprecationHandler.INSTANCE,
json
);
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
return parser;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
*/
package org.opensearch.flowframework.template;

import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;

public class WorkflowEdgeTests extends OpenSearchTestCase {

@Override
public void setUp() throws Exception {
super.setUp();
}

public void testEdge() {
public void testEdge() throws IOException {
WorkflowEdge edgeAB = new WorkflowEdge("A", "B");
assertEquals("A", edgeAB.source());
assertEquals("B", edgeAB.destination());
Expand All @@ -28,5 +31,15 @@ public void testEdge() {

WorkflowEdge edgeAC = new WorkflowEdge("A", "C");
assertNotEquals(edgeAB, edgeAC);

String expectedJson = "{\"source\":\"A\",\"dest\":\"B\"}";
String json = TemplateTestJsonUtil.parseToJson(edgeAB);
assertEquals(expectedJson, json);

XContentParser parser = TemplateTestJsonUtil.jsonToParser(json);
WorkflowEdge edgeX = WorkflowEdge.parse(parser);
assertEquals("A", edgeX.source());
assertEquals("B", edgeX.destination());
assertEquals("A->B", edgeX.toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.flowframework.template;

import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.util.Map;

public class WorkflowNodeTests extends OpenSearchTestCase {

@Override
public void setUp() throws Exception {
super.setUp();
}

public void testNode() throws IOException {
WorkflowNode nodeA = new WorkflowNode("A", "a-type", Map.of("foo", "bar"));
assertEquals("A", nodeA.id());
assertEquals("a-type", nodeA.type());
assertEquals(Map.of("foo", "bar"), nodeA.inputs());

// node equality is based only on ID
WorkflowNode nodeA2 = new WorkflowNode("A", "a2-type", Map.of("bar", "baz"));
assertEquals(nodeA, nodeA2);

WorkflowNode nodeB = new WorkflowNode("B", "b-type", Map.of("baz", "qux"));
assertNotEquals(nodeA, nodeB);

String expectedJson = "{\"id\":\"A\",\"type\":\"a-type\",\"inputs\":{\"foo\":\"bar\"}}";
String json = TemplateTestJsonUtil.parseToJson(nodeA);
assertEquals(expectedJson, json);

XContentParser parser = TemplateTestJsonUtil.jsonToParser(json);
WorkflowNode nodeX = WorkflowNode.parse(parser);
assertEquals("A", nodeX.id());
assertEquals("a-type", nodeX.type());
assertEquals(Map.of("foo", "bar"), nodeX.inputs());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.flowframework.template;

import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.flowframework.workflow.Workflow;
import org.opensearch.test.OpenSearchTestCase;

import java.io.IOException;
import java.util.List;
import java.util.Map;

public class WorkflowTests extends OpenSearchTestCase {

@Override
public void setUp() throws Exception {
super.setUp();
}

public void testWorkflow() throws IOException {
WorkflowNode nodeA = new WorkflowNode("A", "a-type", Map.of("foo", "bar"));
WorkflowNode nodeB = new WorkflowNode("B", "b-type", Map.of("baz", "qux"));
WorkflowEdge edgeAB = new WorkflowEdge("A", "B");
List<WorkflowNode> nodes = List.of(nodeA, nodeB);
List<WorkflowEdge> edges = List.of(edgeAB);

Workflow workflow = new Workflow(Map.of("key", "value"), nodes, edges);
assertEquals(Map.of("key", "value"), workflow.userParams());
assertEquals(List.of(nodeA, nodeB), workflow.nodes());
assertEquals(List.of(edgeAB), workflow.edges());

String expectedJson = "{\"user_params\":{\"key\":\"value\"},"
+ "\"nodes\":[{\"id\":\"A\",\"type\":\"a-type\",\"inputs\":{\"foo\":\"bar\"}},"
+ "{\"id\":\"B\",\"type\":\"b-type\",\"inputs\":{\"baz\":\"qux\"}}],"
+ "\"edges\":[{\"source\":\"A\",\"dest\":\"B\"}]}";
String json = TemplateTestJsonUtil.parseToJson(workflow);
assertEquals(expectedJson, json);

XContentParser parser = TemplateTestJsonUtil.jsonToParser(json);
Workflow workflowX = Workflow.parse(parser);
assertEquals(Map.of("key", "value"), workflowX.userParams());
assertEquals(List.of(nodeA, nodeB), workflowX.nodes());
assertEquals(List.of(edgeAB), workflowX.edges());
}
}

0 comments on commit 9d94bce

Please sign in to comment.