11import * as fs from 'fs' ;
22import * as path from 'path' ;
3- import { parse } from '@asyncapi/parser ' ;
3+ import { CommonInputModel } from '../../src/models ' ;
44import { InputProcessor } from '../../src/processors/InputProcessor' ;
5+ import { JsonSchemaInputProcessor } from '../../src/processors/JsonSchemaInputProcessor' ;
6+ import { AsyncAPIInputProcessor } from '../../src/processors/AsyncAPIInputProcessor' ;
7+ import { AbstractInputProcessor } from '../../src/processors' ;
58
6- describe ( 'InputProcessor' , function ( ) {
7- /**
8- * The input schema when processed should be equals to the expected CommonInputModel
9- *
10- * @param inputSchemaPath
11- * @param expectedCommonModulePath
12- */
13- const expectFunction = async ( inputSchemaPath : string , expectedCommonModulePath : string ) => {
14- const processor = new InputProcessor ( ) ;
15- const inputSchemaString = fs . readFileSync ( path . resolve ( __dirname , inputSchemaPath ) , 'utf8' ) ;
16- const expectedCommonInputModelString = fs . readFileSync ( path . resolve ( __dirname , expectedCommonModulePath ) , 'utf8' ) ;
17- const inputSchema = JSON . parse ( inputSchemaString ) ;
18- const expectedCommonInputModel = JSON . parse ( expectedCommonInputModelString ) ;
19- const commonInputModel = await processor . process ( inputSchema ) ;
20- expect ( commonInputModel ) . toEqual ( expectedCommonInputModel ) ;
9+ describe ( 'InputProcessor' , function ( ) {
10+ beforeEach ( ( ) => {
11+ jest . resetAllMocks ( ) ;
12+ } ) ;
13+ afterAll ( ( ) => {
14+ jest . restoreAllMocks ( ) ;
15+ } ) ;
16+
17+ class TempProcessor extends AbstractInputProcessor {
18+ process ( input : any ) : Promise < CommonInputModel > { return Promise . resolve ( new CommonInputModel ( ) ) ; }
19+ shouldProcess ( input : any ) : boolean { return true ; }
20+ }
21+ test ( 'should add processor to map' , async function ( ) {
22+ const testProcessor = new TempProcessor ( ) ;
23+ const processor = new InputProcessor ( ) ;
24+ processor . setProcessor ( 'some_key' , testProcessor ) ;
25+ const foundProcessor = processor . getProcessors ( ) . get ( 'some_key' ) ;
26+ expect ( foundProcessor ) . toEqual ( testProcessor ) ;
27+ } ) ;
28+ test ( 'overwriting processor should use new and not old' , async function ( ) {
29+ const testProcessor = new TempProcessor ( ) ;
30+ const processor = new InputProcessor ( ) ;
31+ const oldDefaultProcessor = processor . getProcessors ( ) . get ( 'default' ) ;
32+ processor . setProcessor ( 'default' , testProcessor ) ;
33+ const currentDefaultProcessor = processor . getProcessors ( ) . get ( 'default' ) ;
34+ expect ( currentDefaultProcessor ?. constructor ) . not . toEqual ( oldDefaultProcessor ?. constructor ) ;
35+ expect ( oldDefaultProcessor ?. constructor ) . toEqual ( oldDefaultProcessor ?. constructor ) ;
36+ expect ( currentDefaultProcessor ?. constructor ) . toEqual ( currentDefaultProcessor ?. constructor ) ;
37+ } ) ;
38+ describe ( 'process()' , function ( ) {
39+ const getProcessors = ( ) => {
40+ const asyncInputProcessor = new AsyncAPIInputProcessor ( ) ;
41+ jest . spyOn ( asyncInputProcessor , 'shouldProcess' ) ;
42+ jest . spyOn ( asyncInputProcessor , 'process' ) ;
43+ const defaultInputProcessor = new JsonSchemaInputProcessor ( ) ;
44+ jest . spyOn ( defaultInputProcessor , 'shouldProcess' ) ;
45+ jest . spyOn ( defaultInputProcessor , 'process' ) ;
46+ const processor = new InputProcessor ( ) ;
47+ processor . setProcessor ( 'asyncapi' , asyncInputProcessor ) ;
48+ processor . setProcessor ( 'default' , defaultInputProcessor ) ;
49+ return { processor, asyncInputProcessor, defaultInputProcessor}
2150 }
22- describe ( 'process()' , function ( ) {
23- describe ( 'should be able to process JSON schema input' , function ( ) {
24- test ( 'with absence types' , async function ( ) {
25- const inputSchemaPath = './JsonSchemaInputProcessor/absence_type.json' ;
26- const expectedCommonModulePath = './JsonSchemaInputProcessor/commonInputModel/absence_type.json' ;
27- await expectFunction ( inputSchemaPath , expectedCommonModulePath ) ;
28- } ) ;
29- test ( 'with conditional schemas' , async function ( ) {
30- const inputSchemaPath = './JsonSchemaInputProcessor/applying_conditional_schemas.json' ;
31- const expectedCommonModulePath = './JsonSchemaInputProcessor/commonInputModel/applying_conditional_schemas.json' ;
32- await expectFunction ( inputSchemaPath , expectedCommonModulePath ) ;
33- } ) ;
34- test ( 'with combination schemas' , async function ( ) {
35- const inputSchemaPath = './JsonSchemaInputProcessor/combination_schemas.json' ;
36- const expectedCommonModulePath = './JsonSchemaInputProcessor/commonInputModel/combination_schemas.json' ;
37- await expectFunction ( inputSchemaPath , expectedCommonModulePath ) ;
38- } ) ;
39- test ( 'with enum schemas' , async function ( ) {
40- const inputSchemaPath = './JsonSchemaInputProcessor/enum.json' ;
41- const expectedCommonModulePath = './JsonSchemaInputProcessor/commonInputModel/enum.json' ;
42- await expectFunction ( inputSchemaPath , expectedCommonModulePath ) ;
43- } ) ;
44- test ( 'with items schemas' , async function ( ) {
45- const inputSchemaPath = './JsonSchemaInputProcessor/items.json' ;
46- const expectedCommonModulePath = './JsonSchemaInputProcessor/commonInputModel/items.json' ;
47- await expectFunction ( inputSchemaPath , expectedCommonModulePath ) ;
48- } ) ;
49- test ( 'with multiple objects' , async function ( ) {
50- const inputSchemaPath = './JsonSchemaInputProcessor/multiple_objects.json' ;
51- const expectedCommonModulePath = './JsonSchemaInputProcessor/commonInputModel/multiple_objects.json' ;
52- await expectFunction ( inputSchemaPath , expectedCommonModulePath ) ;
53- } ) ;
54- } ) ;
51+ test ( 'should throw error when no default processor found' , async function ( ) {
52+ const processor = new InputProcessor ( ) ;
53+ const map = processor . getProcessors ( ) ;
54+ map . delete ( 'default' ) ;
55+ await expect ( processor . process ( { } ) )
56+ . rejects
57+ . toThrow ( 'No default processor found' ) ;
58+ } ) ;
59+ test ( 'should be able to process default JSON schema input' , async function ( ) {
60+ const { processor, asyncInputProcessor, defaultInputProcessor} = getProcessors ( ) ;
61+ const inputSchemaString = fs . readFileSync ( path . resolve ( __dirname , './JsonSchemaInputProcessor/basic.json' ) , 'utf8' ) ;
62+ const inputSchema = JSON . parse ( inputSchemaString ) ;
63+ await processor . process ( inputSchema ) ;
64+ expect ( asyncInputProcessor . process ) . not . toHaveBeenCalled ( ) ;
65+ expect ( asyncInputProcessor . shouldProcess ) . toHaveBeenNthCalledWith ( 1 , inputSchema ) ;
66+ expect ( defaultInputProcessor . process ) . toHaveBeenNthCalledWith ( 1 , inputSchema ) ;
67+ expect ( defaultInputProcessor . shouldProcess ) . toHaveBeenNthCalledWith ( 1 , inputSchema ) ;
68+ } ) ;
5569
56- describe ( 'should be able to process AsyncAPI schema input' , function ( ) {
57- test ( 'with pure object' , async function ( ) {
58- const processor = new InputProcessor ( ) ;
59- const basicDocString = fs . readFileSync ( path . resolve ( __dirname , './AsyncAPIInputProcessor/basic.json' ) , 'utf8' ) ;
60- const expectedCommonInputModelString = fs . readFileSync ( path . resolve ( __dirname , './AsyncAPIInputProcessor/commonInputModel/basic.json' ) , 'utf8' ) ;
61- const basicDoc = JSON . parse ( basicDocString ) ;
62- const expectedCommonInputModel = JSON . parse ( expectedCommonInputModelString ) ;
63- const commonInputModel = await processor . process ( basicDoc ) ;
64- expect ( commonInputModel ) . toMatchObject ( expectedCommonInputModel ) ;
65- } ) ;
66- test ( 'with parsed document' , async function ( ) {
67- const processor = new InputProcessor ( ) ;
68- const basicDocString = fs . readFileSync ( path . resolve ( __dirname , './AsyncAPIInputProcessor/basic.json' ) , 'utf8' ) ;
69- const expectedCommonInputModelString = fs . readFileSync ( path . resolve ( __dirname , './AsyncAPIInputProcessor/commonInputModel/basic.json' ) , 'utf8' ) ;
70- const expectedCommonInputModel = JSON . parse ( expectedCommonInputModelString ) ;
71- const parsedObject = await parse ( basicDocString ) ;
72- const commonInputModel = await processor . process ( parsedObject ) ;
73- expect ( commonInputModel ) . toMatchObject ( expectedCommonInputModel ) ;
74- } ) ;
75- } ) ;
70+ test ( 'should be able to process AsyncAPI schema input' , async function ( ) {
71+ const { processor, asyncInputProcessor, defaultInputProcessor} = getProcessors ( ) ;
72+ const inputSchemaString = fs . readFileSync ( path . resolve ( __dirname , './AsyncAPIInputProcessor/basic.json' ) , 'utf8' ) ;
73+ const inputSchema = JSON . parse ( inputSchemaString ) ;
74+ await processor . process ( inputSchema ) ;
75+ expect ( asyncInputProcessor . process ) . toHaveBeenNthCalledWith ( 1 , inputSchema ) ;
76+ expect ( asyncInputProcessor . shouldProcess ) . toHaveBeenNthCalledWith ( 1 , inputSchema ) ;
77+ expect ( defaultInputProcessor . process ) . not . toHaveBeenCalled ( ) ;
78+ expect ( defaultInputProcessor . shouldProcess ) . not . toHaveBeenCalled ( ) ;
7679 } ) ;
80+ } ) ;
7781} ) ;
0 commit comments