diff --git a/src/test/java/org/opensearch/flowframework/template/TemplateParserTests.java b/src/test/java/org/opensearch/flowframework/template/TemplateParserTests.java index 1d85e059c..85a4a15c2 100644 --- a/src/test/java/org/opensearch/flowframework/template/TemplateParserTests.java +++ b/src/test/java/org/opensearch/flowframework/template/TemplateParserTests.java @@ -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; @@ -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 { @@ -33,12 +29,7 @@ public class TemplateParserTests extends OpenSearchTestCase { // Wrap parser into string list private static List 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()); } diff --git a/src/test/java/org/opensearch/flowframework/template/GraphJsonUtil.java b/src/test/java/org/opensearch/flowframework/template/TemplateTestJsonUtil.java similarity index 52% rename from src/test/java/org/opensearch/flowframework/template/GraphJsonUtil.java rename to src/test/java/org/opensearch/flowframework/template/TemplateTestJsonUtil.java index 026d4f6e6..bd59fd61b 100644 --- a/src/test/java/org/opensearch/flowframework/template/GraphJsonUtil.java +++ b/src/test/java/org/opensearch/flowframework/template/TemplateTestJsonUtil.java @@ -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" + "\"}"; @@ -34,4 +43,17 @@ private static String arrayField(String fieldName, List 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; + } } diff --git a/src/test/java/org/opensearch/flowframework/template/WorkflowEdgeTests.java b/src/test/java/org/opensearch/flowframework/template/WorkflowEdgeTests.java index 6e863445a..358081ef8 100644 --- a/src/test/java/org/opensearch/flowframework/template/WorkflowEdgeTests.java +++ b/src/test/java/org/opensearch/flowframework/template/WorkflowEdgeTests.java @@ -8,8 +8,11 @@ */ 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 @@ -17,7 +20,7 @@ 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()); @@ -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()); } } diff --git a/src/test/java/org/opensearch/flowframework/template/WorkflowNodeTests.java b/src/test/java/org/opensearch/flowframework/template/WorkflowNodeTests.java new file mode 100644 index 000000000..83d0dcd5a --- /dev/null +++ b/src/test/java/org/opensearch/flowframework/template/WorkflowNodeTests.java @@ -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()); + } +} diff --git a/src/test/java/org/opensearch/flowframework/template/WorkflowTests.java b/src/test/java/org/opensearch/flowframework/template/WorkflowTests.java new file mode 100644 index 000000000..ee149ecae --- /dev/null +++ b/src/test/java/org/opensearch/flowframework/template/WorkflowTests.java @@ -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 nodes = List.of(nodeA, nodeB); + List 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()); + } +}