forked from asyncapi/parser-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply-traits-v3.spec.ts
353 lines (334 loc) · 12.2 KB
/
apply-traits-v3.spec.ts
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
import { xParserObjectUniqueId } from '../../src/constants';
import { AsyncAPIDocumentV3 } from '../../src/models';
import { Parser } from '../../src/parser';
import type { v3 } from '../../src/spec-types';
describe('custom operations - apply traits v3', function() {
const parser = new Parser();
it('should apply traits to operations', async function() {
const documentRaw = {
asyncapi: '3.0.0',
info: {
title: 'Valid AsyncApi document',
version: '1.0',
},
channels: {
channel1: {}
},
operations: {
someOperation1: {
action: 'send',
channel: {
$ref: '#/channels/channel1'
},
traits: [
{
description: 'some description'
},
{
description: 'another description'
}
]
},
someOperation2: {
action: 'send',
channel: {
$ref: '#/channels/channel1'
},
description: 'root description',
traits: [
{
description: 'some description'
},
{
description: 'another description'
}
]
}
}
};
const { document, diagnostics } = await parser.parse(documentRaw);
expect(diagnostics).toHaveLength(0);
const v3Document = document as AsyncAPIDocumentV3;
expect(v3Document).toBeInstanceOf(AsyncAPIDocumentV3);
const someOperation1 = v3Document?.json()?.operations?.someOperation1 as v3.OperationObject;
delete someOperation1?.traits;
expect(someOperation1).toEqual({ action: 'send', channel: { 'x-parser-unique-object-id': 'channel1' }, description: 'another description', 'x-parser-unique-object-id': 'someOperation1' });
const someOperation2 = v3Document?.json()?.operations?.someOperation2 as v3.OperationObject;
delete someOperation2?.traits;
expect(someOperation2).toEqual({ action: 'send', channel: { 'x-parser-unique-object-id': 'channel1' }, description: 'root description', 'x-parser-unique-object-id': 'someOperation2' });
});
it('should apply traits to messages (channels)', async function() {
const documentRaw = {
asyncapi: '3.0.0',
info: {
title: 'Valid AsyncApi document',
version: '1.0',
},
channels: {
someChannel1: {
messages: {
someMessage: {
traits: [
{
summary: 'some summary',
description: 'some description'
},
{
description: 'another description'
}
]
}
}
},
someChannel2: {
messages: {
someMessage: {
summary: 'root summary',
description: 'root description',
traits: [
{
summary: 'some summary',
description: 'some description'
},
{
description: 'another description'
}
]
}
}
}
}
};
const { diagnostics, document } = await parser.parse(documentRaw);
const v3Document = document as AsyncAPIDocumentV3;
expect(v3Document).toBeInstanceOf(AsyncAPIDocumentV3);
const message1 = (v3Document?.json()?.channels?.someChannel1 as v3.ChannelObject).messages?.someMessage;
delete (message1 as v3.MessageObject)?.traits;
expect(message1).toEqual({ summary: 'some summary', description: 'another description', 'x-parser-message-name': 'someMessage', 'x-parser-unique-object-id': 'someMessage' });
const message2 = (v3Document?.json()?.channels?.someChannel2 as v3.ChannelObject).messages?.someMessage;
delete (message2 as v3.MessageObject)?.traits;
expect(message2).toEqual({ summary: 'root summary', description: 'root description', 'x-parser-message-name': 'someMessage', 'x-parser-unique-object-id': 'someMessage' });
});
it('should apply traits to messages (components)', async function() {
const documentRaw = {
asyncapi: '3.0.0',
info: {
title: 'Valid AsyncApi document',
version: '1.0',
},
components: {
messages: {
someMessage1: {
traits: [
{
summary: 'some summary',
description: 'some description'
},
{
description: 'another description'
}
]
},
someMessage2: {
summary: 'root summary',
description: 'root description',
traits: [
{
summary: 'some summary',
description: 'some description'
},
{
description: 'another description'
}
]
}
}
}
};
const { document } = await parser.parse(documentRaw);
const v3Document = document as AsyncAPIDocumentV3;
expect(v3Document).toBeInstanceOf(AsyncAPIDocumentV3);
const message1 = v3Document?.json()?.components?.messages?.someMessage1;
delete (message1 as v3.MessageObject)?.traits;
expect(message1).toEqual({ summary: 'some summary', description: 'another description', 'x-parser-message-name': 'someMessage1', 'x-parser-unique-object-id': 'someMessage1' });
const message2 = v3Document?.json()?.components?.messages?.someMessage2;
delete (message2 as v3.MessageObject)?.traits;
expect(message2).toEqual({ summary: 'root summary', description: 'root description', 'x-parser-message-name': 'someMessage2', 'x-parser-unique-object-id': 'someMessage2' });
});
describe('iterative functions should still work after traits have been applied', function() {
const documentRaw = {
asyncapi: '3.0.0',
info: {
title: 'Streetlights Kafka API',
version: '1.0.0',
description: 'The Smartylighting Streetlights API allows you to remotely manage the city lights.\n\n### Check out its awesome features:\n\n* Turn a specific streetlight on/off 🌃\n* Dim a specific streetlight 😎\n* Receive real-time information about environmental lighting conditions 📈\n',
license: {
name: 'Apache 2.0',
url: 'https://www.apache.org/licenses/LICENSE-2.0'
}
},
defaultContentType: 'application/json',
channels: {
'smartylighting.streetlights.1.0.event.{streetlightId}.lighting.measured': {
address: 'smartylighting.streetlights.1.0.event.{streetlightId}.lighting.measured',
messages: {
lightMeasured: {
$ref: '#/components/messages/lightMeasured'
}
},
description: 'The topic on which measured values may be produced and consumed.',
parameters: {
streetlightId: {
$ref: '#/components/parameters/streetlightId'
}
}
}
},
operations: {
receiveLightMeasurement: {
action: 'receive',
channel: {
$ref: '#/channels/smartylighting.streetlights.1.0.event.{streetlightId}.lighting.measured'
},
summary: 'Inform about environmental lighting conditions of a particular streetlight.',
traits: [
{
$ref: '#/components/operationTraits/kafka'
}
],
messages: [
{
$ref: '#/channels/smartylighting.streetlights.1.0.event.{streetlightId}.lighting.measured/messages/lightMeasured'
}
]
}
},
components: {
messages: {
lightMeasured: {
name: 'lightMeasured',
title: 'Light measured',
summary: 'Inform about environmental lighting conditions of a particular streetlight.',
contentType: 'application/json',
traits: [
{
$ref: '#/components/messageTraits/commonHeaders'
}
],
payload: {
$ref: '#/components/schemas/lightMeasuredPayload'
}
}
},
schemas: {
lightMeasuredPayload: {
type: 'object',
properties: {
lumens: {
type: 'integer',
minimum: 0,
description: 'Light intensity measured in lumens.'
},
sentAt: {
$ref: '#/components/schemas/sentAt'
}
}
},
sentAt: {
type: 'string',
format: 'date-time',
description: 'Date and time when the message was sent.'
}
},
parameters: {
streetlightId: {
description: 'The ID of the streetlight.'
}
},
messageTraits: {
commonHeaders: {
headers: {
type: 'object',
properties: {
'my-app-header': {
type: 'integer',
minimum: 0,
maximum: 100
}
}
}
}
},
operationTraits: {
kafka: {
bindings: {
kafka: {
clientId: {
type: 'string',
enum: [
'my-app-id'
]
}
}
}
}
}
}
};
let v3Document: AsyncAPIDocumentV3;
const expectedOperationId = 'receiveLightMeasurement';
const expectedChannelId = 'smartylighting.streetlights.1.0.event.{streetlightId}.lighting.measured';
const expectedMessageId = 'lightMeasured';
beforeAll(async () => {
const { document, diagnostics } = await parser.parse(documentRaw);
expect(diagnostics.length).toEqual(0);
v3Document = document as AsyncAPIDocumentV3;
});
it('should be able to go from operation -> channel', () => {
const operations = v3Document.operations().all();
expect(operations.length).toEqual(1);
const operation = operations[0];
expect(operation.id()).toEqual(expectedOperationId);
const operationChannels = operation.channels().all();
expect(operationChannels.length).toEqual(1);
const lightMeasuredChannel = operationChannels[0];
expect(lightMeasuredChannel.json()[xParserObjectUniqueId]).toEqual(expectedChannelId);
const messages = lightMeasuredChannel.messages().all();
expect(messages.length).toEqual(1);
const message = messages[0];
expect(message.id()).toEqual(expectedMessageId);
});
it('should be able to go from channel -> operation', () => {
const channels = v3Document.channels().all();
expect(channels.length).toEqual(1);
const channel = channels[0];
expect(channel.json()[xParserObjectUniqueId]).toEqual(expectedChannelId);
const channelOperations = channel.operations().all();
expect(channelOperations.length).toEqual(1);
const operation = channelOperations[0];
expect(operation.id()).toEqual(expectedOperationId);
const messages = operation.messages().all();
expect(messages.length).toEqual(1);
const message = messages[0];
expect(message.id()).toEqual(expectedMessageId);
});
it('should be able to go in full circle operation -> channel -> operation', () => {
const operations = v3Document.operations().all();
expect(operations.length).toEqual(1);
const operation = operations[0];
expect(operation.id()).toEqual(expectedOperationId);
const operationChannels = operation.channels().all();
expect(operationChannels.length).toEqual(1);
const lightMeasuredChannel = operationChannels[0];
expect(lightMeasuredChannel.json()[xParserObjectUniqueId]).toEqual(expectedChannelId);
const channelOperations = lightMeasuredChannel.operations().all();
expect(channelOperations.length).toEqual(1);
const circularOperation = channelOperations[0];
expect(circularOperation.id()).toEqual(expectedOperationId);
const messages = circularOperation.messages().all();
expect(messages.length).toEqual(1);
const message = messages[0];
expect(message.id()).toEqual(expectedMessageId);
});
});
});