• 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 */
15import { ClassFilter, NotClassFilter, SuiteAndItNameFilter, TestTypesFilter } from './Filter'
16
17class ConfigService {
18    constructor(attr) {
19        this.id = attr.id
20        this.supportAsync = false
21        this.random = false
22        this.filterValid = []
23    }
24
25    init (coreContext) {
26        this.coreContext = coreContext
27    }
28
29    isNormalInteger (str) {
30        const n = Math.floor(Number(str));
31        return n !== Infinity && String(n) === String(str) && n >= 0;
32    }
33
34    basicParamValidCheck (params) {
35        let size = params.size
36        if (size !== undefined && size !== '' && size !== null) {
37            let sizeArray = ["small", "medium", "large"]
38            if (sizeArray.indexOf(size) === -1) {
39                this.filterValid.push("size:" + size)
40            }
41        }
42        let level = params.level
43        if (level !== undefined && level !== '' && level !== null) {
44            let levelArray = ["0", "1", "2", "3", "4"]
45            if (levelArray.indexOf(level) === -1) {
46                this.filterValid.push("level:" + level)
47            }
48        }
49        let testType = params.testType
50        if (testType !== undefined && testType !== '' && testType !== null) {
51            let testTypeArray = ["function", "performance", "reliability", "security"]
52            if (testTypeArray.indexOf(testType) === -1) {
53                this.filterValid.push("testType:" + testType)
54            }
55        }
56    }
57
58    filterParamValidCheck (params) {
59        let timeout = params.timeout
60        if (timeout !== undefined && timeout !== '' && timeout !== null) {
61            if (!this.isNormalInteger(timeout)) {
62                this.filterValid.push("timeout:" + timeout)
63            }
64        }
65        let classes = params.class
66        let nameRule = /^[A-Za-z]{1}[\w#,.]*$/
67        if (classes !== undefined && classes !== '' && classes !== null) {
68            let classArray = classes.split(',')
69            for (let className of classArray) {
70                if (!className.match(nameRule)) {
71                    this.filterValid.push("class:" + classes)
72                    break
73                }
74            }
75        }
76        let notClasses = params.notClass
77        if (notClasses !== undefined && notClasses !== '' && notClasses !== null) {
78            let notClassArray = notClasses.split(',')
79            for (let notClassName of notClassArray) {
80                if (!notClassName.match(nameRule)) {
81                    this.filterValid.push("notClass:" + notClasses)
82                    break
83                }
84            }
85        }
86    }
87
88    setConfig (params) {
89        this.basicParamValidCheck(params)
90        this.filterParamValidCheck(params)
91        try {
92            this.class = params.class
93            this.notClass = params.notClass
94            this.flag = params.flag || { flag: false }
95            this.suite = params.suite
96            this.itName = params.itName
97            this.filter = params.filter
98            this.testType = params.testType
99            this.level = params.level
100            this.size = params.size
101            this.timeout = params.timeout
102            this.filterParam = {
103                testType: {
104                    'function': 1,
105                    'performance': 1 << 1,
106                    'reliability': 1 << 3,
107                    'security': 1 << 4
108                },
109                level: {
110                    '0': 1 << 24,
111                    '1': 1 << 25,
112                    '2': 1 << 26,
113                    '3': 1 << 27,
114                    '4': 1 << 28,
115                },
116                size: {
117                    'small': 1 << 16,
118                    'medium': 1 << 17,
119                    'large': 1 << 18,
120                }
121            }
122            this.parseParams()
123        } catch (err) {
124            this.filter = 0
125            this.flag = false
126            this.suite = null
127            this.itName = null
128            this.testType = null
129            this.level = null
130            this.size = null
131            this.class = null
132            this.notClass = null
133            this.timeout = null
134        }
135    }
136
137    parseParams () {
138        if (this.filter != null) {
139            return
140        }
141        let testTypeFilter = 0
142        let sizeFilter = 0
143        let levelFilter = 0
144        if (this.testType != null) {
145            testTypeFilter = this.testType.split(',')
146                .map(item => this.filterParam.testType[item] || 0)
147                .reduce((pre, cur) => pre | cur, 0)
148        }
149        if (this.level != null) {
150            levelFilter = this.level.split(',')
151                .map(item => this.filterParam.level[item] || 0)
152                .reduce((pre, cur) => pre | cur, 0)
153        }
154        if (this.size != null) {
155            sizeFilter = this.size.split(',')
156                .map(item => this.filterParam.size[item] || 0)
157                .reduce((pre, cur) => pre | cur, 0)
158        }
159        this.filter = testTypeFilter | sizeFilter | levelFilter
160        console.info('filter params:' + this.filter)
161    }
162
163    isCurrentSuite (description) {
164        if (this.suite !== undefined && this.suite !== '' && this.suite !== null) {
165            let suiteArray = this.suite.split(',')
166            return suiteArray.indexOf(description) !== -1;
167        }
168        return false
169    }
170
171    filterSuite (currentSuiteName) {
172        let filterArray = []
173        if (this.suite !== undefined && this.suite !== '' && this.suite !== null) {
174            filterArray.push(new SuiteAndItNameFilter(currentSuiteName, '', this.suite))
175        }
176        if (this.class !== undefined && this.class !== '' && this.class !== null) {
177            filterArray.push(new ClassFilter(currentSuiteName, '', this.class))
178        }
179        if (this.notClass !== undefined && this.notClass !== '' && this.notClass !== null) {
180            filterArray.push(new NotClassFilter(currentSuiteName, '', this.notClass))
181        }
182
183        let result = filterArray.map(item => item.filterSuite()).reduce((pre, cur) => pre || cur, false)
184        return result
185    }
186
187    filterDesc (currentSuiteName, desc, fi, coreContext) {
188        let filterArray = []
189        if (this.itName !== undefined && this.itName !== '' && this.itName !== null) {
190            filterArray.push(new SuiteAndItNameFilter(currentSuiteName, desc, this.itName))
191        }
192        if (this.class !== undefined && this.class !== '' && this.class !== null) {
193            filterArray.push(new ClassFilter(currentSuiteName, desc, this.class))
194        }
195        if (this.notClass !== undefined && this.notClass !== '' && this.notClass !== null) {
196            filterArray.push(new NotClassFilter(currentSuiteName, desc, this.notClass))
197        }
198        if (typeof (this.filter) !== 'undefined' && this.filter !== 0 && fi !== 0) {
199            filterArray.push(new TestTypesFilter('', '', fi, this.filter))
200        }
201        let result = filterArray.map(item => item.filterIt()).reduce((pre, cur) => pre || cur, false)
202        return result
203    }
204
205    isRandom () {
206        return this.random || false
207    }
208
209    setSupportAsync (value) {
210        this.supportAsync = value
211    }
212
213    isSupportAsync () {
214        return this.supportAsync
215    }
216
217    translateParams (parameters) {
218        const keySet = new Set([
219            '-s class', '-s notClass', '-s suite', '-s itName',
220            '-s level', '-s testType', '-s size', '-s timeout'
221        ])
222        let targetParams = {}
223        for (const key in parameters) {
224            if (keySet.has(key)) {
225                targetParams[key.substring(3)] = parameters[key]
226            }
227        }
228        return targetParams
229    }
230    translateParamsToString (parameters) {
231        const keySet = new Set([
232            '-s class', '-s notClass', '-s suite', '-s itName',
233            '-s level', '-s testType', '-s size', '-s timeout'
234        ])
235        let targetParams = '';
236        for (const key in parameters) {
237            if (keySet.has(key)) {
238                targetParams += ' ' + key + ' ' + parameters[key]
239            }
240        }
241        return targetParams.trim()
242    }
243
244    execute () {
245    }
246}
247
248export {
249    ConfigService
250}
251