Skip to content

Commit

Permalink
updating unit tests for FlowFrameworkPluginTests, adding WorkflowRequ…
Browse files Browse the repository at this point in the history
…estResponse unit tests

Signed-off-by: Joshua Palis <[email protected]>
  • Loading branch information
joshpalis committed Oct 5, 2023
1 parent cc32079 commit a51c681
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public void tearDown() throws Exception {
public void testPlugin() throws IOException {
try (FlowFrameworkPlugin ffp = new FlowFrameworkPlugin()) {
assertEquals(2, ffp.createComponents(client, null, threadPool, null, null, null, null, null, null, null, null).size());
assertEquals(2, ffp.getRestHandlers(null, null, null, null, null, null, null).size());
assertEquals(2, ffp.getActions().size());
assertEquals(1, ffp.getExecutorBuilders(null).size());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* 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.transport;

import org.opensearch.Version;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.BytesStreamInput;
import org.opensearch.flowframework.model.Template;
import org.opensearch.flowframework.model.Workflow;
import org.opensearch.flowframework.model.WorkflowEdge;
import org.opensearch.flowframework.model.WorkflowNode;
import org.opensearch.test.OpenSearchTestCase;

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

public class WorkflowRequestResponseTests extends OpenSearchTestCase {

private Template template;

@Override
public void setUp() throws Exception {
super.setUp();
List<String> operations = List.of("operation");
Version templateVersion = Version.fromString("1.0.0");
List<Version> compatibilityVersions = List.of(Version.fromString("2.0.0"), Version.fromString("3.0.0"));
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);

this.template = new Template(
"test",
"description",
"use case",
operations,
templateVersion,
compatibilityVersions,
Map.ofEntries(Map.entry("userKey", "userValue"), Map.entry("userMapKey", Map.of("nestedKey", "nestedValue"))),
Map.of("workflow", workflow)
);
}

public void testNullIdWorkflowRequest() throws IOException {
WorkflowRequest nullIdRequest = new WorkflowRequest(null, template);
assertNull(nullIdRequest.getWorkflowId());
assertEquals(template, nullIdRequest.getTemplate());

BytesStreamOutput out = new BytesStreamOutput();
nullIdRequest.writeTo(out);
BytesStreamInput in = new BytesStreamInput(BytesReference.toBytes(out.bytes()));

WorkflowRequest streamInputRequest = new WorkflowRequest(in);

assertEquals(nullIdRequest.getWorkflowId(), streamInputRequest.getWorkflowId());
assertEquals(nullIdRequest.getTemplate().toJson(), streamInputRequest.getTemplate().toJson());
}

public void testNullTemplateWorkflowRequest() throws IOException {
WorkflowRequest nullTemplateRequest = new WorkflowRequest("123", null);
assertNotNull(nullTemplateRequest.getWorkflowId());
assertNull(nullTemplateRequest.getTemplate());

BytesStreamOutput out = new BytesStreamOutput();
nullTemplateRequest.writeTo(out);
BytesStreamInput in = new BytesStreamInput(BytesReference.toBytes(out.bytes()));

WorkflowRequest streamInputRequest = new WorkflowRequest(in);

assertEquals(nullTemplateRequest.getWorkflowId(), streamInputRequest.getWorkflowId());
assertEquals(nullTemplateRequest.getTemplate(), streamInputRequest.getTemplate());
}

public void testWorkflowRequest() throws IOException {
WorkflowRequest workflowRequest = new WorkflowRequest("123", template);
assertNotNull(workflowRequest.getWorkflowId());
assertEquals(template, workflowRequest.getTemplate());

BytesStreamOutput out = new BytesStreamOutput();
workflowRequest.writeTo(out);
BytesStreamInput in = new BytesStreamInput(BytesReference.toBytes(out.bytes()));

WorkflowRequest streamInputRequest = new WorkflowRequest(in);

assertEquals(workflowRequest.getWorkflowId(), streamInputRequest.getWorkflowId());
assertEquals(workflowRequest.getTemplate().toJson(), streamInputRequest.getTemplate().toJson());

}

public void testWorkflowResponse() throws IOException {
WorkflowResponse response = new WorkflowResponse("123");
assertEquals("123", response.getWorkflowId());

BytesStreamOutput out = new BytesStreamOutput();
response.writeTo(out);
BytesStreamInput in = new BytesStreamInput(BytesReference.toBytes(out.bytes()));

WorkflowResponse streamInputResponse = new WorkflowResponse(in);

assertEquals(response.getWorkflowId(), streamInputResponse.getWorkflowId());
}

}

0 comments on commit a51c681

Please sign in to comment.