blob: 5968d406711e3678eea5d7b3b0cfa83dea3c50d5 [file] [log] [blame]
ayalaben3fb47c42018-06-24 10:42:43 +03001/*
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
25function getPath(path, context) {
26 let compiled = _.template(path);
27 return compiled(context);
28}
29
30/**
31 * @module ContextData
32 * @description Use with "Given". Use ONLY for local testing when you know the value of the Item you want to use
33 * instead of creating a new one.
34 * @step Item {string} and version Id {string}
35 **/
36Given('Item {string} and version Id {string}', function (string, string2) {
37 this.context.item.id = string;
38 this.context.item.versionId = string2;
39});
40/**
41 * @module ContextData
talig02201e42018-08-05 10:47:36 +030042 * @exampleFile ResponseDataChecks.feature
ayalaben3fb47c42018-06-24 10:42:43 +030043 * @description Response Data::<br>
44 * """<br>
45 * {jsonObject}<br>
46 * """<br>
47 * @step Use with "Given". Use ONLY for local testing, creates a response data object
48 **/
49Given('Response Data:', function (docString) {
50 this.context.responseData = JSON.parse(docString);
51});
52
53/**
54 * @module ContextData
talig39c01dd2018-07-15 15:41:31 +030055 * @description Copy a property from the response data to context Item data, example: item.componentId
ayalaben3fb47c42018-06-24 10:42:43 +030056 * @step I want to save on the context for {string} property {string} with value {string}
57 **/
58Then('I want to save on the context for {string} property {string} with value {string}', function(string, string1, string2) {
talig39c01dd2018-07-15 15:41:31 +030059 assert.equal(_.includes(['Item'], string), true);
ayalaben3fb47c42018-06-24 10:42:43 +030060 let val = _.get(this.context.responseData, string2);
61 _.set(this.context, string1, val);
62});
63/**
64 * @module ContextData
65 * @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
66 * @exampleFile Example_Rest_Calls.feature
67 * @step I want to save to property {string} from response data path {string}
68 **/
69Then('I want to copy to property {string} from response data path {string}', function(string, string2) {
70 let val = _.get(this.context.responseData, string2);
71 _.set(this.context, string, val);
72});
73/**
74 * @module ContextData
75 * @description This will set the value of a saved property on the context
76 * @exampleFile Example_Rest_Calls.feature
77 * @step I want to set property {string} to value {string}
78 **/
79Then('I want to set property {string} to value {string}', function(string, string2) {
80 _.set(this.context, string, string2);
81});
82
83/**
taligff78ef42018-08-09 13:13:23 +030084 * @module ContextData
85 * @description sets context property to a random value
86 * @exampleFile Example_Rest_Calls.feature
87 * @step I want to update the input property {string} with a random value
88 **/
89Then('I want to set property {string} with a random value', function (string) {
90 _.set(this.context, string, util.random());
91});
92
93/**
ayalaben3fb47c42018-06-24 10:42:43 +030094 * @module ResponseData
95 * @description Will check the output data for a property and a value. property can be a path (example: results[0].id)
talig02201e42018-08-05 10:47:36 +030096 * @exampleFile ResponseDataChecks.feature
ayalaben3fb47c42018-06-24 10:42:43 +030097 * @step I want to check property {string} for value {string}
98 **/
99Then('I want to check property {string} for value {string}', function(string, string2) {
100 assert.equal(_.get(this.context.responseData, string), string2);
101});
102/**
103 * @module ResponseData
104 * @description Will check the output data for a property and a value. property can be a path
105 * (example: results[0].id). Supports comparison to a long String by allowing a line break
106 * @exampleFile VirtualMachineInterfaceValidationHeatResourceMissingProperties.feature
107 * @step I want to check property {string} for value {string}
108 **/
109
110Then('I want to check property {string} for value:', function(string, docString) {
111 assert.equal(_.get(this.context.responseData, string), docString.trim());
112});
113/**
114 * @module ResponseData
115 * @description Will check the output data for a property and a integer. property can be a path (example: results[0].id)
talig02201e42018-08-05 10:47:36 +0300116 * @exampleFile ResponseDataChecks.feature
ayalaben3fb47c42018-06-24 10:42:43 +0300117 * @step I want to check property {string} for value {int}
118 **/
119Then('I want to check property {string} for value {int}', function(string, int) {
120 assert.equal(_.get(this.context.responseData, string), int);
121});
talig02201e42018-08-05 10:47:36 +0300122
ayalaben3fb47c42018-06-24 10:42:43 +0300123/**
124 * @module ResponseData
125 * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id)
talig02201e42018-08-05 10:47:36 +0300126 * @exampleFile ResponseDataChecks.feature
ayalaben3fb47c42018-06-24 10:42:43 +0300127 * @step I want to check property {string} to be "True/False"
128 **/
129Then('I want to check property {string} to be {word}', function(string, string2) {
130 assert.equal(_.get(this.context.responseData, string), string2.toLowerCase() == "true");
131});
132/**
133 * @module ResponseData
134 * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id)
talig02201e42018-08-05 10:47:36 +0300135 * @exampleFile ResponseDataChecks.feature
ayalaben3fb47c42018-06-24 10:42:43 +0300136 * @step I want to check property {string} to have length {int}
137 **/
138Then('I want to check property {string} to have length {int}', function(string, intLength) {
139 let arrayProp = _.get(this.context.responseData, string);
140 assert.equal(arrayProp.length, intLength);
141});
142/**
143 * @module ResponseData
144 * @description Will check the output data for a property and make sure it exists
talig02201e42018-08-05 10:47:36 +0300145 * @exampleFile ResponseDataChecks.feature
ayalaben3fb47c42018-06-24 10:42:43 +0300146 * @step I want to check property {string} exists
147 **/
148Then('I want to check property {string} exists', function(string) {
149 assert.equal(_.has(this.context.responseData, string), true);
150});
151/**
152 * @module ResponseData
153 * @description Will check the output data for a property and make sure it does not exist
talig02201e42018-08-05 10:47:36 +0300154 * @exampleFile ResponseDataChecks.feature
ayalaben3fb47c42018-06-24 10:42:43 +0300155 * @step I want to check property {string} does not exist
156 **/
157Then('I want to check property {string} does not exist', function(string) {
158 assert.equal(_.has(this.context.responseData, string), false);
159});
160
161/**
talig02201e42018-08-05 10:47:36 +0300162 * @module ResponseData
163 * @description Will check the output data for a property and a value. property can be a path (example: results[0].id)
164 * @exampleFile ResponseDataChecks.feature
165 * @step I want to check property {string} for value {string}
166 **/
167Then('I want to check in the list {string} property {string} with value {string} exists', function(listPath, propertyPath, value) {
168 var list = _.get(this.context.responseData, listPath);
169 assert.notEqual(list.find(element => _.get(element, propertyPath) === value), undefined);
170});
171
172/**
173 * @module ResponseData
174 * @description Will check the output data for a property and a value. property can be a path (example: results[0].id)
175 * @exampleFile ResponseDataChecks.feature
176 * @step I want to check property {string} for value {string}
177 **/
178Then('I want to check in the list {string} property {string} with value {string} does not exist', function(listPath, propertyPath, value) {
179 var list = _.get(this.context.responseData, listPath);
180 assert.equal(list.find(element => _.get(element, propertyPath) === value), undefined);
181});
182
183/**
184 * @module ResponseData
185 * @description Will check the output data for a property and a value. property can be a path (example: results[0].id)
186 * @exampleFile ResponseDataChecks.feature
187 * @step I want to check property {string} for value {string}
188 **/
189Then('I want to check in the list {string} property {string} with value of saved property {string} exists', function(listPath, propertyPath, valueProperty) {
190 var list = _.get(this.context.responseData, listPath);
191 var value = _.get(this.context, valueProperty);
192 assert.notEqual(list.find(element => _.get(element, propertyPath) === value), undefined);
193});
194
195/**
196 * @module ResponseData
197 * @description Will check the output data for a property and a value. property can be a path (example: results[0].id)
198 * @exampleFile ResponseDataChecks.feature
199 * @step I want to check property {string} for value {string}
200 **/
201Then('I want to check in the list {string} property {string} with value of saved property {string} does not exist', function(listPath, propertyPath, valueProperty) {
202 var list = _.get(this.context.responseData, listPath);
203 var value = _.get(this.context, valueProperty);
204 assert.equal(list.find(element => _.get(element, propertyPath) === value), undefined);
205});
206
207/**
ayalaben3fb47c42018-06-24 10:42:43 +0300208* @module ContextData
209* @description Use during development to see what is on the context
talig02201e42018-08-05 10:47:36 +0300210 * @exampleFile ResponseDataChecks.feature
talig8f91bb12018-07-23 10:15:59 +0300211* @step I want to print the context data
ayalaben3fb47c42018-06-24 10:42:43 +0300212**/
213Then('I want to print the context data', function() {
214 console.log('------------ context ---------------');
215 console.log(JSON.stringify(this.context, null, 2));
216 console.log('--------------------------------------');
217});
218/**
219 * @module ContextData
220 * @description Set this in order to check that the following Rest call will not have response code 200
221 * @exampleFile Example_Rest_Calls.feature
222 * @step I want the following to fail
223 **/
224Then('I want the following to fail', function() {
225 this.context.shouldFail = true;
226});
227
228/**
229 * @module ContextData
talig39c01dd2018-07-15 15:41:31 +0300230 * @description Set this in order to check that the following Rest call will not have response code 200
231 * @exampleFile Example_Rest_Calls.feature
232 * @step I want the following to fail
233 **/
234Then('I want the following to fail with response status code {int}', function(int) {
235 this.context.shouldFail = true;
236 this.context.responseStatusCode = int;
237});
238
239/**
240 * @module ContextData
ayalaben3fb47c42018-06-24 10:42:43 +0300241 * @description Set this in order to check that the following Rest call will have the error code on the return data
242 * @exampleFile Example_VSP.feature
243 * @step I want the following to fail with error code {string}
244 **/
245Then('I want the following to fail with error code {string}', function(string) {
246 this.context.shouldFail = true;
247 this.context.errorCode = string;
248});
249
250
251/**
252 * @module ContextData
253 * @description Set this in order to check that the following Rest call will have the error message on the return data
254 * @exampleFile DeleteVLMCertified.feature
255 * @step I want the following to fail with error message {string}
256 **/
257Then('I want the following to fail with error message {string}', function(string) {
258 this.context.shouldFail = true;
259 let errorMessage = getPath(string, this.context);
260 this.context.errorMessage = errorMessage;
261});
262
263/**
264 * @module ZipData
265 * @description Use this in order to extract a file from a zip file and to compare it to a local file (string comparison).
266 * @exampleFile Example_VSP.feature
267 * @step I want to compare the content of the entry {string} in the zip {string} with file {string}
268 **/
269Then ('I want to compare the content of the entry {string} in the zip {string} with file {string}', function (string, string2, string3) {
270 let zipFile = fs.readFileSync(string2, 'binary');
271 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
272 let fileData = zip.files[string]._data;
273 let compareFileData = fs.readFileSync(string3, {encoding: 'ascii'});
274 assert.equal(normalizeNewline(compareFileData), normalizeNewline(fileData));
275});
276
277/**
278 * @module ZipData
279 * @description Loads the yaml from zip file onto the context responseData as JSON for running checks on the output
280 * @exampleFile Example_VSP.feature
281 * @step I want to load the yaml content of the entry {string} in the zip {string} to context
282 **/
283Then ('I want to load the yaml content of the entry {string} in the zip {string} to context', function (string, string2, callback) {
284 let zipFile = fs.readFileSync(string2, 'binary');
285 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
286 let fileData = zip.files[string]._data;
287 let nativeObject = YAML.parse(fileData);
288 this.context.responseData = nativeObject;
289 callback();
290});
291
292
293/**
294 * @module ZipData
295 * @description Loads the json from zip file onto the context responseData for running check son the output
296 * @exampleFile Example_VSP.feature
297 * @step I want to load the json content of the entry {string} in the zip {string} to context
298 **/
299When('I want to load the json content of the entry {string} in the zip {string} to context', function (string, string2, callback) {
300 let zipFile = fs.readFileSync(string2, 'binary');
301 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
302 let str = zip.files[string]._data;
303 this.context.responseData = JSON.parse(str);
304 callback();
305});
306
ayalaben3fb47c42018-06-24 10:42:43 +0300307Then('I want to check that property {string} in the response equals to value of saved property {string}', function(propertyPath, valueProperty) {
308 const results = this.context.responseData;
talig8f91bb12018-07-23 10:15:59 +0300309 assert.equal(results[propertyPath], _.get(this.context, valueProperty));
ayalaben3fb47c42018-06-24 10:42:43 +0300310});
311
312/**
313 * @module ResponseData
314 * @description Check that the itemId from context exits in result of responseData
315 * exampleFile ArchiveItem.feature
316 * step I want to check that item exits in response
317 **/
318Then('I want to check that item exits in response', function() {
319
320 const id = this.context.item.id;
321 const results = this.context.responseData.results;
322 var testResult = false;
323
324 for(var i=0; i< results.length; i++){
325 if ( id == results[i].id){
326 testResult = true;
327 }
328 }
329
330 assert.equal(testResult,true);
331});
332
333
334/**
335 * @module ResponseData
336 * @description Check that the itemId from context does NOT exits in result of responseData
337 * exampleFile ArchiveItem.feature
338 * step I want to check that item does not exits in response
339 **/
340Then('I want to check that item does not exits in response', function() {
341
342 const id = this.context.item.id;
343 const results = this.context.responseData.results;
344 var testResult = false;
345
346 for(var i=0; i< results.length; i++){
347 if ( id == results[i].id){
348 testResult = true;
349 }
350 }
351
352 assert.equal(testResult,false);
353});