|
| 1 | +package persistent |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "github.com/stretchr/testify/assert" |
| 6 | + "github.com/qa-dev/jsonwire-grid/jsonwire" |
| 7 | + "encoding/json" |
| 8 | + "github.com/stretchr/testify/mock" |
| 9 | + "errors" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNodeHelperFactory_create(t *testing.T) { |
| 13 | + nhf := new(nodeHelperFactory) |
| 14 | + nodeHelper := nhf.create(nil) |
| 15 | + assert.NotNil(t, nodeHelper) |
| 16 | +} |
| 17 | + |
| 18 | +func TestNodeHelper_removeAllSessions_Positive_NothingToRemove(t *testing.T) { |
| 19 | + cm := new(jsonwire.ClientMock) |
| 20 | + nodeHelper := &nodeHelper{cm} |
| 21 | + message := new(jsonwire.Sessions) |
| 22 | + cm.On("Sessions").Return(message, nil) |
| 23 | + _, err := nodeHelper.removeAllSessions() |
| 24 | + assert.Nil(t, err) |
| 25 | +} |
| 26 | + |
| 27 | +func TestNodeHelper_removeAllSessions_Positive_SuccessRemove(t *testing.T) { |
| 28 | + cm := new(jsonwire.ClientMock) |
| 29 | + nodeHelper := &nodeHelper{cm} |
| 30 | + sessions := new(jsonwire.Sessions) |
| 31 | + sessions.Value = []struct { |
| 32 | + ID string `json:"id"` |
| 33 | + Capabilities json.RawMessage `json:"capabilities"` |
| 34 | + }{ |
| 35 | + {ID: "lrololo", Capabilities: nil}, |
| 36 | + } |
| 37 | + cm.On("Sessions").Return(sessions, nil) |
| 38 | + message := new(jsonwire.Message) |
| 39 | + cm.On("CloseSession", mock.AnythingOfType("string")).Return(message, nil) |
| 40 | + _, err := nodeHelper.removeAllSessions() |
| 41 | + assert.Nil(t, err) |
| 42 | +} |
| 43 | + |
| 44 | +func TestNodeHelper_removeAllSessions_Negative_Sessions_Error(t *testing.T) { |
| 45 | + cm := new(jsonwire.ClientMock) |
| 46 | + nodeHelper := &nodeHelper{cm} |
| 47 | + cm.On("Sessions").Return(new(jsonwire.Sessions), errors.New("Err")) |
| 48 | + _, err := nodeHelper.removeAllSessions() |
| 49 | + assert.NotNil(t, err) |
| 50 | +} |
| 51 | + |
| 52 | +func TestNodeHelper_removeAllSessions_Negative_Sessions_MessageStatusNotOk(t *testing.T) { |
| 53 | + cm := new(jsonwire.ClientMock) |
| 54 | + nodeHelper := &nodeHelper{cm} |
| 55 | + sessions := new(jsonwire.Sessions) |
| 56 | + sessions.Status = 99999 |
| 57 | + cm.On("Sessions").Return(new(jsonwire.Sessions), errors.New("Err")) |
| 58 | + _, err := nodeHelper.removeAllSessions() |
| 59 | + assert.NotNil(t, err) |
| 60 | +} |
| 61 | + |
| 62 | +func TestNodeHelper_removeAllSessions_Negative_CloseSession_Error(t *testing.T) { |
| 63 | + cm := new(jsonwire.ClientMock) |
| 64 | + nodeHelper := &nodeHelper{cm} |
| 65 | + sessions := new(jsonwire.Sessions) |
| 66 | + sessions.Value = []struct { |
| 67 | + ID string `json:"id"` |
| 68 | + Capabilities json.RawMessage `json:"capabilities"` |
| 69 | + }{ |
| 70 | + {ID: "lrololo", Capabilities: nil}, |
| 71 | + } |
| 72 | + cm.On("Sessions").Return(sessions, nil) |
| 73 | + message := new(jsonwire.Message) |
| 74 | + cm.On("CloseSession", mock.AnythingOfType("string")).Return(message, errors.New("Err")) |
| 75 | + _, err := nodeHelper.removeAllSessions() |
| 76 | + assert.NotNil(t, err) |
| 77 | +} |
| 78 | + |
| 79 | +func TestNodeHelper_removeAllSessions_Negative_CloseSession_MessageStatusNotOk(t *testing.T) { |
| 80 | + cm := new(jsonwire.ClientMock) |
| 81 | + nodeHelper := &nodeHelper{cm} |
| 82 | + sessions := new(jsonwire.Sessions) |
| 83 | + sessions.Value = []struct { |
| 84 | + ID string `json:"id"` |
| 85 | + Capabilities json.RawMessage `json:"capabilities"` |
| 86 | + }{ |
| 87 | + {ID: "lrololo", Capabilities: nil}, |
| 88 | + } |
| 89 | + cm.On("Sessions").Return(sessions, nil) |
| 90 | + message := new(jsonwire.Message) |
| 91 | + message.Status = 999999 |
| 92 | + cm.On("CloseSession", mock.AnythingOfType("string")).Return(message, errors.New("Err")) |
| 93 | + _, err := nodeHelper.removeAllSessions() |
| 94 | + assert.NotNil(t, err) |
| 95 | +} |
| 96 | + |
0 commit comments