go.uber.org/cadence@v1.2.9/mocks/mock_test.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package mocks 22 23 import ( 24 "context" 25 "testing" 26 27 "go.uber.org/cadence/.gen/go/shared" 28 29 "github.com/stretchr/testify/mock" 30 "github.com/stretchr/testify/require" 31 32 "go.uber.org/cadence/client" 33 "go.uber.org/cadence/workflow" 34 ) 35 36 func Test_MockClient(t *testing.T) { 37 testWorkflowID := "test-workflowid" 38 testRunID := "test-runid" 39 testWorkflowName := "workflow" 40 testWorkflowInput := "input" 41 42 mockClient := &Client{} 43 44 mockClient.On("StartWorkflow", mock.Anything, mock.Anything, mock.Anything, mock.Anything). 45 Return(&workflow.Execution{ID: testWorkflowID, RunID: testRunID}, nil).Once() 46 we, err := mockClient.StartWorkflow(context.Background(), client.StartWorkflowOptions{}, testWorkflowName, testWorkflowInput) 47 mockClient.AssertExpectations(t) 48 require.NoError(t, err) 49 require.Equal(t, testWorkflowID, we.ID) 50 require.Equal(t, testRunID, we.RunID) 51 52 mockClient.On("SignalWithStartWorkflow", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). 53 Return(&workflow.Execution{ID: testWorkflowID, RunID: testRunID}, nil).Once() 54 we, err = mockClient.SignalWithStartWorkflow(context.Background(), "wid", "signal", "val", client.StartWorkflowOptions{}, testWorkflowName, testWorkflowInput) 55 mockClient.AssertExpectations(t) 56 require.NoError(t, err) 57 require.Equal(t, testWorkflowID, we.ID) 58 require.Equal(t, testRunID, we.RunID) 59 60 mockWfRun := &WorkflowRun{} 61 mockClient.On("ExecuteWorkflow", mock.Anything, mock.Anything, mock.Anything, mock.Anything). 62 Return(mockWfRun, nil).Once() 63 wfRun, err := mockClient.ExecuteWorkflow(context.Background(), client.StartWorkflowOptions{}, testWorkflowName, testWorkflowInput) 64 mockClient.AssertExpectations(t) 65 require.NoError(t, err) 66 require.Equal(t, testWorkflowID, we.ID) 67 require.Equal(t, testRunID, we.RunID) 68 69 mockWfRun.On("GetID").Return(testWorkflowID).Once() 70 mockWfRun.On("GetRunID").Return(testRunID).Once() 71 mockWfRun.On("Get", mock.Anything, mock.Anything).Return(nil).Once() 72 require.Equal(t, testWorkflowID, wfRun.GetID()) 73 require.Equal(t, testRunID, wfRun.GetRunID()) 74 require.NoError(t, wfRun.Get(context.Background(), &testWorkflowID)) 75 76 mockWfRun.On("GetID").Return(testWorkflowID).Once() 77 mockWfRun.On("GetRunID").Return(testRunID).Once() 78 mockWfRun.On("Get", mock.Anything, mock.Anything).Return(nil).Once() 79 mockClient.On("GetWorkflow", mock.Anything, mock.Anything, mock.Anything). 80 Return(mockWfRun).Once() 81 wfRun = mockClient.GetWorkflow(context.Background(), testWorkflowID, testRunID) 82 mockClient.AssertExpectations(t) 83 require.Equal(t, testWorkflowID, wfRun.GetID()) 84 require.Equal(t, testRunID, wfRun.GetRunID()) 85 require.NoError(t, wfRun.Get(context.Background(), &testWorkflowID)) 86 87 mockHistoryIter := &HistoryEventIterator{} 88 mockHistoryIter.On("HasNext").Return(true).Once() 89 mockHistoryIter.On("Next").Return(&shared.HistoryEvent{}, nil).Once() 90 mockClient.On("GetWorkflowHistory", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything). 91 Return(mockHistoryIter).Once() 92 historyIter := mockClient.GetWorkflowHistory(context.Background(), testWorkflowID, testRunID, true, shared.HistoryEventFilterTypeCloseEvent) 93 mockClient.AssertExpectations(t) 94 require.NotNil(t, historyIter) 95 require.Equal(t, true, historyIter.HasNext()) 96 next, err := historyIter.Next() 97 require.NotNil(t, next) 98 require.NoError(t, err) 99 }