Workflow Docker containers
[sdc/sdc-workflow-designer.git] / workflow / 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 Example_ResponseData_CheckAndManipulation.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 Example_ResponseData_CheckAndManipulation.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 Example_ResponseData_CheckAndManipulation.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  * @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"
117  **/
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");
120 });
121 /**
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}
126  **/
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);
130 });
131 /**
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
136  **/
137 Then('I want to check property {string} exists', function(string)  {
138         assert.equal(_.has(this.context.responseData, string), true);
139 });
140 /**
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
145  **/
146 Then('I want to check property {string} does not exist', function(string)  {
147         assert.equal(_.has(this.context.responseData, string), false);
148 });
149
150 /**
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
155 **/
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('--------------------------------------');
160 });
161 /**
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
166  **/
167 Then('I want the following to fail', function()  {
168         this.context.shouldFail = true;
169 });
170
171 /**
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
176  **/
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;
180 });
181
182 /**
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}
187  **/
188 Then('I want the following to fail with error code {string}', function(string)  {
189         this.context.shouldFail = true;
190         this.context.errorCode = string;
191 });
192
193
194 /**
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}
199  **/
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;
204 });
205
206 /**
207  * @module ZipData
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}
211  **/
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));
218 });
219
220 /**
221  * @module ZipData
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
225  **/
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;
232         callback();
233 });
234
235
236 /**
237  * @module ZipData
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
241  **/
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);
247         callback();
248 });
249
250 /**
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
256  **/
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);
260 });
261
262 /**
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
268  **/
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);
272 });
273
274
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]);
278 });
279
280 /**
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
285  **/
286 Then('I want to check that item exits in response', function() {
287
288   const id = this.context.item.id;
289   const results = this.context.responseData.results;
290   var testResult = false;
291
292   for(var i=0; i< results.length; i++){
293      if ( id == results[i].id){
294             testResult = true;
295      }
296   }
297
298    assert.equal(testResult,true);
299 });
300
301
302 /**
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
307  **/
308 Then('I want to check that item does not 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,false);
321 });