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);
84 * @module ResponseData
85 * @description Will check the output data for a property and a value. property can be a path (example: results[0].id)
86 * @exampleFile ResponseDataChecks.feature
87 * @step I want to check property {string} for value {string}
89 Then('I want to check property {string} for value {string}', function(string, string2) {
90 assert.equal(_.get(this.context.responseData, string), string2);
93 * @module ResponseData
94 * @description Will check the output data for a property and a value. property can be a path
95 * (example: results[0].id). Supports comparison to a long String by allowing a line break
96 * @exampleFile VirtualMachineInterfaceValidationHeatResourceMissingProperties.feature
97 * @step I want to check property {string} for value {string}
100 Then('I want to check property {string} for value:', function(string, docString) {
101 assert.equal(_.get(this.context.responseData, string), docString.trim());
104 * @module ResponseData
105 * @description Will check the output data for a property and a integer. property can be a path (example: results[0].id)
106 * @exampleFile ResponseDataChecks.feature
107 * @step I want to check property {string} for value {int}
109 Then('I want to check property {string} for value {int}', function(string, int) {
110 assert.equal(_.get(this.context.responseData, string), int);
114 * @module ResponseData
115 * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id)
116 * @exampleFile ResponseDataChecks.feature
117 * @step I want to check property {string} to be "True/False"
119 Then('I want to check property {string} to be {word}', function(string, string2) {
120 assert.equal(_.get(this.context.responseData, string), string2.toLowerCase() == "true");
123 * @module ResponseData
124 * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id)
125 * @exampleFile ResponseDataChecks.feature
126 * @step I want to check property {string} to have length {int}
128 Then('I want to check property {string} to have length {int}', function(string, intLength) {
129 let arrayProp = _.get(this.context.responseData, string);
130 assert.equal(arrayProp.length, intLength);
133 * @module ResponseData
134 * @description Will check the output data for a property and make sure it exists
135 * @exampleFile ResponseDataChecks.feature
136 * @step I want to check property {string} exists
138 Then('I want to check property {string} exists', function(string) {
139 assert.equal(_.has(this.context.responseData, string), true);
142 * @module ResponseData
143 * @description Will check the output data for a property and make sure it does not exist
144 * @exampleFile ResponseDataChecks.feature
145 * @step I want to check property {string} does not exist
147 Then('I want to check property {string} does not exist', function(string) {
148 assert.equal(_.has(this.context.responseData, string), false);
152 * @module ResponseData
153 * @description Will check the output data for a property and a value. property can be a path (example: results[0].id)
154 * @exampleFile ResponseDataChecks.feature
155 * @step I want to check property {string} for value {string}
157 Then('I want to check in the list {string} property {string} with value {string} exists', function(listPath, propertyPath, value) {
158 var list = _.get(this.context.responseData, listPath);
159 assert.notEqual(list.find(element => _.get(element, propertyPath) === value), undefined);
163 * @module ResponseData
164 * @description Will check the output data for a property and a value. property can be a path (example: results[0].id)
165 * @exampleFile ResponseDataChecks.feature
166 * @step I want to check property {string} for value {string}
168 Then('I want to check in the list {string} property {string} with value {string} does not exist', function(listPath, propertyPath, value) {
169 var list = _.get(this.context.responseData, listPath);
170 assert.equal(list.find(element => _.get(element, propertyPath) === value), undefined);
174 * @module ResponseData
175 * @description Will check the output data for a property and a value. property can be a path (example: results[0].id)
176 * @exampleFile ResponseDataChecks.feature
177 * @step I want to check property {string} for value {string}
179 Then('I want to check in the list {string} property {string} with value of saved property {string} exists', function(listPath, propertyPath, valueProperty) {
180 var list = _.get(this.context.responseData, listPath);
181 var value = _.get(this.context, valueProperty);
182 assert.notEqual(list.find(element => _.get(element, propertyPath) === value), undefined);
186 * @module ResponseData
187 * @description Will check the output data for a property and a value. property can be a path (example: results[0].id)
188 * @exampleFile ResponseDataChecks.feature
189 * @step I want to check property {string} for value {string}
191 Then('I want to check in the list {string} property {string} with value of saved property {string} does not exist', function(listPath, propertyPath, valueProperty) {
192 var list = _.get(this.context.responseData, listPath);
193 var value = _.get(this.context, valueProperty);
194 assert.equal(list.find(element => _.get(element, propertyPath) === value), undefined);
198 * @module ContextData
199 * @description Use during development to see what is on the context
200 * @exampleFile ResponseDataChecks.feature
201 * @step I want to print the context data
203 Then('I want to print the context data', function() {
204 console.log('------------ context ---------------');
205 console.log(JSON.stringify(this.context, null, 2));
206 console.log('--------------------------------------');
209 * @module ContextData
210 * @description Set this in order to check that the following Rest call will not have response code 200
211 * @exampleFile Example_Rest_Calls.feature
212 * @step I want the following to fail
214 Then('I want the following to fail', function() {
215 this.context.shouldFail = true;
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 with response status code {int}', function(int) {
225 this.context.shouldFail = true;
226 this.context.responseStatusCode = int;
230 * @module ContextData
231 * @description Set this in order to check that the following Rest call will have the error code on the return data
232 * @exampleFile Example_VSP.feature
233 * @step I want the following to fail with error code {string}
235 Then('I want the following to fail with error code {string}', function(string) {
236 this.context.shouldFail = true;
237 this.context.errorCode = string;
242 * @module ContextData
243 * @description Set this in order to check that the following Rest call will have the error message on the return data
244 * @exampleFile DeleteVLMCertified.feature
245 * @step I want the following to fail with error message {string}
247 Then('I want the following to fail with error message {string}', function(string) {
248 this.context.shouldFail = true;
249 let errorMessage = getPath(string, this.context);
250 this.context.errorMessage = errorMessage;
255 * @description Use this in order to extract a file from a zip file and to compare it to a local file (string comparison).
256 * @exampleFile Example_VSP.feature
257 * @step I want to compare the content of the entry {string} in the zip {string} with file {string}
259 Then ('I want to compare the content of the entry {string} in the zip {string} with file {string}', function (string, string2, string3) {
260 let zipFile = fs.readFileSync(string2, 'binary');
261 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
262 let fileData = zip.files[string]._data;
263 let compareFileData = fs.readFileSync(string3, {encoding: 'ascii'});
264 assert.equal(normalizeNewline(compareFileData), normalizeNewline(fileData));
269 * @description Loads the yaml from zip file onto the context responseData as JSON for running checks on the output
270 * @exampleFile Example_VSP.feature
271 * @step I want to load the yaml content of the entry {string} in the zip {string} to context
273 Then ('I want to load the yaml content of the entry {string} in the zip {string} to context', function (string, string2, callback) {
274 let zipFile = fs.readFileSync(string2, 'binary');
275 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
276 let fileData = zip.files[string]._data;
277 let nativeObject = YAML.parse(fileData);
278 this.context.responseData = nativeObject;
285 * @description Loads the json from zip file onto the context responseData for running check son the output
286 * @exampleFile Example_VSP.feature
287 * @step I want to load the json content of the entry {string} in the zip {string} to context
289 When('I want to load the json content of the entry {string} in the zip {string} to context', function (string, string2, callback) {
290 let zipFile = fs.readFileSync(string2, 'binary');
291 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
292 let str = zip.files[string]._data;
293 this.context.responseData = JSON.parse(str);
297 Then('I want to check that property {string} in the response equals to value of saved property {string}', function(propertyPath, valueProperty) {
298 const results = this.context.responseData;
299 assert.equal(results[propertyPath], _.get(this.context, valueProperty));
303 * @module ResponseData
304 * @description Check that the itemId from context exits in result of responseData
305 * exampleFile ArchiveItem.feature
306 * step I want to check that item exits in response
308 Then('I want to check that item exits in response', function() {
310 const id = this.context.item.id;
311 const results = this.context.responseData.results;
312 var testResult = false;
314 for(var i=0; i< results.length; i++){
315 if ( id == results[i].id){
320 assert.equal(testResult,true);
325 * @module ResponseData
326 * @description Check that the itemId from context does NOT exits in result of responseData
327 * exampleFile ArchiveItem.feature
328 * step I want to check that item does not exits in response
330 Then('I want to check that item does not exits in response', function() {
332 const id = this.context.item.id;
333 const results = this.context.responseData.results;
334 var testResult = false;
336 for(var i=0; i< results.length; i++){
337 if ( id == results[i].id){
342 assert.equal(testResult,false);