| 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/testtool" |
| 13 | ) |
| 14 | |
| 15 | var _ = Describe("qps_histories", func() { |
| 16 | var ( |
| 17 | cl *testtool.TestClient |
| 18 | err error |
| 19 | reqId string |
| 20 | s1, s2 contracts.QpsHistory |
| 21 | slist contracts.QpsHistoryList |
| 22 | ) |
| 23 | BeforeEach(func() { |
| 24 | cl = testtool.NewTestClient("", "http://localhost", nil) |
| 25 | s1 = contracts.QpsHistory{ |
| 26 | ServiceCode: "f00001", |
| 27 | Name: "hoge", |
| 28 | Values: []contracts.QpsValue{ |
| 29 | { |
| 30 | Month: "2021-01", |
| 31 | Qps: 100, |
| 32 | }, |
| 33 | { |
| 34 | Month: "2021-02", |
| 35 | Qps: 100, |
| 36 | }, |
| 37 | }, |
| 38 | } |
| 39 | s2 = contracts.QpsHistory{ |
| 40 | ServiceCode: "f00002", |
| 41 | Name: "hogehoge", |
| 42 | Values: []contracts.QpsValue{ |
| 43 | { |
| 44 | Month: "2021-07", |
| 45 | Qps: 20, |
| 46 | }, |
| 47 | { |
| 48 | Month: "2021-09", |
| 49 | Qps: 100, |
| 50 | }, |
| 51 | }, |
| 52 | } |
| 53 | |
| 54 | slist = contracts.QpsHistoryList{ |
| 55 | AttributeMeta: contracts.AttributeMeta{ |
| 56 | ContractID: "f1", |
| 57 | }, |
| 58 | Items: []contracts.QpsHistory{s1, s2}, |
| 59 | } |
| 60 | }) |
| 61 | Describe("QpsHistoryList", func() { |
| 62 | var c contracts.QpsHistoryList |
| 63 | Context("List", func() { |
| 64 | BeforeEach(func() { |
| 65 | httpmock.RegisterResponder(http.MethodGet, "http://localhost/contracts/f1/qps/histories", httpmock.NewBytesResponder(200, []byte(`{ |
| 66 | "request_id": "3649092EAD70427A854CE933973AFFF8", |
| 67 | "results": [ |
| 68 | { |
| 69 | "service_code": "f00001", |
| 70 | "name": "hoge", |
| 71 | "values": [ |
| 72 | { |
| 73 | "month": "2021-01", |
| 74 | "qps": 100 |
| 75 | }, |
| 76 | { |
| 77 | "month": "2021-02", |
| 78 | "qps": 100 |
| 79 | } |
| 80 | ] |
| 81 | }, |
| 82 | { |
| 83 | "service_code": "f00002", |
| 84 | "name": "hogehoge", |
| 85 | "values": [ |
| 86 | { |
| 87 | "month": "2021-07", |
| 88 | "qps": 20 |
| 89 | }, |
| 90 | { |
| 91 | "month": "2021-09", |
| 92 | "qps": 100 |
| 93 | } |
| 94 | ] |
| 95 | } |
| 96 | ] |
| 97 | }`))) |
| 98 | }) |
| 99 | AfterEach(func() { |
| 100 | httpmock.Reset() |
| 101 | }) |
| 102 | When("returns list ", func() { |
| 103 | BeforeEach(func() { |
| 104 | c = contracts.QpsHistoryList{ |
| 105 | AttributeMeta: contracts.AttributeMeta{ |
| 106 | ContractID: "f1", |
| 107 | }, |
| 108 | } |
| 109 | reqId, err = cl.List(context.Background(), &c, nil) |
| 110 | }) |
| 111 | It("returns normal", func() { |
| 112 | Expect(err).To(Succeed()) |
| 113 | Expect(reqId).To(Equal("3649092EAD70427A854CE933973AFFF8")) |
| 114 | Expect(c).To(Equal(slist)) |
| 115 | }) |
| 116 | }) |
| 117 | }) |
| 118 | Context("SetPathParams", func() { |
| 119 | When("no arguments, nothing to do", func() { |
| 120 | BeforeEach(func() { |
| 121 | err = slist.SetPathParams() |
| 122 | }) |
| 123 | It("returns error", func() { |
| 124 | Expect(err).To(Succeed()) |
| 125 | }) |
| 126 | }) |
| 127 | When("enough arguments", func() { |
| 128 | BeforeEach(func() { |
| 129 | err = slist.SetPathParams("id12") |
| 130 | }) |
| 131 | It("not returns error", func() { |
| 132 | Expect(err).To(Succeed()) |
| 133 | }) |
| 134 | It("can set ContractID", func() { |
| 135 | Expect(slist.GetContractID()).To(Equal("id12")) |
| 136 | }) |
| 137 | }) |
| 138 | When("arguments has extra value", func() { |
| 139 | BeforeEach(func() { |
| 140 | err = slist.SetPathParams("f1", 2) |
| 141 | }) |
| 142 | It("returns error", func() { |
| 143 | Expect(err).To(HaveOccurred()) |
| 144 | }) |
| 145 | }) |
| 146 | When("arguments type missmatch (ContractID)", func() { |
| 147 | BeforeEach(func() { |
| 148 | err = slist.SetPathParams(2) |
| 149 | }) |
| 150 | It("returns error", func() { |
| 151 | Expect(err).To(HaveOccurred()) |
| 152 | }) |
| 153 | }) |
| 154 | }) |
| 155 | Context("api.Spec common test", func() { |
| 156 | var nilSpec *contracts.QpsHistoryList |
| 157 | testtool.TestDeepCopyObject(&slist, nilSpec) |
| 158 | testtool.TestGetName(&slist, "qps/histories") |
| 159 | testtool.TestGetPathMethodForList(&slist, "/contracts/f1/qps/histories") |
| 160 | }) |
| 161 | Context("api.ListSpec common test", func() { |
| 162 | testtool.TestGetItems(&slist, &slist.Items) |
| 163 | testtool.TestLen(&slist, 2) |
| 164 | Context("Index", func() { |
| 165 | When("index exist", func() { |
| 166 | It("returns item", func() { |
| 167 | Expect(slist.Index(0)).To(Equal(s1)) |
| 168 | }) |
| 169 | }) |
| 170 | }) |
| 171 | }) |
| 172 | }) |
| 173 | Describe("QpsHistory", func() { |
| 174 | var s, copy, nilSpec *contracts.QpsHistory |
| 175 | BeforeEach(func() { |
| 176 | s = &contracts.QpsHistory{} |
| 177 | }) |
| 178 | Context("DeepCopyt", func() { |
| 179 | When("object is not nil", func() { |
| 180 | BeforeEach(func() { |
| 181 | copy = s.DeepCopy() |
| 182 | }) |
| 183 | It("returns copy", func() { |
| 184 | Expect(copy).NotTo(BeNil()) |
| 185 | Expect(copy).To(Equal(s)) |
| 186 | }) |
| 187 | }) |
| 188 | When("object is not nil", func() { |
| 189 | BeforeEach(func() { |
| 190 | copy = nilSpec.DeepCopy() |
| 191 | }) |
| 192 | It("returns nil", func() { |
| 193 | Expect(copy).To(BeNil()) |
| 194 | }) |
| 195 | }) |
| 196 | }) |
| 197 | }) |
| 198 | Describe("QpsValue", func() { |
| 199 | var s, copy, nilSpec *contracts.QpsValue |
| 200 | BeforeEach(func() { |
| 201 | s = &contracts.QpsValue{} |
| 202 | }) |
| 203 | Context("DeepCopy", func() { |
| 204 | When("object is not nil", func() { |
| 205 | BeforeEach(func() { |
| 206 | copy = s.DeepCopy() |
| 207 | }) |
| 208 | It("returns copy", func() { |
| 209 | Expect(copy).NotTo(BeNil()) |
| 210 | Expect(copy).To(Equal(s)) |
| 211 | }) |
| 212 | }) |
| 213 | When("object is not nil", func() { |
| 214 | BeforeEach(func() { |
| 215 | copy = nilSpec.DeepCopy() |
| 216 | }) |
| 217 | It("returns nil", func() { |
| 218 | Expect(copy).To(BeNil()) |
| 219 | }) |
| 220 | }) |
| 221 | }) |
| 222 | }) |
| 223 | }) |