-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
228 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
module Floe | ||
class Workflow | ||
class ItemProcessor | ||
attr_reader :processor_config, :payload, :states, :start_at | ||
|
||
def initialize(payload, name = nil) | ||
@payload = payload | ||
@name = name | ||
|
||
raise Floe::InvalidWorkflowError, "Missing field \"States\" for state [#{name}]" if payload["States"].nil? | ||
raise Floe::InvalidWorkflowError, "Missing field \"StartAt\" for state [#{name}]" if payload["StartAt"].nil? | ||
raise Floe::InvalidWorkflowError, "\"StartAt\" not in the \"States\" field for state [#{name}]" unless payload["States"].key?(payload["StartAt"]) | ||
|
||
@processor_config = payload.fetch("ProcessorConfig", "INLINE") | ||
@states = payload["States"].to_a.map { |state_name, state| State.build!(self, state_name, state) } | ||
@states_by_name = @states.to_h { |state| [state.name, state] } | ||
end | ||
|
||
def value(_context, input = {}) | ||
# TODO: Run the states to get the output | ||
input | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
RSpec.describe Floe::Workflow::ItemProcessor do | ||
it "raises an exception for missing States field" do | ||
payload = {"StartAt" => "Missing"} | ||
expect { described_class.new(payload, "Map") } | ||
.to raise_error(Floe::InvalidWorkflowError, "Missing field \"States\" for state [Map]") | ||
end | ||
|
||
it "raises an exception for missing StartAt field" do | ||
payload = {"States" => {}} | ||
expect { described_class.new(payload, "Map") } | ||
.to raise_error(Floe::InvalidWorkflowError, "Missing field \"StartAt\" for state [Map]") | ||
end | ||
|
||
it "raises an exception if StartAt isn't in States" do | ||
payload = {"StartAt" => "First", "States" => {"Second" => {"Type" => "Succeed"}}} | ||
expect { described_class.new(payload, "Map") } | ||
.to raise_error(Floe::InvalidWorkflowError, "\"StartAt\" not in the \"States\" field for state [Map]") | ||
end | ||
|
||
it "raises an exception if a Next state isn't in States" do | ||
payload = {"StartAt" => "First", "States" => {"First" => {"Type" => "Pass", "Next" => "Last"}}} | ||
expect { described_class.new(payload, "Map") } | ||
.to raise_error(Floe::InvalidWorkflowError, "\"Next\" [Last] not in \"States\" for state [First]") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
RSpec.describe Floe::Workflow::States::Map do | ||
let(:input) { {} } | ||
let(:ctx) { Floe::Workflow::Context.new(:input => input) } | ||
let(:state) { workflow.current_state } | ||
let(:input) do | ||
{ | ||
"ship-date" => "2016-03-14T01:59:00Z", | ||
"detail" => { | ||
"delivery-partner" => "UQS", | ||
"shipped" => [ | ||
{"prod" => "R31", "dest-code" => 9511, "quantity" => 1344}, | ||
{"prod" => "S39", "dest-code" => 9511, "quantity" => 40}, | ||
{"prod" => "R31", "dest-code" => 9833, "quantity" => 12}, | ||
{"prod" => "R40", "dest-code" => 9860, "quantity" => 887}, | ||
{"prod" => "R40", "dest-code" => 9511, "quantity" => 1220} | ||
] | ||
} | ||
} | ||
end | ||
let(:workflow) do | ||
payload = { | ||
"Validate-All" => { | ||
"Type" => "Map", | ||
"InputPath" => "$.detail", | ||
"ItemsPath" => "$.shipped", | ||
"MaxConcurrency" => 0, | ||
"ItemProcessor" => { | ||
"StartAt" => "Validate", | ||
"States" => { | ||
"Validate" => { | ||
"Type" => "Pass", | ||
"OutputPath" => "$.Payload", | ||
"End" => true | ||
} | ||
} | ||
}, | ||
"ResultPath" => "$.detail.result", | ||
"End" => true, | ||
} | ||
} | ||
make_workflow(ctx, payload) | ||
end | ||
|
||
describe "#initialize" do | ||
it "raises an InvalidWorkflowError with a missing ItemProcessor" do | ||
payload = { | ||
"Validate-All" => { | ||
"Type" => "Map", | ||
"End" => true | ||
} | ||
} | ||
|
||
expect { make_workflow(ctx, payload) } | ||
.to raise_error(Floe::InvalidWorkflowError, "Missing \"InputProcessor\" field in state [Validate-All]") | ||
end | ||
|
||
it "raises an InvalidWorkflowError with a missing Next and End" do | ||
payload = { | ||
"Validate-All" => { | ||
"Type" => "Map", | ||
"ItemProcessor" => { | ||
"StartAt" => "Validate", | ||
"States" => {"Validate" => {"Type" => "Succeed"}} | ||
} | ||
} | ||
} | ||
|
||
expect { make_workflow(ctx, payload) } | ||
.to raise_error(Floe::InvalidWorkflowError, "Missing \"Next\" field in state [Validate-All]") | ||
end | ||
|
||
it "raises an InvalidWorkflowError if a state in ItemProcessor attempts to transition to a state in the outer workflow" do | ||
payload = { | ||
"StartAt" => "MapState", | ||
"States" => { | ||
"MapState" => { | ||
"Type" => "Map", | ||
"Next" => "PassState", | ||
"ItemProcessor" => { | ||
"StartAt" => "Validate", | ||
"States" => { | ||
"Validate" => { | ||
"Type" => "Pass", | ||
"Next" => "PassState" | ||
} | ||
} | ||
} | ||
}, | ||
"PassState" => { | ||
"Type" => "Pass", | ||
"Next" => "SucceedState" | ||
}, | ||
"SucceedState" => { | ||
"Type" => "Succeed" | ||
} | ||
} | ||
} | ||
|
||
expect { Floe::Workflow.new(payload, ctx) } | ||
.to raise_error(Floe::InvalidWorkflowError, "\"Next\" [PassState] not in \"States\" for state [Validate]") | ||
end | ||
end | ||
|
||
it "#end?" do | ||
expect(state.end?).to be true | ||
end | ||
|
||
describe "#run_nonblock!" do | ||
it "has no next" do | ||
state.run_nonblock! | ||
expect(ctx.next_state).to eq(nil) | ||
end | ||
end | ||
end |