1/* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16const SUITES_KEY = 'suites'; 17const SPECS_KEY = 'items'; 18const DESCRIBE_KEY = 'describe'; 19const IT_KEY = 'it'; 20const PARAMS_KEY = 'params'; 21const STRESS_KEY = 'stress'; 22 23class ObjectUtils { 24 static get(object, name, defaultValue) { 25 let result = defaultValue; 26 for (const key in object) { 27 if (key === name) { 28 return object[key]; 29 } 30 } 31 return result; 32 } 33 34 static has(object, key) { 35 return Object.prototype.hasOwnProperty.call(object, key); 36 } 37} 38 39class DataDriver { 40 constructor(attr) { 41 this.id = 'dataDriver'; 42 this.data = attr.data || {}; 43 } 44 45 init(coreContext) { 46 this.coreContext = coreContext; 47 this.suiteService = this.coreContext.getDefaultService('suite'); 48 this.specService = this.coreContext.getDefaultService('spec'); 49 } 50 51 getSpecParams() { 52 let specParams = []; 53 let suiteDesc = this.suiteService.getCurrentRunningSuite().description; 54 let specDesc = this.specService.getCurrentRunningSpec().description; 55 let suites = ObjectUtils.get(this.data, SUITES_KEY, []); 56 for (const suiteItem of suites) { 57 let describeValue = ObjectUtils.get(suiteItem, DESCRIBE_KEY, ''); 58 if (ObjectUtils.has(suiteItem, DESCRIBE_KEY) && (typeof describeValue === 'object') && describeValue.constructor === Array && describeValue.includes(suiteDesc)) { 59 let specs = ObjectUtils.get(suiteItem, SPECS_KEY, []); 60 for (const specItem of specs) { 61 if (ObjectUtils.has(specItem, IT_KEY) && ObjectUtils.get(specItem, IT_KEY) === specDesc) { 62 return ObjectUtils.get(specItem, PARAMS_KEY, specParams); 63 } 64 } 65 } 66 } 67 return specParams; 68 } 69 70 getSuiteParams() { 71 let suiteParams = {}; 72 let suiteDesc = this.suiteService.getCurrentRunningSuite().description; 73 let suites = ObjectUtils.get(this.data, SUITES_KEY, []); 74 for (const suiteItem of suites) { 75 let describeValue = ObjectUtils.get(suiteItem, DESCRIBE_KEY, []); 76 if (ObjectUtils.has(suiteItem, DESCRIBE_KEY) && (typeof describeValue === 'object') && describeValue.constructor === Array && describeValue.includes(suiteDesc)) { 77 suiteParams = Object.assign({}, suiteParams, ObjectUtils.get(suiteItem, PARAMS_KEY, suiteParams)); 78 } 79 } 80 return suiteParams; 81 } 82 83 getSpecStress(specDesc) { 84 let stress = 1; 85 let suiteDesc = this.suiteService.getCurrentRunningSuite().description; 86 let suites = ObjectUtils.get(this.data, SUITES_KEY, []); 87 for (const suiteItem of suites) { 88 let describeValue = ObjectUtils.get(suiteItem, DESCRIBE_KEY, ''); 89 if (ObjectUtils.has(suiteItem, DESCRIBE_KEY) && (typeof describeValue === 'object') && describeValue.constructor === Array && describeValue.includes(suiteDesc)) { 90 let specs = ObjectUtils.get(suiteItem, SPECS_KEY, []); 91 for (const specItem of specs) { 92 if (ObjectUtils.has(specItem, IT_KEY) && ObjectUtils.get(specItem, IT_KEY) === specDesc) { 93 let tempStress = ObjectUtils.get(specItem, STRESS_KEY, stress); 94 return (Number.isInteger(tempStress) && tempStress >= 1) ? tempStress : stress; 95 } 96 } 97 } 98 } 99 return stress; 100 } 101 102 getSuiteStress(suiteDesc) { 103 let stress = 1; 104 let suites = ObjectUtils.get(this.data, SUITES_KEY, []); 105 for (const suiteItem of suites) { 106 let describeValue = ObjectUtils.get(suiteItem, DESCRIBE_KEY, []); 107 if (ObjectUtils.has(suiteItem, DESCRIBE_KEY) && (typeof describeValue === 'object') && describeValue.constructor === Array && describeValue.includes(suiteDesc)) { 108 let tempStress = ObjectUtils.get(suiteItem, STRESS_KEY, stress); 109 return (Number.isInteger(tempStress) && tempStress >= 1) ? tempStress : stress; 110 } 111 } 112 return stress; 113 } 114} 115 116export default DataDriver; 117