/* * Copyright (C) 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 . */ import {SCThings, SCThingType} from '@openstapps/core'; import {readdir, readFile} from 'fs'; import {join} from 'path'; import {promisify} from 'util'; import {ConnectorClient} from './connectorClient'; import {HttpClientInterface} from './httpClientInterface'; /** * Options to set up indexing core test files to backend */ export interface E2EOptions { /** * File path of the directory containing core test files */ samples: string; /** * URL of the backend to index to */ to: string; } /** * Function to add all the SCThings that getItemsFromSamples() returns to the backend * * @param client HTTP client * @param options Map of options */ export async function indexSamples(client: HttpClientInterface, options: E2EOptions): Promise { const api = new ConnectorClient(client, options.to); const items = await getItemsFromSamples(options.samples); if (items.length === 0) { throw new Error('Could not index samples. None were retrived from the file system.'); } try { // sort items by type const itemMap: Map = new Map(); for (const item of items) { if (!itemMap.has(item.type)) { itemMap.set(item.type, []); } const currentItems = itemMap.get(item.type) as SCThings[]; currentItems.push(item); itemMap.set(item.type, currentItems); } // add items depending on their type property with one type per bulk for (const type of itemMap.keys()) { const currentBulk = await api.bulk(type, 'stapps-core-sample-data'); for (const item of (itemMap.get(type) as SCThings[])) { await currentBulk!.add(item); } await currentBulk.done(); } } catch (err) { throw err; } } /** * Get all SCThings from the predefined core test json files * * @param samplesDirectory Filepath to the directory containing to the core test json files * @returns an Array of all the SCThings specified for test usage */ export async function getItemsFromSamples(samplesDirectory: string): Promise { const readDirPromised = promisify(readdir); const readFilePromised = promisify(readFile); const things: T[] = []; try { const fileNames = await readDirPromised(samplesDirectory); for (const fileName of fileNames) { const filePath = join(samplesDirectory, fileName); if (filePath.endsWith('.json')) { const fileContent = await readFilePromised(filePath, {encoding: 'utf8'}); const schemaObject = JSON.parse(fileContent); if (schemaObject.errorNames.length === 0 && typeof schemaObject.instance.type === 'string') { things.push(schemaObject.instance); } } } } catch (error) { return error; } return things; }