2 * Copyright © 2016-2017 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 const {Then, When, Given} = require('cucumber');
17 const assert = require('assert');
18 const _ = require('lodash');
19 const normalizeNewline = require('normalize-newline');
21 YAML = require('yamljs');
22 const fs = require('fs');
23 const util = require('./Utils.js');
25 function getPath(path, context) {
26 let compiled = _.template(path);
27 return compiled(context);
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}
36 Given('Item {string} and version Id {string}', function (string, string2) {
37 this.context.item.id = string;
38 this.context.item.versionId = string2;
42 * @exampleFile ResponseDataChecks.feature
43 * @description Response Data::<br>
47 * @step Use with "Given". Use ONLY for local testing, creates a response data object
49 Given('Response Data:', function (docString) {
50 this.context.responseData = JSON.parse(docString);
55 * @description Copy a property from the response data to context Item data, example: item.componentId
56 * @step I want to save on the context for {string} property {string} with value {string}
58 Then('I want to save on the context for {string} property {string} with value {string}', function(string, string1, string2) {
59 assert.equal(_.includes(['Item'], string), true);
60 let val = _.get(this.context.responseData, string2);
61 _.set(this.context, string1, val);
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}
69 Then('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);
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}
79 Then('I want to set property {string} to value {string}', function(string, string2) {
80 _.set(this.context, string, string2);
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
89 Then('I want to set property {string} with a random value', function (string) {
90 _.set(this.context, string, util.random());
94 * @module ResponseData
95 * @description Will check the output data for a property and a value. property can be a path (example: results[0].id)
96 * @exampleFile ResponseDataChecks.feature
97 * @step I want to check property {string} for value {string}
99 Then('I want to check property {string} for value {string}', function(string, string2) {
100 assert.equal(_.get(this.context.responseData, string), string2);
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}
110 Then('I want to check property {string} for value:', function(string, docString) {
111 assert.equal(_.get(this.context.responseData, string), docString.trim());
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)
116 * @exampleFile ResponseDataChecks.feature
117 * @step I want to check property {string} for value {int}
119 Then('I want to check property {string} for value {int}', function(string, int) {
120 assert.equal(_.get(this.context.responseData, string), int);
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)
126 * @exampleFile ResponseDataChecks.feature
127 * @step I want to check property {string} to be "True/False"
129 Then('I want to check property {string} to be {word}', function(string, string2) {
130 assert.equal(_.get(this.context.responseData, string), string2.toLowerCase() == "true");
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)
135 * @exampleFile ResponseDataChecks.feature
136 * @step I want to check property {string} to have length {int}
138 Then('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);
143 * @module ResponseData
144 * @description Will check the output data for a property and make sure it exists
145 * @exampleFile ResponseDataChecks.feature
146 * @step I want to check property {string} exists
148 Then('I want to check property {string} exists', function(string) {
149 assert.equal(_.has(this.context.responseData, string), true);
152 * @module ResponseData
153 * @description Will check the output data for a property and make sure it does not exist
154 * @exampleFile ResponseDataChecks.feature
155 * @step I want to check property {string} does not exist
157 Then('I want to check property {string} does not exist', function(string) {
158 assert.equal(_.has(this.context.responseData, string), false);
162 * @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}
167 Then('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);
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}
178 Then('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);
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}
189 Then('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);
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}
201 Then('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);
208 * @module ContextData
209 * @description Use during development to see what is on the context
210 * @exampleFile ResponseDataChecks.feature
211 * @step I want to print the context data
213 Then('I want to print the context data', function() {
214 console.log('------------ context ---------------');
215 console.log(JSON.stringify(this.context, null, 2));
216 console.log('--------------------------------------');
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
224 Then('I want the following to fail', function() {
225 this.context.shouldFail = true;
229 * @module ContextData
230 * @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
234 Then('I want the following to fail with response status code {int}', function(int) {
235 this.context.shouldFail = true;
236 this.context.responseStatusCode = int;
240 * @module ContextData
241 * @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}
245 Then('I want the following to fail with error code {string}', function(string) {
246 this.context.shouldFail = true;
247 this.context.errorCode = string;
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}
257 Then('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;
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}
269 Then ('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));
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
283 Then ('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;
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
299 When('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);
307 Then('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;
309 assert.equal(results[propertyPath], _.get(this.context, valueProperty));
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
318 Then('I want to check that item exits in response', function() {
320 const id = this.context.item.id;
321 const results = this.context.responseData.results;
322 var testResult = false;
324 for(var i=0; i< results.length; i++){
325 if ( id == results[i].id){
330 assert.equal(testResult,true);
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
340 Then('I want to check that item does not exits in response', function() {
342 const id = this.context.item.id;
343 const results = this.context.responseData.results;
344 var testResult = false;
346 for(var i=0; i< results.length; i++){
347 if ( id == results[i].id){
352 assert.equal(testResult,false);