Skip to content

Commit

Permalink
Change WorkflowData from interface to class
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Sep 25, 2023
1 parent a574f47 commit 0e56b4a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 38 deletions.
7 changes: 1 addition & 6 deletions src/main/java/demo/CreateIndexWorkflowStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ public CompletableFuture<WorkflowData> execute(List<WorkflowData> data) {
} catch (InterruptedException e) {}
// Simulate response of created index
CreateIndexResponse response = new CreateIndexResponse(true, true, inputIndex);
future.complete(new WorkflowData() {
@Override
public Map<String, Object> getContent() {
return Map.of("index", response.index());
}
});
future.complete(new WorkflowData(Map.of("index", response.index())));
});

return future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,10 @@ public static List<ProcessNode> parseJsonGraphToSequence(String json, Map<String
WorkflowData inputData = WorkflowData.EMPTY;
if (List.of("create_index", "create_another_index").contains(nodeId)) {
CreateIndexRequest request = new CreateIndexRequest(nodeObject.get("index_name").getAsString());
inputData = new WorkflowData() {

@Override
public Map<String, Object> getContent() {
// See CreateIndexRequest ParseFields for source of content keys needed
return Map.of("mappings", request.mappings(), "settings", request.settings(), "aliases", request.aliases());
}

@Override
public Map<String, String> getParams() {
// See RestCreateIndexAction for source of param keys needed
return Map.of("index", request.index());
}

};
inputData = new WorkflowData(
Map.of("mappings", request.mappings(), "settings", request.settings(), "aliases", request.aliases()),
Map.of("index", request.index())
);
}
nodes.add(new ProcessNode(nodeId, workflowStep, inputData));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,53 @@
import java.util.Map;

/**
* Interface representing data provided as input to, and produced as output from, {@link WorkflowStep}s.
* Class encapsulating data provided as input to, and produced as output from, {@link WorkflowStep}s.
*/
public interface WorkflowData {
public class WorkflowData {

/**
* An object representing no data, useful when a workflow step has no required input or output.
*/
WorkflowData EMPTY = new WorkflowData() {
};
public static WorkflowData EMPTY = new WorkflowData();

private final Map<String, Object> content;
private final Map<String, String> params;

private WorkflowData() {
this(Collections.emptyMap(), Collections.emptyMap());
}

/**
* Instantiate this object with content and empty params.
* @param content The content map
*/
public WorkflowData(Map<String, Object> content) {
this(content, Collections.emptyMap());
}

/**
* Instantiate this object with content and params.
* @param content The content map
* @param params The params map
*/
public WorkflowData(Map<String, Object> content, Map<String, String> params) {
this.content = Map.copyOf(content);
this.params = Map.copyOf(params);
}

/**
* Accesses a map containing the content of the workflow step. This represents the data associated with a Rest API request.
* @return the content of this step.
* Returns a map which represents the content associated with a Rest API request or response.
* @return the content of this data.
*/
default Map<String, Object> getContent() {
return Collections.emptyMap();
public Map<String, Object> getContent() {
return this.content;
};

/**
* Accesses a map containing the params of this workflow step. This represents the params associated with a Rest API request, parsed from the URI.
* @return the params of this step.
* Returns a map represents the params associated with a Rest API request, parsed from the URI.
* @return the params of this data.
*/
default Map<String, String> getParams() {
return Collections.emptyMap();
public Map<String, String> getParams() {
return this.params;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import org.opensearch.test.OpenSearchTestCase;

import java.util.Collections;
import java.util.Map;

public class WorkflowDataTests extends OpenSearchTestCase {

Expand All @@ -20,9 +20,19 @@ public void setUp() throws Exception {
}

public void testWorkflowData() {
WorkflowData data = new WorkflowData() {
};
assertEquals(Collections.emptyMap(), data.getParams());
assertEquals(Collections.emptyMap(), data.getContent());

WorkflowData empty = WorkflowData.EMPTY;
assertTrue(empty.getParams().isEmpty());
assertTrue(empty.getContent().isEmpty());

Map<String, Object> expectedContent = Map.of("baz", new String[] { "qux", "quxx" });
WorkflowData contentOnly = new WorkflowData(expectedContent);
assertTrue(contentOnly.getParams().isEmpty());
assertEquals(expectedContent, contentOnly.getContent());

Map<String, String> expectedParams = Map.of("foo", "bar");
WorkflowData contentAndParams = new WorkflowData(expectedContent, expectedParams);
assertEquals(expectedParams, contentAndParams.getParams());
assertEquals(expectedContent, contentAndParams.getContent());
}
}

0 comments on commit 0e56b4a

Please sign in to comment.