Debian Codegraph @

pkg/apis/dpf/v1/common_configs/cc_sec_notified_server_test.go

raw
1 package common_configs_test
2
3 import (
4 "context"
5 "net"
6 "net/http"
7
8 . "github.com/onsi/ginkgo"
9 . "github.com/onsi/gomega"
10
11 "github.com/jarcoal/httpmock"
12 "github.com/mimuret/golang-iij-dpf/pkg/apis/dpf/v1/common_configs"
13 "github.com/mimuret/golang-iij-dpf/pkg/testtool"
14 )
15
16 var _ = Describe("cc_sec_notified_servers", func() {
17 var (
18 c common_configs.CcSecNotifiedServer
19 cl *testtool.TestClient
20 err error
21 reqId string
22 s1, s2 common_configs.CcSecNotifiedServer
23 slist common_configs.CcSecNotifiedServerList
24 )
25 BeforeEach(func() {
26 cl = testtool.NewTestClient("", "http://localhost", nil)
27 s1 = common_configs.CcSecNotifiedServer{
28 AttributeMeta: common_configs.AttributeMeta{
29 CommonConfigID: 1,
30 },
31 ID: 1,
32 Address: net.ParseIP("192.168.0.1"),
33 TsigID: 0,
34 }
35 s2 = common_configs.CcSecNotifiedServer{
36 AttributeMeta: common_configs.AttributeMeta{
37 CommonConfigID: 1,
38 },
39 ID: 2,
40 Address: net.ParseIP("2001:db8::1"),
41 TsigID: 2,
42 }
43 slist = common_configs.CcSecNotifiedServerList{
44 AttributeMeta: common_configs.AttributeMeta{
45 CommonConfigID: 1,
46 },
47 Items: []common_configs.CcSecNotifiedServer{s1, s2},
48 }
49 })
50 Describe("CcSecNotifiedServer", func() {
51 Context("Read", func() {
52 BeforeEach(func() {
53 httpmock.RegisterResponder(http.MethodGet, "http://localhost/common_configs/1/cc_sec_notified_servers/1", httpmock.NewBytesResponder(200, []byte(`{
54 "request_id": "5D7FECB0A56C49C2B73BD45DD174A575",
55 "result": {
56 "id": 1,
57 "address": "192.168.0.1",
58 "tsig_id": null
59 }
60 }`)))
61 httpmock.RegisterResponder(http.MethodGet, "http://localhost/common_configs/1/cc_sec_notified_servers/2", httpmock.NewBytesResponder(200, []byte(`{
62 "request_id": "5ABBBEF090DF4F208AD9EDEAC7D702A5",
63 "result": {
64 "id": 2,
65 "address": "2001:db8::1",
66 "tsig_id": 2
67 }
68 }`)))
69 })
70 AfterEach(func() {
71 httpmock.Reset()
72 })
73 When("returns IPv4 server", func() {
74 BeforeEach(func() {
75 c = common_configs.CcSecNotifiedServer{
76 AttributeMeta: common_configs.AttributeMeta{
77 CommonConfigID: 1,
78 },
79 ID: 1,
80 }
81 reqId, err = cl.Read(context.Background(), &c)
82 })
83 It("returns normal", func() {
84 Expect(err).To(Succeed())
85 Expect(reqId).To(Equal("5D7FECB0A56C49C2B73BD45DD174A575"))
86 Expect(c).To(Equal(s1))
87 })
88 })
89 When("returns IPv6 server", func() {
90 BeforeEach(func() {
91 c = common_configs.CcSecNotifiedServer{
92 AttributeMeta: common_configs.AttributeMeta{
93 CommonConfigID: 1,
94 },
95 ID: 2,
96 }
97 reqId, err = cl.Read(context.Background(), &c)
98 })
99 It("returns normal", func() {
100 Expect(err).To(Succeed())
101 Expect(reqId).To(Equal("5ABBBEF090DF4F208AD9EDEAC7D702A5"))
102 Expect(c).To(Equal(s2))
103 })
104 })
105 })
106 Context("Create", func() {
107 var (
108 id1, bs1 = testtool.CreateAsyncResponse()
109 id2, bs2 = testtool.CreateAsyncResponse()
110 )
111 BeforeEach(func() {
112 httpmock.RegisterResponder(http.MethodPost, "http://localhost/common_configs/2/cc_sec_notified_servers", httpmock.NewBytesResponder(202, bs1))
113 httpmock.RegisterResponder(http.MethodPost, "http://localhost/common_configs/3/cc_sec_notified_servers", httpmock.NewBytesResponder(202, bs2))
114 })
115 AfterEach(func() {
116 httpmock.Reset()
117 })
118 When("ipv4 host", func() {
119 BeforeEach(func() {
120 s := common_configs.CcSecNotifiedServer{
121 AttributeMeta: common_configs.AttributeMeta{
122 CommonConfigID: 2,
123 },
124 Address: net.ParseIP("192.168.10.1"),
125 TsigID: 1,
126 }
127 reqId, err = cl.Create(context.Background(), &s, nil)
128 })
129 It("returns job_id", func() {
130 Expect(err).To(Succeed())
131 Expect(reqId).To(Equal(id1))
132 })
133 It("post json", func() {
134 Expect(cl.RequestBody["/common_configs/2/cc_sec_notified_servers"]).To(MatchJSON(`{
135 "address": "192.168.10.1",
136 "tsig_id": 1
137 }`))
138 })
139 })
140 When("ipv6 host", func() {
141 BeforeEach(func() {
142 s := common_configs.CcSecNotifiedServer{
143 AttributeMeta: common_configs.AttributeMeta{
144 CommonConfigID: 3,
145 },
146 Address: net.ParseIP("2001:db8::1"),
147 }
148 reqId, err = cl.Create(context.Background(), &s, nil)
149 })
150 It("returns job_id", func() {
151 Expect(err).To(Succeed())
152 Expect(reqId).To(Equal(id2))
153 })
154 It("post json", func() {
155 Expect(cl.RequestBody["/common_configs/3/cc_sec_notified_servers"]).To(MatchJSON(`{
156 "address": "2001:db8::1",
157 "tsig_id": null
158 }`))
159 })
160 })
161 })
162 Context("Update", func() {
163 var (
164 id1, bs1 = testtool.CreateAsyncResponse()
165 id2, bs2 = testtool.CreateAsyncResponse()
166 )
167 BeforeEach(func() {
168 httpmock.RegisterResponder(http.MethodPatch, "http://localhost/common_configs/1/cc_sec_notified_servers/1", httpmock.NewBytesResponder(202, bs1))
169 httpmock.RegisterResponder(http.MethodPatch, "http://localhost/common_configs/1/cc_sec_notified_servers/2", httpmock.NewBytesResponder(202, bs2))
170 })
171 AfterEach(func() {
172 httpmock.Reset()
173 })
174 When("ipv4 host", func() {
175 BeforeEach(func() {
176 reqId, err = cl.Update(context.Background(), &s1, nil)
177 })
178 It("returns job_id", func() {
179 Expect(err).To(Succeed())
180 Expect(reqId).To(Equal(id1))
181 })
182 It("post json", func() {
183 Expect(cl.RequestBody["/common_configs/1/cc_sec_notified_servers/1"]).To(MatchJSON(`{
184 "address": "192.168.0.1",
185 "tsig_id": null
186 }`))
187 })
188 })
189 When("ipv6 host", func() {
190 BeforeEach(func() {
191 reqId, err = cl.Update(context.Background(), &s2, nil)
192 })
193 It("returns job_id", func() {
194 Expect(err).To(Succeed())
195 Expect(reqId).To(Equal(id2))
196 })
197 It("post json", func() {
198 Expect(cl.RequestBody["/common_configs/1/cc_sec_notified_servers/2"]).To(MatchJSON(`{
199 "address": "2001:db8::1",
200 "tsig_id": 2
201 }`))
202 })
203 })
204 })
205 Context("Delete", func() {
206 var (
207 id1, bs1 = testtool.CreateAsyncResponse()
208 id2, bs2 = testtool.CreateAsyncResponse()
209 )
210 BeforeEach(func() {
211 httpmock.RegisterResponder(http.MethodDelete, "http://localhost/common_configs/1/cc_sec_notified_servers/1", httpmock.NewBytesResponder(202, bs1))
212 httpmock.RegisterResponder(http.MethodDelete, "http://localhost/common_configs/1/cc_sec_notified_servers/2", httpmock.NewBytesResponder(202, bs2))
213 })
214 AfterEach(func() {
215 httpmock.Reset()
216 })
217 When("returns IPv4 server", func() {
218 BeforeEach(func() {
219 reqId, err = cl.Delete(context.Background(), &s1)
220 })
221 It("returns normal", func() {
222 Expect(err).To(Succeed())
223 Expect(reqId).To(Equal(id1))
224 })
225 })
226 When("returns IPv6 server", func() {
227 BeforeEach(func() {
228 reqId, err = cl.Delete(context.Background(), &s2)
229 })
230 It("returns normal", func() {
231 Expect(err).To(Succeed())
232 Expect(reqId).To(Equal(id2))
233 })
234 })
235 })
236 Context("SetPathParams", func() {
237 When("no arguments, nothing to do", func() {
238 BeforeEach(func() {
239 err = s1.SetPathParams()
240 })
241 It("returns error", func() {
242 Expect(err).To(Succeed())
243 })
244 })
245 When("enough arguments", func() {
246 BeforeEach(func() {
247 err = s1.SetPathParams(100, 200)
248 })
249 It("not returns error", func() {
250 Expect(err).To(Succeed())
251 })
252 It("can set CommonConfigID", func() {
253 Expect(s1.GetCommonConfigID()).To(Equal(int64(100)))
254 })
255 It("can set Id", func() {
256 Expect(s1.GetID()).To(Equal(int64(200)))
257 })
258 })
259 When("not enough arguments", func() {
260 BeforeEach(func() {
261 err = s1.SetPathParams(100)
262 })
263 It("returns error", func() {
264 Expect(err).To(HaveOccurred())
265 })
266 })
267 When("arguments has extra value", func() {
268 BeforeEach(func() {
269 err = s1.SetPathParams(100, 1, 2)
270 })
271 It("returns error", func() {
272 Expect(err).To(HaveOccurred())
273 })
274 })
275 When("arguments type missmatch (CommonConfigID)", func() {
276 BeforeEach(func() {
277 err = s1.SetPathParams("2", 1)
278 })
279 It("returns error", func() {
280 Expect(err).To(HaveOccurred())
281 })
282 })
283 When("arguments type missmatch (Id)", func() {
284 BeforeEach(func() {
285 err = s1.SetPathParams(100, "1")
286 })
287 It("returns error", func() {
288 Expect(err).To(HaveOccurred())
289 })
290 })
291 })
292 Context("api.Spec common test", func() {
293 var nilSpec *common_configs.CcSecNotifiedServer
294 testtool.TestDeepCopyObject(&s1, nilSpec)
295 testtool.TestGetName(&s1, "cc_sec_notified_servers")
296 testtool.TestGetPathMethodForSpec(&s1, "/common_configs/1/cc_sec_notified_servers", "/common_configs/1/cc_sec_notified_servers/1")
297 })
298 Context("contracts.ChildSpec common test", func() {
299 Context("GetID", func() {
300 It("returns Id", func() {
301 Expect(s1.GetID()).To(Equal(s1.ID))
302 })
303 })
304 Context("SetID", func() {
305 BeforeEach(func() {
306 s1.SetID(2)
307 })
308 It("can set Id", func() {
309 Expect(s1.GetID()).To(Equal(int64(2)))
310 })
311 })
312 })
313 })
314 Describe("CcSecNotifiedServerList", func() {
315 var c common_configs.CcSecNotifiedServerList
316 Context("List", func() {
317 BeforeEach(func() {
318 httpmock.RegisterResponder(http.MethodGet, "http://localhost/common_configs/1/cc_sec_notified_servers", httpmock.NewBytesResponder(200, []byte(`{
319 "request_id": "450B94B486D34D548313707FC7590CE0",
320 "results": [
321 {
322 "id": 1,
323 "address": "192.168.0.1",
324 "tsig_id": null
325 },
326 {
327 "id": 2,
328 "address": "2001:db8::1",
329 "tsig_id": 2
330 }
331 ]
332 }`)))
333 })
334 AfterEach(func() {
335 httpmock.Reset()
336 })
337 When("returns list ", func() {
338 BeforeEach(func() {
339 c = common_configs.CcSecNotifiedServerList{
340 AttributeMeta: common_configs.AttributeMeta{
341 CommonConfigID: 1,
342 },
343 }
344 reqId, err = cl.List(context.Background(), &c, nil)
345 })
346 It("returns normal", func() {
347 Expect(err).To(Succeed())
348 Expect(reqId).To(Equal("450B94B486D34D548313707FC7590CE0"))
349 Expect(c).To(Equal(slist))
350 })
351 })
352 })
353 Context("SetPathParams", func() {
354 When("no arguments, nothing to do", func() {
355 BeforeEach(func() {
356 err = slist.SetPathParams()
357 })
358 It("returns error", func() {
359 Expect(err).To(Succeed())
360 })
361 })
362 When("enough arguments", func() {
363 BeforeEach(func() {
364 err = slist.SetPathParams(99)
365 })
366 It("not returns error", func() {
367 Expect(err).To(Succeed())
368 })
369 It("can set CommonConfigID", func() {
370 Expect(slist.GetCommonConfigID()).To(Equal(int64(99)))
371 })
372 })
373 When("arguments has extra value", func() {
374 BeforeEach(func() {
375 err = slist.SetPathParams(100, 2)
376 })
377 It("returns error", func() {
378 Expect(err).To(HaveOccurred())
379 })
380 })
381 When("arguments type missmatch (CommonConfigID)", func() {
382 BeforeEach(func() {
383 err = slist.SetPathParams("2")
384 })
385 It("returns error", func() {
386 Expect(err).To(HaveOccurred())
387 })
388 })
389 })
390 Context("api.Spec common test", func() {
391 var nilSpec *common_configs.CcSecNotifiedServerList
392 testtool.TestDeepCopyObject(&slist, nilSpec)
393 testtool.TestGetName(&slist, "cc_sec_notified_servers")
394 testtool.TestGetPathMethodForList(&slist, "/common_configs/1/cc_sec_notified_servers")
395 })
396 Context("api.ListSpec common test", func() {
397 testtool.TestGetItems(&slist, &slist.Items)
398 testtool.TestLen(&slist, 2)
399 Context("Index", func() {
400 When("index exist", func() {
401 It("returns item", func() {
402 Expect(slist.Index(0)).To(Equal(s1))
403 })
404 })
405 })
406 })
407 })
408 })