-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add base classes for declarative workload module and
unit tests for the base classes. Signed-off-by: Aastha Bist <[email protected]>
- Loading branch information
1 parent
4ecfca7
commit 0053563
Showing
8 changed files
with
620 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
...ore/lib/worker/workload/declarative/value-providers/parameter-reference-value-provider.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const ValueProviderInterface = require('./value-provider-interface'); | ||
|
||
/** | ||
* Class with implementation of ParameterReferenceValueProvider | ||
*/ | ||
class ParameterReferenceValueProvider extends ValueProviderInterface { | ||
/** | ||
* Initialize an instance of ParameterReferenceValueProvider | ||
* @param {object} options Options for configured parameter generation | ||
* @param {object} variables Caliper provided variables | ||
* @param {object} parameters User provided variables | ||
* @param {object} valueProviderFactory ValueProviderFactory object reference | ||
*/ | ||
constructor(options, variables, parameters, valueProviderFactory) { | ||
super(options, variables, parameters, valueProviderFactory); | ||
|
||
if(this.options === undefined) { | ||
throw new Error(`Incorrect options value: ${this.options}`); | ||
} | ||
|
||
if(this.parameters === undefined) { | ||
throw new Error(`Incorrect parameters value: ${this.parameters}`); | ||
} | ||
|
||
if ( | ||
this.options.name === undefined || | ||
typeof this.options.name !== 'string' | ||
) { | ||
throw new Error( | ||
`Incorrect parameter name: ${options.name}` | ||
); | ||
} | ||
|
||
if (this.parameters[this.options.name] === undefined) { | ||
throw new Error(`Parameter ${this.options.name} cannot be found among available parameters`); | ||
} | ||
} | ||
/** | ||
* Generates value for corresponding parameter | ||
* @returns {any} value for parameter according to options.name | ||
*/ | ||
generateValue() { | ||
return this.parameters[this.options.name]; | ||
} | ||
} | ||
|
||
module.exports = ParameterReferenceValueProvider; |
76 changes: 76 additions & 0 deletions
76
...per-core/lib/worker/workload/declarative/value-providers/uniform-random-value-provider.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const ValueProviderInterface = require('./value-provider-interface'); | ||
|
||
/** | ||
* Class with implementation of UniformRandomValueProvider | ||
*/ | ||
class UniformRandomValueProvider extends ValueProviderInterface { | ||
/** | ||
* Initialize an instance of UniformRandomValueProvider | ||
* @param {object} options Options for configured parameter generation | ||
* @param {object} variables Caliper provided variables | ||
* @param {object} parameters User provided variables | ||
* @param {object} valueProviderFactory ValueProviderFactory object reference | ||
*/ | ||
constructor(options, variables, parameters, valueProviderFactory) { | ||
super(options, variables, parameters, valueProviderFactory); | ||
|
||
if (this.options === undefined){ | ||
throw new Error(`Incorrect options value: ${this.options}`); | ||
} | ||
|
||
if (this.options.min === undefined || typeof this.options.min !== 'number') { | ||
throw new Error( | ||
`Incorrect configuration for min: ${this.options.min}` | ||
); | ||
} | ||
|
||
if (this.options.max === undefined || typeof this.options.max !== 'number') { | ||
throw new Error( | ||
`Incorrect configuration for max: ${this.options.max}` | ||
); | ||
} | ||
|
||
/** | ||
* Minimum possible generated Random Number from given range in options | ||
* @type {number} | ||
*/ | ||
this.min = this.options.min; | ||
|
||
/** | ||
* Maximum possible generated Random Number from given range in options | ||
* @type {number} | ||
*/ | ||
this.max = this.options.max; | ||
|
||
if (this.min > this.max) { | ||
const temp = this.min; | ||
this.min = this.max; | ||
this.max = temp; | ||
} | ||
} | ||
/** | ||
* Generate values for corresponding options provided | ||
* @returns {number} Generated random value in given inclusive range | ||
*/ | ||
generateValue() { | ||
return Math.floor(Math.random() * (this.max - this.min + 1)) + this.min; | ||
} | ||
} | ||
|
||
module.exports = UniformRandomValueProvider; |
49 changes: 49 additions & 0 deletions
49
.../caliper-core/lib/worker/workload/declarative/value-providers/value-provider-interface.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
/** | ||
* Interface for value providers | ||
*/ | ||
class ValueProviderInterface { | ||
/** | ||
* Initialize an instance of ValueProviderInterface | ||
* @param {object} options Options for configured parameter generation | ||
* @param {object} variables Caliper provided variables | ||
* @param {object} parameters User provided variables | ||
* @param {object} valueProviderFactory ValueProviderFactory object reference | ||
*/ | ||
constructor( | ||
options, | ||
variables, | ||
parameters, | ||
valueProviderFactory | ||
) { | ||
this.options = options; | ||
this.variables = variables; | ||
this.parameters = parameters; | ||
this.valueProviderFactory = valueProviderFactory; | ||
} | ||
/** | ||
* Generate values for corresponding options provided | ||
* @returns {object} Generated Value | ||
*/ | ||
generateValue() { | ||
throw new Error('generateValue() must be implemented'); | ||
// eslint-disable-next-line no-unreachable | ||
return undefined; | ||
} | ||
} | ||
|
||
module.exports = ValueProviderInterface; |
63 changes: 63 additions & 0 deletions
63
...core/lib/worker/workload/declarative/value-providers/variable-reference-value-provider.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const ValueProviderInterface = require('./value-provider-interface'); | ||
|
||
/** | ||
* Class with implementation of VariableReferenceValueProvider | ||
*/ | ||
class VariableReferenceValueProvider extends ValueProviderInterface { | ||
/** | ||
* Initialize an instance of VariableReferenceValueProvider | ||
* @param {object} options Options for configured parameter generation | ||
* @param {object} variables Caliper provided variables | ||
* @param {object} parameters User provided variables | ||
* @param {object} valueProviderFactory ValueProviderFactory object reference | ||
*/ | ||
constructor(options, variables, parameters, valueProviderFactory) { | ||
super(options, variables, parameters, valueProviderFactory); | ||
|
||
if(this.options === undefined) { | ||
throw new Error(`Incorrect options value: ${this.options}`); | ||
} | ||
|
||
if(this.variables === undefined) { | ||
throw new Error(`Incorrect variables value: ${this.variables}`); | ||
} | ||
|
||
if ( | ||
this.options.name === undefined || | ||
typeof this.options.name !== 'string' | ||
) { | ||
throw new Error( | ||
`Incorrect variable name: ${options.name}` | ||
); | ||
} | ||
|
||
if (this.variables[this.options.name] === undefined) { | ||
throw new Error(`Variable ${this.options.name} cannot be found among available variables`); | ||
} | ||
} | ||
/** | ||
* Generates value for corresponding variable | ||
* @returns {any} value for variable according to options.name | ||
*/ | ||
generateValue() { | ||
return this.variables[this.options.name]; | ||
} | ||
} | ||
|
||
module.exports = VariableReferenceValueProvider; |
94 changes: 94 additions & 0 deletions
94
...st/worker/workload/declarative/value-providers/parameter-reference-value-provider.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
const chai = require('chai'); | ||
const expect = chai.expect; | ||
const ParameterReferenceValueProvider = require('../../../../../lib/worker/workload/declarative/value-providers/parameter-reference-value-provider'); | ||
|
||
describe('ParameterReferenceValueProvider', () => { | ||
describe('Constructor', () => { | ||
it('should throw an error on undefined options', () => { | ||
const wrapper = () => new ParameterReferenceValueProvider(undefined, {}, {}, {}); | ||
expect(wrapper).to.throw(Error, 'Incorrect options value: undefined'); | ||
}); | ||
|
||
it('should throw an error on undefined parameters', () => { | ||
const wrapper = () => new ParameterReferenceValueProvider({}, {}, undefined, {}); | ||
expect(wrapper).to.throw(Error, 'Incorrect parameters value: undefined'); | ||
}); | ||
it('should throw an error on undefined name in options', () => { | ||
const options = {}; | ||
const parameters = { | ||
marbleNumber: 1 | ||
}; | ||
|
||
const wrapper = () => new ParameterReferenceValueProvider(options, {}, parameters, {}); | ||
expect(wrapper).to.throw(Error, 'Incorrect parameter name: undefined'); | ||
}); | ||
|
||
it('should throw an error on non-string name in options', () =>{ | ||
const options = { | ||
name: 2 | ||
}; | ||
const parameters = { | ||
marbleNumber: 1 | ||
}; | ||
|
||
const wrapper = () => new ParameterReferenceValueProvider(options, {}, parameters, {}); | ||
expect(wrapper).to.throw(Error, 'Incorrect parameter name: 2'); | ||
}); | ||
|
||
it('should throw an error on undefined name in parameters', () => { | ||
const options = { | ||
name: 'marbleName' | ||
}; | ||
const parameters = { | ||
marbleNumber: 1 | ||
}; | ||
|
||
const wrapper = () => new ParameterReferenceValueProvider(options, {}, parameters, {}); | ||
expect(wrapper).to.throw(Error, 'Parameter marbleName cannot be found among available parameters'); | ||
}); | ||
}); | ||
|
||
describe('generateValue', () => { | ||
it('should return a numerical parameter value', () => { | ||
const options = { | ||
name: 'marbleNumber' | ||
}; | ||
const parameters = { | ||
marbleNumber: 1 | ||
}; | ||
const valueProvider = new ParameterReferenceValueProvider(options, {}, parameters, {}); | ||
const result = valueProvider.generateValue(); | ||
|
||
expect(result).to.equal(1); | ||
}); | ||
|
||
it('should return a string parameter value', () => { | ||
const options = { | ||
name: 'marbleNumber' | ||
}; | ||
const parameters = { | ||
marbleNumber: 'answer' | ||
}; | ||
|
||
const valueProvider = new ParameterReferenceValueProvider(options, {}, parameters, {}); | ||
const result = valueProvider.generateValue(); | ||
|
||
expect(result).to.equal('answer'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.