import { Entity , EntityConstructor } from "./model/entity" ; import { EntityMapperService } from "./entity-mapper.service" ; import { UpdatedEntity } from "./model/entity-update" ; import { NEVER , Observable } from "rxjs" ; export function mockEntityMapper ( withData = [ ] ) { const ems = new MockEntityMapperService ( ) ; ems . addAll ( withData ) ; return ems ; } /**
 * Mocked entity mapper. Can/should only be used whenever some data should be saved
 * and loaded. This is not to be used to mock database/anything related to the Schema-service.
 */ export class MockEntityMapperService extends EntityMapperService { private data : Map < string , Map < string , Entity > > = new Map ( ) ; constructor ( ) { super ( null , null ) ; } /**
     * like  save , but synchronous
     *  param entity public add ( entity ) { const type = entity . getType ( ) ; if ( ! this . data . get ( type ) ) { this . data . set ( type , new Map ( ) ) ; } this . data . get ( type ) . set ( entity . getId ( ) , entity ) ; } param entity public contains ( entity ) { return ( this . data . has ( entity . getType ( ) ) && this . data . get ( entity . getType ( ) ) . has ( entity . getId ( ) ) ) ; } param entities public addAll ( entities ) { entities . forEach ( ( e ) => this . add ( e ) ) ; } /**
     * like  load but synchronous
     *  param entityType param id public get ( entityType , id ) { const result = this . data . get ( entityType ) ?. get ( id ) ; if ( ! result ) { throw { status : 404 } ; } return result ; } /**
     * like  loadType but synchronous
     *  param entityType public getAll < T extends Entity > ( entityType ) { return [ ... ( this . data . get ( entityType ) ?. values ( ) || [ ] ) ] as T [ ] ; } /**
     * like  remove but synchronous
     *  param entity public delete ( entity ) { const entities = this . data . get ( entity . getType ( ) ) ; if ( entities ) { entities . delete ( entity . getId ( ) ) ; } } public async load < T extends Entity > ( entityType , id ) { const type = new entityType ( ) . getType ( ) ; return this . get ( type , id ) as T ; } async loadType < T extends Entity > ( entityType ) { const type = new entityType ( ) . getType ( ) ; return this . getAll ( type ) as T [ ] ; } async save < T extends Entity > ( entity , forceUpdate = false ) { this . add ( entity ) ; return Promise . resolve ( ) ; } remove < T extends Entity > ( entity ) { this . delete ( entity ) ; return Promise . resolve ( ) ; } receiveUpdates < T extends Entity > ( entityType ) { return NEVER ; } }