ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 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 |
talig | 02201e4 | 2018-08-05 10:47:36 +0300 | [diff] [blame] | 42 | * @exampleFile ResponseDataChecks.feature |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 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 |
talig | 39c01dd | 2018-07-15 15:41:31 +0300 | [diff] [blame] | 55 | * @description Copy a property from the response data to context Item data, example: item.componentId |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 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) { |
talig | 39c01dd | 2018-07-15 15:41:31 +0300 | [diff] [blame] | 59 | assert.equal(_.includes(['Item'], string), true); |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 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 | /** |
talig | ff78ef4 | 2018-08-09 13:13:23 +0300 | [diff] [blame^] | 84 | * @module ContextData |
| 85 | * @description sets context property to a random value |
| 86 | * @exampleFile Example_Rest_Calls.feature |
| 87 | * @step I want to update the input property {string} with a random value |
| 88 | **/ |
| 89 | Then('I want to set property {string} with a random value', function (string) { |
| 90 | _.set(this.context, string, util.random()); |
| 91 | }); |
| 92 | |
| 93 | /** |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 94 | * @module ResponseData |
| 95 | * @description Will check the output data for a property and a value. property can be a path (example: results[0].id) |
talig | 02201e4 | 2018-08-05 10:47:36 +0300 | [diff] [blame] | 96 | * @exampleFile ResponseDataChecks.feature |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 97 | * @step I want to check property {string} for value {string} |
| 98 | **/ |
| 99 | Then('I want to check property {string} for value {string}', function(string, string2) { |
| 100 | assert.equal(_.get(this.context.responseData, string), string2); |
| 101 | }); |
| 102 | /** |
| 103 | * @module ResponseData |
| 104 | * @description Will check the output data for a property and a value. property can be a path |
| 105 | * (example: results[0].id). Supports comparison to a long String by allowing a line break |
| 106 | * @exampleFile VirtualMachineInterfaceValidationHeatResourceMissingProperties.feature |
| 107 | * @step I want to check property {string} for value {string} |
| 108 | **/ |
| 109 | |
| 110 | Then('I want to check property {string} for value:', function(string, docString) { |
| 111 | assert.equal(_.get(this.context.responseData, string), docString.trim()); |
| 112 | }); |
| 113 | /** |
| 114 | * @module ResponseData |
| 115 | * @description Will check the output data for a property and a integer. property can be a path (example: results[0].id) |
talig | 02201e4 | 2018-08-05 10:47:36 +0300 | [diff] [blame] | 116 | * @exampleFile ResponseDataChecks.feature |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 117 | * @step I want to check property {string} for value {int} |
| 118 | **/ |
| 119 | Then('I want to check property {string} for value {int}', function(string, int) { |
| 120 | assert.equal(_.get(this.context.responseData, string), int); |
| 121 | }); |
talig | 02201e4 | 2018-08-05 10:47:36 +0300 | [diff] [blame] | 122 | |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 123 | /** |
| 124 | * @module ResponseData |
| 125 | * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id) |
talig | 02201e4 | 2018-08-05 10:47:36 +0300 | [diff] [blame] | 126 | * @exampleFile ResponseDataChecks.feature |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 127 | * @step I want to check property {string} to be "True/False" |
| 128 | **/ |
| 129 | Then('I want to check property {string} to be {word}', function(string, string2) { |
| 130 | assert.equal(_.get(this.context.responseData, string), string2.toLowerCase() == "true"); |
| 131 | }); |
| 132 | /** |
| 133 | * @module ResponseData |
| 134 | * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id) |
talig | 02201e4 | 2018-08-05 10:47:36 +0300 | [diff] [blame] | 135 | * @exampleFile ResponseDataChecks.feature |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 136 | * @step I want to check property {string} to have length {int} |
| 137 | **/ |
| 138 | Then('I want to check property {string} to have length {int}', function(string, intLength) { |
| 139 | let arrayProp = _.get(this.context.responseData, string); |
| 140 | assert.equal(arrayProp.length, intLength); |
| 141 | }); |
| 142 | /** |
| 143 | * @module ResponseData |
| 144 | * @description Will check the output data for a property and make sure it exists |
talig | 02201e4 | 2018-08-05 10:47:36 +0300 | [diff] [blame] | 145 | * @exampleFile ResponseDataChecks.feature |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 146 | * @step I want to check property {string} exists |
| 147 | **/ |
| 148 | Then('I want to check property {string} exists', function(string) { |
| 149 | assert.equal(_.has(this.context.responseData, string), true); |
| 150 | }); |
| 151 | /** |
| 152 | * @module ResponseData |
| 153 | * @description Will check the output data for a property and make sure it does not exist |
talig | 02201e4 | 2018-08-05 10:47:36 +0300 | [diff] [blame] | 154 | * @exampleFile ResponseDataChecks.feature |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 155 | * @step I want to check property {string} does not exist |
| 156 | **/ |
| 157 | Then('I want to check property {string} does not exist', function(string) { |
| 158 | assert.equal(_.has(this.context.responseData, string), false); |
| 159 | }); |
| 160 | |
| 161 | /** |
talig | 02201e4 | 2018-08-05 10:47:36 +0300 | [diff] [blame] | 162 | * @module ResponseData |
| 163 | * @description Will check the output data for a property and a value. property can be a path (example: results[0].id) |
| 164 | * @exampleFile ResponseDataChecks.feature |
| 165 | * @step I want to check property {string} for value {string} |
| 166 | **/ |
| 167 | Then('I want to check in the list {string} property {string} with value {string} exists', function(listPath, propertyPath, value) { |
| 168 | var list = _.get(this.context.responseData, listPath); |
| 169 | assert.notEqual(list.find(element => _.get(element, propertyPath) === value), undefined); |
| 170 | }); |
| 171 | |
| 172 | /** |
| 173 | * @module ResponseData |
| 174 | * @description Will check the output data for a property and a value. property can be a path (example: results[0].id) |
| 175 | * @exampleFile ResponseDataChecks.feature |
| 176 | * @step I want to check property {string} for value {string} |
| 177 | **/ |
| 178 | Then('I want to check in the list {string} property {string} with value {string} does not exist', function(listPath, propertyPath, value) { |
| 179 | var list = _.get(this.context.responseData, listPath); |
| 180 | assert.equal(list.find(element => _.get(element, propertyPath) === value), undefined); |
| 181 | }); |
| 182 | |
| 183 | /** |
| 184 | * @module ResponseData |
| 185 | * @description Will check the output data for a property and a value. property can be a path (example: results[0].id) |
| 186 | * @exampleFile ResponseDataChecks.feature |
| 187 | * @step I want to check property {string} for value {string} |
| 188 | **/ |
| 189 | Then('I want to check in the list {string} property {string} with value of saved property {string} exists', function(listPath, propertyPath, valueProperty) { |
| 190 | var list = _.get(this.context.responseData, listPath); |
| 191 | var value = _.get(this.context, valueProperty); |
| 192 | assert.notEqual(list.find(element => _.get(element, propertyPath) === value), undefined); |
| 193 | }); |
| 194 | |
| 195 | /** |
| 196 | * @module ResponseData |
| 197 | * @description Will check the output data for a property and a value. property can be a path (example: results[0].id) |
| 198 | * @exampleFile ResponseDataChecks.feature |
| 199 | * @step I want to check property {string} for value {string} |
| 200 | **/ |
| 201 | Then('I want to check in the list {string} property {string} with value of saved property {string} does not exist', function(listPath, propertyPath, valueProperty) { |
| 202 | var list = _.get(this.context.responseData, listPath); |
| 203 | var value = _.get(this.context, valueProperty); |
| 204 | assert.equal(list.find(element => _.get(element, propertyPath) === value), undefined); |
| 205 | }); |
| 206 | |
| 207 | /** |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 208 | * @module ContextData |
| 209 | * @description Use during development to see what is on the context |
talig | 02201e4 | 2018-08-05 10:47:36 +0300 | [diff] [blame] | 210 | * @exampleFile ResponseDataChecks.feature |
talig | 8f91bb1 | 2018-07-23 10:15:59 +0300 | [diff] [blame] | 211 | * @step I want to print the context data |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 212 | **/ |
| 213 | Then('I want to print the context data', function() { |
| 214 | console.log('------------ context ---------------'); |
| 215 | console.log(JSON.stringify(this.context, null, 2)); |
| 216 | console.log('--------------------------------------'); |
| 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', function() { |
| 225 | this.context.shouldFail = true; |
| 226 | }); |
| 227 | |
| 228 | /** |
| 229 | * @module ContextData |
talig | 39c01dd | 2018-07-15 15:41:31 +0300 | [diff] [blame] | 230 | * @description Set this in order to check that the following Rest call will not have response code 200 |
| 231 | * @exampleFile Example_Rest_Calls.feature |
| 232 | * @step I want the following to fail |
| 233 | **/ |
| 234 | Then('I want the following to fail with response status code {int}', function(int) { |
| 235 | this.context.shouldFail = true; |
| 236 | this.context.responseStatusCode = int; |
| 237 | }); |
| 238 | |
| 239 | /** |
| 240 | * @module ContextData |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 241 | * @description Set this in order to check that the following Rest call will have the error code on the return data |
| 242 | * @exampleFile Example_VSP.feature |
| 243 | * @step I want the following to fail with error code {string} |
| 244 | **/ |
| 245 | Then('I want the following to fail with error code {string}', function(string) { |
| 246 | this.context.shouldFail = true; |
| 247 | this.context.errorCode = string; |
| 248 | }); |
| 249 | |
| 250 | |
| 251 | /** |
| 252 | * @module ContextData |
| 253 | * @description Set this in order to check that the following Rest call will have the error message on the return data |
| 254 | * @exampleFile DeleteVLMCertified.feature |
| 255 | * @step I want the following to fail with error message {string} |
| 256 | **/ |
| 257 | Then('I want the following to fail with error message {string}', function(string) { |
| 258 | this.context.shouldFail = true; |
| 259 | let errorMessage = getPath(string, this.context); |
| 260 | this.context.errorMessage = errorMessage; |
| 261 | }); |
| 262 | |
| 263 | /** |
| 264 | * @module ZipData |
| 265 | * @description Use this in order to extract a file from a zip file and to compare it to a local file (string comparison). |
| 266 | * @exampleFile Example_VSP.feature |
| 267 | * @step I want to compare the content of the entry {string} in the zip {string} with file {string} |
| 268 | **/ |
| 269 | Then ('I want to compare the content of the entry {string} in the zip {string} with file {string}', function (string, string2, string3) { |
| 270 | let zipFile = fs.readFileSync(string2, 'binary'); |
| 271 | let zip = new JSZip(zipFile, {base64: false, checkCRC32: true}); |
| 272 | let fileData = zip.files[string]._data; |
| 273 | let compareFileData = fs.readFileSync(string3, {encoding: 'ascii'}); |
| 274 | assert.equal(normalizeNewline(compareFileData), normalizeNewline(fileData)); |
| 275 | }); |
| 276 | |
| 277 | /** |
| 278 | * @module ZipData |
| 279 | * @description Loads the yaml from zip file onto the context responseData as JSON for running checks on the output |
| 280 | * @exampleFile Example_VSP.feature |
| 281 | * @step I want to load the yaml content of the entry {string} in the zip {string} to context |
| 282 | **/ |
| 283 | Then ('I want to load the yaml content of the entry {string} in the zip {string} to context', function (string, string2, callback) { |
| 284 | let zipFile = fs.readFileSync(string2, 'binary'); |
| 285 | let zip = new JSZip(zipFile, {base64: false, checkCRC32: true}); |
| 286 | let fileData = zip.files[string]._data; |
| 287 | let nativeObject = YAML.parse(fileData); |
| 288 | this.context.responseData = nativeObject; |
| 289 | callback(); |
| 290 | }); |
| 291 | |
| 292 | |
| 293 | /** |
| 294 | * @module ZipData |
| 295 | * @description Loads the json from zip file onto the context responseData for running check son the output |
| 296 | * @exampleFile Example_VSP.feature |
| 297 | * @step I want to load the json content of the entry {string} in the zip {string} to context |
| 298 | **/ |
| 299 | When('I want to load the json content of the entry {string} in the zip {string} to context', function (string, string2, callback) { |
| 300 | let zipFile = fs.readFileSync(string2, 'binary'); |
| 301 | let zip = new JSZip(zipFile, {base64: false, checkCRC32: true}); |
| 302 | let str = zip.files[string]._data; |
| 303 | this.context.responseData = JSON.parse(str); |
| 304 | callback(); |
| 305 | }); |
| 306 | |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 307 | Then('I want to check that property {string} in the response equals to value of saved property {string}', function(propertyPath, valueProperty) { |
| 308 | const results = this.context.responseData; |
talig | 8f91bb1 | 2018-07-23 10:15:59 +0300 | [diff] [blame] | 309 | assert.equal(results[propertyPath], _.get(this.context, valueProperty)); |
ayalaben | 3fb47c4 | 2018-06-24 10:42:43 +0300 | [diff] [blame] | 310 | }); |
| 311 | |
| 312 | /** |
| 313 | * @module ResponseData |
| 314 | * @description Check that the itemId from context exits in result of responseData |
| 315 | * exampleFile ArchiveItem.feature |
| 316 | * step I want to check that item exits in response |
| 317 | **/ |
| 318 | Then('I want to check that item exits in response', function() { |
| 319 | |
| 320 | const id = this.context.item.id; |
| 321 | const results = this.context.responseData.results; |
| 322 | var testResult = false; |
| 323 | |
| 324 | for(var i=0; i< results.length; i++){ |
| 325 | if ( id == results[i].id){ |
| 326 | testResult = true; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | assert.equal(testResult,true); |
| 331 | }); |
| 332 | |
| 333 | |
| 334 | /** |
| 335 | * @module ResponseData |
| 336 | * @description Check that the itemId from context does NOT exits in result of responseData |
| 337 | * exampleFile ArchiveItem.feature |
| 338 | * step I want to check that item does not exits in response |
| 339 | **/ |
| 340 | Then('I want to check that item does not exits in response', function() { |
| 341 | |
| 342 | const id = this.context.item.id; |
| 343 | const results = this.context.responseData.results; |
| 344 | var testResult = false; |
| 345 | |
| 346 | for(var i=0; i< results.length; i++){ |
| 347 | if ( id == results[i].id){ |
| 348 | testResult = true; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | assert.equal(testResult,false); |
| 353 | }); |