-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon-example-data.js
More file actions
164 lines (147 loc) · 6.37 KB
/
Copy pathcommon-example-data.js
File metadata and controls
164 lines (147 loc) · 6.37 KB
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
/*
* Copyright 2018-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict'
/**
* Common-Example-Data:
* Utility module to export some useful data to use in examples.
*/
// strict-related options for ce creation
const ceOptionsNoStrict = { strict: false } // same as default in ce
const ceOptionsStrict = { strict: true }
// strict-related options for ce validation
const valOptionsNoOverride = { strict: null } // same as default in validator
const valOptionsNoStrict = { strict: false }
const valOptionsStrict = { strict: true }
// other general ce validation (and others) related options
const valOnlyValidAllInstance = { onlyValid: false } // all instances, valid and not, default
const valOnlyValidInstance = { onlyValid: true } // only valid instances
const valDebugInfoDisable = { printDebugInfo: false } // default
const valDebugInfoEnable = { printDebugInfo: true }
const valExcludeExtensionsDisable = { skipExtensions: false } // default
const valExcludeExtensionsEnable = { skipExtensions: true }
// define some common attributes
const commonEventTime = new Date()
const fixedEventTime = { time: commonEventTime } // set a fixed value, to use mainly in tests
const ceCommonOptions = {
// time: new Date(), // same as default
// time: commonEventTime, // to simplify tests, keep it with a fixed value here
time: null, // more useful here, like in normal situations where event timestamp has to be created each time
datacontenttype: 'application/json',
dataschema: 'http://my-schema.localhost.localdomain/v1/',
subject: 'subject',
...ceOptionsNoStrict // same as default in ce
}
const ceCommonOptionsStrict = { ...ceCommonOptions, ...ceOptionsStrict }
const ceCommonOptionsWithFixedTime = { ...ceCommonOptions, ...fixedEventTime }
const ceCommonOptionsWithFixedTimeStrict = { ...ceCommonOptionsStrict, ...fixedEventTime }
const ceCommonOptionsForTextData = { ...ceCommonOptions, datacontenttype: 'text/plain' }
const ceCommonOptionsForTextDataStrict = { ...ceCommonOptionsForTextData, ...ceOptionsStrict }
const ceCommonOptionsForTextDataWithFixedTime = { ...ceCommonOptionsForTextData, ...fixedEventTime }
const ceCommonOptionsForXMLData = { ...ceCommonOptions, datacontenttype: 'application/xml' }
const ceCommonOptionsForXMLDataStrict = { ...ceCommonOptionsForXMLData, ...ceOptionsStrict }
const ceCommonOptionsForXMLWithFixedTime = { ...ceCommonOptionsForXMLData, ...fixedEventTime }
const ceCommonExtensions = { exampleextension: 'value' } // example extension
const ceReservedExtensions = { id: -1, data: 'data attribute in extension' } // example (bad) extension, use a standard property in extensions, not good for creation in strict mode
const ceNamespace = 'com.github.smartiniOnGitHub.cloudeventjs.testevent-v1.0.0'
const ceServerUrl = '/test'
const ceCommonData = { hello: 'world', year: 2020, enabled: true }
const ceDataAsJSONString = JSON.stringify(ceCommonData) // same as = '{ "hello": "world", "year": 2020, "enabled": true }'
const ceDataAsString = 'Hello World, 2020'
const ceDataAsStringEncoded = 'SGVsbG8gV29ybGQsIDIwMjA='
const ceDataXMLAsString = '<data "hello"="world" "year"="2020" />'
const ceOptionsWithDataInBase64 = { ...ceCommonOptions, datainbase64: ceDataAsStringEncoded }
const ceDataNested = {
...ceCommonData,
nested1: {
level1attribute: 'level1attributeValue',
nested2: {
level2attribute: 'level2attributeValue',
nested3: {
level3attribute: 'level3attributeValue'
}
}
}
}
// ----
const ceCommonOptionsWithSomeOptionalsNull = {
time: commonEventTime, // to simplify tests, keep it with a fixed value here
datacontenttype: null,
dataschema: null,
subject: null,
...ceOptionsNoStrict
}
const ceCommonOptionsWithAllOptionalsNull = { ...ceCommonOptionsWithSomeOptionalsNull, time: null }
const ceCommonOptionsWithSomeOptionalsNullStrict = { ...ceCommonOptionsWithSomeOptionalsNull, ...ceOptionsStrict }
const ceCommonOptionsWithAllOptionalsNullStrict = { ...ceCommonOptionsWithAllOptionalsNull, ...ceOptionsStrict }
const ceCommonExtensionsWithNullValue = { exampleextension: null }
const ceExtensionStrict = { strictvalidation: true }
const ceMapData = new Map() // empty Map
ceMapData.set('key-1', 'value 1')
ceMapData.set('key-2', 'value 2')
const ceArrayData = [null, 'value 1', 'value 2', 'value 3'] // set even one item as null
// sample function to calculate a random string (given the length), to use in tests here
function getRandomString (length) {
let str = Math.random().toString(36).substring(2)
while (str.length < length) {
str += Math.random().toString(36).substring(2)
}
return str.substring(0, length)
}
module.exports = {
ceArrayData,
ceCommonData,
ceCommonExtensions,
ceCommonExtensionsWithNullValue,
ceCommonOptions,
ceCommonOptionsForTextData,
ceCommonOptionsForTextDataStrict,
ceCommonOptionsForTextDataWithFixedTime,
ceCommonOptionsForXMLData,
ceCommonOptionsForXMLDataStrict,
ceCommonOptionsForXMLWithFixedTime,
ceCommonOptionsStrict,
ceCommonOptionsWithAllOptionalsNull,
ceCommonOptionsWithAllOptionalsNullStrict,
ceCommonOptionsWithFixedTime,
ceCommonOptionsWithFixedTimeStrict,
ceCommonOptionsWithSomeOptionalsNull,
ceCommonOptionsWithSomeOptionalsNullStrict,
ceDataAsJSONString,
ceDataAsString,
ceDataAsStringEncoded,
ceDataNested,
ceDataXMLAsString,
ceExtensionStrict,
ceMapData,
ceNamespace,
ceOptionsNoStrict,
ceOptionsStrict,
ceOptionsWithDataInBase64,
ceReservedExtensions,
ceServerUrl,
commonEventTime,
fixedEventTime,
getRandomString,
valDebugInfoDisable,
valDebugInfoEnable,
valExcludeExtensionsDisable,
valExcludeExtensionsEnable,
valOnlyValidAllInstance,
valOnlyValidInstance,
valOptionsNoOverride,
valOptionsNoStrict,
valOptionsStrict
}