import { BaseTelemetryProperties , QueuedMessageProperties } from "../resources" ; import { LambdaSchemaValidator , BasePropertiesValidator } from "../schema" ; import assert from "assert" ; describe ( "LumberjackSchemaValidator" , ( ) => { it ( "Makes sure BasePropertiesValidator can use BaseLumberjackSchemaValidator's base functionality and validation passes." , async ( ) => { const validator = new BasePropertiesValidator ( ) ; const props = new Map < string , any > ( ) ; props . set ( BaseTelemetryProperties . tenantId , "testTenant" ) ; props . set ( BaseTelemetryProperties . documentId , "testDocument" ) ; const result = validator . validate ( props ) ; assert . strictEqual ( result . validationPassed , true ) ; assert . strictEqual ( result . validationFailedForProperties . length , 0 ) ; } ) ; it ( "Makes sure BasePropertiesValidator validation fails due to missing properties on the object being validated." , async ( ) => { const validator = new BasePropertiesValidator ( ) ; const props = new Map < string , any > ( ) ; props . set ( BaseTelemetryProperties . tenantId , undefined ) ; const result = validator . validate ( props ) ; assert . strictEqual ( result . validationPassed , false ) ; assert . strictEqual ( result . validationFailedForProperties . length , 1 ) ; assert . strictEqual ( result . validationFailedForProperties . indexOf ( "tenantId" ) , - 1 ) ; assert . notStrictEqual ( result . validationFailedForProperties . indexOf ( "documentId" ) , - 1 ) ; } ) ; it ( "Makes sure BasePropertiesValidator validation fails due to incorrect data types and values." , async ( ) => { const validator = new BasePropertiesValidator ( ) ; const props = new Map < string , any > ( ) ; props . set ( BaseTelemetryProperties . tenantId , "" ) ; props . set ( BaseTelemetryProperties . documentId , 5 ) ; const result = validator . validate ( props ) ; assert . strictEqual ( result . validationPassed , false ) ; assert . strictEqual ( result . validationFailedForProperties . length , 2 ) ; assert . notStrictEqual ( result . validationFailedForProperties . indexOf ( "tenantId" ) , - 1 ) ; assert . notStrictEqual ( result . validationFailedForProperties . indexOf ( "documentId" ) , - 1 ) ; } ) ; it ( "Makes sure LambdaSchemaValidator can use BasePropertiesValidator's base functionality and validation passes." , async ( ) => { const validator = new LambdaSchemaValidator ( ) ; const props = new Map < string , any > ( ) ; props . set ( BaseTelemetryProperties . tenantId , "testTenant" ) ; props . set ( BaseTelemetryProperties . documentId , "testDocument" ) ; props . set ( QueuedMessageProperties . topic , "testClient" ) ; props . set ( QueuedMessageProperties . partition , 1 ) ; props . set ( QueuedMessageProperties . offset , 2 ) ; const result = validator . validate ( props ) ; assert . strictEqual ( result . validationPassed , true ) ; assert . strictEqual ( result . validationFailedForProperties . length , 0 ) ; } ) ; it ( "Makes sure LambdaSchemaValidator validation fails if BasePropertiesValidator's required properties are not present." , async ( ) => { const validator = new LambdaSchemaValidator ( ) ; const props = new Map < string , any > ( ) ; props . set ( QueuedMessageProperties . topic , "testClient" ) ; props . set ( QueuedMessageProperties . partition , 1 ) ; props . set ( QueuedMessageProperties . offset , undefined ) ; const result = validator . validate ( props ) ; assert . strictEqual ( result . validationPassed , false ) ; assert . strictEqual ( result . validationFailedForProperties . length , 2 ) ; assert . notStrictEqual ( result . validationFailedForProperties . indexOf ( "tenantId" ) , - 1 ) ; assert . notStrictEqual ( result . validationFailedForProperties . indexOf ( "documentId" ) , - 1 ) ; } ) ; it ( "Makes sure LambdaSchemaValidator validation fails if properties are missing or types/values are incorrect." , async ( ) => { const validator = new LambdaSchemaValidator ( ) ; const props = new Map < string , any > ( ) ; props . set ( BaseTelemetryProperties . tenantId , "testTenant" ) ; props . set ( BaseTelemetryProperties . documentId , "testDocument" ) ; props . set ( QueuedMessageProperties . partition , - 4 ) ; props . set ( QueuedMessageProperties . offset , "test" ) ; const result = validator . validate ( props ) ; assert . strictEqual ( result . validationPassed , false ) ; assert . strictEqual ( result . validationFailedForProperties . length , 3 ) ; assert . notStrictEqual ( result . validationFailedForProperties . indexOf ( "topic" ) , - 1 ) ; assert . notStrictEqual ( result . validationFailedForProperties . indexOf ( "partition" ) , - 1 ) ; assert . notStrictEqual ( result . validationFailedForProperties . indexOf ( "offset" ) , - 1 ) ; } ) ; } ) ;