blob: 389112da842a8e337a9c773efbaff3c431becb13 [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 {Then, When, Given} = require('cucumber');
17const assert = require('assert');
18const _ = require('lodash');
19const normalizeNewline = require('normalize-newline');
20require('node-zip');
21YAML = require('yamljs');
22const fs = require('fs');
23const util = require('./Utils.js');
24
25/**
26 * @module ContextData
27 * @description Use with "Given". Use ONLY for local testing when you know the value of the Item you want to use
28 * instead of creating a new one.
29 * @step Item {string} and version Id {string}
30 **/
31Given('Item {string} and version Id {string}', function (string, string2) {
32 this.context.item.id = string;
33 this.context.item.versionId = string2;
34});
35/**
36 * @module ContextData
37 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
38 * @description Response Data::<br>
39 * """<br>
40 * {jsonObject}<br>
41 * """<br>
42 * @step Use with "Given". Use ONLY for local testing, creates a response data object
43 **/
44Given('Response Data:', function (docString) {
45 this.context.responseData = JSON.parse(docString);
46});
47
48/**
49 * @module ContextData
50 * @description Sets the server for the test. overrides configuration.
51 * @step Server with IP
52 **/
53Given('Server host {string}', function (string) {
54 this.setServer(string);
55});
56
57/**
58 * @module ContextData
59 * @description Copy a property from the response data to context Item/VLM/VSP data, example: vsp.componentId
60 * @step I want to save on the context for {string} property {string} with value {string}
61 **/
62Then('I want to save on the context for {string} property {string} with value {string}', function(string, string1, string2) {
63 assert.equal(_.includes(['VLM', 'VSP', 'Item'], string), true);
64 let val = _.get(this.context.responseData, string2);
65 _.set(this.context, string1, val);
66});
67/**
68 * @module ContextData
69 * @description Copy a property from the response data to saved data on the context. Example: save newly generated IDs. Response data value can be from a path, xample: results[0].id
70 * @exampleFile Example_Rest_Calls.feature
71 * @step I want to save to property {string} from response data path {string}
72 **/
73Then('I want to copy to property {string} from response data path {string}', function(string, string2) {
74 let val = _.get(this.context.responseData, string2);
75 _.set(this.context, string, val);
76});
77/**
78 * @module ContextData
79 * @description This will set the value of a saved property on the context
80 * @exampleFile Example_Rest_Calls.feature
81 * @step I want to set property {string} to value {string}
82 **/
83Then('I want to set property {string} to value {string}', function(string, string2) {
84 _.set(this.context, string, string2);
85});
86
87/**
88 * @module ResponseData
89 * @description Will check the output data for a property and a value. property can be a path (example: results[0].id)
90 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
91 * @step I want to check property {string} for value {string}
92 **/
93Then('I want to check property {string} for value {string}', function(string, string2) {
94 assert.equal(_.get(this.context.responseData, string), string2);
95});
96/**
97 * @module ResponseData
98 * @description Will check the output data for a property and a integer. property can be a path (example: results[0].id)
99 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
100 * @step I want to check property {string} for value {int}
101 **/
102Then('I want to check property {string} for value {int}', function(string, int) {
103 assert.equal(_.get(this.context.responseData, string), int);
104});
105/**
106 * @module ResponseData
107 * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id)
108 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
109 * @step I want to check property {string} to be "True/False"
110 **/
111Then('I want to check property {string} to be {string}', function(string, string2) {
112 assert.equal(_.get(this.context.responseData, string), string2.toLowerCase());
113});
114/**
115 * @module ResponseData
116 * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id)
117 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
118 * @step I want to check property {string} to have length {int}
119 **/
120Then('I want to check property {string} to have length {int}', function(string, intLength) {
121 let arrayProp = _.get(this.context.responseData, string);
122 assert.equal(arrayProp.length, intLength);
123});
124/**
125 * @module ResponseData
126 * @description Will check the output data for a property and make sure it exists
127 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
128 * @step I want to check property {string} exists
129 **/
130Then('I want to check property {string} exists', function(string) {
131 assert.equal(_.has(this.context.responseData, string), true);
132});
133/**
134 * @module ResponseData
135 * @description Will check the output data for a property and make sure it does not exist
136 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
137 * @step I want to check property {string} does not exist
138 **/
139Then('I want to check property {string} does not exist', function(string) {
140 assert.equal(_.has(this.context.responseData, string), false);
141});
142
143/**
144* @module ContextData
145* @description Use during development to see what is on the context
146 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
147* @step I want to print context data
148**/
149Then('I want to print the context data', function() {
150 console.log('------------ context ---------------');
151 console.log(JSON.stringify(this.context, null, 2));
152 console.log('--------------------------------------');
153});
154/**
155 * @module ContextData
156 * @description Set this in order to check that the following Rest call will not have response code 200
157 * @exampleFile Example_Rest_Calls.feature
158 * @step I want the following to fail
159 **/
160Then('I want the following to fail', function() {
161 this.context.shouldFail = true;
162});
163
164/**
165 * @module ContextData
166 * @description Set this in order to check that the following Rest call will have the error code on the return data
167 * @exampleFile Example_VSP.feature
168 * @step I want the following to fail with error code {string}
169 **/
170Then('I want the following to fail with error code {string}', function(string) {
171 this.context.shouldFail = true;
172 this.context.errorCode = string;
173});
174
175/**
176 * @module ZipData
177 * @description Use this in order to extract a file from a zip file and to compare it to a local file (string comparison).
178 * @exampleFile Example_VSP.feature
179 * @step I want to compare the content of the entry {string} in the zip {string} with file {string}
180 **/
181Then ('I want to compare the content of the entry {string} in the zip {string} with file {string}', function (string, string2, string3) {
182 let zipFile = fs.readFileSync(string2, 'binary');
183 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
184 let fileData = zip.files[string]._data;
185 let compareFileData = fs.readFileSync(string3, {encoding: 'ascii'});
186 assert.equal(normalizeNewline(compareFileData), normalizeNewline(fileData));
187});
188
189/**
190 * @module ZipData
191 * @description Loads the yaml from zip file onto the context responseData as JSON for running checks on the output
192 * @exampleFile Example_VSP.feature
193 * @step I want to load the yaml content of the entry {string} in the zip {string} to context
194 **/
195Then ('I want to load the yaml content of the entry {string} in the zip {string} to context', function (string, string2, callback) {
196 let zipFile = fs.readFileSync(string2, 'binary');
197 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
198 let fileData = zip.files[string]._data;
199 let nativeObject = YAML.parse(fileData);
200 this.context.responseData = nativeObject;
201 callback();
202});
203
204
205/**
206 * @module ZipData
207 * @description Loads the json from zip file onto the context responseData for running check son the output
208 * @exampleFile Example_VSP.feature
209 * @step I want to load the json content of the entry {string} in the zip {string} to context
210 **/
211When('I want to load the json content of the entry {string} in the zip {string} to context', function (string, string2, callback) {
212 let zipFile = fs.readFileSync(string2, 'binary');
213 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
214 let str = zip.files[string]._data;
215 this.context.responseData = JSON.parse(str);
216 callback();
217});