• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2024 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    getSpecParamsValue(specs) {
52        let specParams = [];
53        let specDesc = this.specService.getCurrentRunningSpec().description;
54        if (specs === null || specs === undefined) {
55            return specParams;
56        }
57        for (const specItem of specs) {
58            if (ObjectUtils.has(specItem, IT_KEY) && ObjectUtils.get(specItem, IT_KEY) === specDesc) {
59                return ObjectUtils.get(specItem, PARAMS_KEY, specParams);
60            }
61        }
62        return specParams;
63    }
64
65    getSpecParams() {
66        let specParams = [];
67        let suiteDesc = this.suiteService.getCurrentRunningSuite().description;
68        let specDesc = this.specService.getCurrentRunningSpec().description;
69        let suites = ObjectUtils.get(this.data, SUITES_KEY, []);
70        for (const suiteItem of suites) {
71            let describeValue = ObjectUtils.get(suiteItem, DESCRIBE_KEY, '');
72            if (ObjectUtils.has(suiteItem, DESCRIBE_KEY) && (typeof describeValue === 'object') && describeValue.constructor === Array && describeValue.includes(suiteDesc)) {
73                let specs = ObjectUtils.get(suiteItem, SPECS_KEY, []);
74                return this.getSpecParamsValue(specs);
75            }
76        }
77        return specParams;
78    }
79
80    getSuiteParams() {
81        let suiteParams = {};
82        let suiteDesc = this.suiteService.getCurrentRunningSuite().description;
83        let suites = ObjectUtils.get(this.data, SUITES_KEY, []);
84        for (const suiteItem of suites) {
85            let describeValue = ObjectUtils.get(suiteItem, DESCRIBE_KEY, []);
86            if (ObjectUtils.has(suiteItem, DESCRIBE_KEY) && (typeof describeValue === 'object') && describeValue.constructor === Array && describeValue.includes(suiteDesc)) {
87                suiteParams = Object.assign({}, suiteParams, ObjectUtils.get(suiteItem, PARAMS_KEY, suiteParams));
88            }
89        }
90        return suiteParams;
91    }
92
93    getStressNum(specs, specDesc) {
94        let stress = 1;
95        if (specs === null || specs === undefined) {
96            return stress;
97        }
98        for (const specItem of specs) {
99            if (ObjectUtils.has(specItem, IT_KEY) && ObjectUtils.get(specItem, IT_KEY) === specDesc) {
100                let tempStress = ObjectUtils.get(specItem, STRESS_KEY, stress);
101                return (Number.isInteger(tempStress) && tempStress >= 1) ? tempStress : stress;
102            }
103        }
104        return stress;
105    }
106
107    getSpecStress(specDesc) {
108        let stress = 1;
109        let suiteDesc = this.suiteService.getCurrentRunningSuite().description;
110        let suites = ObjectUtils.get(this.data, SUITES_KEY, []);
111        for (const suiteItem of suites) {
112            let describeValue = ObjectUtils.get(suiteItem, DESCRIBE_KEY, '');
113            if (ObjectUtils.has(suiteItem, DESCRIBE_KEY) && (typeof describeValue === 'object') && describeValue.constructor === Array && describeValue.includes(suiteDesc)) {
114                let specs = ObjectUtils.get(suiteItem, SPECS_KEY, []);
115                return this.getStressNum(specs, specDesc);
116            }
117        }
118        return stress;
119    }
120
121    getSuiteStress(suiteDesc) {
122        let stress = 1;
123        let suites = ObjectUtils.get(this.data, SUITES_KEY, []);
124        for (const suiteItem of suites) {
125            let describeValue = ObjectUtils.get(suiteItem, DESCRIBE_KEY, []);
126            if (ObjectUtils.has(suiteItem, DESCRIBE_KEY) && (typeof describeValue === 'object') && describeValue.constructor === Array && describeValue.includes(suiteDesc)) {
127                let tempStress = ObjectUtils.get(suiteItem, STRESS_KEY, stress);
128                return (Number.isInteger(tempStress) && tempStress >= 1) ? tempStress : stress;
129            }
130        }
131        return stress;
132    }
133}
134
135export default DataDriver;
136