-
Notifications
You must be signed in to change notification settings - Fork 30
/
integration.test.js
196 lines (167 loc) · 7.67 KB
/
integration.test.js
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
/* eslint-disable sonarjs/no-duplicate-string */
const path = require('path');
const Generator = require('@asyncapi/generator');
const { readFile } = require('fs').promises;
const MAIN_TEST_RESULT_PATH = path.join('test', 'temp', 'integrationTestResult');
const generateFolderName = () => {
// you always want to generate to new directory to make sure test runs in clear environment
return path.resolve(MAIN_TEST_RESULT_PATH, Date.now().toString());
};
describe('template integration tests for generated files using the generator and mqtt example - v2 spec', () => {
jest.setTimeout(30000);
it.each`
server | description
${'production'} | ${'should generate proper handlers and routes files'}
${'production-mqtts'} | ${'should use mqtt logic for mqtts protocol'}
`(
'$description',
async ({ server}) => {
const outputDir = generateFolderName();
const params = {
server
};
const mqttExamplePath = './mocks/mqtt/asyncapi.yml';
const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
await generator.generateFromFile(path.resolve('test', mqttExamplePath));
const expectedFiles = [
'/src/api/handlers/smartylighting-streetlights-1-0-action-{streetlightId}-dim.js',
'/src/api/handlers/smartylighting-streetlights-1-0-action-{streetlightId}-turn-off.js',
'/src/api/handlers/smartylighting-streetlights-1-0-action-{streetlightId}-turn-on.js',
'/src/api/handlers/smartylighting-streetlights-1-0-event-{streetlightId}-lighting-measured.js',
'/src/api/routes/smartylighting-streetlights-1-0-action-{streetlightId}-dim.js',
'/src/api/routes/smartylighting-streetlights-1-0-action-{streetlightId}-turn-off.js',
'/src/api/routes/smartylighting-streetlights-1-0-action-{streetlightId}-turn-on.js',
'/src/api/routes/smartylighting-streetlights-1-0-event-{streetlightId}-lighting-measured.js',
'/src/api/index.js',
'/config/common.yml',
'/package.json'
];
for (const index in expectedFiles) {
const file = await readFile(path.join(outputDir, expectedFiles[index]), 'utf8');
expect(file).toMatchSnapshot();
}
}
);
});
describe('template integration tests for generated files using the generator and kafka example - v2 spec', () => {
jest.setTimeout(30000);
const outputDir = generateFolderName();
const params = {
server: 'test',
securityScheme: 'certs',
certFilesDir: './mocks/kafka/dummyCerts'
};
const kafkaExamplePath = './mocks/kafka/asyncapi.yml';
it('should generate proper config for X509 security', async() => {
const expectedSecuritySetting = 'rejectUnauthorized: true';
const expectedConfigFile = '/config/common.yml';
const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
await generator.generateFromFile(path.resolve('test', kafkaExamplePath));
const file = await readFile(path.join(outputDir, expectedConfigFile), 'utf8');
expect(file.includes(expectedSecuritySetting)).toBeTruthy();
});
it('should generate proper variable that points to custom cert files location', async() => {
const expectedVariable = 'const certFilesDir = \'./mocks/kafka/dummyCerts\';';
const expectedFile = '/src/api/index.js';
const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
await generator.generateFromFile(path.resolve('test', kafkaExamplePath));
const file = await readFile(path.join(outputDir, expectedFile), 'utf8');
expect(file.includes(expectedVariable)).toBeTruthy();
});
});
describe('template integration tests for generated files using the generator and mqtt example - v3 spec', () => {
jest.setTimeout(30000);
it.each`
server | description
${'production'} | ${'should generate proper handlers and routes files'}
`(
'$description',
async ({ server}) => {
const outputDir = generateFolderName();
const params = {
server
};
const mqttExamplePath = './mocks/mqtt/asyncapi-v3.yml';
const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
await generator.generateFromFile(path.resolve('test', mqttExamplePath));
const expectedFiles = [
'/src/api/handlers/lightTurnOff.js',
'/src/api/handlers/lightingMeasured.js',
'/src/api/handlers/lightTurnOn.js',
'/src/api/handlers/lightsDim.js',
'/src/api/routes/lightTurnOff.js',
'/src/api/routes/lightingMeasured.js',
'/src/api/routes/lightTurnOn.js',
'/src/api/routes/lightsDim.js',
'/src/api/index.js',
'/config/common.yml',
'/package.json'
];
for (const index in expectedFiles) {
const file = await readFile(path.join(outputDir, expectedFiles[index]), 'utf8');
expect(file).toMatchSnapshot();
}
}
);
});
describe('template integration tests for generated files using the generator and kafka example - v3 spec', () => {
jest.setTimeout(30000);
const outputDir = generateFolderName();
const kafkaExamplePath = './mocks/kafka/asyncapi-v3.yml';
it.each`
server | description
${'scram-connections'} | ${'should generate proper handlers and routes files'}
`(
'$description',
async ({ server}) => {
const params = {
server
};
const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
await generator.generateFromFile(path.resolve('test', kafkaExamplePath));
const expectedFiles = [
'/src/api/handlers/lightTurnOff.js',
'/src/api/handlers/lightingMeasured.js',
'/src/api/handlers/lightTurnOn.js',
'/src/api/handlers/lightsDim.js',
'/src/api/routes/lightTurnOff.js',
'/src/api/routes/lightingMeasured.js',
'/src/api/routes/lightTurnOn.js',
'/src/api/routes/lightsDim.js',
'/src/api/index.js',
'/config/common.yml',
'/package.json'
];
for (const index in expectedFiles) {
const file = await readFile(path.join(outputDir, expectedFiles[index]), 'utf8');
expect(file).toMatchSnapshot();
}
}
);
it('should generate proper config for X509 security', async() => {
const params = {
server: 'scram-connections',
securityScheme: 'certs',
certFilesDir: './mocks/kafka/dummyCerts'
};
const expectedSecuritySetting = 'rejectUnauthorized: true';
const expectedConfigFile = '/config/common.yml';
const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
await generator.generateFromFile(path.resolve('test', kafkaExamplePath));
const file = await readFile(path.join(outputDir, expectedConfigFile), 'utf8');
expect(file.includes(expectedSecuritySetting)).toBeTruthy();
});
it('should generate proper variable that points to custom cert files location', async() => {
const params = {
server: 'scram-connections',
securityScheme: 'certs',
certFilesDir: './mocks/kafka/dummyCerts'
};
const expectedVariable = 'const certFilesDir = \'./mocks/kafka/dummyCerts\';';
const expectedFile = '/src/api/index.js';
const generator = new Generator(path.normalize('./'), outputDir, { forceWrite: true, templateParams: params });
await generator.generateFromFile(path.resolve('test', kafkaExamplePath));
const file = await readFile(path.join(outputDir, expectedFile), 'utf8');
expect(file.includes(expectedVariable)).toBeTruthy();
});
});