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'); |
| 18 | |
| 19 | let config = require('../config.json'); |
| 20 | let localConfig = {}; |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 21 | try { |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 22 | localConfig = require('../devConfig.json'); |
| 23 | } catch (e) { |
| 24 | try { |
ilanap | 8664180 | 2019-01-01 17:22:07 +0200 | [diff] [blame^] | 25 | localConfig = require('../environments/dockerConfig.json'); |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 26 | } catch (e) { |
| 27 | console.error("no env configuration was found!"); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | config = _.merge(config, localConfig); |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 32 | var {setDefaultTimeout} = require('cucumber'); |
| 33 | |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 34 | |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 35 | /** |
| 36 | * @module Context |
| 37 | * @description Context that is used per feature file and can be accessed as 'this.context' in all steps.<Br> |
| 38 | *<Br> |
| 39 | * Contains the following items:<br> |
| 40 | * <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> |
| 41 | * <li>this.context.vlm <ul>When a VLM has been created, this has the an id and versionId set to the correct IDs.</ul> |
| 42 | * <li>this.context.vsp <ul>When a VSP has been created, this has the an id and versionId and componentId set to the correct IDs.</ul> |
| 43 | * <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> |
| 44 | * <li>this.context <ul>Object with properties that were saved in the steps.</ul> |
| 45 | * <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> |
| 46 | * <li>this.context.responseData <ul>Response from the last REST call.</ul> |
| 47 | **/ |
| 48 | class CustomWorld { |
| 49 | constructor(options) { |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 50 | this.context = {}; |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 51 | this.context.headers = {}; |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 52 | let typeName; |
| 53 | for (typeName in config) { |
| 54 | this.context.headers[typeName] = {}; |
| 55 | if (config[typeName].user) { |
| 56 | this.context.headers[typeName]['USER_ID'] = config[typeName].user; |
| 57 | } |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 58 | } |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 59 | |
| 60 | this.context.vlm = {id: null, versionId: null}; |
| 61 | this.context.vsp = {id: null, versionId: null}; |
| 62 | this.context.item = {id: null, versionId: null, componentId: null}; |
| 63 | |
| 64 | this.context.shouldFail = false; |
| 65 | this.context.errorCode = null; |
| 66 | this.context.inputData = null; |
| 67 | this.context.responseData = null; |
| 68 | |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 69 | this.context.defaultServerType = 'onboarding'; |
| 70 | |
ilanap | 041deed | 2018-02-20 11:57:12 +0200 | [diff] [blame] | 71 | this.config = config; |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 72 | |
| 73 | let context = this.context; |
| 74 | this.context.getUrlForType = (function(type) { |
ilanap | 041deed | 2018-02-20 11:57:12 +0200 | [diff] [blame] | 75 | var _server = context.server; |
| 76 | var _config = config; |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 77 | return function(type) { |
ilanap | 041deed | 2018-02-20 11:57:12 +0200 | [diff] [blame] | 78 | let typeData = _config[type]; |
| 79 | let _url = _config.protocol + '://' + |
ilanap | ad3c41b | 2018-04-25 13:39:19 +0300 | [diff] [blame] | 80 | typeData.server + ':' + |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 81 | typeData.port + '/' + |
ilanap | 041deed | 2018-02-20 11:57:12 +0200 | [diff] [blame] | 82 | typeData.prefix; |
| 83 | return _url; |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 84 | } |
| 85 | })(); |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 86 | |
| 87 | setDefaultTimeout(60 * 1000); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
ilanap | bd76c09 | 2018-02-19 10:36:26 +0200 | [diff] [blame] | 92 | setWorldConstructor(CustomWorld); |