-
Notifications
You must be signed in to change notification settings - Fork 1
/
common_tests.go
178 lines (162 loc) · 4.98 KB
/
common_tests.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
package audit
import (
"fmt"
"github.com/tarent/gomulocity/generic"
"net/http"
"net/http/httptest"
"time"
)
func buildAuditApi(url string) AuditApi {
httpClient := http.DefaultClient
client := &generic.Client{
HTTPClient: httpClient,
BaseURL: url,
Username: "foo",
Password: "bar",
}
return NewAuditApi(client)
}
func buildHttpServer(status int, body string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(status)
_, _ = w.Write([]byte(body))
}))
}
var t, _ = time.Parse(time.RFC3339, "2020-07-21T09:47:21.092Z")
var creationTime, _ = time.Parse(time.RFC3339, "2020-07-21T09:47:22.166Z")
var auditID = "100197821"
var testAuditRecord = &AuditRecord{
Severity: "MAJOR",
Activity: "Alarm created",
CreationTime: creationTime,
Source: struct {
Self string `json:"self"`
ID string `json:"id"`
}{
Self: "https://t200588189.cumulocity.com/inventory/managedObjects/10013234",
ID: "10013234",
},
Type: "Alarm",
Self: "https://t200588189.cumulocity.com/audit/auditRecords/" + auditID,
Time: t,
Text: "Device name: 'TestDevice123', alarm text: 'Alarm 1.3 occured'",
ID: auditID,
User: "gfa-agent",
Application: "Omniscape",
Changes: []Changes{
{
NewValue: "WARNING",
Attribute: "severity",
Type: "com.cumulocity.model.event.CumulocitySeverities",
PreviousValue: "MINOR",
},
},
AuditSourceDevice: struct {
ID string `json:"id"`
}{
ID: "3249962",
},
}
var testAuditRecordJSON = `
{
"id": "` + auditID + `",
"type": "Alarm",
"self": "https://t200588189.cumulocity.com/audit/auditRecords/` + auditID + `",
"time": "2020-07-21T09:47:21.092Z",
"text": "Device name: 'TestDevice123', alarm text: 'Alarm 1.3 occured'",
"user": "gfa-agent",
"source": {
"id": "10013234",
"self": "https://t200588189.cumulocity.com/inventory/managedObjects/10013234"
},
"changes": [
{
"type": "com.cumulocity.model.event.CumulocitySeverities",
"newValue": "WARNING",
"attribute": "severity",
"previousValue": "MINOR"
}
],
"severity": "MAJOR",
"activity": "Alarm created",
"application": "Omniscape",
"creationTime": "2020-07-21T09:47:22.166Z",
"com_cumulocity_model_event_AuditSourceDevice": {
"id": "3249962"
}
}
`
var testAuditRecords = &AuditRecordCollection{
Self: "https://t200588189.cumulocity.com/audit/auditRecords?pageSize=5¤tPage=2",
Next: "https://t200588189.cumulocity.com/audit/auditRecords?pageSize=5¤tPage=3",
Prev: "https://t200588189.cumulocity.com/audit/auditRecords?pageSize=5¤tPage=1",
AuditRecords: []AuditRecord{*testAuditRecord},
Statistics: &generic.PagingStatistics{
TotalPages: 1946,
PageSize: 5,
CurrentPage: 1,
},
}
var testAuditRecordsJSON = `
{
"self":"https://t200588189.cumulocity.com/audit/auditRecords?pageSize=5¤tPage=2",
"next":"https://t200588189.cumulocity.com/audit/auditRecords?pageSize=5¤tPage=3",
"prev":"https://t200588189.cumulocity.com/audit/auditRecords?pageSize=5¤tPage=1",
"auditRecords":[` + testAuditRecordJSON + `],
"statistics":{
"totalPages":1946,
"pageSize":5,
"currentPage":1
}
}
`
var testCreateAuditRecord = &AuditRecord{
Activity: "Alarm created",
Type: "Alarm",
Time: t,
Text: "Device name: 'TestDevice123', alarm text: 'Alarm 1.3 occured'",
}
var testCreateAuditRecordJSON = `
{
"activity":"Alarm created",
"type":"alarm",
"time":"2020-07-21T09:47:21.092Z",
"text":"Device name: 'TestDevice123', alarm text: 'Alarm 1.3 occured'"
}
`
var erroneousResponseJSON = `
{
"error": "general/internalError",
"info": "https://www.cumulocity.com/guides/reference-guide/#error_reporting"
}
`
var erroneousResponseCreateAuditJSON = `
{
"error": "undefined/validationError",
"message": "Following mandatory fields should be included: activity,type,time,text",
"info": "https://www.cumulocity.com/guides/reference-guide/#error_reporting"
}
`
var createErroneousResponse = func(status int, message string) *generic.Error {
return &generic.Error{
ErrorType: fmt.Sprintf("%v: general/internalError", status),
Message: message,
Info: "https://www.cumulocity.com/guides/reference-guide/#error_reporting",
}
}
var createErroneousResponseCreateAudit = func(status int) *generic.Error {
return &generic.Error{
ErrorType: fmt.Sprintf("%v: undefined/validationError", status),
Message: "Following mandatory fields should be included: activity,type,time,text",
Info: "https://www.cumulocity.com/guides/reference-guide/#error_reporting",
}
}
var auditCollectionTemplate = `{
"next": "https://t200588189.cumulocity.com/audit/auditRecords?pageSize=5¤tPage=2",
"self": "https://t200588189.cumulocity.com/audit/auditRecords?pageSize=5¤tPage=1",
"auditRecords": [%s],
"statistics": {
"currentPage": 1,
"pageSize": 5
}
}`