180499dad1d72d23635f18982dccfa9c2be066b6
[ric-plt/sdlgo.git] / internal / cli / remove_test.go
1 /*
2    Copyright (c) 2021 AT&T Intellectual Property.
3    Copyright (c) 2018-2021 Nokia.
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
18 /*
19  * This source code is part of the near-RT RIC (RAN Intelligent Controller)
20  * platform project (RICP).
21  */
22
23 package cli_test
24
25 import (
26         "bytes"
27         "fmt"
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"
31         "testing"
32 )
33
34 var removeMocks *RemoveMocks
35
36 type RemoveMocks struct {
37         sdlIface *mocks.MockSdlApi
38         ns       string
39         keys     []string
40         ret      error
41 }
42
43 func setupRemoveCliMock(ns string, keys []string, ret error) {
44         removeMocks = new(RemoveMocks)
45         removeMocks.ns = ns
46         removeMocks.keys = keys
47         removeMocks.ret = ret
48 }
49
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
54 }
55
56 func runRemoveCli() (string, string, error) {
57         bufStdout := new(bytes.Buffer)
58         bufStderr := new(bytes.Buffer)
59         cmd := cli.NewRemoveCmdForTest(newMockSdlRemoveApi)
60         cmd.SetOut(bufStdout)
61         cmd.SetErr(bufStderr)
62         args := []string{removeMocks.ns}
63         args = append(args, removeMocks.keys...)
64         cmd.SetArgs(args)
65         err := cmd.Execute()
66
67         return bufStdout.String(), bufStderr.String(), err
68 }
69
70 func TestCliRemoveCanShowHelp(t *testing.T) {
71         var expOkErr error
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")
75         tests := []struct {
76                 args      []string
77                 expErr    error
78                 expOutput string
79         }{
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},
84         }
85
86         for _, test := range tests {
87                 buf := new(bytes.Buffer)
88                 cmd := cli.NewRemoveCmdForTest(newMockSdlRemoveApi)
89                 cmd.SetOut(buf)
90                 cmd.SetArgs(test.args)
91
92                 err := cmd.Execute()
93
94                 stdout := buf.String()
95                 assert.Equal(t, test.expErr, err)
96                 assert.Contains(t, stdout, test.expOutput)
97         }
98 }
99
100 func TestCliRemoveCommandWithOneKeySuccess(t *testing.T) {
101         setupRemoveCliMock("some-ns", []string{"some-key"}, nil)
102
103         stdout, stderr, err := runRemoveCli()
104
105         assert.Nil(t, err)
106         assert.Equal(t, "", stdout)
107         assert.Equal(t, "", stderr)
108         removeMocks.sdlIface.AssertExpectations(t)
109 }
110
111 func TestCliRemoveCommandWithMultipleKeysSuccess(t *testing.T) {
112         setupRemoveCliMock("some-ns", []string{"some-key-1", "some-key-1", "some-key-3"}, nil)
113
114         stdout, stderr, err := runRemoveCli()
115
116         assert.Nil(t, err)
117         assert.Equal(t, "", stdout)
118         assert.Equal(t, "", stderr)
119         removeMocks.sdlIface.AssertExpectations(t)
120 }
121
122 func TestCliRemoveCommandFailure(t *testing.T) {
123         expErr := fmt.Errorf("some-error")
124         setupRemoveCliMock("some-ns", []string{"some-key"}, expErr)
125
126         _, stderr, err := runRemoveCli()
127
128         assert.Equal(t, expErr, err)
129         assert.Contains(t, stderr, expErr.Error())
130         removeMocks.sdlIface.AssertExpectations(t)
131 }