From 88f018327357c5e2d3b8830a013ad04762d2dad0 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Fri, 22 Sep 2023 22:48:05 -0700 Subject: [PATCH] Finish up exceptional cases Signed-off-by: Daniel Widdis --- .../flowframework/template/TemplateTests.java | 58 +++++++++++++++---- ...s.java => WorkflowProcessSorterTests.java} | 13 ++++- 2 files changed, 60 insertions(+), 11 deletions(-) rename src/test/java/org/opensearch/flowframework/template/{TemplateParserTests.java => WorkflowProcessSorterTests.java} (89%) diff --git a/src/test/java/org/opensearch/flowframework/template/TemplateTests.java b/src/test/java/org/opensearch/flowframework/template/TemplateTests.java index ed3fc459b..70d341152 100644 --- a/src/test/java/org/opensearch/flowframework/template/TemplateTests.java +++ b/src/test/java/org/opensearch/flowframework/template/TemplateTests.java @@ -18,6 +18,15 @@ public class TemplateTests extends OpenSearchTestCase { + private String expectedPrefix = + "{\"name\":\"test\",\"description\":\"a test template\",\"use_case\":\"test use case\",\"operations\":[\"operation\"]," + + "\"version\":{\"template\":\"1.2.3\",\"compatibility\":[\"4.5.6\",\"7.8.9\"]},\"user_inputs\":{"; + private String expectedKV1 = "\"userKey\":\"userValue\""; + private String expectedKV2 = "\"userMapKey\":{\"nestedKey\":\"nestedValue\"}"; + private String expectedSuffix = "},\"workflows\":{\"workflow\":{\"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\"}]}}}"; + @Override public void setUp() throws Exception { super.setUp(); @@ -41,7 +50,7 @@ public void testTemplate() throws IOException { List.of("operation"), templateVersion, compatibilityVersion, - Map.of("userKey", "userValue"), + Map.ofEntries(Map.entry("userKey", "userValue"), Map.entry("userMapKey", Map.of("nestedKey", "nestedValue"))), Map.of("workflow", workflow) ); @@ -51,19 +60,18 @@ public void testTemplate() throws IOException { assertEquals(List.of("operation"), template.operations()); assertEquals(templateVersion, template.templateVersion()); assertEquals(compatibilityVersion, template.compatibilityVersion()); - assertEquals(Map.of("userKey", "userValue"), template.userInputs()); + Map inputsMap = template.userInputs(); + assertEquals("userValue", inputsMap.get("userKey")); + assertEquals(Map.of("nestedKey", "nestedValue"), inputsMap.get("userMapKey")); Workflow wf = template.workflows().get("workflow"); assertNotNull(wf); assertEquals("Workflow [userParams={key=value}, nodes=[A, B], edges=[A->B]]", wf.toString()); - String expectedJson = "{\"name\":\"test\",\"description\":\"a test template\",\"use_case\":\"test use case\"," - + "\"operations\":[\"operation\"],\"version\":{\"template\":\"1.2.3\",\"compatibility\":[\"4.5.6\",\"7.8.9\"]}," - + "\"user_inputs\":{\"userKey\":\"userValue\"},\"workflows\":{\"workflow\":{\"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(template); - assertEquals(expectedJson, json); + assertTrue(json.startsWith(expectedPrefix)); + assertTrue(json.contains(expectedKV1)); + assertTrue(json.contains(expectedKV2)); + assertTrue(json.endsWith(expectedSuffix)); Template templateX = Template.parse(json); assertEquals("test", templateX.name()); @@ -72,9 +80,39 @@ public void testTemplate() throws IOException { assertEquals(List.of("operation"), templateX.operations()); assertEquals(templateVersion, templateX.templateVersion()); assertEquals(compatibilityVersion, templateX.compatibilityVersion()); - assertEquals(Map.of("userKey", "userValue"), templateX.userInputs()); + Map inputsMapX = template.userInputs(); + assertEquals("userValue", inputsMapX.get("userKey")); + assertEquals(Map.of("nestedKey", "nestedValue"), inputsMapX.get("userMapKey")); Workflow wfX = templateX.workflows().get("workflow"); assertNotNull(wfX); assertEquals("Workflow [userParams={key=value}, nodes=[A, B], edges=[A->B]]", wfX.toString()); } + + public void testExceptions() throws IOException { + String json = expectedPrefix + expectedKV1 + "," + expectedKV2 + expectedSuffix; + IOException e; + + String badTemplateField = json.replace("use_case", "badField"); + e = assertThrows(IOException.class, () -> Template.parse(badTemplateField)); + assertEquals("Unable to parse field [badField] in a template object.", e.getMessage()); + + String badVersionField = json.replace("compatibility", "badField"); + e = assertThrows(IOException.class, () -> Template.parse(badVersionField)); + assertEquals("Unable to parse field [version] in a version object.", e.getMessage()); + + String badUserInputType = json.replace("{\"nestedKey\":\"nestedValue\"}},", "[]"); + e = assertThrows(IOException.class, () -> Template.parse(badUserInputType)); + assertEquals("Unable to parse field [userMapKey] in a user inputs object.", e.getMessage()); + } + + public void testStrings() throws IOException { + Template t = Template.parse(expectedPrefix + expectedKV1 + "," + expectedKV2 + expectedSuffix); + assertTrue(t.toJson().contains(expectedPrefix)); + assertTrue(t.toJson().contains(expectedKV1)); + assertTrue(t.toJson().contains(expectedKV2)); + assertTrue(t.toJson().contains(expectedSuffix)); + + assertTrue(t.toYaml().contains("a test template")); + assertTrue(t.toString().contains("a test template")); + } } diff --git a/src/test/java/org/opensearch/flowframework/template/TemplateParserTests.java b/src/test/java/org/opensearch/flowframework/template/WorkflowProcessSorterTests.java similarity index 89% rename from src/test/java/org/opensearch/flowframework/template/TemplateParserTests.java rename to src/test/java/org/opensearch/flowframework/template/WorkflowProcessSorterTests.java index f7bdea986..537fedbc7 100644 --- a/src/test/java/org/opensearch/flowframework/template/TemplateParserTests.java +++ b/src/test/java/org/opensearch/flowframework/template/WorkflowProcessSorterTests.java @@ -21,7 +21,7 @@ import static org.opensearch.flowframework.template.TemplateTestJsonUtil.node; import static org.opensearch.flowframework.template.TemplateTestJsonUtil.workflow; -public class TemplateParserTests extends OpenSearchTestCase { +public class WorkflowProcessSorterTests extends OpenSearchTestCase { private static final String MUST_HAVE_AT_LEAST_ONE_NODE = "A workflow must have at least one node."; private static final String NO_START_NODE_DETECTED = "No start node detected: all nodes have a predecessor."; @@ -131,4 +131,15 @@ public void testNoEdges() throws IOException { assertTrue(workflow.contains("A")); assertTrue(workflow.contains("B")); } + + public void testExceptions() throws IOException { + Exception ex = assertThrows( + IllegalArgumentException.class, + () -> parse(workflow(List.of(node("A"), node("B")), List.of(edge("C", "B")))) + ); + assertEquals("Edge source C does not correspond to a node.", ex.getMessage()); + + ex = assertThrows(IllegalArgumentException.class, () -> parse(workflow(List.of(node("A"), node("B")), List.of(edge("A", "C"))))); + assertEquals("Edge destination C does not correspond to a node.", ex.getMessage()); + } }