diff --git a/iwftest/README.md b/iwftest/README.md index 326ac83..0ff7921 100644 --- a/iwftest/README.md +++ b/iwftest/README.md @@ -9,62 +9,5 @@ The APIs are generated by the below commands: ## Usage -See the [samples](https://github.com/indeedeng/iwf-golang-samples) for more details: +See the [example](./example) for more details. -```go -package subscription - -import ( - "github.com/golang/mock/gomock" - "github.com/indeedeng/iwf-golang-sdk/iwf" - "github.com/indeedeng/iwf-golang-sdk/iwftest" - "github.com/stretchr/testify/assert" - "testing" - "time" -) - -var mockWfCtx *iwftest.MockWorkflowContext -var mockPersistence *iwftest.MockPersistence -var mockCommunication *iwftest.MockCommunication -var emptyCmdResults = iwf.CommandResults{} -var emptyObj = iwftest.NewTestObject(nil) -var mockSvc *MockMyService - -func beforeEach(t *testing.T) { - ctrl := gomock.NewController(t) - - mockSvc = NewMockMyService(ctrl) - mockWfCtx = iwftest.NewMockWorkflowContext(ctrl) - mockPersistence = iwftest.NewMockPersistence(ctrl) - mockCommunication = iwftest.NewMockCommunication(ctrl) -} - - -func TestInitState_WaitUntil(t *testing.T) { - beforeEach(t) - - state := NewInitState() - - mockPersistence.EXPECT().SetDataObject(keyCustomer, testCustomer) - cmdReq, err := state.WaitUntil(mockWfCtx, testCustomerObj, mockPersistence, mockCommunication) - assert.Nil(t, err) - assert.Equal(t, iwf.EmptyCommandRequest(), cmdReq) -} - - -func TestTrialState_WaitUntil(t *testing.T) { - beforeEach(t) - - state := NewTrialState(mockSvc) - - mockSvc.EXPECT().sendEmail(testCustomer.Email, gomock.Any(), gomock.Any()) - mockPersistence.EXPECT().GetDataObject(keyCustomer, gomock.Any()).SetArg(1, testCustomer) - cmdReq, err := state.WaitUntil(mockWfCtx, emptyObj, mockPersistence, mockCommunication) - assert.Nil(t, err) - firingTime := cmdReq.Commands[0].TimerCommand.FiringUnixTimestampSeconds - assert.Equal(t, iwf.AllCommandsCompletedRequest( - iwf.NewTimerCommand("", time.Unix(firingTime, 0)), - ), cmdReq) -} - -``` diff --git a/iwftest/communication.go b/iwftest/communication.go index 14a9ebc..0588ef0 100644 --- a/iwftest/communication.go +++ b/iwftest/communication.go @@ -9,6 +9,7 @@ import ( gomock "github.com/golang/mock/gomock" iwfidl "github.com/indeedeng/iwf-golang-sdk/gen/iwfidl" + iwf "github.com/indeedeng/iwf-golang-sdk/iwf" ) // MockCommunication is a mock of Communication interface. @@ -48,6 +49,20 @@ func (mr *MockCommunicationMockRecorder) GetToPublishInternalChannel() *gomock.C return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetToPublishInternalChannel", reflect.TypeOf((*MockCommunication)(nil).GetToPublishInternalChannel)) } +// GetToTriggerStateMovements mocks base method. +func (m *MockCommunication) GetToTriggerStateMovements() []iwf.StateMovement { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetToTriggerStateMovements") + ret0, _ := ret[0].([]iwf.StateMovement) + return ret0 +} + +// GetToTriggerStateMovements indicates an expected call of GetToTriggerStateMovements. +func (mr *MockCommunicationMockRecorder) GetToTriggerStateMovements() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetToTriggerStateMovements", reflect.TypeOf((*MockCommunication)(nil).GetToTriggerStateMovements)) +} + // PublishInternalChannel mocks base method. func (m *MockCommunication) PublishInternalChannel(channelName string, value interface{}) { m.ctrl.T.Helper() @@ -60,6 +75,22 @@ func (mr *MockCommunicationMockRecorder) PublishInternalChannel(channelName, val return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PublishInternalChannel", reflect.TypeOf((*MockCommunication)(nil).PublishInternalChannel), channelName, value) } +// TriggerStateMovements mocks base method. +func (m *MockCommunication) TriggerStateMovements(movements ...iwf.StateMovement) { + m.ctrl.T.Helper() + varargs := []interface{}{} + for _, a := range movements { + varargs = append(varargs, a) + } + m.ctrl.Call(m, "TriggerStateMovements", varargs...) +} + +// TriggerStateMovements indicates an expected call of TriggerStateMovements. +func (mr *MockCommunicationMockRecorder) TriggerStateMovements(movements ...interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TriggerStateMovements", reflect.TypeOf((*MockCommunication)(nil).TriggerStateMovements), movements...) +} + // MockcommunicationInternal is a mock of communicationInternal interface. type MockcommunicationInternal struct { ctrl *gomock.Controller @@ -96,3 +127,17 @@ func (mr *MockcommunicationInternalMockRecorder) GetToPublishInternalChannel() * mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetToPublishInternalChannel", reflect.TypeOf((*MockcommunicationInternal)(nil).GetToPublishInternalChannel)) } + +// GetToTriggerStateMovements mocks base method. +func (m *MockcommunicationInternal) GetToTriggerStateMovements() []iwf.StateMovement { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetToTriggerStateMovements") + ret0, _ := ret[0].([]iwf.StateMovement) + return ret0 +} + +// GetToTriggerStateMovements indicates an expected call of GetToTriggerStateMovements. +func (mr *MockcommunicationInternalMockRecorder) GetToTriggerStateMovements() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetToTriggerStateMovements", reflect.TypeOf((*MockcommunicationInternal)(nil).GetToTriggerStateMovements)) +} diff --git a/iwftest/example/example.go b/iwftest/example/example.go new file mode 100644 index 0000000..35c8432 --- /dev/null +++ b/iwftest/example/example.go @@ -0,0 +1,24 @@ +package example + +import "github.com/indeedeng/iwf-golang-sdk/iwf" + +func NewInitState() iwf.WorkflowState { + return initState{} +} + +type initState struct { + iwf.WorkflowStateDefaults +} + +const keyCustomer = "customer" + +func (b initState) WaitUntil(ctx iwf.WorkflowContext, input iwf.Object, persistence iwf.Persistence, communication iwf.Communication) (*iwf.CommandRequest, error) { + var customer string + input.Get(&customer) + persistence.SetDataAttribute(keyCustomer, customer) + return iwf.EmptyCommandRequest(), nil +} + +func (b initState) Execute(ctx iwf.WorkflowContext, input iwf.Object, commandResults iwf.CommandResults, persistence iwf.Persistence, communication iwf.Communication) (*iwf.StateDecision, error) { + return iwf.GracefulCompletingWorkflow, nil +} diff --git a/iwftest/example/example_test.go b/iwftest/example/example_test.go new file mode 100644 index 0000000..0aca24b --- /dev/null +++ b/iwftest/example/example_test.go @@ -0,0 +1,46 @@ +package example + +import ( + "github.com/golang/mock/gomock" + "github.com/indeedeng/iwf-golang-sdk/iwf" + "github.com/indeedeng/iwf-golang-sdk/iwftest" + "github.com/stretchr/testify/assert" + "testing" +) + +var mockWfCtx *iwftest.MockWorkflowContext +var mockPersistence *iwftest.MockPersistence +var mockCommunication *iwftest.MockCommunication +var emptyCmdResults = iwf.CommandResults{} +var testCustomer = "customer1" +var emptyObj = iwftest.NewTestObject(testCustomer) + +func beforeEach(t *testing.T) { + ctrl := gomock.NewController(t) + + mockWfCtx = iwftest.NewMockWorkflowContext(ctrl) + mockPersistence = iwftest.NewMockPersistence(ctrl) + mockCommunication = iwftest.NewMockCommunication(ctrl) +} + +func TestInitState_WaitUntil(t *testing.T) { + beforeEach(t) + + state := NewInitState() + + mockPersistence.EXPECT().SetDataAttribute(keyCustomer, testCustomer) + cmdReq, err := state.WaitUntil(mockWfCtx, emptyObj, mockPersistence, mockCommunication) + assert.Nil(t, err) + assert.Equal(t, iwf.EmptyCommandRequest(), cmdReq) +} + +func TestInitState_Execute(t *testing.T) { + beforeEach(t) + + state := NewInitState() + input := iwftest.NewTestObject(testCustomer) + + decision, err := state.Execute(mockWfCtx, input, emptyCmdResults, mockPersistence, mockCommunication) + assert.Nil(t, err) + assert.Equal(t, iwf.GracefulCompletingWorkflow, decision) +}