refactor: provide common functions and abstraction

This commit is contained in:
Michel Jonathan Schmitz
2019-05-16 08:22:43 +02:00
parent 8ec4401d21
commit 7dfbb9a650
15 changed files with 2379 additions and 1759 deletions

45
test/Connector.spec.ts Normal file
View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2018, 2019 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 {SCThingOriginType} from '@openstapps/core';
import {expect} from 'chai';
import {suite, test} from 'mocha-typescript';
import {MinimalConnector} from '../src/MinimalConnector';
@suite
export class ConnectorSpec {
private static connector: MinimalConnector;
static async before() {
this.connector = new MinimalConnector('f-u', 'minimal-connector');
}
@test
testCreateRemoteOrigin() {
const remoteOrigin = ConnectorSpec.connector.createRemoteOrigin();
expect(remoteOrigin.name).to.equal(ConnectorSpec.connector.origin);
expect(remoteOrigin.type).to.equal(SCThingOriginType.Remote);
expect(new Date().valueOf()).to.be.at.least(Date.parse(remoteOrigin.indexed).valueOf());
}
@test
async testAutomaticMissingUIDGeneration() {
const uuidRegExp = new RegExp('^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$');
const messages = await ConnectorSpec.connector.getItems();
for(const message of messages){
expect(message.uid).to.match(uuidRegExp);
}
}
}

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2018, 2019 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 {SCThings, SCThingType} from '@openstapps/core';
import {Validator} from '@openstapps/core-tools/lib/validate';
import {expect} from 'chai';
import {suite, test} from 'mocha-typescript';
import {join} from 'path';
import {MinimalConnector} from '../src/MinimalConnector';
@suite
export class MinimalConnectorSpec {
private static connector: MinimalConnector;
private static validator: Validator;
static async before() {
this.validator = new Validator();
await this.validator.addSchemas(
join(__dirname, '..', 'node_modules', '@openstapps', 'core', 'lib', 'schema'),
);
this.connector = new MinimalConnector('f-u', 'minimal-connector');
}
/**
* Gets the `SCThingType`-key as string
*
* @param instance Contains `type` with the value of a SCThingType-key
*/
static getSchemaNameFromType<T extends SCThings>(instance: T): string {
const type = instance.type;
const index = Object.values(SCThingType).indexOf(type);
const key = Object.keys(SCThingType)[index];
return `SC${key}`;
}
/**
* Checks, if the items are valid
*
* @param things Items fetched by the connector
*/
static validateThings<T extends SCThings>(things: T[]) {
things.forEach((thing: T) => {
const schemaName = this.getSchemaNameFromType<T>(thing);
// validate thing
const validatorResult = MinimalConnectorSpec.validator.validate(thing, schemaName);
expect(validatorResult.errors).to.have.lengthOf(
0,
JSON.stringify({
errors: validatorResult.errors,
thing: thing,
}),
);
});
}
@test
getSampleThings() {
return MinimalConnectorSpec.connector
.getItems()
.then(<T extends SCThings>(items: T[]) => {
if (items.length > 0) {
MinimalConnectorSpec.validateThings(items);
}
});
}
}

94
test/common.spec.ts Normal file
View File

@@ -0,0 +1,94 @@
/*
* Copyright (C) 2018, 2019 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 {SCThingType, SCBulkResponse, SCLicensePlate} from '@openstapps/core';
import {expect} from 'chai';
import {suite, test} from 'mocha-typescript';
import {MinimalConnector} from '../src/MinimalConnector';
import {createUUID, executeConnector, isValidSCNamespace} from '../src/common';
import nock = require('nock');
@suite
export class CommonSpec {
private static connector: MinimalConnector;
static async before() {
this.connector = new MinimalConnector('f-u', 'minimal-connector');
}
@test
public isValidSCNamespace() {
const existingAddedSCLIcensePlate: SCLicensePlate = 'f-u';
const notASCLicensePlate: string = 'NOT-A-LICENSE';
const existingButUnaddedLicensePlate: SCLicensePlate = 'a-fh';
expect(isValidSCNamespace(existingAddedSCLIcensePlate)).to.be.equal(true);
expect(isValidSCNamespace(notASCLicensePlate)).to.be.equal(false);
expect(isValidSCNamespace(existingButUnaddedLicensePlate)).to.be.equal(false);
}
@test
testCreateUUID() {
const item = {
type: 'Not even a thing'
};
expect(createUUID(item,'f-u')).to.equal('3ac2b548-75d3-5326-920a-241e514fe445');
// ID was generated once before!
}
@test
async testExecuteConnector() {
const source = CommonSpec.connector.origin;
const bulkOpen: SCBulkResponse = {
source: source,
state: 'in progress',
type: SCThingType.Message,
uid: '744321ca-cc95-4967-b8df-42c98b792db6',
};
nock('http://localhost:3000')
.post('/bulk')
.reply(200, bulkOpen);
const nockBulkAdd = nock('http://localhost:3000')
.post('/bulk/744321ca-cc95-4967-b8df-42c98b792db6')
.reply(201, {})
.persist(); // otherwise consumed!
nock('http://localhost:3000')
.post('/bulk/744321ca-cc95-4967-b8df-42c98b792db6/done')
.reply(204, {});
// should succeed
expect(await CommonSpec.runExecuteConnector()).to.equal(true);
nockBulkAdd.persist(false);
// should fail due to nockBulkAdd being consumed
expect(await CommonSpec.runExecuteConnector()).to.equal(false);
}
/**
* Executes the connector
*/
static async runExecuteConnector(): Promise<boolean> {
try{
await executeConnector('http://localhost:3000', CommonSpec.connector)
return true;
}catch(err){
return false;
}
}
}

View File

@@ -1,51 +0,0 @@
/*
* Copyright (C) 2018-2019 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 {SCThings} from '@openstapps/core';
import {Validator} from '@openstapps/core-tools/lib/validate';
import {expect} from 'chai';
import {suite, test} from 'mocha-typescript';
import {MinimalConnector} from '../src';
@suite
export class MinimalConnectorSpec {
private static connector: MinimalConnector;
private static validator: Validator;
static async before() {
this.validator = new Validator();
await this.validator.addSchemas('./node_modules/@openstapps/core/lib/schema');
this.connector = new MinimalConnector();
}
static validateThings(things: SCThings[]) {
things.forEach((thing: SCThings) => {
// validate thing
expect(MinimalConnectorSpec.validator.validateThing(thing).errors).to.have.lengthOf(0, JSON.stringify({
errors: MinimalConnectorSpec.validator.validateThing(thing).errors,
thing: thing,
}));
});
}
@test
getSampleThings() {
return MinimalConnectorSpec.connector.getItems().then(<T extends SCThings>(items: T[]) => {
if (items.length > 0) {
MinimalConnectorSpec.validateThings(items);
}
});
}
}