1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
package module
import (
"fmt"
"testing"
"github.com/onap/multicloud-k8s/src/orchestrator/pkg/infra/db"
"github.com/pkg/errors"
"github.com/stretchr/testify/mock"
)
type mockValues struct {
mock.Mock
}
func (m *mockValues) DBInsert(name string, key db.Key, query interface{}, meta string, c interface{}) error {
fmt.Println("Mocked Insert operation in Mongo")
args := m.Called(name, key, nil, meta, c)
return args.Error(0)
}
func (m *mockValues) DBFind(name string, key db.Key, meta string) ([][]byte, error) {
fmt.Println("Mocked Mongo DB Find Operation")
args := m.Called(name, key, meta)
return args.Get(0).([][]byte), args.Error(1)
}
func (m *mockValues) DBUnmarshal(value []byte, out interface{}) error {
fmt.Println("Mocked Mongo DB Unmarshal Operation")
args := m.Called(value)
return args.Error(0)
}
func (m *mockValues) DBRemove(name string, key db.Key) error {
fmt.Println("Mocked Mongo DB Remove operation")
args := m.Called(name, key)
return args.Error(0)
}
func (m *mockValues) CheckProject(project string) error {
fmt.Println("Mocked Check Project exists")
args := m.Called(project)
return args.Error(0)
}
func (m *mockValues) CheckLogicalCloud(project, logicalCloud string) error {
fmt.Println("Mocked Check Logical Cloud exists")
args := m.Called(project, logicalCloud)
return args.Error(0)
}
func TestCreateLogicalCloud(t *testing.T) {
mData := MetaDataList{
LogicalCloudName: "test_asdf",
}
lc := LogicalCloud{
MetaData: mData,
}
data1 := [][]byte{}
key := LogicalCloudKey{
Project: "test_project",
LogicalCloudName: "test_asdf",
}
myMocks := new(mockValues)
// just to get an error value
err1 := errors.New("math: square root of negative number")
myMocks.On("CheckProject", "test_project").Return(nil)
myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", lc).Return(nil)
myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, err1)
lcClient := LogicalCloudClient{"test_dcm", "test_meta", "test_context", myMocks}
_, err := lcClient.Create("test_project", lc)
if err != nil {
t.Errorf("Some error occured!")
}
}
func TestGetLogicalCloud(t *testing.T) {
key := LogicalCloudKey{
Project: "test_project",
LogicalCloudName: "test_asdf",
}
data1 := [][]byte{
[]byte("abc"),
}
data2 := []byte("abc")
myMocks := new(mockValues)
myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil)
myMocks.On("DBUnmarshal", data2).Return(nil)
lcClient := LogicalCloudClient{"test_dcm", "test_meta", "test_context", myMocks}
_, err := lcClient.Get("test_project", "test_asdf")
if err != nil {
t.Errorf("Some error occured!")
}
}
func TestDeleteLogicalCloud(t *testing.T) {
key := LogicalCloudKey{
Project: "test_project",
LogicalCloudName: "test_asdf",
}
myMocks := new(mockValues)
myMocks.On("DBRemove", "test_dcm", key).Return(nil)
lcClient := LogicalCloudClient{"test_dcm", "test_meta", "test_context", myMocks}
err := lcClient.Delete("test_project", "test_asdf")
if err != nil {
t.Errorf("Some error occured!")
}
}
func TestUpdateLogicalCloud(t *testing.T) {
key := LogicalCloudKey{
Project: "test_project",
LogicalCloudName: "test_asdf",
}
mData := MetaDataList{
LogicalCloudName: "test_asdf",
}
lc := LogicalCloud{
MetaData: mData,
}
data1 := [][]byte{
[]byte("abc"),
}
data2 := []byte("abc")
myMocks := new(mockValues)
myMocks.On("DBInsert", "test_dcm", key, nil, "test_meta", lc).Return(nil)
myMocks.On("DBFind", "test_dcm", key, "test_meta").Return(data1, nil)
myMocks.On("DBUnmarshal", data2).Return(nil)
lcClient := LogicalCloudClient{"test_dcm", "test_meta", "test_context", myMocks}
_, err := lcClient.Update("test_project", "test_asdf", lc)
if err != nil {
t.Errorf("Some error occured!")
}
}
|