/* * Copyright (C) 2019, 2020 StApps * This program 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 Affero 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 . */ import {SCSearchQuery, SCSearchResponse, SCThings, SCUuid} from '@openstapps/core'; import {Express} from 'express'; import {configureApp} from '../src/app'; import express from 'express'; import http from 'http'; import {Bulk, BulkStorage} from '../src/storage/bulk-storage'; import {Database} from '../src/storage/database'; import getPort from 'get-port'; /** * Adds routers and configures an (express) app * */ export async function startApp(): Promise { const app = express(); await configureApp(app); const server = http.createServer(app); // get a random free port const port = await getPort(); server.listen(port); server.on('error', err => { throw err; }); return new Promise(resolve => server.on('listening', () => { app.set( 'bulk', bulkStorage, ); resolve(app); })); } /** * An elasticsearch mock */ export class ElasticsearchMock implements Database { // @ts-ignore private bulk: Bulk | undefined; private storageMock = new Map(); constructor() { // Nothing to do here } bulkCreated(bulk: Bulk): Promise { this.bulk = bulk; return Promise.resolve(undefined); } bulkExpired(_bulk: Bulk): Promise { return Promise.resolve(undefined); } bulkUpdated(_bulk: Bulk): Promise { return Promise.resolve(undefined); } get(uid: SCUuid): Promise { // @ts-ignore return Promise.resolve(this.storageMock.get(uid)); } init(): Promise { return Promise.resolve(); } post(_thing: SCThings, _bulk: Bulk): Promise { return Promise.resolve(); } put(thing: SCThings): Promise { this.storageMock.set(thing.uid, thing); return Promise.resolve(); } search(_params: SCSearchQuery): Promise { return Promise.resolve({data: [], facets: [], pagination: {count: 0, offset: 0, total: 0}, stats: {time: 0}}); } } export const bulkStorage = new BulkStorage(new ElasticsearchMock()); export class FooError extends Error { } export const DEFAULT_TEST_TIMEOUT = 10000;