2019-11-12 16:41:31 -05:00
|
|
|
/**
|
|
|
|
* Delta publisher test
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2019 R-T Specialty, LLC.
|
|
|
|
*
|
|
|
|
* This file is part of the Liza Data Collection Framework.
|
|
|
|
*
|
|
|
|
* liza is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2019-12-02 10:00:23 -05:00
|
|
|
import { AmqpConnection } from '../../src/system/amqp/AmqpConnection';
|
|
|
|
import { Delta, DeltaResult, DeltaType } from '../../src/bucket/delta';
|
2019-11-25 12:26:39 -05:00
|
|
|
import { DeltaPublisher as Sut } from '../../src/system/DeltaPublisher';
|
2019-12-10 17:24:19 -05:00
|
|
|
import { DocumentId, DocumentMeta } from '../../src/document/Document';
|
|
|
|
import { EventEmitter } from 'events';
|
2019-12-02 10:00:23 -05:00
|
|
|
import { hasContext } from '../../src/error/ContextError';
|
|
|
|
import { AmqpError } from '../../src/error/AmqpError';
|
|
|
|
import { Channel } from 'amqplib';
|
2019-12-10 17:24:19 -05:00
|
|
|
import { MessageWriter } from '../../src/system/MessageWriter';
|
2019-11-12 16:41:31 -05:00
|
|
|
|
2019-12-09 12:06:52 -05:00
|
|
|
|
2019-11-12 16:41:31 -05:00
|
|
|
import { expect, use as chai_use } from 'chai';
|
|
|
|
chai_use( require( 'chai-as-promised' ) );
|
|
|
|
|
|
|
|
|
|
|
|
describe( 'server.DeltaPublisher', () =>
|
|
|
|
{
|
|
|
|
describe( '#publish', () =>
|
|
|
|
{
|
|
|
|
it( 'sends a message', () =>
|
|
|
|
{
|
2019-12-02 10:00:23 -05:00
|
|
|
let publish_called = false;
|
|
|
|
const delta = createMockDelta();
|
|
|
|
const bucket = createMockBucketData();
|
|
|
|
const ratedata = createMockBucketData();
|
|
|
|
const emitter = new EventEmitter();
|
|
|
|
const conn = createMockAmqpConnection();
|
2019-12-10 17:24:19 -05:00
|
|
|
const writer = createMockWriter();
|
|
|
|
const meta = <DocumentMeta>{
|
|
|
|
id: <DocumentId>123,
|
|
|
|
entity_name: 'Some Agency',
|
|
|
|
entity_id: 234,
|
|
|
|
startDate: <UnixTimestamp>345,
|
|
|
|
lastUpdate: <UnixTimestamp>456,
|
|
|
|
};
|
|
|
|
|
2019-12-02 10:00:23 -05:00
|
|
|
conn.getAmqpChannel = () =>
|
|
|
|
{
|
|
|
|
return <Channel>{
|
|
|
|
publish: ( _: any, __: any, buf: any, ___: any ) =>
|
|
|
|
{
|
|
|
|
expect( buf instanceof Buffer ).to.be.true;
|
2019-11-12 16:41:31 -05:00
|
|
|
|
2019-12-02 10:00:23 -05:00
|
|
|
publish_called = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-12-10 17:24:19 -05:00
|
|
|
const sut = new Sut( emitter, ts_ctr, conn, writer );
|
2019-12-02 10:00:23 -05:00
|
|
|
|
|
|
|
return expect(
|
2019-12-10 17:24:19 -05:00
|
|
|
sut.publish( meta, delta, bucket, ratedata )
|
2019-12-02 10:00:23 -05:00
|
|
|
).to.eventually.deep.equal( undefined )
|
|
|
|
.then( _ =>
|
|
|
|
{
|
|
|
|
expect( publish_called ).to.be.true;
|
|
|
|
} );
|
2019-11-13 14:08:42 -05:00
|
|
|
} );
|
|
|
|
|
2019-12-02 10:00:23 -05:00
|
|
|
( <[string, () => Channel | undefined, Error, string ][]>[
|
|
|
|
[
|
|
|
|
'Throws an error when publishing was unsuccessful',
|
|
|
|
() =>
|
|
|
|
{
|
|
|
|
return <Channel>{
|
|
|
|
publish: ( _: any, __: any, _buf: any, ___: any ) =>
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
Error,
|
|
|
|
'Delta publish failed'
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'Throws an error when no amqp channel is found',
|
|
|
|
() =>
|
|
|
|
{
|
|
|
|
return undefined;
|
|
|
|
},
|
|
|
|
AmqpError,
|
|
|
|
'Error sending message: No channel'
|
|
|
|
]
|
|
|
|
] ).forEach( ( [ label, getChannelF, error_type, err_msg ] ) =>
|
|
|
|
it( label, () =>
|
2019-11-13 14:08:42 -05:00
|
|
|
{
|
2019-12-02 10:00:23 -05:00
|
|
|
const delta = createMockDelta();
|
|
|
|
const bucket = createMockBucketData();
|
|
|
|
const ratedata = createMockBucketData();
|
|
|
|
const emitter = new EventEmitter();
|
|
|
|
const conn = createMockAmqpConnection();
|
2019-12-10 17:24:19 -05:00
|
|
|
const writer = createMockWriter();
|
|
|
|
const meta = <DocumentMeta>{
|
|
|
|
id: <DocumentId>123,
|
|
|
|
entity_name: 'Some Agency',
|
|
|
|
entity_id: 234,
|
|
|
|
startDate: <UnixTimestamp>345,
|
|
|
|
lastUpdate: <UnixTimestamp>456,
|
|
|
|
};
|
|
|
|
|
2019-12-02 10:00:23 -05:00
|
|
|
const expected = {
|
2019-12-10 17:24:19 -05:00
|
|
|
doc_id: meta.id,
|
2019-12-17 11:45:52 -05:00
|
|
|
quote_id: meta.id,
|
2019-12-02 10:00:23 -05:00
|
|
|
delta_type: delta.type,
|
|
|
|
delta_ts: delta.timestamp
|
|
|
|
}
|
2019-11-13 14:08:42 -05:00
|
|
|
|
2019-12-02 10:00:23 -05:00
|
|
|
conn.getAmqpChannel = getChannelF;
|
|
|
|
|
2019-12-10 17:24:19 -05:00
|
|
|
const result = new Sut( emitter, ts_ctr, conn, writer )
|
|
|
|
.publish( meta, delta, bucket, ratedata );
|
2019-12-02 10:00:23 -05:00
|
|
|
|
|
|
|
return Promise.all( [
|
|
|
|
expect( result ).to.eventually.be.rejectedWith(
|
|
|
|
error_type, err_msg
|
|
|
|
),
|
|
|
|
result.catch( e =>
|
|
|
|
{
|
|
|
|
if ( !hasContext( e ) )
|
|
|
|
{
|
|
|
|
return expect.fail();
|
|
|
|
}
|
|
|
|
|
|
|
|
return expect( e.context ).to.deep.equal( expected );
|
|
|
|
} )
|
|
|
|
] );
|
|
|
|
} ) );
|
2019-11-13 14:08:42 -05:00
|
|
|
|
|
|
|
|
2019-12-10 17:24:19 -05:00
|
|
|
it( 'writer#write rejects', () =>
|
2019-11-13 14:08:42 -05:00
|
|
|
{
|
2019-12-10 17:24:19 -05:00
|
|
|
const delta = createMockDelta();
|
|
|
|
const bucket = createMockBucketData();
|
|
|
|
const ratedata = createMockBucketData();
|
|
|
|
const emitter = new EventEmitter();
|
|
|
|
const conn = createMockAmqpConnection();
|
|
|
|
const writer = createMockWriter();
|
|
|
|
const error = new Error( 'Bad thing happened' );
|
|
|
|
const meta = <DocumentMeta>{
|
|
|
|
id: <DocumentId>123,
|
|
|
|
entity_name: 'Some Agency',
|
|
|
|
entity_id: 234,
|
|
|
|
startDate: <UnixTimestamp>345,
|
|
|
|
lastUpdate: <UnixTimestamp>456,
|
|
|
|
};
|
|
|
|
|
|
|
|
writer.write = (
|
|
|
|
_: any,
|
|
|
|
__: any,
|
|
|
|
___: any,
|
|
|
|
____: any,
|
|
|
|
_____: any
|
|
|
|
): Promise<Buffer> =>
|
2019-11-13 14:08:42 -05:00
|
|
|
{
|
2019-12-10 17:24:19 -05:00
|
|
|
return Promise.reject( error );
|
|
|
|
};
|
2019-11-13 14:08:42 -05:00
|
|
|
|
2019-12-10 17:24:19 -05:00
|
|
|
const result = new Sut( emitter, ts_ctr, conn, writer )
|
|
|
|
.publish( meta, delta, bucket, ratedata );
|
|
|
|
|
|
|
|
return Promise.all( [
|
|
|
|
expect( result ).to.eventually.be.rejectedWith( error ),
|
|
|
|
result.catch( e =>
|
|
|
|
{
|
|
|
|
return expect( e ).to.deep.equal( error );
|
|
|
|
} )
|
|
|
|
] );
|
|
|
|
} )
|
2019-11-13 14:08:42 -05:00
|
|
|
} );
|
2019-11-12 16:41:31 -05:00
|
|
|
} );
|
|
|
|
|
2019-12-02 10:00:23 -05:00
|
|
|
|
2019-11-13 14:08:42 -05:00
|
|
|
function ts_ctr(): UnixTimestamp
|
|
|
|
{
|
|
|
|
return <UnixTimestamp>Math.floor( new Date().getTime() / 1000 );
|
|
|
|
}
|
2019-11-12 16:41:31 -05:00
|
|
|
|
2019-12-02 10:00:23 -05:00
|
|
|
|
|
|
|
function createMockAmqpConnection(): AmqpConnection
|
2019-11-12 16:41:31 -05:00
|
|
|
{
|
2019-12-02 10:00:23 -05:00
|
|
|
return <AmqpConnection>{
|
|
|
|
connect: () => {},
|
|
|
|
getExchangeName: () => { 'Foo' },
|
|
|
|
};
|
2019-11-12 16:41:31 -05:00
|
|
|
}
|
2019-11-13 14:08:42 -05:00
|
|
|
|
|
|
|
|
2019-12-02 10:00:23 -05:00
|
|
|
function createMockBucketData(): Record<string, any>
|
|
|
|
{
|
|
|
|
return {
|
|
|
|
foo: [ 'bar', 'baz' ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function createMockDelta(): Delta<any>
|
|
|
|
{
|
|
|
|
return <Delta<any>>{
|
|
|
|
type: <DeltaType>'data',
|
|
|
|
timestamp: <UnixTimestamp>123123123,
|
|
|
|
data: <DeltaResult<any>>{},
|
|
|
|
}
|
2019-12-09 12:06:52 -05:00
|
|
|
}
|
2019-12-09 14:03:47 -05:00
|
|
|
|
|
|
|
|
2019-12-10 17:24:19 -05:00
|
|
|
function createMockWriter(): MessageWriter
|
2019-12-09 14:03:47 -05:00
|
|
|
{
|
2019-12-10 17:24:19 -05:00
|
|
|
return <MessageWriter>{
|
|
|
|
write( _: any, __:any, ___:any, ____:any, _____:any ): Promise<Buffer>
|
2019-12-09 14:03:47 -05:00
|
|
|
{
|
2019-12-10 17:24:19 -05:00
|
|
|
return Promise.resolve( Buffer.from( '' ) );
|
|
|
|
}
|
2019-12-09 14:03:47 -05:00
|
|
|
};
|
2019-12-10 17:24:19 -05:00
|
|
|
}
|