Skip to content

Commit

Permalink
Adding unit tests for create and provision rest actions
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Palis <[email protected]>
  • Loading branch information
joshpalis committed Oct 5, 2023
1 parent 524407e commit 6aaa8c7
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.rest;

import org.opensearch.client.node.NodeClient;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.flowframework.FlowFrameworkPlugin;
import org.opensearch.rest.RestHandler.Route;
import org.opensearch.rest.RestRequest;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.rest.FakeRestRequest;

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

import static org.mockito.Mockito.mock;

public class RestCreateWorkflowActionTests extends OpenSearchTestCase {

private String invalidTemplate;
private RestCreateWorkflowAction createWorkflowRestAction;
private String createWorkflowPath;
private String updateWorkflowPath;
private NodeClient nodeClient;

@Override
public void setUp() throws Exception {
super.setUp();
// Invalid template configuration, missing name field
this.invalidTemplate = "{\"description\":\"description\","
+ "\"use_case\":\"use case\","
+ "\"operations\":[\"operation\"],"
+ "\"version\":{\"template\":\"1.0.0\",\"compatibility\":[\"2.0.0\",\"3.0.0\"]},"
+ "\"user_inputs\":{\"userKey\":\"userValue\",\"userMapKey\":{\"nestedKey\":\"nestedValue\"}},"
+ "\"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\"}]}}}";
this.createWorkflowRestAction = new RestCreateWorkflowAction();
this.createWorkflowPath = String.format(Locale.ROOT, "%s", FlowFrameworkPlugin.WORKFLOWS_URI);
this.updateWorkflowPath = String.format(Locale.ROOT, "%s/{%s}", FlowFrameworkPlugin.WORKFLOWS_URI, "workflow_id");
this.nodeClient = mock(NodeClient.class);
}

public void testRestCreateWorkflowActionName() {
String name = createWorkflowRestAction.getName();
assertEquals("create_workflow_action", name);
}

public void testRestCreateWorkflowActionRoutes() {
List<Route> routes = createWorkflowRestAction.routes();
assertEquals(2, routes.size());
assertEquals(RestRequest.Method.POST, routes.get(0).getMethod());
assertEquals(RestRequest.Method.PUT, routes.get(1).getMethod());
assertEquals(this.createWorkflowPath, routes.get(0).getPath());
assertEquals(this.updateWorkflowPath, routes.get(1).getPath());

}

public void testInvalidCreateWorkflowRequest() throws IOException {
RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.POST)
.withPath(this.createWorkflowPath)
.withContent(new BytesArray(invalidTemplate), MediaTypeRegistry.JSON)
.build();

IOException ex = expectThrows(IOException.class, () -> { createWorkflowRestAction.prepareRequest(request, nodeClient); });
assertEquals("An template object requires a name.", ex.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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.rest;

import org.opensearch.client.node.NodeClient;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.xcontent.MediaTypeRegistry;
import org.opensearch.flowframework.FlowFrameworkPlugin;
import org.opensearch.rest.RestHandler.Route;
import org.opensearch.rest.RestRequest;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.rest.FakeRestRequest;

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

import static org.mockito.Mockito.mock;

public class RestProvisionWorkflowActionTests extends OpenSearchTestCase {

private String invalidTemplate;
private RestProvisionWorkflowAction provisionWorkflowRestAction;
private String provisionInlineWorkflowPath;
private String provisionSavedWorkflowPath;
private NodeClient nodeClient;

@Override
public void setUp() throws Exception {
super.setUp();
// Invalid template configuration, missing name field
this.invalidTemplate = "{\"description\":\"description\","
+ "\"use_case\":\"use case\","
+ "\"operations\":[\"operation\"],"
+ "\"version\":{\"template\":\"1.0.0\",\"compatibility\":[\"2.0.0\",\"3.0.0\"]},"
+ "\"user_inputs\":{\"userKey\":\"userValue\",\"userMapKey\":{\"nestedKey\":\"nestedValue\"}},"
+ "\"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\"}]}}}";
this.provisionWorkflowRestAction = new RestProvisionWorkflowAction();
this.provisionInlineWorkflowPath = String.format(Locale.ROOT, "%s/%s", FlowFrameworkPlugin.WORKFLOWS_URI, "_provision");
this.provisionSavedWorkflowPath = String.format(
Locale.ROOT,
"%s/{%s}/%s",
FlowFrameworkPlugin.WORKFLOWS_URI,
"workflow_id",
"_provision"
);
this.nodeClient = mock(NodeClient.class);
}

public void testRestProvisionWorkflowActionName() {
String name = provisionWorkflowRestAction.getName();
assertEquals("provision_workflow_action", name);
}

public void testRestProvisiionWorkflowActionRoutes() {
List<Route> routes = provisionWorkflowRestAction.routes();
assertEquals(2, routes.size());
assertEquals(RestRequest.Method.POST, routes.get(0).getMethod());
assertEquals(RestRequest.Method.POST, routes.get(1).getMethod());
assertEquals(this.provisionInlineWorkflowPath, routes.get(0).getPath());
assertEquals(this.provisionSavedWorkflowPath, routes.get(1).getPath());
}

public void testNullWorkflowIdAndTemplate() throws IOException {

// Request with no content or params
RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.POST)
.withPath(this.provisionInlineWorkflowPath)
.build();

IOException ex = expectThrows(IOException.class, () -> { provisionWorkflowRestAction.prepareRequest(request, nodeClient); });
assertEquals("workflow_id and template cannot be both null", ex.getMessage());
}

public void testInvalidProvisionInlineWorkflowRequest() throws IOException {
RestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.POST)
.withPath(this.provisionInlineWorkflowPath)
.withContent(new BytesArray(invalidTemplate), MediaTypeRegistry.JSON)
.build();

IOException ex = expectThrows(IOException.class, () -> { provisionWorkflowRestAction.prepareRequest(request, nodeClient); });
assertEquals("An template object requires a name.", ex.getMessage());
}

}

0 comments on commit 6aaa8c7

Please sign in to comment.