Debian Codegraph @

pkg/apis/dpf/v1/zones/zone_apply_test.go

raw
1 package zones_test
2
3 import (
4 "context"
5 "net/http"
6
7 "github.com/jarcoal/httpmock"
8 . "github.com/onsi/ginkgo"
9 . "github.com/onsi/gomega"
10
11 "github.com/mimuret/golang-iij-dpf/pkg/api"
12 "github.com/mimuret/golang-iij-dpf/pkg/apis/dpf/v1/zones"
13 "github.com/mimuret/golang-iij-dpf/pkg/testtool"
14 )
15
16 var _ = Describe("zones", func() {
17 var (
18 cl *testtool.TestClient
19 err error
20 reqId string
21 s zones.ZoneApply
22 )
23 BeforeEach(func() {
24 cl = testtool.NewTestClient("", "http://localhost", nil)
25 s = zones.ZoneApply{
26 AttributeMeta: zones.AttributeMeta{
27 ZoneID: "m1",
28 },
29 Description: "commit records",
30 }
31 })
32 Describe("ZoneApply", func() {
33 Context("Apply", func() {
34 id1, bs1 := testtool.CreateAsyncResponse()
35 BeforeEach(func() {
36 httpmock.RegisterResponder(http.MethodPatch, "http://localhost/zones/m1/changes", httpmock.NewBytesResponder(202, bs1))
37 reqId, err = cl.Apply(context.Background(), &s, nil)
38 })
39 AfterEach(func() {
40 httpmock.Reset()
41 })
42 It("returns job_id", func() {
43 Expect(err).To(Succeed())
44 Expect(reqId).To(Equal(id1))
45 })
46 It("post json", func() {
47 Expect(cl.RequestBody["/zones/m1/changes"]).To(MatchJSON(`{
48 "description": "commit records"
49 }`))
50 })
51 })
52 Context("Cancel", func() {
53 id1, bs1 := testtool.CreateAsyncResponse()
54 BeforeEach(func() {
55 httpmock.RegisterResponder(http.MethodDelete, "http://localhost/zones/m1/changes", httpmock.NewBytesResponder(202, bs1))
56 reqId, err = cl.Cancel(context.Background(), &s)
57 })
58 AfterEach(func() {
59 httpmock.Reset()
60 })
61 It("returns job_id", func() {
62 Expect(err).To(Succeed())
63 Expect(reqId).To(Equal(id1))
64 })
65 It("post json", func() {
66 Expect(cl.RequestBody["/zones/m1/changes"]).To(Equal(""))
67 })
68 })
69 Context("SetPathParams", func() {
70 When("no arguments, nothing to do", func() {
71 BeforeEach(func() {
72 err = s.SetPathParams()
73 })
74 It("returns error", func() {
75 Expect(err).To(Succeed())
76 })
77 })
78 When("enough arguments", func() {
79 BeforeEach(func() {
80 err = s.SetPathParams("m100")
81 })
82 It("not returns error", func() {
83 Expect(err).To(Succeed())
84 })
85 It("can set ZonetId", func() {
86 Expect(s.ZoneID).To(Equal("m100"))
87 })
88 })
89 When("arguments has extra value", func() {
90 BeforeEach(func() {
91 err = s.SetPathParams("m100", 2)
92 })
93 It("returns error", func() {
94 Expect(err).To(HaveOccurred())
95 })
96 })
97 When("arguments type missmatch (ZoneID)", func() {
98 BeforeEach(func() {
99 err = s.SetPathParams(2)
100 })
101 It("returns error", func() {
102 Expect(err).To(HaveOccurred())
103 })
104 })
105 })
106 Context("api.Spec common test", func() {
107 var nilSpec *zones.ZoneApply
108 testtool.TestDeepCopyObject(&s, nilSpec)
109 testtool.TestGetName(&s, "apply")
110
111 Context("GetPathMethod", func() {
112 When("action is ActionApply", func() {
113 testtool.TestGetPathMethod(&s, api.ActionApply, http.MethodPatch, "/zones/m1/changes")
114 })
115 When("other", func() {
116 testtool.TestGetPathMethod(&s, api.ActionRead, "", "")
117 })
118 })
119 })
120 })
121 })