/**
 * Test suite for Sensors Things API
 */ import { Components , Configuration , Container , CoreTypes , DiscoverEvent } from "../.." ; import { SensorThingsTypes } from "../../sensor-things" ; import * as mocks from "./sensor-things.mock" ; import { SensorThingsCollection } from "./sensor-things.mock" ; import { delayAction , Spy } from "./utils" ; describe ( "Sensor things" , ( ) => { const TEST_TIMEOUT = 10000 ; const SENSOR_THINGS_TYPES_SET = [ SensorThingsTypes . OBJECT_TYPE_FEATURE_OF_INTEREST , SensorThingsTypes . OBJECT_TYPE_OBSERVATION , SensorThingsTypes . OBJECT_TYPE_SENSOR , SensorThingsTypes . OBJECT_TYPE_THING , ] ; describe ( "Communication" , ( ) => { const components1 = { controllers : { MockReceiverController : mocks . MockReceiverController , } , } ; const configuration1 = { communication : { shouldAutoStart : true , binding : global [ "test_binding" ] , } , } ; const components2 = { controllers : { MockEmitterController : mocks . MockEmitterController , } , } ; const responseDelay = 1000 ; const configuration2 = { communication : { shouldAutoStart : true , binding : global [ "test_binding" ] , } , controllers : { MockEmitterController : { name : "MockEmitterController1" , responseDelay : responseDelay , } , } , } ; const components3 = { controllers : { MockEmitterController : mocks . MockEmitterController , } , } ; const configuration3 = { communication : { shouldAutoStart : true , binding : global [ "test_binding" ] , } , controllers : { MockEmitterController : { name : "MockEmitterController2" , responseDelay : responseDelay , } , } , } ; let container1 ; let container2 ; let container3 ; beforeAll ( done => { Spy . reset ( ) ; container2 = Container . resolve ( components2 , configuration2 ) ; container3 = Container . resolve ( components3 , configuration3 ) ; delayAction ( 1000 , done , ( ) => { container1 = Container . resolve ( components1 , configuration1 ) ; } ) ; } ) ; afterAll ( done => { container1 . shutdown ( ) ; container2 . shutdown ( ) ; container3 . shutdown ( ) ; Spy . reset ( ) ; delayAction ( 1000 , done , ( ) => { } ) ; } , TEST_TIMEOUT ) ; it ( "All sensorThings objects can be discovered" , ( done ) => { const testFunction = ( elementArray ) => { if ( elementArray . length === 0 ) { done ( ) ; return ; } const element = elementArray . shift ( ) ; expect ( ( ) => container2 . communicationManager . publishDiscover ( DiscoverEvent . withObjectTypes ( [ element ] ) ) ) . not . toThrow ( ) ; testFunction ( elementArray ) ; } ; testFunction ( SENSOR_THINGS_TYPES_SET . slice ( ) ) ; } ) ; it ( "All sensorThings objects can be advertised and received" , ( done ) => { note const testFunction = ( elementArray ) => { if ( elementArray . length === 0 ) { done ( ) ; return ; } const element = elementArray . shift ( ) ; const logger = { count : 0 , eventData : [ ] , } ; const eventCount = 5 ; const receiverController = container1 . getController < mocks . MockReceiverController > ( "MockReceiverController" ) ; receiverController . watchForAdvertiseEvents ( logger , element ) ; delayAction ( 500 , undefined , ( ) => { container2 . getController < mocks . MockEmitterController > ( "MockEmitterController" ) . publishAdvertiseEvents ( eventCount , element ) ; delayAction ( 2000 , undefined , ( ) => { expect ( logger . count ) . toBe ( eventCount ) ; expect ( logger . eventData . length ) . toBe ( eventCount ) ; for ( let i = 1 ; i <= eventCount ; i ++ ) { expect ( logger . eventData [ i - 1 ] . object . name ) . toBe ( "Advertised_" + i ) ; } testFunction ( elementArray ) ; } ) ; } ) ; } ; testFunction ( SENSOR_THINGS_TYPES_SET . slice ( ) ) ; } , 15 * TEST_TIMEOUT ) ; it ( "All sensorThings objects can be received through Channel" , ( done ) => { note const testFunction = ( elementArray ) => { if ( elementArray . length === 0 ) { done ( ) ; return ; } const element = elementArray . shift ( ) ; const logger = { count : 0 , eventData : [ ] , } ; const eventCount = 4 ; const channelId = "42" ; const receiverController = container1 . getController < mocks . MockReceiverController > ( "MockReceiverController" ) ; receiverController . watchForChannelEvents ( logger , channelId ) ; delayAction ( 500 , undefined , ( ) => { container2 . getController < mocks . MockEmitterController > ( "MockEmitterController" ) . publishChannelEvents ( eventCount , element , channelId ) ; delayAction ( 1500 , undefined , ( ) => { expect ( logger . count ) . toBe ( eventCount ) ; expect ( logger . eventData . length ) . toBe ( eventCount ) ; for ( let i = 1 ; i <= eventCount ; i ++ ) { expect ( logger . eventData [ i - 1 ] . object . name ) . toBe ( "Channeled_" + i ) ; } testFunction ( elementArray ) ; } ) ; } ) ; } ; testFunction ( SENSOR_THINGS_TYPES_SET . slice ( ) ) ; } , 15 * TEST_TIMEOUT ) ; } ) ; describe ( "Validators tests" , ( ) => { it ( "All default sensorThings objects are considered as objects" , ( ) => { expect ( CoreTypes . isObject ( SensorThingsCollection . featureOfInterest ) ) . toBe ( true ) ; expect ( CoreTypes . isObject ( SensorThingsCollection . observation ) ) . toBe ( true ) ; expect ( CoreTypes . isObject ( SensorThingsCollection . sensor ) ) . toBe ( true ) ; expect ( CoreTypes . isObject ( SensorThingsCollection . thing ) ) . toBe ( true ) ; } , TEST_TIMEOUT ) ; it ( "All default sensorThings objects are considered as objects after deserialization(serialization())" , ( ) => { expect ( CoreTypes . isObject ( JSON . parse ( JSON . stringify ( SensorThingsCollection . featureOfInterest ) ) ) ) . toBe ( true ) ; expect ( CoreTypes . isObject ( JSON . parse ( JSON . stringify ( SensorThingsCollection . observation ) ) ) ) . toBe ( true ) ; expect ( CoreTypes . isObject ( JSON . parse ( JSON . stringify ( SensorThingsCollection . sensor ) ) ) ) . toBe ( true ) ; expect ( CoreTypes . isObject ( JSON . parse ( JSON . stringify ( SensorThingsCollection . thing ) ) ) ) . toBe ( true ) ; } , TEST_TIMEOUT ) ; it ( "Validators validate their object" , ( ) => { expect ( SensorThingsTypes . isFeatureOfInterest ( SensorThingsCollection . featureOfInterest ) ) . toBe ( true ) ; expect ( SensorThingsTypes . isObservation ( SensorThingsCollection . observation ) ) . toBe ( true ) ; expect ( SensorThingsTypes . isSensor ( SensorThingsCollection . sensor ) ) . toBe ( true ) ; expect ( SensorThingsTypes . isThing ( SensorThingsCollection . thing ) ) . toBe ( true ) ; } , TEST_TIMEOUT ) ; it ( "Sensor validator validates only its type" , ( ) => { expect ( SensorThingsTypes . isSensor ( SensorThingsCollection . featureOfInterest ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isSensor ( SensorThingsCollection . location ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isSensor ( SensorThingsCollection . sensor ) ) . toBe ( true ) ; expect ( SensorThingsTypes . isSensor ( SensorThingsCollection . observation ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isSensor ( SensorThingsCollection . thing ) ) . toBe ( false ) ; } ) ; it ( "Sensor with an undefined Unit of Measurement is not valid" , ( ) => { const sensor = SensorThingsCollection . sensor ; sensor . unitOfMeasurement = undefined ; expect ( SensorThingsTypes . isSensor ( sensor ) ) . toBe ( false ) ; } ) ; it ( "Sensor with an undefined Observed Property is not valid" , ( ) => { const sensor = SensorThingsCollection . sensor ; sensor . observedProperty = undefined ; expect ( SensorThingsTypes . isSensor ( sensor ) ) . toBe ( false ) ; } ) ; it ( "Feature Of Interest validator validates only its type" , ( ) => { expect ( SensorThingsTypes . isFeatureOfInterest ( SensorThingsCollection . featureOfInterest ) ) . toBe ( true ) ; expect ( SensorThingsTypes . isFeatureOfInterest ( SensorThingsCollection . location ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isFeatureOfInterest ( SensorThingsCollection . sensor ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isFeatureOfInterest ( SensorThingsCollection . observation ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isFeatureOfInterest ( SensorThingsCollection . thing ) ) . toBe ( false ) ; } ) ; it ( "Observation validator validates only its type" , ( ) => { expect ( SensorThingsTypes . isObservation ( SensorThingsCollection . featureOfInterest ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isObservation ( SensorThingsCollection . location ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isObservation ( SensorThingsCollection . sensor ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isObservation ( SensorThingsCollection . observation ) ) . toBe ( true ) ; expect ( SensorThingsTypes . isObservation ( SensorThingsCollection . thing ) ) . toBe ( false ) ; } ) ; it ( "Thing validator validates only its type" , ( ) => { expect ( SensorThingsTypes . isThing ( SensorThingsCollection . featureOfInterest ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isThing ( SensorThingsCollection . location ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isThing ( SensorThingsCollection . sensor ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isThing ( SensorThingsCollection . observation ) ) . toBe ( false ) ; expect ( SensorThingsTypes . isThing ( SensorThingsCollection . thing ) ) . toBe ( true ) ; } ) ; } ) ; } ) ;