2 Copyright (c) 2021 AT&T Intellectual Property.
3 Copyright (c) 2018-2021 Nokia.
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 * platform project (RICP).
28 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/cli"
29 "gerrit.o-ran-sc.org/r/ric-plt/sdlgo/internal/mocks"
30 "github.com/stretchr/testify/assert"
34 var removeMocks *RemoveMocks
36 type RemoveMocks struct {
37 sdlIface *mocks.MockSdlApi
43 func setupRemoveCliMock(ns string, keys []string, ret error) {
44 removeMocks = new(RemoveMocks)
46 removeMocks.keys = keys
50 func newMockSdlRemoveApi() cli.ISyncStorage {
51 removeMocks.sdlIface = new(mocks.MockSdlApi)
52 removeMocks.sdlIface.On("Remove", removeMocks.ns, removeMocks.keys).Return(removeMocks.ret)
53 return removeMocks.sdlIface
56 func runRemoveCli() (string, string, error) {
57 bufStdout := new(bytes.Buffer)
58 bufStderr := new(bytes.Buffer)
59 cmd := cli.NewRemoveCmdForTest(newMockSdlRemoveApi)
62 args := []string{removeMocks.ns}
63 args = append(args, removeMocks.keys...)
67 return bufStdout.String(), bufStderr.String(), err
70 func TestCliRemoveCanShowHelp(t *testing.T) {
72 expHelp := "Usage:\n " + "remove <namespace> <key> [<key2>... <keyN>] [flags]"
73 expFlagErr := fmt.Errorf("unknown flag: --some-unknown-flag")
74 expArgCntLtErr := fmt.Errorf("requires at least 2 arg(s), only received 1")
80 {args: []string{"-h"}, expErr: expOkErr, expOutput: expHelp},
81 {args: []string{"--help"}, expErr: expOkErr, expOutput: expHelp},
82 {args: []string{"--some-unknown-flag"}, expErr: expFlagErr, expOutput: expHelp},
83 {args: []string{"some-ns"}, expErr: expArgCntLtErr, expOutput: expHelp},
86 for _, test := range tests {
87 buf := new(bytes.Buffer)
88 cmd := cli.NewRemoveCmdForTest(newMockSdlRemoveApi)
90 cmd.SetArgs(test.args)
94 stdout := buf.String()
95 assert.Equal(t, test.expErr, err)
96 assert.Contains(t, stdout, test.expOutput)
100 func TestCliRemoveCommandWithOneKeySuccess(t *testing.T) {
101 setupRemoveCliMock("some-ns", []string{"some-key"}, nil)
103 stdout, stderr, err := runRemoveCli()
106 assert.Equal(t, "", stdout)
107 assert.Equal(t, "", stderr)
108 removeMocks.sdlIface.AssertExpectations(t)
111 func TestCliRemoveCommandWithMultipleKeysSuccess(t *testing.T) {
112 setupRemoveCliMock("some-ns", []string{"some-key-1", "some-key-1", "some-key-3"}, nil)
114 stdout, stderr, err := runRemoveCli()
117 assert.Equal(t, "", stdout)
118 assert.Equal(t, "", stderr)
119 removeMocks.sdlIface.AssertExpectations(t)
122 func TestCliRemoveCommandFailure(t *testing.T) {
123 expErr := fmt.Errorf("some-error")
124 setupRemoveCliMock("some-ns", []string{"some-key"}, expErr)
126 _, stderr, err := runRemoveCli()
128 assert.Equal(t, expErr, err)
129 assert.Contains(t, stderr, expErr.Error())
130 removeMocks.sdlIface.AssertExpectations(t)