/
settings_reports_api_mocked.go
218 lines (163 loc) · 6.98 KB
/
settings_reports_api_mocked.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package settings
import (
"encoding/json"
"fmt"
"testing"
"github.com/flexiant/concerto/api/types"
"github.com/flexiant/concerto/utils"
"github.com/stretchr/testify/assert"
)
// GetSettingsReportListMocked test mocked function
func GetSettingsReportListMocked(t *testing.T, settingsReportsIn *[]types.SettingsReport) *[]types.SettingsReport {
assert := assert.New(t)
// wire up
cs := &utils.MockConcertoService{}
rs, err := NewSettingsReportService(cs)
assert.Nil(err, "Couldn't load report service")
assert.NotNil(rs, "Report service not instanced")
// to json
arIn, err := json.Marshal(settingsReportsIn)
assert.Nil(err, "Admin reports test data corrupted")
// call service
cs.On("Get", "/v1/settings/reports").Return(arIn, 200, nil)
arOut, err := rs.GetSettingsReportList()
assert.Nil(err, "Error getting settings report")
// exclude time data from comparison
assert.Equal(len(*settingsReportsIn), len(arOut), "Returned wrong number of elements")
for i := 0; i < len(*settingsReportsIn); i++ {
(*settingsReportsIn)[i].StartTime = arOut[i].StartTime
(*settingsReportsIn)[i].EndTime = arOut[i].EndTime
}
assert.Equal(*settingsReportsIn, arOut, "GetSettingsReportList returned different settings reports")
return &arOut
}
// GetSettingsReportListErrMocked test mocked function
func GetSettingsReportListErrMocked(t *testing.T, settingsReportsIn *[]types.SettingsReport) *[]types.SettingsReport {
assert := assert.New(t)
// wire up
cs := &utils.MockConcertoService{}
rs, err := NewSettingsReportService(cs)
assert.Nil(err, "Couldn't load report service")
assert.NotNil(rs, "Report service not instanced")
// to json
arIn, err := json.Marshal(settingsReportsIn)
assert.Nil(err, "Admin reports test data corrupted")
// call service
cs.On("Get", "/v1/settings/reports").Return(arIn, 200, fmt.Errorf("Mocked error"))
arOut, err := rs.GetSettingsReportList()
assert.NotNil(err, "We are expecting an error")
assert.Nil(arOut, "Expecting nil output")
assert.Equal(err.Error(), "Mocked error", "Error should be 'Mocked error'")
return &arOut
}
// GetSettingsReportListFailStatusMocked test mocked function
func GetSettingsReportListFailStatusMocked(t *testing.T, settingsReportsIn *[]types.SettingsReport) *[]types.SettingsReport {
assert := assert.New(t)
// wire up
cs := &utils.MockConcertoService{}
rs, err := NewSettingsReportService(cs)
assert.Nil(err, "Couldn't load report service")
assert.NotNil(rs, "Report service not instanced")
// to json
arIn, err := json.Marshal(settingsReportsIn)
assert.Nil(err, "Admin reports test data corrupted")
// call service
cs.On("Get", "/v1/settings/reports").Return(arIn, 499, nil)
arOut, err := rs.GetSettingsReportList()
assert.NotNil(err, "We are expecting an status code error")
assert.Nil(arOut, "Expecting nil output")
assert.Contains(err.Error(), "499", "Error should contain http code 499")
return &arOut
}
// GetSettingsReportListFailJSONMocked test mocked function
func GetSettingsReportListFailJSONMocked(t *testing.T, settingsReportsIn *[]types.SettingsReport) *[]types.SettingsReport {
assert := assert.New(t)
// wire up
cs := &utils.MockConcertoService{}
rs, err := NewSettingsReportService(cs)
assert.Nil(err, "Couldn't load report service")
assert.NotNil(rs, "Report service not instanced")
// wrong json
arIn := []byte{10, 20, 30}
// call service
cs.On("Get", "/v1/settings/reports").Return(arIn, 200, nil)
arOut, err := rs.GetSettingsReportList()
assert.NotNil(err, "We are expecting a marshalling error")
assert.Contains(err.Error(), "invalid character", "Error message should include the string 'invalid character'")
return &arOut
}
// GetSettingsReportMocked test mocked function
func GetSettingsReportMocked(t *testing.T, settingsReport *types.SettingsReport) *types.SettingsReport {
assert := assert.New(t)
// wire up
cs := &utils.MockConcertoService{}
ars, err := NewSettingsReportService(cs)
assert.Nil(err, "Couldn't load settingsReport service")
assert.NotNil(ars, "SettingsReport service not instanced")
// to json
arIn, err := json.Marshal(settingsReport)
assert.Nil(err, "SettingsReport test data corrupted")
// call service
cs.On("Get", fmt.Sprintf("/v1/settings/reports/%s", settingsReport.ID)).Return(arIn, 200, nil)
arOut, err := ars.GetSettingsReport(settingsReport.ID)
assert.Nil(err, "Error getting settingsReport")
// exclude time data from comparison
settingsReport.StartTime = arOut.StartTime
settingsReport.EndTime = arOut.EndTime
assert.Equal(*settingsReport, *arOut, "GetSettingsReport returned different settingsReports")
return arOut
}
// GetSettingsReportErrMocked test mocked function
func GetSettingsReportErrMocked(t *testing.T, settingsReport *types.SettingsReport) *types.SettingsReport {
assert := assert.New(t)
// wire up
cs := &utils.MockConcertoService{}
ars, err := NewSettingsReportService(cs)
assert.Nil(err, "Couldn't load settingsReport service")
assert.NotNil(ars, "SettingsReport service not instanced")
// to json
arIn, err := json.Marshal(settingsReport)
assert.Nil(err, "SettingsReport test data corrupted")
// call service
cs.On("Get", fmt.Sprintf("/v1/settings/reports/%s", settingsReport.ID)).Return(arIn, 200, fmt.Errorf("Mocked error"))
arOut, err := ars.GetSettingsReport(settingsReport.ID)
assert.NotNil(err, "We are expecting an error")
assert.Nil(arOut, "Expecting nil output")
assert.Equal(err.Error(), "Mocked error", "Error should be 'Mocked error'")
return arOut
}
// GetSettingsReportFailStatusMocked test mocked function
func GetSettingsReportFailStatusMocked(t *testing.T, settingsReport *types.SettingsReport) *types.SettingsReport {
assert := assert.New(t)
// wire up
cs := &utils.MockConcertoService{}
ars, err := NewSettingsReportService(cs)
assert.Nil(err, "Couldn't load settingsReport service")
assert.NotNil(ars, "SettingsReport service not instanced")
// to json
arIn, err := json.Marshal(settingsReport)
assert.Nil(err, "SettingsReport test data corrupted")
// call service
cs.On("Get", fmt.Sprintf("/v1/settings/reports/%s", settingsReport.ID)).Return(arIn, 499, nil)
arOut, err := ars.GetSettingsReport(settingsReport.ID)
assert.NotNil(err, "We are expecting an status code error")
assert.Nil(arOut, "Expecting nil output")
return arOut
}
// GetSettingsReportFailJSONMocked test mocked function
func GetSettingsReportFailJSONMocked(t *testing.T, settingsReport *types.SettingsReport) *types.SettingsReport {
assert := assert.New(t)
// wire up
cs := &utils.MockConcertoService{}
ars, err := NewSettingsReportService(cs)
assert.Nil(err, "Couldn't load settingsReport service")
assert.NotNil(ars, "SettingsReport service not instanced")
// wrong json
arIn := []byte{10, 20, 30}
// call service
cs.On("Get", fmt.Sprintf("/v1/settings/reports/%s", settingsReport.ID)).Return(arIn, 200, nil)
arOut, err := ars.GetSettingsReport(settingsReport.ID)
assert.NotNil(err, "We are expecting a marshalling error")
assert.Contains(err.Error(), "invalid character", "Error message should include the string 'invalid character'")
return arOut
}