-
Notifications
You must be signed in to change notification settings - Fork 9
/
vitotrol_test.go
488 lines (434 loc) · 11.5 KB
/
vitotrol_test.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
package vitotrol
import (
"encoding/xml"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
td "github.com/maxatome/go-testdeep"
)
var _ = []HasResultHeader{
(*LoginResponse)(nil),
(*GetDevicesResponse)(nil),
(*RequestRefreshStatusResponse)(nil),
(*RequestWriteStatusResponse)(nil),
}
const (
respHeader = `<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body>`
respFooter = `</soap:Body></soap:Envelope>`
)
func extractRequestBody(t *td.T, r *http.Request, reqBody interface{}, testName string) bool {
t.Helper()
bodyRaw, err := io.ReadAll(r.Body)
if !t.CmpNoError(err, "%s: request body ReadAll OK", testName) {
return false
}
err = xml.Unmarshal(bodyRaw, reqBody)
return t.CmpNoError(err, "%s: request body Unmarshal OK", testName)
}
func virginInstance(pOrig interface{}) interface{} {
return reflect.New(
reflect.Indirect(reflect.ValueOf(pOrig)).Type()).
Interface()
}
func testSendRequestAny(t *td.T,
sendReq func(v *Session) bool, soapAction string,
expectedRequest interface{}, serverResponse string,
testName string,
) bool {
t.Helper()
ts := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
// Check header
t.CmpDeeply(r.Header.Get("SOAPAction"), soapURL+soapAction,
"%s: SOAPAction header matches", testName)
t.CmpDeeply(r.Header.Get("Content-Type"), "text/xml; charset=utf-8",
"%s: Content-Type header matches", testName)
if cookie := r.Header.Get("Cookie"); cookie != "" {
w.Header().Add("Set-Cookie", cookie)
}
// Extract request body in the same struct type as the expectedRequest
recvReq := virginInstance(expectedRequest)
if !extractRequestBody(t, r, recvReq, testName) {
w.WriteHeader(http.StatusInternalServerError)
return
}
t.CmpDeeply(recvReq, expectedRequest, "%s: request OK", testName)
// Send response
fmt.Fprintln(w, respHeader+serverResponse+respFooter)
}))
defer ts.Close()
MainURL = ts.URL
return sendReq(&Session{})
}
//
// sendRequest
//
type TestResponse struct {
TestResult TestResult `xml:"Body>TestResponse>TestResult"`
}
type TestResult struct {
ResultHeader
Pipo string `xml:"Pipo"`
}
func (r *TestResponse) ResultHeader() *ResultHeader {
return &r.TestResult.ResultHeader
}
func TestSendRequestErrors(tt *testing.T) {
t := td.NewT(tt)
v := &Session{}
// bad URL -> parse URL will fail
MainURL = ":"
var resp TestResponse
err := v.sendRequest("bad", `<xxx></xxx>`, &resp)
t.CmpError(err)
// bad scheme -> Do request will fail
MainURL = "bad-scheme:..."
err = v.sendRequest("bad", `<xxx></xxx>`, &resp)
t.CmpError(err)
// HTTP status error
ts := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer ts.Close()
MainURL = ts.URL
err = v.sendRequest("bad", `<xxx></xxx>`, &resp)
t.CmpError(err)
}
func TestSendRequest(tt *testing.T) {
t := td.NewT(tt)
type testRequest struct {
Foo string `xml:"Body>Test>Foo"`
Bar string `xml:"Body>Test>Bar"`
}
// No problem
testSendRequestAny(t,
// Send request and check result
func(v *Session) bool {
v.Cookies = []string{"foo=123", "bar=456"}
var resp TestResponse
err := v.sendRequest("foobar", `
<Test>
<Foo>foo</Foo>
<Bar>bar</Bar>
</Test>`, &resp)
if !t.CmpNoError(err) {
return false
}
return t.CmpDeeply(&resp,
&TestResponse{
TestResult: TestResult{
ResultHeader: ResultHeader{
ErrorNum: 0,
ErrorStr: "Kein Fehler",
},
Pipo: "hello",
},
})
},
// SOAP action
"foobar",
// Expected request
&testRequest{
Foo: "foo",
Bar: "bar",
},
// Response to reply
`<TestResponse xmlns="http://www/">
<TestResult>
<Ergebnis>0</Ergebnis>
<ErgebnisText>Kein Fehler</ErgebnisText>
<Pipo>hello</Pipo>
</TestResult>
</TestResponse>`,
"sendRequest")
// XML decoding error
testSendRequestAny(t,
// Send request and check result
func(v *Session) bool {
v.Debug = true
var resp TestResponse
err := v.sendRequest("foobar", `
<Test>
<Foo>foo</Foo>
<Bar>bar</Bar>
</Test>`, &resp)
return t.CmpError(err)
},
// SOAP action
"foobar",
// Expected request
&testRequest{
Foo: "foo",
Bar: "bar",
},
// Response to reply
"<bad XML>",
"sendRequest XML error")
// Applicative error
testSendRequestAny(t,
// Send request and check result
func(v *Session) bool {
var resp TestResponse
err := v.sendRequest("foobar", `
<Test>
<Foo>foo</Foo>
<Bar>bar</Bar>
</Test>`, &resp)
if !t.CmpError(err) || !t.Isa(err, &ResultHeader{}) {
return false
}
return t.CmpDeeply(err.(*ResultHeader),
td.Struct(&ResultHeader{
ErrorNum: 42,
ErrorStr: "ERROR!!!",
}, nil))
},
// SOAP action
"foobar",
// Expected request
&testRequest{
Foo: "foo",
Bar: "bar",
},
// Response to reply
`<TestResponse xmlns="http://www/">
<TestResult>
<Ergebnis>42</Ergebnis>
<ErgebnisText>ERROR!!!</ErgebnisText>
<Pipo>hello</Pipo>
</TestResult>
</TestResponse>`,
"sendRequest app error")
}
// Login.
func TestLogin(tt *testing.T) {
t := td.NewT(tt)
type loginRequest struct {
AppID string `xml:"Body>Login>AppId"`
AppVersion string `xml:"Body>Login>AppVersion"`
Password string `xml:"Body>Login>Passwort"`
System string `xml:"Body>Login>Betriebssystem"`
Login string `xml:"Body>Login>Benutzer"`
}
expectedRequest := &loginRequest{
AppID: "prod",
AppVersion: "4.3.1",
Password: "bingo",
System: "Android",
Login: "pipo",
}
// No problem
testSendRequestAny(t,
// Send request and check result
func(v *Session) bool {
return t.CmpNoError(v.Login("pipo", "bingo"))
},
// SOAP action
"Login",
expectedRequest,
// Response to reply
`<LoginResponse xmlns="http://www.e-controlnet.de/services/vii/">
<LoginResult>
<Ergebnis>0</Ergebnis>
<ErgebnisText>Kein Fehler</ErgebnisText>
<TechVersion>2.5.6.0</TechVersion>
<Anrede>1</Anrede>
<Vorname>Maxime</Vorname>
<Nachname>Soulé</Nachname>
</LoginResult>
</LoginResponse>`,
"Login")
// With an error
testSendRequestAny(t,
// Send request and check result
func(v *Session) bool {
return t.CmpError(v.Login("pipo", "bingo"))
},
// SOAP action
"Login",
expectedRequest,
// Response to reply
`<bad XML>`,
"Login with error")
}
// GetDevices.
func TestGetDevices(tt *testing.T) {
t := td.NewT(tt)
type getDevicesRequest struct {
Dummy string `xml:"Body>GetDevices,omitempty"`
}
expectedRequest := &getDevicesRequest{}
// No problem
testSendRequestAny(t,
// Send request and check result
func(v *Session) bool {
err := v.GetDevices()
if !t.CmpNoError(err) {
return false
}
return t.CmpDeeply(v.Devices,
[]Device{
{
LocationID: 31456,
LocationName: "Paris",
DeviceID: 40213,
DeviceName: "VT 200 (HO1C)",
HasError: true,
IsConnected: true,
Attributes: map[AttrID]*Value{},
Timesheets: map[TimesheetID]map[string]TimeslotSlice{},
},
})
},
// SOAP action
"GetDevices",
expectedRequest,
// Response to reply
`<GetDevicesResponse xmlns="http://www.e-controlnet.de/services/vii/GetDevices">
<GetDevicesResult>
<Ergebnis>0</Ergebnis>
<ErgebnisText>Kein Fehler</ErgebnisText>
<AnlageListe>
<AnlageV2>
<AnlageId>31456</AnlageId>
<AnlageName>Paris</AnlageName>
<AnlageStandort>Paris</AnlageStandort>
<AnlageTyp />
<GeraeteListe>
<GeraetV2>
<GeraetId>40213</GeraetId>
<GeraetName>VT 200 (HO1C)</GeraetName>
<GeraetTyp>350</GeraetTyp>
<Heizkreise>
<BenutzerHeizkreis>
<HeizkreisId>19179</HeizkreisId>
<HeizkreisBezeichnung>viessmann.eventtypegroupHC.name.VScotHO1_72~HC1</HeizkreisBezeichnung>
<Benutzerfreigabe>true</Benutzerfreigabe>
</BenutzerHeizkreis>
</Heizkreise>
<ViaFreigabe>true</ViaFreigabe>
<Regelungstype>GWG</Regelungstype>
<Regelungsadresse>VScotHO1_72</Regelungsadresse>
<HatFehler>true</HatFehler>
<IstVerbunden>true</IstVerbunden>
</GeraetV2>
</GeraeteListe>
<VerbindungsTyp />
<HatFehler>false</HatFehler>
<IstVerbunden>true</IstVerbunden>
</AnlageV2>
</AnlageListe>
</GetDevicesResult>
</GetDevicesResponse>`,
"GetDevices")
// With an error
testSendRequestAny(t,
// Send request and check result
func(v *Session) bool {
return t.CmpError(v.GetDevices())
},
// SOAP action
"GetDevices",
expectedRequest,
// Response to reply
`<bad XML>`,
"GetDevices with error")
}
//
// RequestRefreshStatus
//
type requestRefreshStatusRequest struct {
AktualisierungsID string `xml:"Body>RequestRefreshStatus>AktualisierungsId"`
}
var requestRefreshStatusTest = testAction{
expectedRequest: &requestRefreshStatusRequest{
AktualisierungsID: "123456789",
},
serverResponse: `<RequestRefreshStatusResponse xmlns="http://www.e-controlnet.de/services/vii/">
<RequestRefreshStatusResult>
<Ergebnis>0</Ergebnis>
<ErgebnisText>Kein Fehler</ErgebnisText>
<Status>4</Status>
</RequestRefreshStatusResult>
</RequestRefreshStatusResponse>`,
}
func TestRequestRefreshStatus(tt *testing.T) {
t := td.NewT(tt)
// No problem
testSendRequestAny(t,
// Send request and check result
func(v *Session) bool {
status, err := v.RequestRefreshStatus("123456789")
return t.CmpNoError(err) && t.CmpDeeply(status, 4)
},
// SOAP action
"RequestRefreshStatus",
requestRefreshStatusTest.expectedRequest,
// Response to reply
requestRefreshStatusTest.serverResponse,
"RequestRefreshStatus")
// With an error
testSendRequestAny(t,
// Send request and check result
func(v *Session) bool {
_, err := v.RequestRefreshStatus("123456789")
return t.CmpError(err)
},
// SOAP action
"RequestRefreshStatus",
requestRefreshStatusTest.expectedRequest,
// Response to reply
`<bad XML>`,
"RequestRefreshStatus with error")
}
//
// RequestWriteStatus
//
type requestWriteStatusRequest struct {
AktualisierungsID string `xml:"Body>RequestWriteStatus>AktualisierungsId"`
}
var requestWriteStatusTest = testAction{
expectedRequest: &requestWriteStatusRequest{
AktualisierungsID: "123456789",
},
serverResponse: `<RequestWriteStatusResponse xmlns="http://www.e-controlnet.de/services/vii/">
<RequestWriteStatusResult>
<Ergebnis>0</Ergebnis>
<ErgebnisText>Kein Fehler</ErgebnisText>
<Status>4</Status>
</RequestWriteStatusResult>
</RequestWriteStatusResponse>`,
}
func TestRequestWriteStatus(tt *testing.T) {
t := td.NewT(tt)
// No problem
testSendRequestAny(t,
// Send request and check result
func(v *Session) bool {
status, err := v.RequestWriteStatus("123456789")
return t.CmpNoError(err) && t.CmpDeeply(status, 4)
},
// SOAP action
"RequestWriteStatus",
requestWriteStatusTest.expectedRequest,
// Response to reply
requestWriteStatusTest.serverResponse,
"RequestWriteStatus")
// With an error
testSendRequestAny(t,
// Send request and check result
func(v *Session) bool {
_, err := v.RequestWriteStatus("123456789")
return t.CmpError(err)
},
// SOAP action
"RequestWriteStatus",
requestWriteStatusTest.expectedRequest,
// Response to reply
`<bad XML>`,
"RequestWriteStatus with error")
}