Files
openstapps/test/connectorClient.spec.ts
Karl-Philipp Wulfert 4839f941c6 feat: add api
2018-11-29 17:38:57 +01:00

342 lines
9.2 KiB
TypeScript

/*
* Copyright (C) 2018 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* 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 General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {
SCBulkAddResponse,
SCBulkAddRoute,
SCBulkDoneResponse,
SCBulkDoneRoute,
SCBulkResponse,
SCBulkRoute,
SCMessage,
SCThingUpdateResponse,
SCThingUpdateRoute,
} from '@openstapps/core';
import * as chai from 'chai';
import {expect} from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as chaiSpies from 'chai-spies';
import {suite, test} from 'mocha-typescript';
import * as moment from 'moment';
import {ConnectorClient} from '../src/connectorClient';
import {EmptyBulkError, NamespaceNotDefinedError} from '../src/errors';
import {HttpClient} from '../src/httpClient';
import {HttpClientRequest, HttpClientResponse} from '../src/httpClientInterface';
chai.should();
chai.use(chaiSpies);
chai.use(chaiAsPromised);
const sandbox = chai.spy.sandbox();
const bulkAddRoute = new SCBulkAddRoute();
const bulkDoneRoute = new SCBulkDoneRoute();
const bulkRoute = new SCBulkRoute();
const thingUpdateRoute = new SCThingUpdateRoute();
const httpClient = new HttpClient();
@suite()
export class ConnectorClientSpec {
async after() {
sandbox.restore();
}
@test
async bulk() {
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCBulkResponse>> => {
return {
body: {
expiration: moment().add(1800, 'seconds').format(),
source: 'foo',
state: 'in progress',
type: 'message',
uid: 'foo',
},
headers: {},
statusCode: bulkRoute.statusCodeSuccess,
};
});
expect(httpClient.request).not.to.have.been.called();
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
await connectorClient.bulk('message', 'foo', 1800);
expect(httpClient.request).to.have.been.first.called.with({
body: {
expiration: moment().add(1800, 'seconds').format(),
source: 'foo',
type: 'message',
},
headers: {},
method: bulkRoute.method,
url: new URL('http://localhost' + bulkRoute.getUrlFragment()),
});
}
@test
async bulkWithoutTimeout() {
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCBulkResponse>> => {
return {
body: {
expiration: moment().add(3600, 'seconds').format(),
source: 'foo',
state: 'in progress',
type: 'message',
uid: 'foo',
},
headers: {},
statusCode: bulkRoute.statusCodeSuccess,
};
});
expect(httpClient.request).not.to.have.been.called();
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
await connectorClient.bulk('message', 'foo');
expect(httpClient.request).to.have.been.first.called.with({
body: {
expiration: moment().add(3600, 'seconds').format(),
source: 'foo',
type: 'message',
},
headers: {},
method: bulkRoute.method,
url: new URL('http://localhost' + bulkRoute.getUrlFragment()),
});
}
@test
async index() {
const messages: SCMessage[] = [
{
audiences: [
'employees',
],
message: 'Lorem ipsum.',
name: 'foo',
origin: {
indexed: 'foo',
name: 'foo',
},
type: 'message',
uid: 'foo',
},
{
audiences: [
'employees',
],
message: 'Lorem ipsum.',
name: 'foo',
origin: {
indexed: 'foo',
name: 'foo',
},
type: 'message',
uid: 'bar',
},
];
type responses = SCBulkResponse | SCBulkAddResponse | SCBulkDoneResponse;
sandbox.on(httpClient, 'request', async (request: HttpClientRequest)
: Promise<HttpClientResponse<responses>> => {
if (request.url.toString() === new URL('http://localhost' + bulkRoute.getUrlFragment()).toString()) {
return {
body: {
expiration: moment().add(3600, 'seconds').format(),
source: 'copy',
state: 'in progress',
type: 'message',
uid: 'foo',
},
headers: {},
statusCode: bulkRoute.statusCodeSuccess,
};
} else if (request.url.toString() === new URL('http://localhost' + bulkAddRoute.getUrlFragment({
UID: 'foo',
})).toString()) {
return {
body: {},
headers: {},
statusCode: bulkAddRoute.statusCodeSuccess,
};
}
return {
body: {},
headers: {},
statusCode: bulkDoneRoute.statusCodeSuccess,
};
});
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
await connectorClient.index(messages, 'copy');
expect(httpClient.request).to.have.been.first.called.with({
body: {
expiration: moment().add(3600, 'seconds').format(),
source: 'copy',
type: 'message',
},
headers: {},
method: bulkRoute.method,
url: new URL('http://localhost' + bulkRoute.getUrlFragment()),
});
}
@test
async indexFails() {
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
return connectorClient.index([]).should.be.rejectedWith(EmptyBulkError);
}
@test
async indexWithoutSource() {
const messages: SCMessage[] = [
{
audiences: [
'employees',
],
message: 'Lorem ipsum.',
name: 'foo',
origin: {
indexed: 'foo',
name: 'foo',
},
type: 'message',
uid: 'foo',
},
{
audiences: [
'employees',
],
message: 'Lorem ipsum.',
name: 'foo',
origin: {
indexed: 'foo',
name: 'foo',
},
type: 'message',
uid: 'bar',
},
];
type responses = SCBulkResponse | SCBulkAddResponse | SCBulkDoneResponse;
sandbox.on(httpClient, 'request', async (request: HttpClientRequest)
: Promise<HttpClientResponse<responses>> => {
if (request.url.toString() === new URL('http://localhost' + bulkRoute.getUrlFragment()).toString()) {
return {
body: {
expiration: moment().add(3600, 'seconds').format(),
source: 'stapps-api',
state: 'in progress',
type: 'message',
uid: 'foo',
},
headers: {},
statusCode: bulkRoute.statusCodeSuccess,
};
} else if (request.url.toString() === new URL('http://localhost' + bulkAddRoute.getUrlFragment({
UID: 'foo',
})).toString()) {
return {
body: {},
headers: {},
statusCode: bulkAddRoute.statusCodeSuccess,
};
}
return {
body: {},
headers: {},
statusCode: bulkDoneRoute.statusCodeSuccess,
};
});
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
await connectorClient.index(messages);
expect(httpClient.request).to.have.been.first.called.with({
body: {
expiration: moment().add(3600, 'seconds').format(),
source: 'stapps-api',
type: 'message',
},
headers: {},
method: bulkRoute.method,
url: new URL('http://localhost' + bulkRoute.getUrlFragment()),
});
}
@test
makeUuid() {
const uuid = ConnectorClient.makeUUID('foo', 'b-tu');
expect(uuid).to.be.equal('abad271e-d9e9-5802-b7bc-96d8a647b451');
expect(ConnectorClient.makeUUID('bar', 'b-tu')).not.to.be.equal(uuid);
expect(ConnectorClient.makeUUID('foo', 'f-u')).not.to.be.equal(uuid);
}
@test
makeUuidFails() {
expect(() => {
ConnectorClient.makeUUID('foo', 'b-u');
}).to.throw(NamespaceNotDefinedError);
}
@test
async update() {
const message: SCMessage = {
audiences: [
'employees',
],
message: 'Lorem ipsum.',
name: 'foo',
origin: {
indexed: 'foo',
name: 'foo',
},
type: 'message',
uid: 'foo',
};
sandbox.on(httpClient, 'request', async (): Promise<HttpClientResponse<SCThingUpdateResponse>> => {
return {
body: {},
headers: {},
statusCode: thingUpdateRoute.statusCodeSuccess,
};
});
expect(httpClient.request).not.to.have.been.called();
const connectorClient = new ConnectorClient(httpClient, 'http://localhost');
await connectorClient.update(message);
expect(httpClient.request).to.have.been.called.with({
body: message,
headers: {},
method: thingUpdateRoute.method,
url: new URL('http://localhost' + thingUpdateRoute.getUrlFragment({
TYPE: 'message',
UID: 'foo',
})),
});
}
}