Debian Codegraph @

pkg/apis/dpf/v1/contracts/contract_zone_common_config_test.go

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