-
-
Notifications
You must be signed in to change notification settings - Fork 186
/
InputProcessor.spec.ts
208 lines (200 loc) · 7.39 KB
/
InputProcessor.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
import * as fs from 'fs';
import * as path from 'path';
import { InputMetaModel, ProcessorOptions } from '../../src/models';
import {
AbstractInputProcessor,
AsyncAPIInputProcessor,
JsonSchemaInputProcessor,
InputProcessor,
SwaggerInputProcessor
} from '../../src/processors';
import { OpenAPIInputProcessor } from '../../src/processors/OpenAPIInputProcessor';
describe('InputProcessor', () => {
beforeEach(() => {
jest.resetAllMocks();
});
afterAll(() => {
jest.restoreAllMocks();
});
class TempProcessor extends AbstractInputProcessor {
process(input: any): Promise<InputMetaModel> {
return Promise.resolve(new InputMetaModel());
}
shouldProcess(input: any): boolean {
return true;
}
}
test('should add processor to map', () => {
const testProcessor = new TempProcessor();
const processor = new InputProcessor();
processor.setProcessor('some_key', testProcessor);
const processors = processor.getProcessors();
const foundProcessor = processors.get('some_key');
expect(foundProcessor).toEqual(testProcessor);
});
test('overwriting processor should use new and not old', () => {
const testProcessor = new TempProcessor();
const processor = new InputProcessor();
const oldDefaultProcessor = processor.getProcessors().get('default');
processor.setProcessor('default', testProcessor);
const currentDefaultProcessor = processor.getProcessors().get('default');
expect(currentDefaultProcessor?.constructor).not.toEqual(
oldDefaultProcessor?.constructor
);
expect(oldDefaultProcessor?.constructor).toEqual(
oldDefaultProcessor?.constructor
);
expect(currentDefaultProcessor?.constructor).toEqual(
currentDefaultProcessor?.constructor
);
});
describe('process()', () => {
const getProcessors = () => {
const asyncInputProcessor = new AsyncAPIInputProcessor();
jest.spyOn(asyncInputProcessor, 'shouldProcess');
jest.spyOn(asyncInputProcessor, 'process');
const defaultInputProcessor = new JsonSchemaInputProcessor();
jest.spyOn(defaultInputProcessor, 'shouldProcess');
jest.spyOn(defaultInputProcessor, 'process');
const swaggerInputProcessor = new SwaggerInputProcessor();
jest.spyOn(swaggerInputProcessor, 'shouldProcess');
jest.spyOn(swaggerInputProcessor, 'process');
const openAPIInputProcessor = new OpenAPIInputProcessor();
jest.spyOn(openAPIInputProcessor, 'shouldProcess');
jest.spyOn(openAPIInputProcessor, 'process');
const processor = new InputProcessor();
processor.setProcessor('asyncapi', asyncInputProcessor);
processor.setProcessor('swagger', swaggerInputProcessor);
processor.setProcessor('openapi', openAPIInputProcessor);
processor.setProcessor('default', defaultInputProcessor);
return {
processor,
asyncInputProcessor,
swaggerInputProcessor,
openAPIInputProcessor,
defaultInputProcessor
};
};
test('should throw error when no default processor found', async () => {
const processor = new InputProcessor();
const map = processor.getProcessors();
map.delete('default');
await expect(processor.process({})).rejects.toThrow(
'No default processor found'
);
});
test('should be able to process default JSON schema input', async () => {
const { processor, asyncInputProcessor, defaultInputProcessor } =
getProcessors();
const inputSchemaString = fs.readFileSync(
path.resolve(__dirname, './JsonSchemaInputProcessor/draft-7.json'),
'utf8'
);
const inputSchema = JSON.parse(inputSchemaString);
await processor.process(inputSchema);
expect(asyncInputProcessor.process).not.toHaveBeenCalled();
expect(asyncInputProcessor.shouldProcess).toHaveBeenNthCalledWith(
1,
inputSchema
);
expect(defaultInputProcessor.process).toHaveBeenNthCalledWith(
1,
inputSchema,
undefined
);
expect(defaultInputProcessor.shouldProcess).toHaveBeenNthCalledWith(
1,
inputSchema
);
});
test('should be able to process AsyncAPI schema input', async () => {
const { processor, asyncInputProcessor, defaultInputProcessor } =
getProcessors();
const inputSchemaString = fs.readFileSync(
path.resolve(__dirname, './AsyncAPIInputProcessor/basic.json'),
'utf8'
);
const inputSchema = JSON.parse(inputSchemaString);
await processor.process(inputSchema);
expect(asyncInputProcessor.process).toHaveBeenNthCalledWith(
1,
inputSchema,
undefined
);
expect(asyncInputProcessor.shouldProcess).toHaveBeenNthCalledWith(
1,
inputSchema
);
expect(defaultInputProcessor.process).not.toHaveBeenCalled();
expect(defaultInputProcessor.shouldProcess).not.toHaveBeenCalled();
});
test('should be able to process Swagger (OpenAPI 2.0) input', async () => {
const { processor, swaggerInputProcessor, defaultInputProcessor } =
getProcessors();
const inputSchemaString = fs.readFileSync(
path.resolve(__dirname, './SwaggerInputProcessor/basic.json'),
'utf8'
);
const inputSchema = JSON.parse(inputSchemaString);
await processor.process(inputSchema);
expect(swaggerInputProcessor.process).toHaveBeenNthCalledWith(
1,
inputSchema,
undefined
);
expect(swaggerInputProcessor.shouldProcess).toHaveBeenNthCalledWith(
1,
inputSchema
);
expect(defaultInputProcessor.process).not.toHaveBeenCalled();
expect(defaultInputProcessor.shouldProcess).not.toHaveBeenCalled();
});
test('should be able to process OpenAPI input', async () => {
const { processor, openAPIInputProcessor, defaultInputProcessor } =
getProcessors();
const inputSchemaString = fs.readFileSync(
path.resolve(__dirname, './OpenAPIInputProcessor/basic.json'),
'utf8'
);
const inputSchema = JSON.parse(inputSchemaString);
await processor.process(inputSchema);
expect(openAPIInputProcessor.process).toHaveBeenNthCalledWith(
1,
inputSchema,
undefined
);
expect(openAPIInputProcessor.shouldProcess).toHaveBeenNthCalledWith(
1,
inputSchema
);
expect(defaultInputProcessor.process).not.toHaveBeenCalled();
expect(defaultInputProcessor.shouldProcess).not.toHaveBeenCalled();
});
test('should be able to process AsyncAPI schema input with options', async () => {
const { processor, asyncInputProcessor, defaultInputProcessor } =
getProcessors();
const options: ProcessorOptions = {
asyncapi: {
source: 'test'
}
};
const inputSchemaString = fs.readFileSync(
path.resolve(__dirname, './AsyncAPIInputProcessor/basic.json'),
'utf8'
);
const inputSchema = JSON.parse(inputSchemaString);
await processor.process(inputSchema, options);
expect(asyncInputProcessor.process).toHaveBeenNthCalledWith(
1,
inputSchema,
options
);
expect(asyncInputProcessor.shouldProcess).toHaveBeenNthCalledWith(
1,
inputSchema
);
expect(defaultInputProcessor.process).not.toHaveBeenCalled();
expect(defaultInputProcessor.shouldProcess).not.toHaveBeenCalled();
});
});
});