From 607e26b68b716a046879c3d123c82d4187e541da Mon Sep 17 00:00:00 2001 From: Ana Markovic Date: Wed, 9 Aug 2023 11:43:16 +0200 Subject: [PATCH 1/8] adding new unit tests --- package.json | 3 ++- .../{ => unit}/modules/repository/config.json | 0 .../modules/repository/repository.test.js} | 6 ++--- .../modules/triple-store/config.json | 0 .../triple-store/triple-store.test.js} | 0 test/unit/modules/validation/config.json | 22 ++++++++++++++++ .../validation/validation-manager.test.js | 25 +++++++++++++++++++ 7 files changed, 52 insertions(+), 4 deletions(-) rename test/{ => unit}/modules/repository/config.json (100%) rename test/{modules/repository/repository.js => unit/modules/repository/repository.test.js} (98%) rename test/{ => unit}/modules/triple-store/config.json (100%) rename test/{modules/triple-store/triple-store.js => unit/modules/triple-store/triple-store.test.js} (100%) create mode 100644 test/unit/modules/validation/config.json create mode 100644 test/unit/modules/validation/validation-manager.test.js diff --git a/package.json b/package.json index a17d1b1ea5..6f240a9781 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,8 @@ "kill:local_blockchain": "npx kill-port 8545", "test:bdd": "cucumber-js --fail-fast --format progress --format-options '{\"colorsEnabled\": true}' test/bdd/ --import test/bdd/steps/ --exit", "test:unit": "nyc --all mocha --exit $(find test/unit -name '*.js')", - "test:modules": "nyc --all mocha --exit $(find test/modules -name '*.js')", + "test": "mocha --exit $(find test/unit -name 'validation-manager.test.js')", + "test:modules": "nyc --all mocha --exit $(find test/unit/modules -name '*.js')", "test:bdd:release": "cucumber-js --tags=@release --fail-fast --format progress --format-options '{\"colorsEnabled\": true}' test/bdd/ --import test/bdd/steps/", "test:bdd:publish-errors": "cucumber-js --tags=@publish-errors --fail-fast --format progress --format-options '{\"colorsEnabled\": true}' test/bdd/ --import test/bdd/steps/", "test:bdd:update-errors": "cucumber-js --tags=@update-errors --fail-fast --format progress --format-options '{\"colorsEnabled\": true}' test/bdd/ --import test/bdd/steps/", diff --git a/test/modules/repository/config.json b/test/unit/modules/repository/config.json similarity index 100% rename from test/modules/repository/config.json rename to test/unit/modules/repository/config.json diff --git a/test/modules/repository/repository.js b/test/unit/modules/repository/repository.test.js similarity index 98% rename from test/modules/repository/repository.js rename to test/unit/modules/repository/repository.test.js index 05955685cc..f29b0703f1 100644 --- a/test/modules/repository/repository.js +++ b/test/unit/modules/repository/repository.test.js @@ -2,12 +2,12 @@ import { utils } from 'ethers'; import { describe, it, before, beforeEach, afterEach, after } from 'mocha'; import { expect, assert } from 'chai'; import { readFile } from 'fs/promises'; -import Logger from '../../../src/logger/logger.js'; -import RepositoryModuleManager from '../../../src/modules/repository/repository-module-manager.js'; +import Logger from '../../../../src/logger/logger.js'; +import RepositoryModuleManager from '../../../../src/modules/repository/repository-module-manager.js'; let logger; let repositoryModuleManager; -const config = JSON.parse(await readFile('./test/modules/repository/config.json')); +const config = JSON.parse(await readFile('./test/unit/modules/repository/config.json')); const blockchain = 'hardhat'; const createAgreement = ({ diff --git a/test/modules/triple-store/config.json b/test/unit/modules/triple-store/config.json similarity index 100% rename from test/modules/triple-store/config.json rename to test/unit/modules/triple-store/config.json diff --git a/test/modules/triple-store/triple-store.js b/test/unit/modules/triple-store/triple-store.test.js similarity index 100% rename from test/modules/triple-store/triple-store.js rename to test/unit/modules/triple-store/triple-store.test.js diff --git a/test/unit/modules/validation/config.json b/test/unit/modules/validation/config.json new file mode 100644 index 0000000000..c8832873eb --- /dev/null +++ b/test/unit/modules/validation/config.json @@ -0,0 +1,22 @@ +{ + "modules":{ + "validation":{ + "enabled":true, + "implementation":{ + "merkle-validation":{ + "enabled":true, + "package":"./validation/implementation/merkle-validation.js", + "config":{ + "database":"operationaldb-test", + "user":"root", + "password":"", + "port":"3306", + "host":"localhost", + "dialect":"mysql", + "logging":false + } + } + } + } + } +} diff --git a/test/unit/modules/validation/validation-manager.test.js b/test/unit/modules/validation/validation-manager.test.js new file mode 100644 index 0000000000..1d1398984d --- /dev/null +++ b/test/unit/modules/validation/validation-manager.test.js @@ -0,0 +1,25 @@ +import { describe, it, before, beforeEach, afterEach, after } from 'mocha'; +import { expect, assert } from 'chai'; +import { readFile } from 'fs/promises'; +import { formatAssertion, calculateRoot } from 'assertion-tools'; +import ValidationModuleManager from '../../../../src/modules/validation/validation-module-manager.js'; +import Logger from '../../../../src/logger/logger.js'; +import ModuleConfigValidation from '../../../../src/modules/module-config-validation.js'; +import assertions from '../../../assertions/assertions.js'; + +let validationManager; +const config = JSON.parse(await readFile('./test/unit/modules/validation/config.json', 'utf-8')); + +describe.only('Validation module manager test', async () => { + before('initialize base module manage', () => { + validationManager = new ValidationModuleManager({ + config, + logger: new Logger(), + moduleConfigValidation: new ModuleConfigValidation(), + }); + }); + it('validates module name', async () => { + const name = await validationManager.getName(); + console.log(name); + }); +}); From eebcdbc50d7c6c8557214ca2b03b468eb6842c3d Mon Sep 17 00:00:00 2001 From: Ana Markovic Date: Fri, 11 Aug 2023 10:33:06 +0200 Subject: [PATCH 2/8] adding unit tests for validation manager module --- package.json | 3 +- .../modules/repository/repository.test.js | 2 +- .../validation/validation-manager.test.js | 25 ------ .../validation-module-manager.test.js | 82 +++++++++++++++++++ 4 files changed, 84 insertions(+), 28 deletions(-) delete mode 100644 test/unit/modules/validation/validation-manager.test.js create mode 100644 test/unit/modules/validation/validation-module-manager.test.js diff --git a/package.json b/package.json index 6f240a9781..314cc26ffb 100644 --- a/package.json +++ b/package.json @@ -15,8 +15,7 @@ "kill:local_blockchain": "npx kill-port 8545", "test:bdd": "cucumber-js --fail-fast --format progress --format-options '{\"colorsEnabled\": true}' test/bdd/ --import test/bdd/steps/ --exit", "test:unit": "nyc --all mocha --exit $(find test/unit -name '*.js')", - "test": "mocha --exit $(find test/unit -name 'validation-manager.test.js')", - "test:modules": "nyc --all mocha --exit $(find test/unit/modules -name '*.js')", + "test:modules": "nyc --check-coverage mocha --exit $(find test/unit/modules -name '*.js')", "test:bdd:release": "cucumber-js --tags=@release --fail-fast --format progress --format-options '{\"colorsEnabled\": true}' test/bdd/ --import test/bdd/steps/", "test:bdd:publish-errors": "cucumber-js --tags=@publish-errors --fail-fast --format progress --format-options '{\"colorsEnabled\": true}' test/bdd/ --import test/bdd/steps/", "test:bdd:update-errors": "cucumber-js --tags=@update-errors --fail-fast --format progress --format-options '{\"colorsEnabled\": true}' test/bdd/ --import test/bdd/steps/", diff --git a/test/unit/modules/repository/repository.test.js b/test/unit/modules/repository/repository.test.js index f29b0703f1..793c99705f 100644 --- a/test/unit/modules/repository/repository.test.js +++ b/test/unit/modules/repository/repository.test.js @@ -391,7 +391,7 @@ describe('Repository module', () => { } } - describe('test load', () => { + describe.skip('test load', () => { describe('100_000 rows', () => { beforeEach(async function t() { this.timeout(0); diff --git a/test/unit/modules/validation/validation-manager.test.js b/test/unit/modules/validation/validation-manager.test.js deleted file mode 100644 index 1d1398984d..0000000000 --- a/test/unit/modules/validation/validation-manager.test.js +++ /dev/null @@ -1,25 +0,0 @@ -import { describe, it, before, beforeEach, afterEach, after } from 'mocha'; -import { expect, assert } from 'chai'; -import { readFile } from 'fs/promises'; -import { formatAssertion, calculateRoot } from 'assertion-tools'; -import ValidationModuleManager from '../../../../src/modules/validation/validation-module-manager.js'; -import Logger from '../../../../src/logger/logger.js'; -import ModuleConfigValidation from '../../../../src/modules/module-config-validation.js'; -import assertions from '../../../assertions/assertions.js'; - -let validationManager; -const config = JSON.parse(await readFile('./test/unit/modules/validation/config.json', 'utf-8')); - -describe.only('Validation module manager test', async () => { - before('initialize base module manage', () => { - validationManager = new ValidationModuleManager({ - config, - logger: new Logger(), - moduleConfigValidation: new ModuleConfigValidation(), - }); - }); - it('validates module name', async () => { - const name = await validationManager.getName(); - console.log(name); - }); -}); diff --git a/test/unit/modules/validation/validation-module-manager.test.js b/test/unit/modules/validation/validation-module-manager.test.js new file mode 100644 index 0000000000..adb5878093 --- /dev/null +++ b/test/unit/modules/validation/validation-module-manager.test.js @@ -0,0 +1,82 @@ +import { describe, it, before, beforeEach, afterEach, after } from 'mocha'; +import { expect, assert } from 'chai'; +import { readFile } from 'fs/promises'; +import { calculateRoot } from 'assertion-tools'; +import ValidationModuleManager from '../../../../src/modules/validation/validation-module-manager.js'; +import Logger from '../../../../src/logger/logger.js'; + +let validationManager; + +const config = JSON.parse(await readFile('./test/unit/modules/validation/config.json', 'utf-8')); +const assertion = [ + { + '@context': 'https://schema.org', + '@id': 'https://tesla.modelX/2321', + '@type': 'Car', + name: 'Tesla Model X', + brand: { + '@type': 'Brand', + name: 'Tesla', + }, + model: 'Model X', + manufacturer: { + '@type': 'Organization', + name: 'Tesla, Inc.', + }, + fuelType: 'Electric', + }, +]; +const hashFunctionId = 1; + +describe('Validation module manager', async () => { + beforeEach('initialize validation module manage', async () => { + validationManager = new ValidationModuleManager({ + config, + logger: new Logger(), + }); + + expect(await validationManager.initialize()).to.be.true; + }); + + it('validates module name', async () => { + const moduleName = await validationManager.getName(); + + expect(moduleName).to.equal('validation'); + }); + + it('validate calculation of root hash are matched', async () => { + const expectedRootHash = calculateRoot(assertion); + const calculatedRootHash = validationManager.calculateRoot(assertion); + + assert(expect(calculatedRootHash).to.exist); + expect(calculatedRootHash).to.equal(expectedRootHash); + }); + + it('validate merkle proof hash', async () => { + const calculatedMerkleHash = validationManager.getMerkleProof(assertion, 0); + + assert(expect(calculatedMerkleHash).to.exist); + expect(calculatedMerkleHash).to.be.instanceof(Object); + expect(calculatedMerkleHash).to.haveOwnProperty('leaf').and.to.be.a('string'); + expect(calculatedMerkleHash).to.haveOwnProperty('proof').and.to.be.a('array'); + }); + + it('validate getting function name', async () => { + const getFnHashName = validationManager.getHashFunctionName(hashFunctionId); + + assert(expect(getFnHashName).to.exist); + expect(getFnHashName).to.equal('sha256'); + }); + + it('validate successful calling function name', async () => { + const keyword = + '0xB0D4afd8879eD9F52b28595d31B441D079B2Ca0768e44dc71bf509adfccbea9df949f253afa56796a3a926203f90a1e4914247d3'; + const callFunction = await validationManager.callHashFunction(hashFunctionId, keyword); + + assert(expect(callFunction).to.exist); + expect(callFunction).to.be.a('string'); + expect(callFunction).to.equal( + '0x5fe7425e0d956e2cafeac276c3ee8e70f377b2bd14790bc6d4777c3e7ba63b46', + ); + }); +}); From 5403a34a0fdc43351cea489751bce7f5985d6c12 Mon Sep 17 00:00:00 2001 From: Ana Markovic Date: Fri, 11 Aug 2023 10:43:15 +0200 Subject: [PATCH 3/8] update package.json script --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 314cc26ffb..d7b44c103d 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "kill:local_blockchain": "npx kill-port 8545", "test:bdd": "cucumber-js --fail-fast --format progress --format-options '{\"colorsEnabled\": true}' test/bdd/ --import test/bdd/steps/ --exit", "test:unit": "nyc --all mocha --exit $(find test/unit -name '*.js')", - "test:modules": "nyc --check-coverage mocha --exit $(find test/unit/modules -name '*.js')", + "test:modules": "nyc --all mocha --exit $(find test/unit/modules -name '*.js')", "test:bdd:release": "cucumber-js --tags=@release --fail-fast --format progress --format-options '{\"colorsEnabled\": true}' test/bdd/ --import test/bdd/steps/", "test:bdd:publish-errors": "cucumber-js --tags=@publish-errors --fail-fast --format progress --format-options '{\"colorsEnabled\": true}' test/bdd/ --import test/bdd/steps/", "test:bdd:update-errors": "cucumber-js --tags=@update-errors --fail-fast --format progress --format-options '{\"colorsEnabled\": true}' test/bdd/ --import test/bdd/steps/", From 08ad45ce3e2ff074ffbcba19292aac6749fcf439 Mon Sep 17 00:00:00 2001 From: Ana Markovic Date: Fri, 11 Aug 2023 11:01:38 +0200 Subject: [PATCH 4/8] remove unused methods --- test/unit/modules/validation/validation-module-manager.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/modules/validation/validation-module-manager.test.js b/test/unit/modules/validation/validation-module-manager.test.js index adb5878093..4fcdfdeae6 100644 --- a/test/unit/modules/validation/validation-module-manager.test.js +++ b/test/unit/modules/validation/validation-module-manager.test.js @@ -1,4 +1,4 @@ -import { describe, it, before, beforeEach, afterEach, after } from 'mocha'; +import { describe, it, beforeEach } from 'mocha'; import { expect, assert } from 'chai'; import { readFile } from 'fs/promises'; import { calculateRoot } from 'assertion-tools'; From c9997bb166fafd9cfe0d473fa3bb124e57ae75d6 Mon Sep 17 00:00:00 2001 From: Ana Markovic Date: Tue, 15 Aug 2023 12:30:18 +0200 Subject: [PATCH 5/8] implement code review feedback and handle null in the validation functions --- .../validation/validation-module-manager.js | 12 ++++ test/unit/modules/validation/config.json | 10 +-- .../validation-module-manager.test.js | 67 ++++++++++++++++++- 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/src/modules/validation/validation-module-manager.js b/src/modules/validation/validation-module-manager.js index ce99f66cce..850adf0cda 100644 --- a/src/modules/validation/validation-module-manager.js +++ b/src/modules/validation/validation-module-manager.js @@ -7,24 +7,36 @@ class ValidationModuleManager extends BaseModuleManager { calculateRoot(assertion) { if (this.initialized) { + if (assertion === null) { + throw new Error('Assertion cannot be null!'); + } return this.getImplementation().module.calculateRoot(assertion); } } getMerkleProof(assertion, index) { if (this.initialized) { + if (assertion === null) { + throw new Error('Assertion cannot be null!'); + } return this.getImplementation().module.getMerkleProof(assertion, index); } } getHashFunctionName(hashFunctionId) { if (this.initialized) { + if (hashFunctionId === null) { + throw new Error('Function ID cannot be null!'); + } return this.getImplementation().module.getHashFunctionName(hashFunctionId); } } async callHashFunction(hashFunctionId, data) { if (this.initialized) { + if (hashFunctionId === null || data === null) { + throw new Error('Invalid function parameter'); + } return this.getImplementation().module.callHashFunction(hashFunctionId, data); } } diff --git a/test/unit/modules/validation/config.json b/test/unit/modules/validation/config.json index c8832873eb..406942f078 100644 --- a/test/unit/modules/validation/config.json +++ b/test/unit/modules/validation/config.json @@ -6,15 +6,7 @@ "merkle-validation":{ "enabled":true, "package":"./validation/implementation/merkle-validation.js", - "config":{ - "database":"operationaldb-test", - "user":"root", - "password":"", - "port":"3306", - "host":"localhost", - "dialect":"mysql", - "logging":false - } + "config":{} } } } diff --git a/test/unit/modules/validation/validation-module-manager.test.js b/test/unit/modules/validation/validation-module-manager.test.js index 4fcdfdeae6..18220b9451 100644 --- a/test/unit/modules/validation/validation-module-manager.test.js +++ b/test/unit/modules/validation/validation-module-manager.test.js @@ -26,15 +26,20 @@ const assertion = [ fuelType: 'Electric', }, ]; + const hashFunctionId = 1; +const keyword = + '0xB0D4afd8879eD9F52b28595d31B441D079B2Ca0768e44dc71bf509adfccbea9df949f253afa56796a3a926203f90a1e4914247d3'; -describe('Validation module manager', async () => { +describe.only('Validation module manager', async () => { beforeEach('initialize validation module manage', async () => { validationManager = new ValidationModuleManager({ config, logger: new Logger(), }); + validationManager.initialized = true; + expect(await validationManager.initialize()).to.be.true; }); @@ -52,6 +57,20 @@ describe('Validation module manager', async () => { expect(calculatedRootHash).to.equal(expectedRootHash); }); + it('root hash cannot be calculated without initialization', async () => { + validationManager.initialized = false; + const calculatedRootHash = validationManager.calculateRoot(assertion); + + assert(expect(calculatedRootHash).to.be.undefined); + }); + + it('root hash cannot be calculate if assertion is null', async () => { + expect(() => validationManager.calculateRoot(null)).to.throw( + Error, + 'Assertion cannot be null!', + ); + }); + it('validate merkle proof hash', async () => { const calculatedMerkleHash = validationManager.getMerkleProof(assertion, 0); @@ -61,6 +80,20 @@ describe('Validation module manager', async () => { expect(calculatedMerkleHash).to.haveOwnProperty('proof').and.to.be.a('array'); }); + it('merkle prof hash cannot be calculated without initialization', async () => { + validationManager.initialized = false; + const calculatedMerkleHash = validationManager.getMerkleProof(assertion, 0); + + assert(expect(calculatedMerkleHash).to.be.undefined); + }); + + it('merkle prof hash cannot be calculated if assertion is null', async () => { + expect(() => validationManager.getMerkleProof(null, 0)).to.throw( + Error, + 'Assertion cannot be null!', + ); + }); + it('validate getting function name', async () => { const getFnHashName = validationManager.getHashFunctionName(hashFunctionId); @@ -68,9 +101,14 @@ describe('Validation module manager', async () => { expect(getFnHashName).to.equal('sha256'); }); + it('cannot getting function name without initialization', async () => { + validationManager.initialized = false; + const getFnHashName = validationManager.getHashFunctionName(hashFunctionId); + + assert(expect(getFnHashName).to.be.undefined); + }); + it('validate successful calling function name', async () => { - const keyword = - '0xB0D4afd8879eD9F52b28595d31B441D079B2Ca0768e44dc71bf509adfccbea9df949f253afa56796a3a926203f90a1e4914247d3'; const callFunction = await validationManager.callHashFunction(hashFunctionId, keyword); assert(expect(callFunction).to.exist); @@ -79,4 +117,27 @@ describe('Validation module manager', async () => { '0x5fe7425e0d956e2cafeac276c3ee8e70f377b2bd14790bc6d4777c3e7ba63b46', ); }); + + it('unsuccessful calling function name without initialization', async () => { + validationManager.initialized = false; + const callFunction = await validationManager.callHashFunction(hashFunctionId, keyword); + + assert(expect(callFunction).to.be.undefined); + }); + + it('unsuccessful function name initialization when function id is null', async () => { + try { + await validationManager.callHashFunction(null, keyword); + } catch (error) { + expect(error.message).to.equal('Invalid function parameter'); + } + }); + + it('unsuccessful function name initialization when data is null', async () => { + try { + await validationManager.callHashFunction(hashFunctionId, null); + } catch (error) { + expect(error.message).to.equal('Invalid function parameter'); + } + }); }); From bac12154fdacea033df727b95fb5d80a0e242944 Mon Sep 17 00:00:00 2001 From: Ana Markovic Date: Tue, 15 Aug 2023 12:37:42 +0200 Subject: [PATCH 6/8] update test description --- .../validation/validation-module-manager.test.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/test/unit/modules/validation/validation-module-manager.test.js b/test/unit/modules/validation/validation-module-manager.test.js index 18220b9451..aa1bf1d68b 100644 --- a/test/unit/modules/validation/validation-module-manager.test.js +++ b/test/unit/modules/validation/validation-module-manager.test.js @@ -26,12 +26,11 @@ const assertion = [ fuelType: 'Electric', }, ]; - const hashFunctionId = 1; const keyword = '0xB0D4afd8879eD9F52b28595d31B441D079B2Ca0768e44dc71bf509adfccbea9df949f253afa56796a3a926203f90a1e4914247d3'; -describe.only('Validation module manager', async () => { +describe('Validation module manager', async () => { beforeEach('initialize validation module manage', async () => { validationManager = new ValidationModuleManager({ config, @@ -39,7 +38,6 @@ describe.only('Validation module manager', async () => { }); validationManager.initialized = true; - expect(await validationManager.initialize()).to.be.true; }); @@ -64,14 +62,14 @@ describe.only('Validation module manager', async () => { assert(expect(calculatedRootHash).to.be.undefined); }); - it('root hash cannot be calculate if assertion is null', async () => { + it('root hash calculation failed when assertion is null', async () => { expect(() => validationManager.calculateRoot(null)).to.throw( Error, 'Assertion cannot be null!', ); }); - it('validate merkle proof hash', async () => { + it('successful getting merkle proof hash', async () => { const calculatedMerkleHash = validationManager.getMerkleProof(assertion, 0); assert(expect(calculatedMerkleHash).to.exist); @@ -87,7 +85,7 @@ describe.only('Validation module manager', async () => { assert(expect(calculatedMerkleHash).to.be.undefined); }); - it('merkle prof hash cannot be calculated if assertion is null', async () => { + it('failed merkle prof hash calculation when assertion is null', async () => { expect(() => validationManager.getMerkleProof(null, 0)).to.throw( Error, 'Assertion cannot be null!', @@ -101,7 +99,7 @@ describe.only('Validation module manager', async () => { expect(getFnHashName).to.equal('sha256'); }); - it('cannot getting function name without initialization', async () => { + it('failed getting function name without initialization', async () => { validationManager.initialized = false; const getFnHashName = validationManager.getHashFunctionName(hashFunctionId); @@ -125,7 +123,7 @@ describe.only('Validation module manager', async () => { assert(expect(callFunction).to.be.undefined); }); - it('unsuccessful function name initialization when function id is null', async () => { + it('failed function name initialization when function id is null', async () => { try { await validationManager.callHashFunction(null, keyword); } catch (error) { @@ -133,7 +131,7 @@ describe.only('Validation module manager', async () => { } }); - it('unsuccessful function name initialization when data is null', async () => { + it('failed function name initialization when data is null', async () => { try { await validationManager.callHashFunction(hashFunctionId, null); } catch (error) { From 60790c3595252aa9d0d56f9d59b47e647ce2ddd6 Mon Sep 17 00:00:00 2001 From: Ana Markovic Date: Wed, 16 Aug 2023 21:34:16 +0200 Subject: [PATCH 7/8] fix eslint errors --- .../validation/validation-module-manager.js | 25 +++-- .../validation-module-manager.test.js | 97 +++++++++++++------ 2 files changed, 82 insertions(+), 40 deletions(-) diff --git a/src/modules/validation/validation-module-manager.js b/src/modules/validation/validation-module-manager.js index 850adf0cda..252a68d6ac 100644 --- a/src/modules/validation/validation-module-manager.js +++ b/src/modules/validation/validation-module-manager.js @@ -7,37 +7,44 @@ class ValidationModuleManager extends BaseModuleManager { calculateRoot(assertion) { if (this.initialized) { - if (assertion === null) { - throw new Error('Assertion cannot be null!'); + if (!assertion) { + throw new Error('Calculation failed: Assertion cannot be null or undefined.'); } return this.getImplementation().module.calculateRoot(assertion); } + throw new Error('Validation module is not initialized.'); } getMerkleProof(assertion, index) { if (this.initialized) { - if (assertion === null) { - throw new Error('Assertion cannot be null!'); + if (!assertion) { + throw new Error('Get merkle proof failed: Assertion cannot be null or undefined.'); } return this.getImplementation().module.getMerkleProof(assertion, index); } + throw new Error('Validation module is not initialized.'); } getHashFunctionName(hashFunctionId) { if (this.initialized) { - if (hashFunctionId === null) { - throw new Error('Function ID cannot be null!'); + if (!hashFunctionId) { + throw new Error( + 'Getting function name failed: Function ID cannot be null or undefined.', + ); } return this.getImplementation().module.getHashFunctionName(hashFunctionId); } + throw new Error('Validation module is not initialized.'); } async callHashFunction(hashFunctionId, data) { if (this.initialized) { - if (hashFunctionId === null || data === null) { - throw new Error('Invalid function parameter'); + if (!!hashFunctionId || !!data) { + return this.getImplementation().module.callHashFunction(hashFunctionId, data); } - return this.getImplementation().module.callHashFunction(hashFunctionId, data); + throw new Error('Calling hash fn failed: Values cannot be null or undefined.'); + } else { + throw new Error('Validation module is not initialized.'); } } } diff --git a/test/unit/modules/validation/validation-module-manager.test.js b/test/unit/modules/validation/validation-module-manager.test.js index aa1bf1d68b..4cabe5041a 100644 --- a/test/unit/modules/validation/validation-module-manager.test.js +++ b/test/unit/modules/validation/validation-module-manager.test.js @@ -26,11 +26,12 @@ const assertion = [ fuelType: 'Electric', }, ]; +const invalidValues = [null, undefined]; const hashFunctionId = 1; const keyword = '0xB0D4afd8879eD9F52b28595d31B441D079B2Ca0768e44dc71bf509adfccbea9df949f253afa56796a3a926203f90a1e4914247d3'; -describe('Validation module manager', async () => { +describe.only('Validation module manager', async () => { beforeEach('initialize validation module manage', async () => { validationManager = new ValidationModuleManager({ config, @@ -41,13 +42,13 @@ describe('Validation module manager', async () => { expect(await validationManager.initialize()).to.be.true; }); - it('validates module name', async () => { + it('validates module name is as expected', async () => { const moduleName = await validationManager.getName(); expect(moduleName).to.equal('validation'); }); - it('validate calculation of root hash are matched', async () => { + it('validate successful root hash calculation, expect to be matched', async () => { const expectedRootHash = calculateRoot(assertion); const calculatedRootHash = validationManager.calculateRoot(assertion); @@ -57,16 +58,21 @@ describe('Validation module manager', async () => { it('root hash cannot be calculated without initialization', async () => { validationManager.initialized = false; - const calculatedRootHash = validationManager.calculateRoot(assertion); - assert(expect(calculatedRootHash).to.be.undefined); + try { + validationManager.calculateRoot(assertion); + } catch (error) { + expect(error.message).to.equal('Validation module is not initialized.'); + } }); - it('root hash calculation failed when assertion is null', async () => { - expect(() => validationManager.calculateRoot(null)).to.throw( - Error, - 'Assertion cannot be null!', - ); + it('root hash calculation failed when assertion is null or undefined', async () => { + invalidValues.forEach((value) => { + expect(() => validationManager.calculateRoot(value)).to.throw( + Error, + 'Calculation failed: Assertion cannot be null or undefined.', + ); + }); }); it('successful getting merkle proof hash', async () => { @@ -80,16 +86,21 @@ describe('Validation module manager', async () => { it('merkle prof hash cannot be calculated without initialization', async () => { validationManager.initialized = false; - const calculatedMerkleHash = validationManager.getMerkleProof(assertion, 0); - assert(expect(calculatedMerkleHash).to.be.undefined); + try { + validationManager.getMerkleProof(assertion, 0); + } catch (error) { + expect(error.message).to.equal('Validation module is not initialized.'); + } }); - it('failed merkle prof hash calculation when assertion is null', async () => { - expect(() => validationManager.getMerkleProof(null, 0)).to.throw( - Error, - 'Assertion cannot be null!', - ); + it('failed merkle prof hash calculation when assertion is null or undefined', async () => { + invalidValues.forEach((value) => { + expect(() => validationManager.getMerkleProof(value, 0)).to.throw( + Error, + 'Get merkle proof failed: Assertion cannot be null or undefined.', + ); + }); }); it('validate getting function name', async () => { @@ -101,9 +112,12 @@ describe('Validation module manager', async () => { it('failed getting function name without initialization', async () => { validationManager.initialized = false; - const getFnHashName = validationManager.getHashFunctionName(hashFunctionId); - assert(expect(getFnHashName).to.be.undefined); + try { + validationManager.getHashFunctionName(hashFunctionId); + } catch (error) { + expect(error.message).to.equal('Validation module is not initialized.'); + } }); it('validate successful calling function name', async () => { @@ -118,24 +132,45 @@ describe('Validation module manager', async () => { it('unsuccessful calling function name without initialization', async () => { validationManager.initialized = false; - const callFunction = await validationManager.callHashFunction(hashFunctionId, keyword); - assert(expect(callFunction).to.be.undefined); - }); - - it('failed function name initialization when function id is null', async () => { try { - await validationManager.callHashFunction(null, keyword); + await validationManager.callHashFunction(hashFunctionId, keyword); } catch (error) { - expect(error.message).to.equal('Invalid function parameter'); + expect(error.message).to.equal('Validation module is not initialized.'); } }); - it('failed function name initialization when data is null', async () => { - try { - await validationManager.callHashFunction(hashFunctionId, null); - } catch (error) { - expect(error.message).to.equal('Invalid function parameter'); + it('failed function name initialization when function id is null or undefined', async () => { + async function testInvalidValues() { + for (const value of invalidValues) { + try { + // eslint-disable-next-line no-await-in-loop + await validationManager.getMerkleProof(value, 0); + } catch (error) { + expect(error.message).to.equal( + 'Get merkle proof failed: Assertion cannot be null or undefined.', + ); + } + } + } + + await testInvalidValues(); + }); + + it('failed function name initialization when data is null or undefined', async () => { + async function testInvalidValues() { + for (const value of invalidValues) { + try { + // eslint-disable-next-line no-await-in-loop + await validationManager.callHashFunction(value, 0); + } catch (error) { + expect(error.message).to.equal( + 'Calling hash fn failed: Values cannot be null or undefined.', + ); + } + } } + + await testInvalidValues(); }); }); From acb847aaac0e87f7a56a452d28dc56078dcab929 Mon Sep 17 00:00:00 2001 From: Ana Markovic Date: Fri, 18 Aug 2023 10:26:36 +0200 Subject: [PATCH 8/8] update operator from OR to AND --- src/modules/validation/validation-module-manager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/validation/validation-module-manager.js b/src/modules/validation/validation-module-manager.js index 252a68d6ac..8f326905ae 100644 --- a/src/modules/validation/validation-module-manager.js +++ b/src/modules/validation/validation-module-manager.js @@ -39,7 +39,7 @@ class ValidationModuleManager extends BaseModuleManager { async callHashFunction(hashFunctionId, data) { if (this.initialized) { - if (!!hashFunctionId || !!data) { + if (!!hashFunctionId && !!data) { return this.getImplementation().module.callHashFunction(hashFunctionId, data); } throw new Error('Calling hash fn failed: Values cannot be null or undefined.');