Merge "RIC:1060: Change in PTL"
[ric-plt/sdlgo.git] / syncstorage_test.go
1 /*
2    Copyright (c) 2021 Nokia.
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15
16    This source code is part of the near-RT RIC (RAN Intelligent Controller)
17    platform project (RICP).
18 */
19 package sdlgo_test
20
21 import (
22         "errors"
23         "testing"
24
25         "gerrit.o-ran-sc.org/r/ric-plt/sdlgo"
26         "github.com/stretchr/testify/assert"
27 )
28
29 func setupSDL() (*mockDB, *sdlgo.SyncStorage) {
30         dbMock := new(mockDB)
31         sdl := sdlgo.NewSyncStorageForTest(dbMock)
32         return dbMock, sdl
33 }
34
35 func TestListKeys(t *testing.T) {
36         dbMock, sdl := setupSDL()
37
38         tests := []struct {
39                 keysPatternMockDB     string
40                 keysReturnValueMockDB []string
41                 keysPattern           string
42                 expected              []string
43         }{
44                 {"{ns1},*", []string{"{ns1},key1", "{ns1},key2"}, "*", []string{"key1", "key2"}},
45                 {"{ns1},ke*", []string{"{ns1},key1", "{ns1},key2"}, "ke*", []string{"key1", "key2"}},
46                 {"{ns1},ke?2", []string{"{ns1},key2"}, "ke?2", []string{"key2"}},
47         }
48
49         for _, test := range tests {
50                 dbMock.On("Keys", test.keysPatternMockDB).Return(test.keysReturnValueMockDB, nil)
51
52                 keys, err := sdl.ListKeys("ns1", test.keysPattern)
53                 assert.Nil(t, err)
54                 assert.Equal(t, test.expected, keys)
55                 dbMock.AssertExpectations(t)
56         }
57 }
58
59 func TestListKeysEmpty(t *testing.T) {
60         dbMock, sdl := setupSDL()
61
62         dbMock.On("Keys", "{ns1},").Return([]string{}, nil)
63
64         keys, err := sdl.ListKeys("ns1", "")
65         assert.Nil(t, err)
66         assert.Nil(t, keys)
67         dbMock.AssertExpectations(t)
68 }
69
70 func TestListKeysError(t *testing.T) {
71         dbMock, sdl := setupSDL()
72
73         errorStringMockDB := string("(empty list or set)")
74         dbMock.On("Keys", "{ns1},").Return([]string{}, errors.New(errorStringMockDB))
75
76         keys, err := sdl.ListKeys("ns1", "")
77         assert.NotNil(t, err)
78         assert.EqualError(t, err, errorStringMockDB)
79         assert.Nil(t, keys)
80         dbMock.AssertExpectations(t)
81 }