Issue-ID: SDC-2483
[sdc.git] / cucumber-js-test-apis-ci / cucumber-common / stepDefinitions / world.js
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 const { setWorldConstructor } = require('cucumber');
17 const _ = require('lodash');
18 const findUp = require('find-up');
19
20
21
22 let configPath = findUp.sync('config.json');
23 configPath = configPath.replace(/\\/g, '/');
24
25 let config = require(configPath);
26 let localConfig = {};
27 try {
28         let devConfigPath = findUp.sync('devConfig.json');
29         devConfigPath = devConfigPath.replace(/\\/g, '/');
30         localConfig = require(devConfigPath);
31 } catch (e) {
32         try {
33                 let envdir = findUp.sync('environments', {type: 'directory'});
34                 envdir = envdir.replace(/\\/g, '/');
35                 localConfig = require(envdir + '/dockerConfig.json');
36         } catch (e) {
37                 console.error("no env configuration was found!");
38         }
39 }
40
41 config = _.merge(config, localConfig);
42 var {setDefaultTimeout} = require('cucumber');
43
44
45 /**
46  * @module Context
47  * @description Context that is used per feature file and can be accessed as 'this.context' in all steps.<Br>
48  *     This class can be extended in order to add additional configurations.
49  *<Br>
50  * Contains the following items:<br>
51  * <li>this.context.server <ul>REST server and onboarding prefix including version. set either in configuration file or from the command line or SERVER environment variable</ul>
52  * <li>this.context.item <ul>When a VLM or VSP has been created, this has the an id and versionId set to the correct IDs.</ul>
53  * <li>this.context <ul>Object with properties that were saved in the steps.</ul>
54  * <li>this.context.inputdata <ul><b>Automatically updated with the last responseData from the Rest call</b><br>Object with properties that were prepares in the steps.</ul>
55  * <li>this.context.responseData <ul>Response from the last REST call.</ul>
56  **/
57 class CustomWorld {
58         constructor(options) {
59                 this.context = {};
60                 this.context.headers = {};
61                 let typeName;
62
63                 this.context.defaultServerType = 'main';
64                 for (typeName in config) {
65                         this.context.headers[typeName] = {};
66                         if (config[typeName].user) {
67                                 this.context.headers[typeName]['USER_ID'] = config[typeName].user;
68                         }
69                         // adding additional headers
70                         if (config[typeName].additionalHeaders) {
71                                 _.assign(this.context.headers[typeName] , config[typeName].additionalHeaders);
72                         }
73                         if (config[typeName].isDefault !== undefined && config[typeName].isDefault) {
74                                 this.context.defaultServerType = typeName;
75                         }
76                 }
77                 this.context.item = {id: null, versionId: null, componentId: null};
78                 // adding the default items that should also be initialized
79                 if (config.initData) {
80                         _.assign(this.context, config.initData);
81                 }
82
83                 this.context.shouldFail = false;
84                 this.context.errorCode = null;
85                 this.context.inputData = null;
86                 this.context.responseData = null;
87
88
89                 this.config = config;
90
91                 let context = this.context;
92                 this.context.getUrlForType = (function(type) {
93                         var _server = context.server;
94                         var _config = config;
95                         return function(type) {
96                                 let typeData = _config[type];
97                                 let _url = typeData.protocol + '://' +
98                                         typeData.server + ':' +
99                                         typeData.port + '/' +
100                                         typeData.prefix;
101                                 return _url;
102                         }
103                 })();
104                 setDefaultTimeout(60 * 1000);
105         }
106 }
107 setWorldConstructor(CustomWorld);