Skip to content

Commit 3f097c0

Browse files
author
priyanshu-kun
committed
Introduce controller tests, implement validation service, and perform refactoring of utility files
1 parent f016cb0 commit 3f097c0

23 files changed

+370
-251
lines changed

src/server/api/controllers/CrawlingController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const { cleanDB } = require('../utils/CleanDBUtils');
2-
const { saveIndexFiles } = require('../utils/SaveIntoDBUtils');
1+
const { cleanDB } = require('../utils/DBUtils');
2+
const { saveIndexFiles } = require('../utils/DBUtils');
33
const { selectDataSources } = require('../utils/SelectDataSourcesUtils');
44

55
module.exports = {

src/server/api/controllers/DownloadAndProcessIndexFilesController.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
const { cleanDB } = require('../utils/CleanDBUtils');
1+
const { cleanDB } = require('../utils/DBUtils');
2+
const { emptyQueue, pushDataToQueue } = require('../utils/QueueUtils');
23
const { connectRabbitMQ } = require('../utils/ConnectRabbitMQUtils');
3-
const { emptyQueue } = require('../utils/EmptyQueueUtils');
44
const {
55
fetchIndexFilesFromDBUtils,
6-
} = require('../utils/FetchIndexFilesFromDBUtils');
7-
const { pushDataToQueue } = require('../utils/PushDataToQueue');
6+
} = require('../utils/DBUtils');
87
const { startWorker } = require('../workers/DownloadAndProcessWorker');
9-
const { removeDist } = require('../utils/RemoveDist');
8+
const { removeDist } = require('../utils/FileHandlingUtils');
109

