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 Example_ResponseData_CheckAndManipulation.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 Example_ResponseData_CheckAndManipulation.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 Example_ResponseData_CheckAndManipulation.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);
113 * @module ResponseData
114 * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id)
115 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
116 * @step I want to check property {string} to be "True/False"
118 Then('I want to check property {string} to be {word}', function(string, string2) {
119 assert.equal(_.get(this.context.responseData, string), string2.toLowerCase() == "true");
122 * @module ResponseData
123 * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id)
124 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
125 * @step I want to check property {string} to have length {int}
127 Then('I want to check property {string} to have length {int}', function(string, intLength) {
128 let arrayProp = _.get(this.context.responseData, string);
129 assert.equal(arrayProp.length, intLength);
132 * @module ResponseData
133 * @description Will check the output data for a property and make sure it exists
134 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
135 * @step I want to check property {string} exists
137 Then('I want to check property {string} exists', function(string) {
138 assert.equal(_.has(this.context.responseData, string), true);
141 * @module ResponseData
142 * @description Will check the output data for a property and make sure it does not exist
143 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
144 * @step I want to check property {string} does not exist
146 Then('I want to check property {string} does not exist', function(string) {
147 assert.equal(_.has(this.context.responseData, string), false);
151 * @module ContextData
152 * @description Use during development to see what is on the context
153 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
154 * @step I want to print context data
156 Then('I want to print the context data', function() {
157 console.log('------------ context ---------------');
158 console.log(JSON.stringify(this.context, null, 2));
159 console.log('--------------------------------------');
162 * @module ContextData
163 * @description Set this in order to check that the following Rest call will not have response code 200
164 * @exampleFile Example_Rest_Calls.feature
165 * @step I want the following to fail
167 Then('I want the following to fail', function() {
168 this.context.shouldFail = true;
172 * @module ContextData
173 * @description Set this in order to check that the following Rest call will not have response code 200
174 * @exampleFile Example_Rest_Calls.feature
175 * @step I want the following to fail
177 Then('I want the following to fail with response status code {int}', function(int) {
178 this.context.shouldFail = true;
179 this.context.responseStatusCode = int;
183 * @module ContextData
184 * @description Set this in order to check that the following Rest call will have the error code on the return data
185 * @exampleFile Example_VSP.feature
186 * @step I want the following to fail with error code {string}
188 Then('I want the following to fail with error code {string}', function(string) {
189 this.context.shouldFail = true;
190 this.context.errorCode = string;
195 * @module ContextData
196 * @description Set this in order to check that the following Rest call will have the error message on the return data
197 * @exampleFile DeleteVLMCertified.feature
198 * @step I want the following to fail with error message {string}
200 Then('I want the following to fail with error message {string}', function(string) {
201 this.context.shouldFail = true;
202 let errorMessage = getPath(string, this.context);
203 this.context.errorMessage = errorMessage;
208 * @description Use this in order to extract a file from a zip file and to compare it to a local file (string comparison).
209 * @exampleFile Example_VSP.feature
210 * @step I want to compare the content of the entry {string} in the zip {string} with file {string}
212 Then ('I want to compare the content of the entry {string} in the zip {string} with file {string}', function (string, string2, string3) {
213 let zipFile = fs.readFileSync(string2, 'binary');
214 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
215 let fileData = zip.files[string]._data;
216 let compareFileData = fs.readFileSync(string3, {encoding: 'ascii'});
217 assert.equal(normalizeNewline(compareFileData), normalizeNewline(fileData));
222 * @description Loads the yaml from zip file onto the context responseData as JSON for running checks on the output
223 * @exampleFile Example_VSP.feature
224 * @step I want to load the yaml content of the entry {string} in the zip {string} to context
226 Then ('I want to load the yaml content of the entry {string} in the zip {string} to context', function (string, string2, callback) {
227 let zipFile = fs.readFileSync(string2, 'binary');
228 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
229 let fileData = zip.files[string]._data;
230 let nativeObject = YAML.parse(fileData);
231 this.context.responseData = nativeObject;
238 * @description Loads the json from zip file onto the context responseData for running check son the output
239 * @exampleFile Example_VSP.feature
240 * @step I want to load the json content of the entry {string} in the zip {string} to context
242 When('I want to load the json content of the entry {string} in the zip {string} to context', function (string, string2, callback) {
243 let zipFile = fs.readFileSync(string2, 'binary');
244 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
245 let str = zip.files[string]._data;
246 this.context.responseData = JSON.parse(str);
251 * @module ResponseData
252 * @description Check that the result list doesn't contain an element with property x which has value
253 * equals to saved property y
254 * @exampleFile ListItemsFilters.feature
255 * @step I want to check that element in the response list with {string} equals to value of saved property {string} does not exist
257 Then('I want to check that element in the response list with {string} equals to value of saved property {string} does not exist', function (propertyPath, valueProperty) {
258 const results = this.context.responseData.results;
259 assert.equal(results.find(result => this.context[valueProperty] === _.get(result, propertyPath)), undefined);
263 * @module ResponseData
264 * @description Check that the result list contains an element with property x which has value
265 * equals to saved property y
266 * @exampleFile ListItemsFilters.feature
267 * @step I want to check that element in the response list with {string} equals to value of saved property {string} exists
269 Then('I want to check that element in the response list with {string} equals to value of saved property {string} exists', function(propertyPath, valueProperty) {
270 const results = this.context.responseData.results;
271 assert.notEqual(results.find(result => this.context[valueProperty] === _.get(result, propertyPath)), undefined);
275 Then('I want to check that property {string} in the response equals to value of saved property {string}', function(propertyPath, valueProperty) {
276 const results = this.context.responseData;
277 assert.equal(results[propertyPath],this.context[valueProperty]);
281 * @module ResponseData
282 * @description Check that the itemId from context exits in result of responseData
283 * exampleFile ArchiveItem.feature
284 * step I want to check that item exits in response
286 Then('I want to check that item exits in response', function() {
288 const id = this.context.item.id;
289 const results = this.context.responseData.results;
290 var testResult = false;
292 for(var i=0; i< results.length; i++){
293 if ( id == results[i].id){
298 assert.equal(testResult,true);
303 * @module ResponseData
304 * @description Check that the itemId from context does NOT exits in result of responseData
305 * exampleFile ArchiveItem.feature
306 * step I want to check that item does not exits in response
308 Then('I want to check that item does not 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,false);