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