Skip to content

Commit

Permalink
Finish up exceptional cases
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Sep 23, 2023
1 parent ad6ea11 commit 88f0183
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)
);

Expand All @@ -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<String, Object> 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());
Expand All @@ -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<String, Object> 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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 88f0183

Please sign in to comment.