blob: 7811aba8866266d29d1dd5ce5a23e2a21d1239a0 [file] [log] [blame]
ilanap637206b2018-02-04 17:06:22 +02001/*
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 */
16const { setWorldConstructor } = require('cucumber');
ilanapad3c41b2018-04-25 13:39:19 +030017const _ = require('lodash');
ilanap061ca932019-08-04 10:16:33 +030018const findUp = require('find-up');
ilanapad3c41b2018-04-25 13:39:19 +030019
ilanap061ca932019-08-04 10:16:33 +030020
21
22let configPath = findUp.sync('config.json');
23configPath = configPath.replace(/\\/g, '/');
24
25let config = require(configPath);
ilanapad3c41b2018-04-25 13:39:19 +030026let localConfig = {};
ilanapbd76c092018-02-19 10:36:26 +020027try {
ilanap061ca932019-08-04 10:16:33 +030028 let devConfigPath = findUp.sync('devConfig.json');
29 devConfigPath = devConfigPath.replace(/\\/g, '/');
30 localConfig = require(devConfigPath);
ilanapad3c41b2018-04-25 13:39:19 +030031} catch (e) {
32 try {
ilanap061ca932019-08-04 10:16:33 +030033 let envdir = findUp.sync('environments', {type: 'directory'});
34 envdir = envdir.replace(/\\/g, '/');
35 localConfig = require(envdir + '/dockerConfig.json');
ilanapad3c41b2018-04-25 13:39:19 +030036 } catch (e) {
37 console.error("no env configuration was found!");
38 }
39}
40
41config = _.merge(config, localConfig);
ilanap637206b2018-02-04 17:06:22 +020042var {setDefaultTimeout} = require('cucumber');
43
ilanapad3c41b2018-04-25 13:39:19 +030044
ilanap637206b2018-02-04 17:06:22 +020045/**
46 * @module Context
47 * @description Context that is used per feature file and can be accessed as 'this.context' in all steps.<Br>
ilanap061ca932019-08-04 10:16:33 +030048 * This class can be extended in order to add additional configurations.
ilanap637206b2018-02-04 17:06:22 +020049 *<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>
ilanap637206b2018-02-04 17:06:22 +020052 * <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 **/
57class CustomWorld {
58 constructor(options) {
ilanapbd76c092018-02-19 10:36:26 +020059 this.context = {};
ilanap637206b2018-02-04 17:06:22 +020060 this.context.headers = {};
ilanapad3c41b2018-04-25 13:39:19 +030061 let typeName;
ilanap061ca932019-08-04 10:16:33 +030062
63 this.context.defaultServerType = 'main';
ilanapad3c41b2018-04-25 13:39:19 +030064 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 }
ilanap061ca932019-08-04 10:16:33 +030069 // 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 }
ilanapbd76c092018-02-19 10:36:26 +020076 }
ilanap637206b2018-02-04 17:06:22 +020077 this.context.item = {id: null, versionId: null, componentId: null};
ilanap061ca932019-08-04 10:16:33 +030078 // adding the default items that should also be initialized
79 if (config.initData) {
80 _.assign(this.context, config.initData);
81 }
ilanap637206b2018-02-04 17:06:22 +020082
83 this.context.shouldFail = false;
84 this.context.errorCode = null;
85 this.context.inputData = null;
86 this.context.responseData = null;
87
ilanapbd76c092018-02-19 10:36:26 +020088
ilanap041deed2018-02-20 11:57:12 +020089 this.config = config;
ilanapbd76c092018-02-19 10:36:26 +020090
91 let context = this.context;
92 this.context.getUrlForType = (function(type) {
ilanap041deed2018-02-20 11:57:12 +020093 var _server = context.server;
94 var _config = config;
ilanapbd76c092018-02-19 10:36:26 +020095 return function(type) {
ilanap041deed2018-02-20 11:57:12 +020096 let typeData = _config[type];
ilanap061ca932019-08-04 10:16:33 +030097 let _url = typeData.protocol + '://' +
ilanapad3c41b2018-04-25 13:39:19 +030098 typeData.server + ':' +
ilanapbd76c092018-02-19 10:36:26 +020099 typeData.port + '/' +
ilanap041deed2018-02-20 11:57:12 +0200100 typeData.prefix;
101 return _url;
ilanapbd76c092018-02-19 10:36:26 +0200102 }
103 })();
ilanap637206b2018-02-04 17:06:22 +0200104 setDefaultTimeout(60 * 1000);
105 }
106}
ilanapbd76c092018-02-19 10:36:26 +0200107setWorldConstructor(CustomWorld);