ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [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'); |
ilanap | 061ca93 | 2019-08-04 10:16:33 +0300 | [diff] [blame^] | 23 | const util = require('../cucumber-common/utils/Utils.js'); |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 24 | |
ayalaben | 705fc2b | 2018-03-15 15:59:25 +0200 | [diff] [blame] | 25 | function getPath(path, context) { |
| 26 | let compiled = _.template(path); |
| 27 | return compiled(context); |
| 28 | } |
| 29 | |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 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 |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 55 | * @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 | **/ |
| 58 | Then('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 | **/ |
| 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 |
katy.rotman | 0f75bea | 2018-02-26 11:48:50 +0200 | [diff] [blame] | 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 |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 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 | **/ |
talig | 4d0fac7 | 2018-04-16 08:50:56 +0300 | [diff] [blame] | 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"); |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 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 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 | **/ |
| 177 | Then('I want the following to fail with error code {string}', function(string) { |
| 178 | this.context.shouldFail = true; |
| 179 | this.context.errorCode = string; |
| 180 | }); |
| 181 | |
ayalaben | aad2781 | 2018-02-18 10:03:22 +0200 | [diff] [blame] | 182 | |
| 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 | **/ |
| 189 | Then('I want the following to fail with error message {string}', function(string) { |
| 190 | this.context.shouldFail = true; |
ayalaben | 705fc2b | 2018-03-15 15:59:25 +0200 | [diff] [blame] | 191 | let errorMessage = getPath(string, this.context); |
| 192 | this.context.errorMessage = errorMessage; |
ayalaben | aad2781 | 2018-02-18 10:03:22 +0200 | [diff] [blame] | 193 | }); |
| 194 | |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 195 | /** |
| 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 | **/ |
| 201 | Then ('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 | **/ |
| 215 | Then ('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 | **/ |
| 231 | When('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(); |
ayalaben | 705fc2b | 2018-03-15 15:59:25 +0200 | [diff] [blame] | 237 | }); |
| 238 | |
| 239 | /** |
| 240 | * @module ResponseData |
talig | 4182fc3 | 2018-04-22 11:04:16 +0300 | [diff] [blame] | 241 | * @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 | **/ |
| 246 | 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) { |
| 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 | **/ |
| 258 | Then('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 |
ayalaben | 705fc2b | 2018-03-15 15:59:25 +0200 | [diff] [blame] | 265 | * @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 | **/ |
| 269 | Then('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 | **/ |
| 291 | Then('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); |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 304 | }); |