1110
module.exports = {
1211
/**
@@ -31,23 +30,26 @@ module.exports = {
3130
const indexFileUrls = await fetchIndexFilesFromDBUtils({ skip, limit, sort });
3231

3332
if (indexFileUrls.length === 0) {
34-
return res.notFound('Before proceeding, execute the initial endpoint to obtain a response from it');
33+
return res.badRequest('Before proceeding, execute the initial endpoint to obtain a response from it');
3534
}
3635

36+
3737
// Empty the queue and clean the database for storing latest data
3838
await emptyQueue();
3939
await cleanDB(APIsDefinitionsModel);
4040
removeDist();
4141

4242
const { channel, QUEUE_NAME, connection } = await connectRabbitMQ();
43+
4344
pushDataToQueue(indexFileUrls, QUEUE_NAME, channel);
4445

45-
await channel.close();
46-
await connection.close();
4746

4847
// Start the worker to process the downloaded files
4948
await startWorker();
5049

50+
await channel.close();
51+
await connection.close();
52+
5153
return res
5254
.status(202)
5355
.json({ message: 'Downloading has been started in background.' });

src/server/api/services/DownloadService.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const axios = require('axios');
2+
const readline = require('readline');
23
const { PROGRESS_BAR_WIDTH } = require('../constants/Constants');
34
const { formatSizeUnits } = require('../utils/FormatSizeUnitsUtils');
45

@@ -28,16 +29,15 @@ module.exports = {
2829
.repeat(Math.round(percent / (100 / PROGRESS_BAR_WIDTH)))
2930
.padEnd(PROGRESS_BAR_WIDTH, ' ');
3031

31-
process.stdout.clearLine();
32-
process.stdout.cursorTo(0);
32+
readline.clearLine(process.stdout);
33+
readline.cursorTo(process.stdout, 0);
3334

3435
process.stdout.write(
3536
`Progress: [${progress}] ${percent}% (${downloadedSize}/${totalSize})`
3637
);
3738
},
3839
});
3940
} catch (error) {
40-
console.error(error);
4141
throw error;
4242
}
4343
},

src/server/api/services/ParserService.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { URL_REGEX, API_DEFINITION_REGEX, SUB_DOMAIN_REGEX, KEYWORD_REGEX } = require('../constants/Constants');
1+
const { URL_REGEX, API_DEFINITION_REGEX } = require('../constants/Constants');
22

33
module.exports = {
44
/**
@@ -8,7 +8,7 @@ module.exports = {
88
* @param {string} text - The input text to parse and extract URLs from.
99
* @throws {Error} If there is an error during the parsing process, it will be thrown.
1010
*/
11-
parsing: function (text) {
11+
parsingService: function (text) {
1212
try {
1313
const matchUrls = text.match(URL_REGEX);
1414
matchUrls.forEach((url) => {

src/server/api/services/ValidateService.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
const { downloadFile } = require('./DownloadService');
2-
3-
const OpenAPISchemaValidator = require('openapi-schema-validator').default;
1+
const SwaggerParser = require('swagger-parser');
42

53
module.exports = {
6-
validate: async function (url) {
4+
validatingService: async function (url) {
75
try {
8-
downloadFile(url).then(response => {
9-
10-
}).catch(error => {
11-
throw error;
12-
});
13-
} catch (error) {
6+
return await SwaggerParser.validate(url);
7+
}
8+
catch(error) {
149
throw error;
1510
}
1611
},

src/server/api/utils/CheckAndCreateDistFiles.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/server/api/utils/CleanDBUtils.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/server/api/utils/ConsumeMessagesFromQueue.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/server/api/utils/DBUtils.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const { validatingService } = require('../services/ValidateService');
2+
3+
module.exports = {
4+
/**
5+
* Asynchronously cleans the database by destroying all records of the specified model.
6+
*
7+
* @function cleanDB
8+
* @param {Object} model - The database model to clean (e.g., Waterline model).
9+
* @throws {Error} If there is an error during the database cleaning process, it will be thrown.
10+
*/
11+
cleanDB: async function (model) {
12+
try {
13+
await model.destroy({});
14+
console.log('\nDatabase is cleaned.\n');
15+
} catch (error) {
16+
throw error;
17+
}
18+
},
19+
20+
/**
21+
* Asynchronously fetches index files from the database using the specified filter options.
22+
*
23+
* @function fetchIndexFilesFromDBUtils
24+
* @param {Object} filter - An object containing filter options for fetching index files.
25+
* @param {number} [filter.skip] - The number of index files to skip before returning results (default: 0).
26+
* @param {number} [filter.limit] - The maximum number of index files to return (default: Infinity).
27+
* @param {'aes' | 'des'} [filter.sort] - The sorting order for the index files (default: 'aes').
28+
* Use 'aes' for ascending and 'des' for descending.
29+
* @returns {Promise<Array>} A Promise that resolves with an array of index files fetched from the database.
30+
* @throws {Error} If there is an error during the database query, it will be thrown.
31+
*/
32+
fetchIndexFilesFromDBUtils: async function (filter) {
33+
try {
34+
const orderMap = {
35+
'aes': 1,
36+
'des': -1
37+
};
38+
39+
const skp = filter.skip === undefined ? 0 : parseInt(filter.skip);
40+
const lmt = filter.limit === undefined ? Infinity : parseInt(filter.limit);
41+
const srt = filter.sort === undefined ? 1 : orderMap[filter.sort];
42+
43+
return await IndexFilesModel.find({})
44+
.sort({ URL: srt })
45+
.skip(skp)
46+
.limit(lmt);
47+
48+
} catch (error) {
49+
throw error;
50+
}
51+
},
52+
53+
/**
54+
* Asynchronously saves a list of index file URLs to the database.
55+
*
56+
* @function saveIndexFiles
57+
* @param {Array<string>} indexFiles - An array of index file URLs to be saved to the database.
58+
* @returns {Promise<void>} A Promise that resolves when all index file URLs have been saved to the database.
59+
* @throws {Error} If there is an error during the database insertion process, it will be thrown.
60+
*/
61+
saveIndexFiles: async function (indexFiles) {
62+
try {
63+
indexFiles.forEach(async url => {
64+
await IndexFilesModel.create({URL: url});
65+
});
66+
} catch (error) {
67+
throw error;
68+
}
69+
},
70+
71+
/**
72+
* Asynchronously saves API definitions to a file and database.
73+
*
74+
* @function saveAPIsDefinitions
75+
* @param {string[]} definitions - An array of API definitions (URLs) to be saved.
76+
* @throws {Error} If there's an error while saving the definitions to the file or the database.
77+
* @returns {Promise<void>} A Promise that resolves once all definitions have been saved.
78+
*/
79+
saveAPIsDefinitions: async function(definitions) {
80+
try {
81+
definitions.forEach(async url => {
82+
validatingService(url).then(async () => {
83+
await APIsDefinitionsModel.create({url});
84+
}).catch(error => {
85+
console.log('Invalid definition!',error.message);
86+
});
87+
});
88+
} catch(error) {
89+
throw error;
90+
}
91+
}
92+
}

src/server/api/utils/EmptyQueueUtils.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)