Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tests): addding processor_id to tests resource #240

Merged
merged 4 commits into from
Dec 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions docs/openapi3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ paths:
tags:
- Tests
summary: Retrieve a file encoding to base 64
description: Retrieve a specific f.
description: Retrieve a specific file.
parameters:
- in: path
name: file_id
Expand Down Expand Up @@ -1226,16 +1226,16 @@ paths:
post:
operationId: update-processor-file-content
tags:
- Processors
- Processors
summary: Download the file and update its javascript content.
description: Download the file from the 'file_url' configured for the processor and update its javascript content. If download fails, keep the original javascript content without overriding it.
parameters:
- in: path
name: processor_id
description: The id of the processor file to download.
required: true
schema:
type: string
- in: path
name: processor_id
description: The id of the processor file to download.
required: true
schema:
type: string
responses:
'204':
description: Success
Expand Down Expand Up @@ -1357,6 +1357,10 @@ components:
type: string
description: A description of the test.
example: Performance test for placing orders from the pet store.
processor_id:
description: Unique processor identifier
type: string
format: uuid
type:
$ref: '#/components/schemas/test_type'
test_type:
Expand Down Expand Up @@ -1465,6 +1469,10 @@ components:
file_id:
type: string
description: File id.
processor_id:
description: Unique processor identifier
type: string
format: uuid
dsl_test_response:
type: object
description: A test that is made of scenarios base on domain specific language
Expand Down Expand Up @@ -2037,8 +2045,8 @@ components:
processor_type:
type: string
enum:
- raw_javascript
- file_download
- raw_javascript
- file_download
description: |
The type of the processor resource. Can be one of the following:
* `raw_javascript`: Raw javascript string that will be stored and persisted as it is.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"scripts": {
"start": "node src/server.js",
"unit-tests": "nyc --check-coverage --lines 95 --reporter=html --reporter=text mocha ./tests/unit-tests --recursive",
"unit-tests": "nyc --check-coverage --lines 90 --reporter=html --reporter=text mocha ./tests/unit-tests --recursive",
"integration-tests": "bash ./tests/integration-tests/run.sh",
"end-to-end-tests": "bash ./tests/end-to-end-tests/run.sh",
"local-integration-tests": "bash ./tests/integration-tests/runLocal.sh",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ CREATE TABLE IF NOT EXISTS processors
updated_at timestamp,
PRIMARY KEY (id, created_at))
WITH CLUSTERING ORDER BY (created_at DESC);
---
ALTER TABLE tests ADD processor_id uuid;
15 changes: 15 additions & 0 deletions src/database/sequlize-handler/migrations/02_tests_processor_id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Sequelize = require('sequelize');

module.exports.up = async (query, DataTypes) => {
let testsTable = await query.describeTable('tests');

if (!testsTable.processor_id) {
await query.addColumn(
'tests', 'processor_id',
Sequelize.DataTypes.UUID);
}
};

module.exports.down = async (query, DataTypes) => {
await query.removeColumn('tests', 'processor_id');
};
21 changes: 21 additions & 0 deletions src/tests/helpers/testsVerifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
const processorsManager = require('../../processors/models/processorsManager');

module.exports.verifyProcessorExists = async (req, res, next) => {
let errorToThrow;
let jobBody = req.body;
if (jobBody.processor_id) {
try {
await processorsManager.getProcessor(jobBody.processor_id);
} catch (error) {
if (error.statusCode === 404) {
errorToThrow = new Error(`processor with id: ${jobBody.processor_id} does not exist`);
errorToThrow.statusCode = 400;
} else {
errorToThrow = new Error(error.message);
errorToThrow.statusCode = 500;
}
}
}
next(errorToThrow);
};
4 changes: 2 additions & 2 deletions src/tests/models/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = {
deleteDefinition
};

