Skip to content

Commit

Permalink
Added tests and javadocs
Browse files Browse the repository at this point in the history
Signed-off-by: Owais Kazi <[email protected]>
  • Loading branch information
owaiskazi19 committed Jan 9, 2024
1 parent 755a4ce commit 92b18ef
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,33 @@
import org.opensearch.env.NodeEnvironment;
import org.opensearch.flowframework.common.FlowFrameworkSettings;
import org.opensearch.flowframework.indices.FlowFrameworkIndicesHandler;
import org.opensearch.flowframework.rest.*;
import org.opensearch.flowframework.transport.*;
import org.opensearch.flowframework.rest.RestCreateWorkflowAction;
import org.opensearch.flowframework.rest.RestDeleteWorkflowAction;
import org.opensearch.flowframework.rest.RestDeprovisionWorkflowAction;
import org.opensearch.flowframework.rest.RestGetWorkflowAction;
import org.opensearch.flowframework.rest.RestGetWorkflowStateAction;
import org.opensearch.flowframework.rest.RestGetWorkflowStepAction;
import org.opensearch.flowframework.rest.RestProvisionWorkflowAction;
import org.opensearch.flowframework.rest.RestSearchWorkflowAction;
import org.opensearch.flowframework.rest.RestSearchWorkflowStateAction;
import org.opensearch.flowframework.transport.CreateWorkflowAction;
import org.opensearch.flowframework.transport.CreateWorkflowTransportAction;
import org.opensearch.flowframework.transport.DeleteWorkflowAction;
import org.opensearch.flowframework.transport.DeleteWorkflowTransportAction;
import org.opensearch.flowframework.transport.DeprovisionWorkflowAction;
import org.opensearch.flowframework.transport.DeprovisionWorkflowTransportAction;
import org.opensearch.flowframework.transport.GetWorkflowAction;
import org.opensearch.flowframework.transport.GetWorkflowStateAction;
import org.opensearch.flowframework.transport.GetWorkflowStateTransportAction;
import org.opensearch.flowframework.transport.GetWorkflowStepAction;
import org.opensearch.flowframework.transport.GetWorkflowStepTransportAction;
import org.opensearch.flowframework.transport.GetWorkflowTransportAction;
import org.opensearch.flowframework.transport.ProvisionWorkflowAction;
import org.opensearch.flowframework.transport.ProvisionWorkflowTransportAction;
import org.opensearch.flowframework.transport.SearchWorkflowAction;
import org.opensearch.flowframework.transport.SearchWorkflowStateAction;
import org.opensearch.flowframework.transport.SearchWorkflowStateTransportAction;
import org.opensearch.flowframework.transport.SearchWorkflowTransportAction;
import org.opensearch.flowframework.util.EncryptorUtils;
import org.opensearch.flowframework.workflow.WorkflowProcessSorter;
import org.opensearch.flowframework.workflow.WorkflowStepFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,26 @@ public TimeValue getTimeout() {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
XContentBuilder xContentBuilder = builder.startObject();
xContentBuilder.startArray(INPUTS_FIELD);

Check warning on line 149 in src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java#L148-L149

Added lines #L148 - L149 were not covered by tests
for (String input: this.inputs) {
for (String input : this.inputs) {
xContentBuilder.value(input);
}
xContentBuilder.endArray();

Check warning on line 153 in src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java#L151-L153

Added lines #L151 - L153 were not covered by tests

xContentBuilder.startArray(OUTPUTS_FIELD);

Check warning on line 155 in src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java#L155

Added line #L155 was not covered by tests
for (String output: this.outputs) {
for (String output : this.outputs) {
xContentBuilder.value(output);
}
xContentBuilder.endArray();

Check warning on line 159 in src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java#L157-L159

Added lines #L157 - L159 were not covered by tests

xContentBuilder.startArray(REQUIRED_PLUGINS);

Check warning on line 161 in src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java#L161

Added line #L161 was not covered by tests
for (String rp: this.requiredPlugins) {
for (String rp : this.requiredPlugins) {
xContentBuilder.value(rp);
}
xContentBuilder.endArray();

Check warning on line 165 in src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java#L163-L165

Added lines #L163 - L165 were not covered by tests

xContentBuilder.field(TIMEOUT, timeout);
if (timeout != null) {
xContentBuilder.field(TIMEOUT, timeout);

Check warning on line 168 in src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java#L168

Added line #L168 was not covered by tests
}

return xContentBuilder.endObject();

Check warning on line 171 in src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/model/WorkflowStepValidator.java#L171

Added line #L171 was not covered by tests
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
/*
* 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.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.ExceptionsHelper;
import org.opensearch.client.node.NodeClient;
import org.opensearch.core.action.ActionListener;
Expand All @@ -8,12 +18,9 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.flowframework.common.FlowFrameworkSettings;
import org.opensearch.flowframework.exception.FlowFrameworkException;
import org.opensearch.flowframework.transport.GetWorkflowAction;
import org.opensearch.flowframework.transport.GetWorkflowStepAction;
import org.opensearch.flowframework.transport.WorkflowRequest;
import org.opensearch.rest.BaseRestHandler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestRequest;

Expand All @@ -24,22 +31,23 @@
import static org.opensearch.flowframework.common.CommonValue.WORKFLOW_URI;
import static org.opensearch.flowframework.common.FlowFrameworkSettings.FLOW_FRAMEWORK_ENABLED;

/**
* Rest Action to facilitate requests to get the workflow steps
*/
public class RestGetWorkflowStepAction extends BaseRestHandler {

private static final String GET_WORKFLOW_STEP_ACTION = "get_workflow_step";
private static final Logger logger = LogManager.getLogger(RestGetWorkflowStepAction.class);
private FlowFrameworkSettings flowFrameworkFeatureEnabledSetting;


/**
* Instantiates a new RestGetWorkflowAction
* Instantiates a new RestGetWorkflowStepAction
* @param flowFrameworkFeatureEnabledSetting Whether this API is enabled
*/
public RestGetWorkflowStepAction(FlowFrameworkSettings flowFrameworkFeatureEnabledSetting) {
this.flowFrameworkFeatureEnabledSetting = flowFrameworkFeatureEnabledSetting;
}


@Override
public String getName() {
return GET_WORKFLOW_STEP_ACTION;
Expand All @@ -55,8 +63,8 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient
try {
if (!flowFrameworkFeatureEnabledSetting.isFlowFrameworkEnabled()) {
throw new FlowFrameworkException(
"This API is disabled. To enable it, update the setting [" + FLOW_FRAMEWORK_ENABLED.getKey() + "] to true.",
RestStatus.FORBIDDEN
"This API is disabled. To enable it, update the setting [" + FLOW_FRAMEWORK_ENABLED.getKey() + "] to true.",
RestStatus.FORBIDDEN
);
}

Expand All @@ -67,8 +75,8 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient
}, exception -> {

Check warning on line 75 in src/main/java/org/opensearch/flowframework/rest/RestGetWorkflowStepAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/rest/RestGetWorkflowStepAction.java#L73-L75

Added lines #L73 - L75 were not covered by tests
try {
FlowFrameworkException ex = exception instanceof FlowFrameworkException
? (FlowFrameworkException) exception
: new FlowFrameworkException(exception.getMessage(), ExceptionsHelper.status(exception));
? (FlowFrameworkException) exception
: new FlowFrameworkException(exception.getMessage(), ExceptionsHelper.status(exception));
XContentBuilder exceptionBuilder = ex.toXContent(channel.newErrorBuilder(), ToXContent.EMPTY_PARAMS);
channel.sendResponse(new BytesRestResponse(ex.getRestStatus(), exceptionBuilder));

Check warning on line 81 in src/main/java/org/opensearch/flowframework/rest/RestGetWorkflowStepAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/rest/RestGetWorkflowStepAction.java#L78-L81

Added lines #L78 - L81 were not covered by tests

Expand All @@ -80,7 +88,7 @@ protected RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient

} catch (FlowFrameworkException ex) {
return channel -> channel.sendResponse(
new BytesRestResponse(ex.getRestStatus(), ex.toXContent(channel.newErrorBuilder(), ToXContent.EMPTY_PARAMS))
new BytesRestResponse(ex.getRestStatus(), ex.toXContent(channel.newErrorBuilder(), ToXContent.EMPTY_PARAMS))
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
/*
* 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.action.ActionType;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.flowframework.model.WorkflowStepValidator;

import static org.opensearch.flowframework.common.CommonValue.TRANSPORT_ACTION_NAME_PREFIX;

/**
* External Action for public facing RestGetWorkflowStepAction
*/
public class GetWorkflowStepAction extends ActionType<GetWorkflowStepResponse> {

/** The name of this action */
public static final String NAME = TRANSPORT_ACTION_NAME_PREFIX + "workflow_step/get";
/** An instance of this action */
public static final GetWorkflowStepAction INSTANCE = new GetWorkflowStepAction();

/**
* Instantiates this class
*/
public GetWorkflowStepAction() {
super(NAME, GetWorkflowStepResponse::new);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +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.transport;

import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.flowframework.model.WorkflowStepValidator;
import org.opensearch.flowframework.model.WorkflowValidator;

import java.io.IOException;

/**
* Transport Response from getting workflow step
*/
public class GetWorkflowStepResponse extends ActionResponse implements ToXContentObject {

private WorkflowValidator workflowValidator;

/**
* Instantiates a new GetWorkflowStepResponse from an input stream
* @param in the input stream to read from
* @throws IOException if the workflow json cannot be read from the input stream
*/
public GetWorkflowStepResponse(StreamInput in) throws IOException {
super(in);
this.workflowValidator = WorkflowValidator.parse(in.readString());
}

Check warning on line 35 in src/main/java/org/opensearch/flowframework/transport/GetWorkflowStepResponse.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/transport/GetWorkflowStepResponse.java#L33-L35

Added lines #L33 - L35 were not covered by tests

/**
* Instantiates a new GetWorkflowStepResponse
* @param workflowValidator the workflow validator
*/
public GetWorkflowStepResponse(WorkflowValidator workflowValidator) {
this.workflowValidator = workflowValidator;
}


@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(workflowValidator.toJson());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,45 @@
/*
* 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.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.ExceptionsHelper;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.client.Client;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.core.action.ActionListener;
import org.opensearch.flowframework.exception.FlowFrameworkException;
import org.opensearch.flowframework.indices.FlowFrameworkIndicesHandler;
import org.opensearch.flowframework.model.WorkflowValidator;
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

/**
* Transport action to retrieve a the workflow step json
*/
public class GetWorkflowStepTransportAction extends HandledTransportAction<WorkflowRequest, GetWorkflowStepResponse> {


private final Logger logger = LogManager.getLogger(GetWorkflowStepTransportAction.class);
private final FlowFrameworkIndicesHandler flowFrameworkIndicesHandler;
private final Client client;

/**
* Instantiates a new GetWorkflowStepTransportAction instance
* @param transportService the transport service
* @param actionFilters action filters
* @param flowFrameworkIndicesHandler The Flow Framework indices handler
* @param client the OpenSearch Client
*/
@Inject
public GetWorkflowStepTransportAction(
TransportService transportService,
ActionFilters actionFilters,
FlowFrameworkIndicesHandler flowFrameworkIndicesHandler,
Client client
) {
public GetWorkflowStepTransportAction(TransportService transportService, ActionFilters actionFilters) {
super(GetWorkflowStepAction.NAME, transportService, actionFilters, WorkflowRequest::new);
this.flowFrameworkIndicesHandler = flowFrameworkIndicesHandler;
this.client = client;
}

@Override
protected void doExecute(Task task, WorkflowRequest request, ActionListener<GetWorkflowStepResponse> listener) {
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {
try {
listener.onResponse(new GetWorkflowStepResponse(WorkflowValidator.parse("mappings/workflow-steps.json")));
} catch (Exception e) {
logger.error("Failed to retrieve workflow step json.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ public void testPlugin() throws IOException {
5,
ffp.createComponents(client, clusterService, threadPool, null, null, null, environment, null, null, null, null).size()
);
assertEquals(8, ffp.getRestHandlers(settings, null, null, null, null, null, null).size());
assertEquals(8, ffp.getActions().size());
assertEquals(9, ffp.getRestHandlers(settings, null, null, null, null, null, null).size());
assertEquals(9, ffp.getActions().size());
assertEquals(1, ffp.getExecutorBuilders(settings).size());
assertEquals(5, ffp.getSettings().size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,12 +438,12 @@ protected Response getWorkflowStatus(String workflowId, boolean all) throws Exce

protected Response getWorkflowStep() throws Exception {
return TestHelpers.makeRequest(
client(),
"GET",
String.format("%s/%s", WORKFLOW_URI, "_step"),
Collections.emptyMap(),
"",
null
client(),
"GET",
String.format(Locale.ROOT, "%s/%s", WORKFLOW_URI, "_step"),
Collections.emptyMap(),
"",
null
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@
import org.opensearch.core.rest.RestStatus;
import org.opensearch.flowframework.FlowFrameworkRestTestCase;
import org.opensearch.flowframework.TestHelpers;
import org.opensearch.flowframework.model.*;
import org.opensearch.flowframework.transport.GetWorkflowStepResponse;
import org.opensearch.flowframework.model.ProvisioningProgress;
import org.opensearch.flowframework.model.ResourceCreated;
import org.opensearch.flowframework.model.State;
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.flowframework.model.WorkflowState;

import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -62,10 +68,6 @@ public void testSearchWorkflows() throws Exception {
}
}

public void testGetWorkflowStep() throws Exception {
getAndAssertWorkflowStep();
}

public void testCreateAndProvisionLocalModelWorkflow() throws Exception {
// Using a 1 step template to register a local model and deploy model
Template template = TestHelpers.createTemplateFromFile("register-deploylocalsparseencodingmodel.json");
Expand Down
Loading

0 comments on commit 92b18ef

Please sign in to comment.