69a703cc94b4985b373fce9da1abbfe215dab217
[sdc/sdc-workflow-designer.git] / workflow-bdd / stepDefinitions / General_Steps.js
1 /*
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  */
16 const {Then, When, Given} = require('cucumber');
17 const assert = require('assert');
18 const _ = require('lodash');
19 const normalizeNewline = require('normalize-newline');
20 require('node-zip');
21 YAML = require('yamljs');
22 const fs = require('fs');
23 const util = require('./Utils.js');
24
25 function 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  **/
36 Given('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
42  * @exampleFile ResponseDataChecks.feature
43  * @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  **/
49 Given('Response Data:', function (docString) {
50         this.context.responseData = JSON.parse(docString);
51 });
52
53 /**
54  * @module ContextData
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}
57  **/
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);
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  **/
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);
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  **/
79 Then('I want to set property {string} to value {string}', function(string, string2)  {
80         _.set(this.context, string, string2);
81 });
82
83 /**
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}
88  **/
89 Then('I want to check property {string} for value {string}', function(string, string2)  {
90         assert.equal(_.get(this.context.responseData, string), string2);
91 });
92 /**
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}
98  **/
99
100 Then('I want to check property {string} for value:', function(string, docString)  {
101         assert.equal(_.get(this.context.responseData, string), docString.trim());
102 });
103 /**
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}
108  **/
109 Then('I want to check property {string} for value {int}', function(string, int)  {
110         assert.equal(_.get(this.context.responseData, string), int);
111 });
112
113 /**
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"
118  **/
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");
121 });
122 /**
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}
127  **/
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);
131 });
132 /**
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
137  **/
138 Then('I want to check property {string} exists', function(string)  {
139         assert.equal(_.has(this.context.responseData, string), true);
140 });
141 /**
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
146  **/
147 Then('I want to check property {string} does not exist', function(string)  {
148         assert.equal(_.has(this.context.responseData, string), false);
149 });
150
151 /**
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}
156  **/
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);
160 });
161
162 /**
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}
167  **/
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);
171 });
172
173 /**
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}
178  **/
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);
183 });
184
185 /**
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}
190  **/
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);
195 });
196
197 /**
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
202 **/
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('--------------------------------------');
207 });
208 /**
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
213  **/
214 Then('I want the following to fail', function()  {
215         this.context.shouldFail = true;
216 });
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  **/
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;
227 });
228
229 /**
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}
234  **/
235 Then('I want the following to fail with error code {string}', function(string)  {
236         this.context.shouldFail = true;
237         this.context.errorCode = string;
238 });
239
240
241 /**
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}
246  **/
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;
251 });
252
253 /**
254  * @module ZipData
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}
258  **/
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));
265 });
266
267 /**
268  * @module ZipData
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
272  **/
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;
279         callback();
280 });
281
282
283 /**
284  * @module ZipData
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
288  **/
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);
294         callback();
295 });
296
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));
300 });
301
302 /**
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
307  **/
308 Then('I want to check that item exits in response', function() {
309
310   const id = this.context.item.id;
311   const results = this.context.responseData.results;
312   var testResult = false;
313
314   for(var i=0; i< results.length; i++){
315      if ( id == results[i].id){
316             testResult = true;
317      }
318   }
319
320    assert.equal(testResult,true);
321 });
322
323
324 /**
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
329  **/
330 Then('I want to check that item does not exits in response', function() {
331
332   const id = this.context.item.id;
333   const results = this.context.responseData.results;
334   var testResult = false;
335
336   for(var i=0; i< results.length; i++){
337      if ( id == results[i].id){
338             testResult = true;
339      }
340   }
341
342    assert.equal(testResult,false);
343 });