• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
15const SUITES_KEY = 'suites'
16const SPECS_KEY = 'items'
17const DESCRIBE_KEY = 'describe'
18const IT_KEY = 'it'
19const PARAMS_KEY = 'params'
20const STRESS_KEY = 'stress'
21
22class ObjectUtils {
23  static get (object, name, defaultValue) {
24    let result = defaultValue
25    for (const key in object) {
26      if (key === name) {
27        return object[key]
28      }
29    }
30    return result
31  }
32
33  static has (object, key) {
34    return Object.prototype.hasOwnProperty.call(object, key)
35  }
36}
37
38class DataDriver {
39  constructor (attr) {
40    this.id = 'dataDriver'
41    this.data = attr.data || {}
42  }
43
44  init (coreContext) {
45    this.coreContext = coreContext
46    this.suiteService = this.coreContext.getDefaultService('suite')
47    this.specService = this.coreContext.getDefaultService('spec')
48  }
49
50  getSpecParams () {
51    let specParams = []
52    let suiteDesc = this.suiteService.getCurrentRunningSuite().description
53    let specDesc = this.specService.getCurrentRunningSpec().description
54    let suites = ObjectUtils.get(this.data, SUITES_KEY, [])
55    for (const suiteItem of suites) {
56      let describeValue = ObjectUtils.get(suiteItem, DESCRIBE_KEY, '')
57      if (ObjectUtils.has(suiteItem, DESCRIBE_KEY) && describeValue === suiteDesc && (typeof describeValue === 'string') && describeValue.constructor === String) {
58        let specs = ObjectUtils.get(suiteItem, SPECS_KEY, [])
59        for (const specItem of specs) {
60          if (ObjectUtils.has(specItem, IT_KEY) && ObjectUtils.get(specItem, IT_KEY) === specDesc) {
61            return ObjectUtils.get(specItem, PARAMS_KEY, specParams)
62          }
63        }
64      }
65    }
66    return specParams
67  }
68
69  getSuiteParams () {
70    let suiteParams = {}
71    let suiteDesc = this.suiteService.getCurrentRunningSuite().description
72    let suites = ObjectUtils.get(this.data, SUITES_KEY, [])
73    for (const suiteItem of suites) {
74      let describeValue = ObjectUtils.get(suiteItem, DESCRIBE_KEY, [])
75      if (ObjectUtils.has(suiteItem, DESCRIBE_KEY) && (typeof describeValue === 'object') && describeValue.constructor === Array && describeValue.includes(suiteDesc)) {
76        suiteParams = Object.assign({}, suiteParams, ObjectUtils.get(suiteItem, PARAMS_KEY, suiteParams))
77      }
78    }
79    return suiteParams
80  }
81
82  getSpecStress (specDesc) {
83    let stress = 1
84    let suiteDesc = this.suiteService.getCurrentRunningSuite().description
85    let suites = ObjectUtils.get(this.data, SUITES_KEY, [])
86    for (const suiteItem of suites) {
87      let describeValue = ObjectUtils.get(suiteItem, DESCRIBE_KEY, '')
88      if (ObjectUtils.has(suiteItem, DESCRIBE_KEY) && describeValue === suiteDesc && (typeof describeValue === 'string') && describeValue.constructor === String) {
89        let specs = ObjectUtils.get(suiteItem, SPECS_KEY, [])
90        for (const specItem of specs) {
91          if (ObjectUtils.has(specItem, IT_KEY) && ObjectUtils.get(specItem, IT_KEY) === specDesc) {
92            let tempStress = ObjectUtils.get(specItem, STRESS_KEY, stress)
93            return (Number.isInteger(tempStress) && tempStress >= 1) ? tempStress : stress
94          }
95        }
96      }
97    }
98    return stress
99  }
100
101  getSuiteStress (suiteDesc) {
102    let stress = 1
103    let suites = ObjectUtils.get(this.data, SUITES_KEY, [])
104    for (const suiteItem of suites) {
105      let describeValue = ObjectUtils.get(suiteItem, DESCRIBE_KEY, [])
106      if (ObjectUtils.has(suiteItem, DESCRIBE_KEY) && describeValue === suiteDesc && (typeof describeValue === 'string') && describeValue.constructor === String) {
107        let tempStress = ObjectUtils.get(suiteItem, STRESS_KEY, stress)
108        return (Number.isInteger(tempStress) && tempStress >= 1) ? tempStress : stress
109      }
110    }
111    return stress
112  }
113}
114
115export default DataDriver
116