| 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/apis/dpf/v1/contracts" |
| 12 |
"github.com/mimuret/golang-iij-dpf/pkg/apis/dpf/v1/core" |
| 13 |
"github.com/mimuret/golang-iij-dpf/pkg/testtool" |
| 14 |
"github.com/mimuret/golang-iij-dpf/pkg/types" |
| 15 |
) |
| 16 |
|
| 17 |
var _ = Describe("contract_zones", func() { |
| 18 |
var ( |
| 19 |
cl *testtool.TestClient |
| 20 |
err error |
| 21 |
reqId string |
| 22 |
s1, s2 core.Zone |
| 23 |
slist contracts.ContractZoneList |
| 24 |
) |
| 25 |
BeforeEach(func() { |
| 26 |
cl = testtool.NewTestClient("", "http://localhost", nil) |
| 27 |
s1 = core.Zone{ |
| 28 |
ID: "m1", |
| 29 |
CommonConfigID: 1, |
| 30 |
ServiceCode: "dpm0000001", |
| 31 |
State: types.StateBeforeStart, |
| 32 |
Favorite: types.FavoriteHighPriority, |
| 33 |
Name: "example.jp.", |
| 34 |
Network: "", |
| 35 |
Description: "zone 1", |
| 36 |
ZoneProxyEnabled: types.Disabled, |
| 37 |
} |
| 38 |
s2 = core.Zone{ |
| 39 |
ID: "m2", |
| 40 |
CommonConfigID: 2, |
| 41 |
ServiceCode: "dpm0000002", |
| 42 |
State: types.StateRunning, |
| 43 |
Favorite: types.FavoriteLowPriority, |
| 44 |
Name: "168.192.in-addr.arpa.", |
| 45 |
Network: "192.168.0.0/16", |
| 46 |
Description: "zone 2", |
| 47 |
ZoneProxyEnabled: types.Enabled, |
| 48 |
} |
| 49 |
|
| 50 |
slist = contracts.ContractZoneList{ |
| 51 |
AttributeMeta: contracts.AttributeMeta{ |
| 52 |
ContractID: "f1", |
| 53 |
}, |
| 54 |
Items: []core.Zone{s1, s2}, |
| 55 |
} |
| 56 |
}) |
| 57 |
Describe("ContractZoneList", func() { |
| 58 |
var c contracts.ContractZoneList |
| 59 |
Context("List", func() { |
| 60 |
BeforeEach(func() { |
| 61 |
httpmock.RegisterResponder(http.MethodGet, "http://localhost/contracts/f1/zones", httpmock.NewBytesResponder(200, []byte(`{ |
| 62 |
"request_id": "E8FCA53D138A4AAD8DCED5514A5AF7CB", |
| 63 |
"results": [ |
| 64 |
{ |
| 65 |
"id": "m1", |
| 66 |
"common_config_id": 1, |
| 67 |
"service_code": "dpm0000001", |
| 68 |
"state": 1, |
| 69 |
"favorite": 1, |
| 70 |
"name": "example.jp.", |
| 71 |
"network": "", |
| 72 |
"description": "zone 1", |
| 73 |
"zone_proxy_enabled": 0 |
| 74 |
}, |
| 75 |
{ |
| 76 |
"id": "m2", |
| 77 |
"common_config_id": 2, |
| 78 |
"service_code": "dpm0000002", |
| 79 |
"state": 2, |
| 80 |
"favorite":2, |
| 81 |
"name": "168.192.in-addr.arpa.", |
| 82 |
"network": "192.168.0.0/16", |
| 83 |
"description": "zone 2", |
| 84 |
"zone_proxy_enabled": 1 |
| 85 |
} |
| 86 |
] |
| 87 |
}`))) |
| 88 |
}) |
| 89 |
AfterEach(func() { |
| 90 |
httpmock.Reset() |
| 91 |
}) |
| 92 |
When("returns list ", func() { |
| 93 |
BeforeEach(func() { |
| 94 |
c = contracts.ContractZoneList{ |
| 95 |
AttributeMeta: contracts.AttributeMeta{ |
| 96 |
ContractID: "f1", |
| 97 |
}, |
| 98 |
} |
| 99 |
reqId, err = cl.List(context.Background(), &c, nil) |
| 100 |
}) |
| 101 |
It("returns normal", func() { |
| 102 |
Expect(err).To(Succeed()) |
| 103 |
Expect(reqId).To(Equal("E8FCA53D138A4AAD8DCED5514A5AF7CB")) |
| 104 |
Expect(c).To(Equal(slist)) |
| 105 |
}) |
| 106 |
}) |
| 107 |
}) |
| 108 |
Context("SetPathParams", func() { |
| 109 |
When("no arguments, nothing to do", func() { |
| 110 |
BeforeEach(func() { |
| 111 |
err = slist.SetPathParams() |
| 112 |
}) |
| 113 |
It("returns error", func() { |
| 114 |
Expect(err).To(Succeed()) |
| 115 |
}) |
| 116 |
}) |
| 117 |
When("enough arguments", func() { |
| 118 |
BeforeEach(func() { |
| 119 |
err = slist.SetPathParams("id12") |
| 120 |
}) |
| 121 |
It("not returns error", func() { |
| 122 |
Expect(err).To(Succeed()) |
| 123 |
}) |
| 124 |
It("can set ContractID", func() { |
| 125 |
Expect(slist.GetContractID()).To(Equal("id12")) |
| 126 |
}) |
| 127 |
}) |
| 128 |
When("arguments has extra value", func() { |
| 129 |
BeforeEach(func() { |
| 130 |
err = slist.SetPathParams("f1", 2) |
| 131 |
}) |
| 132 |
It("returns error", func() { |
| 133 |
Expect(err).To(HaveOccurred()) |
| 134 |
}) |
| 135 |
}) |
| 136 |
When("arguments type missmatch (ContractID)", func() { |
| 137 |
BeforeEach(func() { |
| 138 |
err = slist.SetPathParams(2) |
| 139 |
}) |
| 140 |
It("returns error", func() { |
| 141 |
Expect(err).To(HaveOccurred()) |
| 142 |
}) |
| 143 |
}) |
| 144 |
}) |
| 145 |
Context("api.Spec common test", func() { |
| 146 |
var nilSpec *contracts.ContractZoneList |
| 147 |
testtool.TestDeepCopyObject(&slist, nilSpec) |
| 148 |
testtool.TestGetName(&slist, "zones") |
| 149 |
testtool.TestGetPathMethodForCountableList(&slist, "/contracts/f1/zones") |
| 150 |
}) |
| 151 |
Context("api.ListSpec common test", func() { |
| 152 |
testtool.TestGetItems(&slist, &slist.Items) |
| 153 |
testtool.TestLen(&slist, 2) |
| 154 |
Context("Index", func() { |
| 155 |
When("index exist", func() { |
| 156 |
It("returns item", func() { |
| 157 |
Expect(slist.Index(0)).To(Equal(s1)) |
| 158 |
}) |
| 159 |
}) |
| 160 |
}) |
| 161 |
}) |
| 162 |
Context("api.CountableListSpec common test", func() { |
| 163 |
testtool.TestGetMaxLimit(&slist, 10000) |
| 164 |
testtool.TestClearItems(&slist) |
| 165 |
testtool.TestAddItem(&slist, s1) |
| 166 |
}) |
| 167 |
}) |
| 168 |
}) |