blob: 9a0dad1749f540f0207fe616f1e9254c547483e5 [file] [log] [blame]
Scott Bakerf53bf152019-05-29 17:50:37 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
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 */
17package commands
18
19import (
20 "context"
Scott Baker20481aa2019-06-20 11:00:54 -070021 corderrors "github.com/opencord/cordctl/error"
Scott Bakerf53bf152019-05-29 17:50:37 -070022 "github.com/stretchr/testify/assert"
23 "testing"
24)
25
26func TestDecodeOperator(t *testing.T) {
27 value, operator, invert, err := DecodeOperator("=something")
28 assert.Equal(t, value, "something")
29 assert.Equal(t, operator, "EQUAL")
30 assert.Equal(t, invert, false)
31 assert.Equal(t, err, nil)
32
33 value, operator, invert, err = DecodeOperator("!=something")
34 assert.Equal(t, value, "something")
35 assert.Equal(t, operator, "EQUAL")
36 assert.Equal(t, invert, true)
37 assert.Equal(t, err, nil)
38
39 value, operator, invert, err = DecodeOperator(">3")
40 assert.Equal(t, value, "3")
41 assert.Equal(t, operator, "GREATER_THAN")
42 assert.Equal(t, invert, false)
43 assert.Equal(t, err, nil)
44
45 value, operator, invert, err = DecodeOperator(">=3")
46 assert.Equal(t, value, "3")
47 assert.Equal(t, operator, "GREATER_THAN_OR_EQUAL")
48 assert.Equal(t, invert, false)
49 assert.Equal(t, err, nil)
50
51 value, operator, invert, err = DecodeOperator("<3")
52 assert.Equal(t, value, "3")
53 assert.Equal(t, operator, "LESS_THAN")
54 assert.Equal(t, invert, false)
55 assert.Equal(t, err, nil)
56
57 value, operator, invert, err = DecodeOperator("<=3")
58 assert.Equal(t, value, "3")
59 assert.Equal(t, operator, "LESS_THAN_OR_EQUAL")
60 assert.Equal(t, invert, false)
61 assert.Equal(t, err, nil)
62}
63
Scott Baker20481aa2019-06-20 11:00:54 -070064func TestCommaSeparatedQueryToMap(t *testing.T) {
Scott Bakerf53bf152019-05-29 17:50:37 -070065 m, err := CommaSeparatedQueryToMap("foo=7,bar!=stuff, x = 5, y= 27", true)
66 assert.Equal(t, err, nil)
67 assert.Equal(t, m["foo"], "=7")
68 assert.Equal(t, m["bar"], "!=stuff")
69 assert.Equal(t, m["x"], "= 5")
70 assert.Equal(t, m["y"], "= 27")
71}
72
Scott Baker20481aa2019-06-20 11:00:54 -070073func TestCommaSeparatedQueryToMapIllegal(t *testing.T) {
74 // Query string missing operator
75 _, err := CommaSeparatedQueryToMap("foo", true)
76
77 _, matched := err.(*corderrors.IllegalQueryError)
78 assert.True(t, matched)
79
80 // Query string is contains an empty element
81 _, err = CommaSeparatedQueryToMap(",foo=bar", true)
82
83 _, matched = err.(*corderrors.IllegalQueryError)
84 assert.True(t, matched)
85}
86
87func TestCommaSeparatedQueryToMapEmpty(t *testing.T) {
88 // Query string missing operator
89 m, err := CommaSeparatedQueryToMap("", true)
90
91 assert.Equal(t, err, nil)
92 assert.Equal(t, len(m), 0)
93}
94
Scott Bakerf53bf152019-05-29 17:50:37 -070095func TestTypeConvert(t *testing.T) {
Scott Baker867aa302019-06-19 13:18:45 -070096 conn, descriptor, err := InitClient(INIT_DEFAULT)
Scott Bakerf53bf152019-05-29 17:50:37 -070097 assert.Equal(t, err, nil)
98 defer conn.Close()
99
100 v, err := TypeConvert(descriptor, "Site", "id", "7")
101 assert.Equal(t, err, nil)
102 assert.Equal(t, v, int32(7))
103
104 v, err = TypeConvert(descriptor, "Site", "name", "foo")
105 assert.Equal(t, err, nil)
106 assert.Equal(t, v, "foo")
107
108 v, err = TypeConvert(descriptor, "Site", "enacted", "123.4")
109 assert.Equal(t, err, nil)
110 assert.Equal(t, v, 123.4)
111}
112
113func TestCheckModelName(t *testing.T) {
Scott Baker867aa302019-06-19 13:18:45 -0700114 conn, descriptor, err := InitClient(INIT_DEFAULT)
Scott Bakerf53bf152019-05-29 17:50:37 -0700115 assert.Equal(t, err, nil)
116 defer conn.Close()
117
118 err = CheckModelName(descriptor, "Slice")
119 assert.Equal(t, err, nil)
120
121 err = CheckModelName(descriptor, "DoesNotExist")
Scott Baker20481aa2019-06-20 11:00:54 -0700122 _, matched := err.(*corderrors.UnknownModelTypeError)
123 assert.True(t, matched)
Scott Bakerf53bf152019-05-29 17:50:37 -0700124}
125
126func TestCreateModel(t *testing.T) {
Scott Baker867aa302019-06-19 13:18:45 -0700127 conn, descriptor, err := InitClient(INIT_DEFAULT)
Scott Bakerf53bf152019-05-29 17:50:37 -0700128 assert.Equal(t, err, nil)
129 defer conn.Close()
130
131 m := make(map[string]interface{})
132 m["name"] = "mockslice3"
133 m["site_id"] = int32(1)
134
135 err = CreateModel(conn, descriptor, "Slice", m)
136 assert.Equal(t, err, nil)
137
138 assert.Equal(t, m["id"], int32(3))
139}
140
141func TestUpdateModel(t *testing.T) {
Scott Baker867aa302019-06-19 13:18:45 -0700142 conn, descriptor, err := InitClient(INIT_DEFAULT)
Scott Bakerf53bf152019-05-29 17:50:37 -0700143 assert.Equal(t, err, nil)
144 defer conn.Close()
145
146 m := make(map[string]interface{})
147 m["id"] = int32(1)
148 m["name"] = "mockslice1_newname"
149
150 err = UpdateModel(conn, descriptor, "Slice", m)
151 assert.Equal(t, err, nil)
152}
153
154func TestGetModel(t *testing.T) {
Scott Baker867aa302019-06-19 13:18:45 -0700155 conn, descriptor, err := InitClient(INIT_DEFAULT)
Scott Bakerf53bf152019-05-29 17:50:37 -0700156 assert.Equal(t, err, nil)
157 defer conn.Close()
158
159 m, err := GetModel(context.Background(), conn, descriptor, "Slice", int32(1))
160 assert.Equal(t, err, nil)
161
162 assert.Equal(t, m.GetFieldByName("id").(int32), int32(1))
163 assert.Equal(t, m.GetFieldByName("name").(string), "mockslice1")
164}
165
Scott Baker20481aa2019-06-20 11:00:54 -0700166func TestGetModelNoExist(t *testing.T) {
167 conn, descriptor, err := InitClient(INIT_DEFAULT)
168 assert.Equal(t, err, nil)
169 defer conn.Close()
170
171 _, err = GetModel(context.Background(), conn, descriptor, "Slice", int32(77))
172 assert.NotEqual(t, err, nil)
173
174 _, matched := err.(*corderrors.ModelNotFoundError)
175 assert.True(t, matched)
176}
177
Scott Bakerf53bf152019-05-29 17:50:37 -0700178func TestListModels(t *testing.T) {
Scott Baker867aa302019-06-19 13:18:45 -0700179 conn, descriptor, err := InitClient(INIT_DEFAULT)
Scott Bakerf53bf152019-05-29 17:50:37 -0700180 assert.Equal(t, err, nil)
181 defer conn.Close()
182
183 m, err := ListModels(context.Background(), conn, descriptor, "Slice")
184 assert.Equal(t, err, nil)
185
186 assert.Equal(t, len(m), 2)
187 assert.Equal(t, m[0].GetFieldByName("id").(int32), int32(1))
188 assert.Equal(t, m[0].GetFieldByName("name").(string), "mockslice1")
189 assert.Equal(t, m[1].GetFieldByName("id").(int32), int32(2))
190 assert.Equal(t, m[1].GetFieldByName("name").(string), "mockslice2")
191}
192
193func TestFilterModels(t *testing.T) {
Scott Baker867aa302019-06-19 13:18:45 -0700194 conn, descriptor, err := InitClient(INIT_DEFAULT)
Scott Bakerf53bf152019-05-29 17:50:37 -0700195 assert.Equal(t, err, nil)
196 defer conn.Close()
197
198 qm := map[string]string{"id": "=1"}
199
Scott Baker1dd06672019-06-14 15:40:56 -0700200 m, err := FilterModels(context.Background(), conn, descriptor, "Slice", FILTER_DEFAULT, qm)
Scott Bakerf53bf152019-05-29 17:50:37 -0700201 assert.Equal(t, err, nil)
202
203 assert.Equal(t, len(m), 1)
204 assert.Equal(t, m[0].GetFieldByName("id").(int32), int32(1))
205 assert.Equal(t, m[0].GetFieldByName("name").(string), "mockslice1")
206}
207
Scott Baker20481aa2019-06-20 11:00:54 -0700208func TestFindModel(t *testing.T) {
209 conn, descriptor, err := InitClient(INIT_DEFAULT)
210 assert.Equal(t, err, nil)
211 defer conn.Close()
212
213 qm := map[string]string{"id": "=1"}
214
215 m, err := FindModel(context.Background(), conn, descriptor, "Slice", qm)
216 assert.Equal(t, err, nil)
217
218 assert.Equal(t, m.GetFieldByName("id").(int32), int32(1))
219 assert.Equal(t, m.GetFieldByName("name").(string), "mockslice1")
220}
221
222func TestFindModelNoExist(t *testing.T) {
223 conn, descriptor, err := InitClient(INIT_DEFAULT)
224 assert.Equal(t, err, nil)
225 defer conn.Close()
226
227 qm := map[string]string{"id": "=77"}
228
229 _, err = FindModel(context.Background(), conn, descriptor, "Slice", qm)
230 assert.NotEqual(t, err, nil)
231
232 _, matched := err.(*corderrors.ModelNotFoundError)
233 assert.True(t, matched)
234}
235
Scott Bakerf53bf152019-05-29 17:50:37 -0700236func TestDeleteModel(t *testing.T) {
Scott Baker867aa302019-06-19 13:18:45 -0700237 conn, descriptor, err := InitClient(INIT_DEFAULT)
Scott Bakerf53bf152019-05-29 17:50:37 -0700238 assert.Equal(t, err, nil)
239 defer conn.Close()
240
241 err = DeleteModel(conn, descriptor, "Slice", int32(1))
242 assert.Equal(t, err, nil)
243}