blob: e59d441e106eb59d83ff9e940fda3c7c264826d0 [file] [log] [blame]
ilanap637206b2018-02-04 17:06:22 +02001/*
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 */
16const {Then, When, Given} = require('cucumber');
17const assert = require('assert');
18const _ = require('lodash');
19const normalizeNewline = require('normalize-newline');
20require('node-zip');
21YAML = require('yamljs');
22const fs = require('fs');
ilanap061ca932019-08-04 10:16:33 +030023const util = require('../cucumber-common/utils/Utils.js');
ilanap637206b2018-02-04 17:06:22 +020024
ayalaben705fc2b2018-03-15 15:59:25 +020025function getPath(path, context) {
26 let compiled = _.template(path);
27 return compiled(context);
28}
29
ilanap637206b2018-02-04 17:06:22 +020030/**
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 **/
36Given('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 **/
49Given('Response Data:', function (docString) {
50 this.context.responseData = JSON.parse(docString);
51});
52
53/**
54 * @module ContextData
ilanap637206b2018-02-04 17:06:22 +020055 * @description Copy a property from the response data to context Item/VLM/VSP data, example: vsp.componentId
56 * @step I want to save on the context for {string} property {string} with value {string}
57 **/
58Then('I want to save on the context for {string} property {string} with value {string}', function(string, string1, string2) {
59 assert.equal(_.includes(['VLM', 'VSP', '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 **/
69Then('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 **/
79Then('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 **/
89Then('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
katy.rotman0f75bea2018-02-26 11:48:50 +020094 * @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
100Then('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
ilanap637206b2018-02-04 17:06:22 +0200105 * @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 **/
109Then('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 **/
talig4d0fac72018-04-16 08:50:56 +0300118Then('I want to check property {string} to be {word}', function(string, string2) {
119 assert.equal(_.get(this.context.responseData, string), string2.toLowerCase() == "true");
ilanap637206b2018-02-04 17:06:22 +0200120});
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 **/
127Then('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 **/
137Then('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 **/
146Then('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**/
156Then('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 **/
167Then('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 have the error code on the return data
174 * @exampleFile Example_VSP.feature
175 * @step I want the following to fail with error code {string}
176 **/
177Then('I want the following to fail with error code {string}', function(string) {
178 this.context.shouldFail = true;
179 this.context.errorCode = string;
180});
181
ayalabenaad27812018-02-18 10:03:22 +0200182
183/**
184 * @module ContextData
185 * @description Set this in order to check that the following Rest call will have the error message on the return data
186 * @exampleFile DeleteVLMCertified.feature
187 * @step I want the following to fail with error message {string}
188 **/
189Then('I want the following to fail with error message {string}', function(string) {
190 this.context.shouldFail = true;
ayalaben705fc2b2018-03-15 15:59:25 +0200191 let errorMessage = getPath(string, this.context);
192 this.context.errorMessage = errorMessage;
ayalabenaad27812018-02-18 10:03:22 +0200193});
194
ilanap637206b2018-02-04 17:06:22 +0200195/**
196 * @module ZipData
197 * @description Use this in order to extract a file from a zip file and to compare it to a local file (string comparison).
198 * @exampleFile Example_VSP.feature
199 * @step I want to compare the content of the entry {string} in the zip {string} with file {string}
200 **/
201Then ('I want to compare the content of the entry {string} in the zip {string} with file {string}', function (string, string2, string3) {
202 let zipFile = fs.readFileSync(string2, 'binary');
203 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
204 let fileData = zip.files[string]._data;
205 let compareFileData = fs.readFileSync(string3, {encoding: 'ascii'});
206 assert.equal(normalizeNewline(compareFileData), normalizeNewline(fileData));
207});
208
209/**
210 * @module ZipData
211 * @description Loads the yaml from zip file onto the context responseData as JSON for running checks on the output
212 * @exampleFile Example_VSP.feature
213 * @step I want to load the yaml content of the entry {string} in the zip {string} to context
214 **/
215Then ('I want to load the yaml content of the entry {string} in the zip {string} to context', function (string, string2, callback) {
216 let zipFile = fs.readFileSync(string2, 'binary');
217 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
218 let fileData = zip.files[string]._data;
219 let nativeObject = YAML.parse(fileData);
220 this.context.responseData = nativeObject;
221 callback();
222});
223
224
225/**
226 * @module ZipData
227 * @description Loads the json from zip file onto the context responseData for running check son the output
228 * @exampleFile Example_VSP.feature
229 * @step I want to load the json content of the entry {string} in the zip {string} to context
230 **/
231When('I want to load the json content of the entry {string} in the zip {string} to context', function (string, string2, callback) {
232 let zipFile = fs.readFileSync(string2, 'binary');
233 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
234 let str = zip.files[string]._data;
235 this.context.responseData = JSON.parse(str);
236 callback();
ayalaben705fc2b2018-03-15 15:59:25 +0200237});
238
239/**
240 * @module ResponseData
talig4182fc32018-04-22 11:04:16 +0300241 * @description Check that the result list doesn't contain an element with property x which has value
242 * equals to saved property y
243 * @exampleFile ListItemsFilters.feature
244 * @step I want to check that element in the response list with {string} equals to value of saved property {string} does not exist
245 **/
246Then('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) {
247 const results = this.context.responseData.results;
248 assert.equal(results.find(result => this.context[valueProperty] === _.get(result, propertyPath)), undefined);
249});
250
251/**
252 * @module ResponseData
253 * @description Check that the result list contains an element with property x which has value
254 * equals to saved property y
255 * @exampleFile ListItemsFilters.feature
256 * @step I want to check that element in the response list with {string} equals to value of saved property {string} exists
257 **/
258Then('I want to check that element in the response list with {string} equals to value of saved property {string} exists', function(propertyPath, valueProperty) {
259 const results = this.context.responseData.results;
260 assert.notEqual(results.find(result => this.context[valueProperty] === _.get(result, propertyPath)), undefined);
261});
262
263/**
264 * @module ResponseData
ayalaben705fc2b2018-03-15 15:59:25 +0200265 * @description Check that the itemId from context exits in result of responseData
266 * exampleFile ArchiveItem.feature
267 * step I want to check that item exits in response
268 **/
269Then('I want to check that item exits in response', function() {
270
271 const id = this.context.item.id;
272 const results = this.context.responseData.results;
273 var testResult = false;
274
275 for(var i=0; i< results.length; i++){
276 if ( id == results[i].id){
277 testResult = true;
278 }
279 }
280
281 assert.equal(testResult,true);
282});
283
284
285/**
286 * @module ResponseData
287 * @description Check that the itemId from context does NOT exits in result of responseData
288 * exampleFile ArchiveItem.feature
289 * step I want to check that item does not exits in response
290 **/
291Then('I want to check that item does not exits in response', function() {
292
293 const id = this.context.item.id;
294 const results = this.context.responseData.results;
295 var testResult = false;
296
297 for(var i=0; i< results.length; i++){
298 if ( id == results[i].id){
299 testResult = true;
300 }
301 }
302
303 assert.equal(testResult,false);
ilanap637206b2018-02-04 17:06:22 +0200304});