| 1 | package zones_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "time" |
| 7 | |
| 8 | . "github.com/onsi/ginkgo" |
| 9 | . "github.com/onsi/gomega" |
| 10 | |
| 11 | "github.com/jarcoal/httpmock" |
| 12 | api "github.com/mimuret/golang-iij-dpf/pkg/api" |
| 13 | "github.com/mimuret/golang-iij-dpf/pkg/apis/dpf/v1/zones" |
| 14 | "github.com/mimuret/golang-iij-dpf/pkg/testtool" |
| 15 | "github.com/mimuret/golang-iij-dpf/pkg/types" |
| 16 | ) |
| 17 | |
| 18 | var _ = Describe("history", func() { |
| 19 | var ( |
| 20 | c zones.HistoryText |
| 21 | cl *testtool.TestClient |
| 22 | err error |
| 23 | reqId string |
| 24 | s1, s2 zones.HistoryText |
| 25 | ) |
| 26 | BeforeEach(func() { |
| 27 | atTime, err := types.ParseTime(time.RFC3339Nano, "2021-06-20T10:50:37.396Z") |
| 28 | Expect(err).To(Succeed()) |
| 29 | |
| 30 | cl = testtool.NewTestClient("", "http://localhost", nil) |
| 31 | s1 = zones.HistoryText{ |
| 32 | AttributeMeta: zones.AttributeMeta{ |
| 33 | ZoneID: "m1", |
| 34 | }, |
| 35 | History: zones.History{ |
| 36 | ID: 1, |
| 37 | CommittedAt: atTime, |
| 38 | Description: "commit 1", |
| 39 | Operator: "user1", |
| 40 | }, |
| 41 | Text: "zone data 1", |
| 42 | } |
| 43 | s2 = zones.HistoryText{ |
| 44 | AttributeMeta: zones.AttributeMeta{ |
| 45 | ZoneID: "m1", |
| 46 | }, |
| 47 | History: zones.History{ |
| 48 | ID: 2, |
| 49 | CommittedAt: atTime, |
| 50 | Description: "commit 2", |
| 51 | Operator: "user2", |
| 52 | }, |
| 53 | Text: "zone data 2", |
| 54 | } |
| 55 | }) |
| 56 | Describe("DsRecordList", func() { |
| 57 | Context("Read", func() { |
| 58 | BeforeEach(func() { |
| 59 | httpmock.RegisterResponder(http.MethodGet, "http://localhost/zones/m1/zone_histories/1/text", httpmock.NewBytesResponder(200, []byte(`{ |
| 60 | "request_id": "CC2BBC44169940CE96D7E8A99FA98958", |
| 61 | "result": { |
| 62 | "id": 1, |
| 63 | "committed_at": "2021-06-20T10:50:37.396Z", |
| 64 | "description": "commit 1", |
| 65 | "operator": "user1", |
| 66 | "text": "zone data 1" |
| 67 | } |
| 68 | }`))) |
| 69 | httpmock.RegisterResponder(http.MethodGet, "http://localhost/zones/m1/zone_histories/2/text", httpmock.NewBytesResponder(200, []byte(`{ |
| 70 | "request_id": "CFBD5CBBE7C147C9BD2549551F018D11", |
| 71 | "result": { |
| 72 | "id": 2, |
| 73 | "committed_at": "2021-06-20T10:50:37.396Z", |
| 74 | "description": "commit 2", |
| 75 | "operator": "user2", |
| 76 | "text": "zone data 2" |
| 77 | } |
| 78 | }`))) |
| 79 | }) |
| 80 | AfterEach(func() { |
| 81 | httpmock.Reset() |
| 82 | }) |
| 83 | When("returns id1", func() { |
| 84 | BeforeEach(func() { |
| 85 | c = zones.HistoryText{ |
| 86 | AttributeMeta: zones.AttributeMeta{ |
| 87 | ZoneID: "m1", |
| 88 | }, |
| 89 | History: zones.History{ |
| 90 | ID: 1, |
| 91 | }, |
| 92 | } |
| 93 | reqId, err = cl.Read(context.Background(), &c) |
| 94 | }) |
| 95 | It("returns normal", func() { |
| 96 | Expect(err).To(Succeed()) |
| 97 | Expect(reqId).To(Equal("CC2BBC44169940CE96D7E8A99FA98958")) |
| 98 | Expect(c).To(Equal(s1)) |
| 99 | }) |
| 100 | }) |
| 101 | When("returns id2", func() { |
| 102 | BeforeEach(func() { |
| 103 | c = zones.HistoryText{ |
| 104 | AttributeMeta: zones.AttributeMeta{ |
| 105 | ZoneID: "m1", |
| 106 | }, |
| 107 | History: zones.History{ |
| 108 | ID: 2, |
| 109 | }, |
| 110 | } |
| 111 | reqId, err = cl.Read(context.Background(), &c) |
| 112 | }) |
| 113 | It("returns normal", func() { |
| 114 | Expect(err).To(Succeed()) |
| 115 | Expect(reqId).To(Equal("CFBD5CBBE7C147C9BD2549551F018D11")) |
| 116 | Expect(c).To(Equal(s2)) |
| 117 | }) |
| 118 | }) |
| 119 | }) |
| 120 | Context("SetPathParams", func() { |
| 121 | When("no arguments, nothing to do", func() { |
| 122 | BeforeEach(func() { |
| 123 | err = s1.SetPathParams() |
| 124 | }) |
| 125 | It("returns error", func() { |
| 126 | Expect(err).To(Succeed()) |
| 127 | }) |
| 128 | }) |
| 129 | When("enough arguments", func() { |
| 130 | BeforeEach(func() { |
| 131 | err = s1.SetPathParams("m10", 10) |
| 132 | }) |
| 133 | It("not returns error", func() { |
| 134 | Expect(err).To(Succeed()) |
| 135 | }) |
| 136 | It("can set ContractID", func() { |
| 137 | Expect(s1.GetZoneID()).To(Equal("m10")) |
| 138 | }) |
| 139 | It("can set HistoryId", func() { |
| 140 | Expect(s1.ID).To(Equal(int64(10))) |
| 141 | }) |
| 142 | }) |
| 143 | When("not enough arguments", func() { |
| 144 | BeforeEach(func() { |
| 145 | err = s1.SetPathParams("m10") |
| 146 | }) |
| 147 | It("not returns error", func() { |
| 148 | Expect(err).To(HaveOccurred()) |
| 149 | }) |
| 150 | }) |
| 151 | When("arguments has extra value", func() { |
| 152 | BeforeEach(func() { |
| 153 | err = s1.SetPathParams("m10", 2, 1) |
| 154 | }) |
| 155 | It("returns error", func() { |
| 156 | Expect(err).To(HaveOccurred()) |
| 157 | }) |
| 158 | }) |
| 159 | When("arguments type missmatch (ZoneID)", func() { |
| 160 | BeforeEach(func() { |
| 161 | err = s1.SetPathParams(2) |
| 162 | }) |
| 163 | It("returns error", func() { |
| 164 | Expect(err).To(HaveOccurred()) |
| 165 | }) |
| 166 | }) |
| 167 | When("arguments type missmatch (HistoryId)", func() { |
| 168 | BeforeEach(func() { |
| 169 | err = s1.SetPathParams("m1", "h1") |
| 170 | }) |
| 171 | It("returns error", func() { |
| 172 | Expect(err).To(HaveOccurred()) |
| 173 | }) |
| 174 | }) |
| 175 | }) |
| 176 | Context("api.Spec common test", func() { |
| 177 | var nilSpec *zones.HistoryText |
| 178 | testtool.TestDeepCopyObject(&s1, nilSpec) |
| 179 | testtool.TestGetName(&s1, "zone_histories/1/text") |
| 180 | |
| 181 | Context("GetPathMethod", func() { |
| 182 | When("action is ActionRead", func() { |
| 183 | testtool.TestGetPathMethod(&s1, api.ActionRead, http.MethodGet, "/zones/m1/zone_histories/1/text") |
| 184 | }) |
| 185 | When("action is other", func() { |
| 186 | testtool.TestGetPathMethod(&s1, api.ActionApply, "", "") |
| 187 | }) |
| 188 | }) |
| 189 | }) |
| 190 | }) |
| 191 | }) |