Debian Codegraph @

aa/aa_test.go

raw symbols SCIP
1 package aa_test
2
3 import (
4 "encoding/json"
5 "net/url"
6 "strings"
7 "testing"
8
9 "github.com/CenturyLinkCloud/clc-sdk/aa"
10 "github.com/CenturyLinkCloud/clc-sdk/api"
11 "github.com/stretchr/testify/assert"
12 "github.com/stretchr/testify/mock"
13 )
14
15 func TestGetAAPolicy(t *testing.T) {
16 assert := assert.New(t)
17
18 client := NewMockClient()
19 client.On("Get", "http://localhost/v2/antiAffinityPolicies/test/12345", mock.Anything).Return(nil)
20 service := aa.New(client)
21 id := "12345"
22 resp, err := service.Get(id)
23
24 assert.Nil(err)
25 assert.Equal(id, resp.ID)
26 client.AssertExpectations(t)
27 }
28
29 func TestGetAllAAPolicy(t *testing.T) {
30 assert := assert.New(t)
31
32 client := NewMockClient()
33 client.On("Get", "http://localhost/v2/antiAffinityPolicies/test", mock.Anything).Return(nil)
34 service := aa.New(client)
35
36 resp, err := service.GetAll()
37
38 assert.Nil(err)
39 assert.Equal(2, len(resp.Items))
40 client.AssertExpectations(t)
41 }
42
43 func TestCreateAAPolicy(t *testing.T) {
44 assert := assert.New(t)
45
46 client := NewMockClient()
47 client.On("Post", "http://localhost/v2/antiAffinityPolicies/test", mock.Anything, mock.Anything).Return(nil)
48 service := aa.New(client)
49 name := "aa1"
50 location := "dc1"
51
52 resp, err := service.Create(name, location)
53
54 assert.Nil(err)
55 assert.Equal(name, resp.Name)
56 assert.Equal(location, resp.Location)
57 assert.NotEmpty(resp.ID)
58 assert.NotEmpty(resp.Links)
59 client.AssertExpectations(t)
60 }
61
62 func TestUpdateAAPolicy(t *testing.T) {
63 assert := assert.New(t)
64
65 client := NewMockClient()
66 client.On("Put", "http://localhost/v2/antiAffinityPolicies/test/12345", mock.Anything, mock.Anything).Return(nil)
67 service := aa.New(client)
68 id := "12345"
69 name := "aa1"
70
71 resp, err := service.Update(id, name)
72
73 assert.Nil(err)
74 assert.Equal(name, resp.Name)
75 client.AssertExpectations(t)
76 }
77
78 func TestDeleteAAPolicy(t *testing.T) {
79 assert := assert.New(t)
80
81 client := NewMockClient()
82 client.On("Delete", "http://localhost/v2/antiAffinityPolicies/test/12345", nil).Return(nil)
83 service := aa.New(client)
84
85 err := service.Delete("12345")
86
87 assert.Nil(err)
88 client.AssertExpectations(t)
89 }
90
91 func NewMockClient() *MockClient {
92 return &MockClient{}
93 }
94
95 type MockClient struct {
96 mock.Mock
97 }
98
99 func (m *MockClient) Get(url string, resp interface{}) error {
100 if strings.HasSuffix(url, "test") {
101 json.Unmarshal([]byte(`{"items":[{"id":"12345","name":"aa1","location":"dc1","links":[{"rel":"self","href":"/v2/antiAffinityPolicies/test/12345","verbs":["GET","DELETE","PUT"]}]},{"id":"67890","name":"aa2","location":"dc2","links":[{"rel":"self","href":"/v2/antiAffinityPolicies/test/67890","verbs":["GET","DELETE","PUT"]}]}],"links":[{"rel":"self","href":"/v2/antiAffinityPolicies/test","verbs":["GET","POST"]}]}`), resp)
102 }
103
104 if strings.HasSuffix(url, "12345") {
105 json.Unmarshal([]byte(`{"id":"12345","name":"aa1","location":"dc1","links":[{"rel":"self","href":"/v2/antiAffinityPolicies/test/12345","verbs":["GET","DELETE","PUT"]}]}`), resp)
106 }
107 args := m.Called(url, resp)
108 return args.Error(0)
109 }
110
111 func (m *MockClient) Post(url string, body, resp interface{}) error {
112 json.Unmarshal([]byte(`{"id":"12345","name":"aa1","location":"dc1","links":[{"rel":"self","href":"/v2/antiAffinityPolicies/test/12345","verbs":["GET","DELETE","PUT"]}]}`), resp)
113 args := m.Called(url, body, resp)
114 return args.Error(0)
115 }
116
117 func (m *MockClient) Put(url string, body, resp interface{}) error {
118 json.Unmarshal([]byte(`{"id":"12345","name":"aa1","location":"dc1","links":[{"rel":"self","href":"/v2/antiAffinityPolicies/test/12345","verbs":["GET","DELETE","PUT"]}]}`), resp)
119 args := m.Called(url, body, resp)
120 return args.Error(0)
121 }
122
123 func (m *MockClient) Patch(url string, body, resp interface{}) error {
124 args := m.Called(url, body, resp)
125 return args.Error(0)
126 }
127
128 func (m *MockClient) Delete(url string, resp interface{}) error {
129 args := m.Called(url, resp)
130 return args.Error(0)
131 }
132
133 func (m *MockClient) Config() *api.Config {
134 u, _ := url.Parse("http://localhost/v2")
135 return &api.Config{
136 User: api.User{
137 Username: "test.user",
138 Password: "s0s3cur3",
139 },
140 Alias: "test",
141 BaseURL: u,
142 }
143 }