|
| 1 | +// Copyright © 2023 Kaleido, Inc. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | + |
| 17 | +package cmd |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/hyperledger/firefly-transaction-manager/internal/apiclient" |
| 24 | + "github.com/hyperledger/firefly-transaction-manager/mocks/apiclientmocks" |
| 25 | + "github.com/stretchr/testify/assert" |
| 26 | + "github.com/stretchr/testify/mock" |
| 27 | +) |
| 28 | + |
| 29 | +func TestEventStreamsDeleteByID(t *testing.T) { |
| 30 | + mc := apiclientmocks.NewFFTMClient(t) |
| 31 | + cmd := buildClientCommand(func() apiclient.FFTMClient { return mc }) |
| 32 | + cmd.SetArgs([]string{"eventstreams", "delete", "--eventstream", "f9506df2-5473-4fd4-9cfb-f835656eaaa7"}) |
| 33 | + mc.On("DeleteEventStream", mock.Anything, "f9506df2-5473-4fd4-9cfb-f835656eaaa7").Return(nil) |
| 34 | + err := cmd.Execute() |
| 35 | + assert.NoError(t, err) |
| 36 | + mc.AssertExpectations(t) |
| 37 | +} |
| 38 | + |
| 39 | +func TestEventStreamsDeleteByName(t *testing.T) { |
| 40 | + mc := apiclientmocks.NewFFTMClient(t) |
| 41 | + cmd := buildClientCommand(func() apiclient.FFTMClient { return mc }) |
| 42 | + cmd.SetArgs([]string{"eventstreams", "delete", "--name", "foo"}) |
| 43 | + mc.On("DeleteEventStreamsByName", mock.Anything, "foo").Return(nil) |
| 44 | + err := cmd.Execute() |
| 45 | + assert.NoError(t, err) |
| 46 | + mc.AssertExpectations(t) |
| 47 | +} |
| 48 | + |
| 49 | +func TestEventStreamsDeleteNoID(t *testing.T) { |
| 50 | + mc := apiclientmocks.NewFFTMClient(t) |
| 51 | + cmd := buildClientCommand(func() apiclient.FFTMClient { return mc }) |
| 52 | + cmd.SetArgs([]string{"eventstreams", "delete"}) |
| 53 | + err := cmd.Execute() |
| 54 | + assert.Regexp(t, "eventstream or name flag must be set", err) |
| 55 | +} |
| 56 | + |
| 57 | +func TestEventStreamsDeleteIDandName(t *testing.T) { |
| 58 | + mc := apiclientmocks.NewFFTMClient(t) |
| 59 | + cmd := buildClientCommand(func() apiclient.FFTMClient { return mc }) |
| 60 | + cmd.SetArgs([]string{"eventstreams", "delete", "--eventstream", "f9506df2-5473-4fd4-9cfb-f835656eaaa7", "--name", "foo"}) |
| 61 | + err := cmd.Execute() |
| 62 | + assert.Regexp(t, "eventstream and name flags cannot be combined", err) |
| 63 | +} |
| 64 | + |
| 65 | +func TestEventStreamsDeleteByNameError(t *testing.T) { |
| 66 | + mc := apiclientmocks.NewFFTMClient(t) |
| 67 | + cmd := buildClientCommand(func() apiclient.FFTMClient { return mc }) |
| 68 | + cmd.SetArgs([]string{"eventstreams", "delete", "--name", "foo"}) |
| 69 | + mc.On("DeleteEventStreamsByName", mock.Anything, "foo").Return(fmt.Errorf("pop")) |
| 70 | + err := cmd.Execute() |
| 71 | + assert.Regexp(t, "pop", err) |
| 72 | + mc.AssertExpectations(t) |
| 73 | +} |
| 74 | + |
| 75 | +func TestEventStreamsDeleteByIDError(t *testing.T) { |
| 76 | + mc := apiclientmocks.NewFFTMClient(t) |
| 77 | + cmd := buildClientCommand(func() apiclient.FFTMClient { return mc }) |
| 78 | + cmd.SetArgs([]string{"eventstreams", "delete", "--eventstream", "f9506df2-5473-4fd4-9cfb-f835656eaaa7"}) |
| 79 | + mc.On("DeleteEventStream", mock.Anything, "f9506df2-5473-4fd4-9cfb-f835656eaaa7").Return(fmt.Errorf("pop")) |
| 80 | + err := cmd.Execute() |
| 81 | + assert.Regexp(t, "pop", err) |
| 82 | + mc.AssertExpectations(t) |
| 83 | +} |
0 commit comments