bde5c24b10637347d8d216ea6800ebb082c65a4c
[ric-plt/sdlgo.git] / internal / cli / keys_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         "errors"
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 mKeys *keysMock
35
36 type keysMock struct {
37         sdlIface *mocks.MockSdlApi
38         ns       string
39         pattern  string
40         keys     []string
41         err      error
42 }
43
44 func setupKeysCliMock(ns, pattern string, keys []string, err error) {
45         mKeys = new(keysMock)
46         mKeys.ns = ns
47         mKeys.pattern = pattern
48         mKeys.keys = keys
49         mKeys.err = err
50 }
51
52 func newMockSdlApi() cli.ISyncStorage {
53         mKeys.sdlIface = new(mocks.MockSdlApi)
54         mKeys.sdlIface.On("ListKeys", mKeys.ns, mKeys.pattern).Return(mKeys.keys, mKeys.err)
55         return mKeys.sdlIface
56 }
57
58 func TestKeysCmdShowHelp(t *testing.T) {
59         var expOkErr error
60         expNokErrZeroArgs := errors.New("accepts between 1 and 2 arg(s), received 0")
61         expNokErrThreeArgs := errors.New("accepts between 1 and 2 arg(s), received 3")
62         expHelp := "Usage:\n  keys <namespace> [pattern|default '*'] [flags]"
63         tests := []struct {
64                 args   []string
65                 expOut string
66                 expErr error
67         }{
68                 {args: []string{"-h"}, expErr: expOkErr, expOut: expHelp},
69                 {args: []string{"--help"}, expErr: expOkErr, expOut: expHelp},
70                 {args: []string{}, expErr: expNokErrZeroArgs, expOut: expHelp},
71                 {args: []string{"ns", "key", "extra-arg"}, expErr: expNokErrThreeArgs, expOut: expHelp},
72         }
73
74         for _, test := range tests {
75                 buf := new(bytes.Buffer)
76                 cmd := cli.NewKeysCmdForTest(newMockSdlApi)
77                 cmd.SetOut(buf)
78                 cmd.SetErr(buf)
79                 cmd.SetArgs(test.args)
80                 err := cmd.Execute()
81                 result := buf.String()
82
83                 assert.Equal(t, test.expErr, err)
84                 assert.Contains(t, result, test.expOut)
85         }
86 }
87
88 func TestKeysCmdSuccess(t *testing.T) {
89         var expOkErr error
90         argsNsPattern := []string{"testns", "*"}
91         argsNs := []string{"testns"}
92         tests := []struct {
93                 args        []string
94                 outListKeys []string
95                 expOut      string
96                 expErr      error
97         }{
98                 {args: argsNs, outListKeys: []string{"key13"}, expOut: "key13", expErr: expOkErr},
99                 {args: argsNsPattern, outListKeys: []string{"key1"}, expOut: "key1", expErr: expOkErr},
100                 {args: argsNsPattern, outListKeys: []string{"key1", "key2"}, expOut: "key1\nkey2", expErr: expOkErr},
101                 {args: argsNsPattern, outListKeys: []string{"key2", "key3", "key1"}, expOut: "key1\nkey2\nkey3", expErr: expOkErr},
102                 {args: argsNsPattern, outListKeys: []string{}, expOut: "", expErr: expOkErr},
103         }
104
105         for _, test := range tests {
106                 buf := new(bytes.Buffer)
107                 if len(test.args) > 1 {
108                         setupKeysCliMock(test.args[0], test.args[1], test.outListKeys, test.expErr)
109                 } else {
110                         setupKeysCliMock(test.args[0], "*", test.outListKeys, test.expErr)
111                 }
112                 cmd := cli.NewKeysCmdForTest(newMockSdlApi)
113                 cmd.SetOut(buf)
114                 cmd.SetErr(buf)
115                 cmd.SetArgs(test.args)
116                 err := cmd.Execute()
117                 result := buf.String()
118
119                 assert.Equal(t, test.expErr, err)
120                 assert.Contains(t, result, test.expOut)
121         }
122 }
123
124 func TestKeysCmdFails(t *testing.T) {
125         argsNs := []string{"testns"}
126         expNokErr := errors.New("Boom!")
127         expNokOut := "Usage:\n  keys <namespace> [pattern|default '*'] [flags]"
128
129         buf := new(bytes.Buffer)
130         setupKeysCliMock("testns", "*", []string{"very wrong"}, expNokErr)
131         cmd := cli.NewKeysCmdForTest(newMockSdlApi)
132         cmd.SetOut(buf)
133         cmd.SetErr(buf)
134         cmd.SetArgs(argsNs)
135         err := cmd.Execute()
136         result := buf.String()
137
138         assert.Equal(t, expNokErr, err)
139         assert.Contains(t, result, expNokOut)
140 }