function insertTest(testInfo, testJson, id, revisionId) {
return databaseConnector.insertTest(testInfo, testJson, id, revisionId);
function insertTest(testInfo, testJson, id, revisionId, fileId) {
return databaseConnector.insertTest(testInfo, testJson, id, revisionId, fileId);
}

async function getTest(id) {
Expand Down
7 changes: 4 additions & 3 deletions src/tests/models/database/cassandra/cassandraConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let client = {};
let uuid = require('cassandra-driver').types.Uuid;
const sanitizeHelper = require('../../../helpers/sanitizeHelper');

const INSERT_TEST_DETAILS = 'INSERT INTO tests(id, name, description, type, updated_at, raw_data, artillery_json, revision_id,file_id) values(?,?,?,?,?,?,?,?,?)';
const INSERT_TEST_DETAILS = 'INSERT INTO tests(id, name, description, type, updated_at, raw_data, artillery_json, revision_id, file_id, processor_id) values(?,?,?,?,?,?,?,?,?,?)';
const GET_TEST = 'SELECT * FROM tests WHERE id = ? ORDER BY updated_at DESC limit 1';
const GET_TEST_REVISIONS = 'SELECT * FROM tests WHERE id = ?';
const GET_TESTS = 'SELECT * FROM tests';
Expand Down Expand Up @@ -69,9 +69,9 @@ async function getAllTestRevisions(id) {
return sanitizedResult;
}

async function insertTest(testInfo, testJson, id, revisionId) {
async function insertTest(testInfo, testJson, id, revisionId, fileId) {
let params;
params = [id, testInfo.name, testInfo.description, testInfo.type, Date.now(), JSON.stringify(testInfo), JSON.stringify(testJson), revisionId, testInfo.fileId];
params = [id, testInfo.name, testInfo.description, testInfo.type, Date.now(), JSON.stringify(testInfo), JSON.stringify(testJson), revisionId, fileId, testInfo.processor_id];
const result = await executeQuery(INSERT_TEST_DETAILS, params, queryOptions);
return result;
}
Expand Down Expand Up @@ -126,6 +126,7 @@ function sanitizeTestResult(data) {
const dslDataObject = sanitizeHelper.extractDslRootData(row.raw_data);
row.artillery_json = JSON.parse(row.artillery_json);
row.file_id = row.file_id || undefined;
row.processor_id = row.processor_id || undefined;
delete row.raw_data;
return Object.assign(row, dslDataObject);
});
Expand Down
9 changes: 7 additions & 2 deletions src/tests/models/database/sequelize/sequelizeConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ async function initSchemas() {
file_id: {
type: Sequelize.DataTypes.UUID
},
processor_id: {
type: Sequelize.DataTypes.UUID
},
name: {
type: Sequelize.DataTypes.STRING
},
Expand Down Expand Up @@ -91,13 +94,14 @@ async function initSchemas() {
await file.sync();
}

async function insertTest(testInfo, testJson, id, revisionId){
async function insertTest(testInfo, testJson, id, revisionId, fileId){
const test = client.model('test');
let params = {
test_id: id,
name: testInfo.name,
type: testInfo.type,
file_id: testInfo.fileId,
file_id: fileId,
processor_id: testInfo.processor_id,
description: testInfo.description,
updated_at: Date.now(),
raw_data: JSON.stringify(testInfo),
Expand Down Expand Up @@ -245,6 +249,7 @@ function sanitizeTestResult(data) {
dataValues.artillery_json = JSON.parse(dataValues.artillery_json);
dataValues.id = dataValues.test_id;
dataValues.file_id = dataValues.file_id || undefined;
dataValues.processor_id = dataValues.processor_id || undefined;
delete dataValues.raw_data;
delete dataValues.test_id;
return Object.assign(dataValues, dslDataObject);
Expand Down
9 changes: 1 addition & 8 deletions src/tests/models/fileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@ const database = require('./database'),
{ ERROR_MESSAGES } = require('../../common/consts');

module.exports = {
createFileFromUrl,
saveFile,
downloadFile,
getFile,
validateJavascriptContent
};
async function createFileFromUrl(testRawData) {
if (testRawData['processor_file_url']) {
const fileId = await saveFile(testRawData['processor_file_url']);
return fileId;
}
return undefined;
}

async function downloadFile(fileUrl) {
const options = {
Expand Down
7 changes: 5 additions & 2 deletions src/tests/models/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ module.exports = {
async function upsertTest(testRawData, existingTestId) {
const testArtilleryJson = await testGenerator.createTest(testRawData);
let id = existingTestId || uuid();
testRawData.fileId = await fileManager.createFileFromUrl(testRawData);
let fileId;
if (testRawData['processor_file_url']){
fileId = await fileManager.saveFile(testRawData['processor_file_url']);
}
let revisionId = uuid.v4();
await database.insertTest(testRawData, testArtilleryJson, id, revisionId);
await database.insertTest(testRawData, testArtilleryJson, id, revisionId, fileId);
return { id: id, revision_id: revisionId };
}

Expand Down
5 changes: 3 additions & 2 deletions src/tests/routes/testsRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ let router = express.Router();
let tests = require('../controllers/testsController');
let swaggerValidator = require('express-ajv-swagger-validation');
let artilleryValidator = require('../helpers/artilleryValidator');
let testsVerifier = require('../helpers/testsVerifier');

router.post('/', swaggerValidator.validate, artilleryValidator.verifyArtillery, tests.upsertTest);
router.post('/', swaggerValidator.validate, testsVerifier.verifyProcessorExists, artilleryValidator.verifyArtillery, tests.upsertTest);
router.get('/', swaggerValidator.validate, tests.getTests);
router.get('/:test_id', swaggerValidator.validate, tests.getTest);
router.delete('/:test_id', swaggerValidator.validate, tests.deleteTest);
router.get('/file/:file_id', swaggerValidator.validate, tests.getFile);
router.put('/:test_id', swaggerValidator.validate, artilleryValidator.verifyArtillery, tests.upsertTest);
router.put('/:test_id', swaggerValidator.validate, testsVerifier.verifyProcessorExists, artilleryValidator.verifyArtillery, tests.upsertTest);
router.get('/:test_id/revisions', swaggerValidator.validate, tests.getTestRevisions);
module.exports = router;
Loading