ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 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'); |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 17 | const _ = require('lodash'); |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 18 | const findUp = require('find-up'); |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 19 | |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 20 | |
| 21 | |
| 22 | let configPath = findUp.sync('config.json'); |
| 23 | configPath = configPath.replace(/\\/g, '/'); |
| 24 | |
| 25 | let config = require(configPath); |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 26 | let localConfig = {}; |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 27 | try { |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 28 | let devConfigPath = findUp.sync('devConfig.json'); |
| 29 | devConfigPath = devConfigPath.replace(/\\/g, '/'); |
| 30 | localConfig = require(devConfigPath); |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 31 | } catch (e) { |
| 32 | try { |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 33 | let envdir = findUp.sync('environments', {type: 'directory'}); |
| 34 | envdir = envdir.replace(/\\/g, '/'); |
| 35 | localConfig = require(envdir + '/dockerConfig.json'); |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 36 | } catch (e) { |
| 37 | console.error("no env configuration was found!"); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | config = _.merge(config, localConfig); |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 42 | var {setDefaultTimeout} = require('cucumber'); |
| 43 | |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 44 | |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 45 | /** |
| 46 | * @module Context |
| 47 | * @description Context that is used per feature file and can be accessed as 'this.context' in all steps.<Br> |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 48 | * This class can be extended in order to add additional configurations. |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 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> |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 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) { |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 59 | this.context = {}; |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 60 | this.context.headers = {}; |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 61 | let typeName; |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 62 | |
| 63 | this.context.defaultServerType = 'main'; |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 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 | } |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 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 | } |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 76 | } |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 77 | this.context.item = {id: null, versionId: null, componentId: null}; |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 78 | // adding the default items that should also be initialized |
| 79 | if (config.initData) { |
| 80 | _.assign(this.context, config.initData); |
| 81 | } |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 82 | |
| 83 | this.context.shouldFail = false; |
| 84 | this.context.errorCode = null; |
| 85 | this.context.inputData = null; |
| 86 | this.context.responseData = null; |
| 87 | |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 88 | |
ilanap | 041deed | 2018-02-20 11:57:12 +0200 | [diff] [blame] | 89 | this.config = config; |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 90 | |
| 91 | let context = this.context; |
| 92 | this.context.getUrlForType = (function(type) { |
ilanap | 041deed | 2018-02-20 11:57:12 +0200 | [diff] [blame] | 93 | var _server = context.server; |
| 94 | var _config = config; |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 95 | return function(type) { |
ilanap | 041deed | 2018-02-20 11:57:12 +0200 | [diff] [blame] | 96 | let typeData = _config[type]; |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame] | 97 | let _url = typeData.protocol + '://' + |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 98 | typeData.server + ':' + |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 99 | typeData.port + '/' + |
ilanap | 041deed | 2018-02-20 11:57:12 +0200 | [diff] [blame] | 100 | typeData.prefix; |
| 101 | return _url; |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 102 | } |
| 103 | })(); |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 104 | setDefaultTimeout(60 * 1000); |
| 105 | } |
| 106 | } |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 107 | setWorldConstructor(CustomWorld); |