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'); |
| 23 | const util = require('./Utils.js'); |
| 24 | |
| 25 | /** |
| 26 | * @module ContextData |
| 27 | * @description Use with "Given". Use ONLY for local testing when you know the value of the Item you want to use |
| 28 | * instead of creating a new one. |
| 29 | * @step Item {string} and version Id {string} |
| 30 | **/ |
| 31 | Given('Item {string} and version Id {string}', function (string, string2) { |
| 32 | this.context.item.id = string; |
| 33 | this.context.item.versionId = string2; |
| 34 | }); |
| 35 | /** |
| 36 | * @module ContextData |
| 37 | * @exampleFile Example_ResponseData_CheckAndManipulation.feature |
| 38 | * @description Response Data::<br> |
| 39 | * """<br> |
| 40 | * {jsonObject}<br> |
| 41 | * """<br> |
| 42 | * @step Use with "Given". Use ONLY for local testing, creates a response data object |
| 43 | **/ |
| 44 | Given('Response Data:', function (docString) { |
| 45 | this.context.responseData = JSON.parse(docString); |
| 46 | }); |
| 47 | |
| 48 | /** |
| 49 | * @module ContextData |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 50 | * @description Copy a property from the response data to context Item/VLM/VSP data, example: vsp.componentId |
| 51 | * @step I want to save on the context for {string} property {string} with value {string} |
| 52 | **/ |
| 53 | Then('I want to save on the context for {string} property {string} with value {string}', function(string, string1, string2) { |
| 54 | assert.equal(_.includes(['VLM', 'VSP', 'Item'], string), true); |
| 55 | let val = _.get(this.context.responseData, string2); |
| 56 | _.set(this.context, string1, val); |
| 57 | }); |
| 58 | /** |
| 59 | * @module ContextData |
| 60 | * @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 |
| 61 | * @exampleFile Example_Rest_Calls.feature |
| 62 | * @step I want to save to property {string} from response data path {string} |
| 63 | **/ |
| 64 | Then('I want to copy to property {string} from response data path {string}', function(string, string2) { |
| 65 | let val = _.get(this.context.responseData, string2); |
| 66 | _.set(this.context, string, val); |
| 67 | }); |
| 68 | /** |
| 69 | * @module ContextData |
| 70 | * @description This will set the value of a saved property on the context |
| 71 | * @exampleFile Example_Rest_Calls.feature |
| 72 | * @step I want to set property {string} to value {string} |
| 73 | **/ |
| 74 | Then('I want to set property {string} to value {string}', function(string, string2) { |
| 75 | _.set(this.context, string, string2); |
| 76 | }); |
| 77 | |
| 78 | /** |
| 79 | * @module ResponseData |
| 80 | * @description Will check the output data for a property and a value. property can be a path (example: results[0].id) |
| 81 | * @exampleFile Example_ResponseData_CheckAndManipulation.feature |
| 82 | * @step I want to check property {string} for value {string} |
| 83 | **/ |
| 84 | Then('I want to check property {string} for value {string}', function(string, string2) { |
| 85 | assert.equal(_.get(this.context.responseData, string), string2); |
| 86 | }); |
| 87 | /** |
| 88 | * @module ResponseData |
katy.rotman | 0f75bea | 2018-02-26 11:48:50 +0200 | [diff] [blame] | 89 | * @description Will check the output data for a property and a value. property can be a path |
| 90 | * (example: results[0].id). Supports comparison to a long String by allowing a line break |
| 91 | * @exampleFile VirtualMachineInterfaceValidationHeatResourceMissingProperties.feature |
| 92 | * @step I want to check property {string} for value {string} |
| 93 | **/ |
| 94 | |
| 95 | Then('I want to check property {string} for value:', function(string, docString) { |
| 96 | assert.equal(_.get(this.context.responseData, string), docString.trim()); |
| 97 | }); |
| 98 | /** |
| 99 | * @module ResponseData |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 100 | * @description Will check the output data for a property and a integer. property can be a path (example: results[0].id) |
| 101 | * @exampleFile Example_ResponseData_CheckAndManipulation.feature |
| 102 | * @step I want to check property {string} for value {int} |
| 103 | **/ |
| 104 | Then('I want to check property {string} for value {int}', function(string, int) { |
| 105 | assert.equal(_.get(this.context.responseData, string), int); |
| 106 | }); |
| 107 | /** |
| 108 | * @module ResponseData |
| 109 | * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id) |
| 110 | * @exampleFile Example_ResponseData_CheckAndManipulation.feature |
| 111 | * @step I want to check property {string} to be "True/False" |
| 112 | **/ |
| 113 | Then('I want to check property {string} to be {string}', function(string, string2) { |
| 114 | assert.equal(_.get(this.context.responseData, string), string2.toLowerCase()); |
| 115 | }); |
| 116 | /** |
| 117 | * @module ResponseData |
| 118 | * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id) |
| 119 | * @exampleFile Example_ResponseData_CheckAndManipulation.feature |
| 120 | * @step I want to check property {string} to have length {int} |
| 121 | **/ |
| 122 | Then('I want to check property {string} to have length {int}', function(string, intLength) { |
| 123 | let arrayProp = _.get(this.context.responseData, string); |
| 124 | assert.equal(arrayProp.length, intLength); |
| 125 | }); |
| 126 | /** |
| 127 | * @module ResponseData |
| 128 | * @description Will check the output data for a property and make sure it exists |
| 129 | * @exampleFile Example_ResponseData_CheckAndManipulation.feature |
| 130 | * @step I want to check property {string} exists |
| 131 | **/ |
| 132 | Then('I want to check property {string} exists', function(string) { |
| 133 | assert.equal(_.has(this.context.responseData, string), true); |
| 134 | }); |
| 135 | /** |
| 136 | * @module ResponseData |
| 137 | * @description Will check the output data for a property and make sure it does not exist |
| 138 | * @exampleFile Example_ResponseData_CheckAndManipulation.feature |
| 139 | * @step I want to check property {string} does not exist |
| 140 | **/ |
| 141 | Then('I want to check property {string} does not exist', function(string) { |
| 142 | assert.equal(_.has(this.context.responseData, string), false); |
| 143 | }); |
| 144 | |
| 145 | /** |
| 146 | * @module ContextData |
| 147 | * @description Use during development to see what is on the context |
| 148 | * @exampleFile Example_ResponseData_CheckAndManipulation.feature |
| 149 | * @step I want to print context data |
| 150 | **/ |
| 151 | Then('I want to print the context data', function() { |
| 152 | console.log('------------ context ---------------'); |
| 153 | console.log(JSON.stringify(this.context, null, 2)); |
| 154 | console.log('--------------------------------------'); |
| 155 | }); |
| 156 | /** |
| 157 | * @module ContextData |
| 158 | * @description Set this in order to check that the following Rest call will not have response code 200 |
| 159 | * @exampleFile Example_Rest_Calls.feature |
| 160 | * @step I want the following to fail |
| 161 | **/ |
| 162 | Then('I want the following to fail', function() { |
| 163 | this.context.shouldFail = true; |
| 164 | }); |
| 165 | |
| 166 | /** |
| 167 | * @module ContextData |
| 168 | * @description Set this in order to check that the following Rest call will have the error code on the return data |
| 169 | * @exampleFile Example_VSP.feature |
| 170 | * @step I want the following to fail with error code {string} |
| 171 | **/ |
| 172 | Then('I want the following to fail with error code {string}', function(string) { |
| 173 | this.context.shouldFail = true; |
| 174 | this.context.errorCode = string; |
| 175 | }); |
| 176 | |
ayalaben | aad2781 | 2018-02-18 10:03:22 +0200 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * @module ContextData |
| 180 | * @description Set this in order to check that the following Rest call will have the error message on the return data |
| 181 | * @exampleFile DeleteVLMCertified.feature |
| 182 | * @step I want the following to fail with error message {string} |
| 183 | **/ |
| 184 | Then('I want the following to fail with error message {string}', function(string) { |
| 185 | this.context.shouldFail = true; |
| 186 | this.context.errorMessage = string; |
| 187 | }); |
| 188 | |
ilanap | 637206b | 2018-02-04 17:06:22 +0200 | [diff] [blame] | 189 | /** |
| 190 | * @module ZipData |
| 191 | * @description Use this in order to extract a file from a zip file and to compare it to a local file (string comparison). |
| 192 | * @exampleFile Example_VSP.feature |
| 193 | * @step I want to compare the content of the entry {string} in the zip {string} with file {string} |
| 194 | **/ |
| 195 | Then ('I want to compare the content of the entry {string} in the zip {string} with file {string}', function (string, string2, string3) { |
| 196 | let zipFile = fs.readFileSync(string2, 'binary'); |
| 197 | let zip = new JSZip(zipFile, {base64: false, checkCRC32: true}); |
| 198 | let fileData = zip.files[string]._data; |
| 199 | let compareFileData = fs.readFileSync(string3, {encoding: 'ascii'}); |
| 200 | assert.equal(normalizeNewline(compareFileData), normalizeNewline(fileData)); |
| 201 | }); |
| 202 | |
| 203 | /** |
| 204 | * @module ZipData |
| 205 | * @description Loads the yaml from zip file onto the context responseData as JSON for running checks on the output |
| 206 | * @exampleFile Example_VSP.feature |
| 207 | * @step I want to load the yaml content of the entry {string} in the zip {string} to context |
| 208 | **/ |
| 209 | Then ('I want to load the yaml content of the entry {string} in the zip {string} to context', function (string, string2, callback) { |
| 210 | let zipFile = fs.readFileSync(string2, 'binary'); |
| 211 | let zip = new JSZip(zipFile, {base64: false, checkCRC32: true}); |
| 212 | let fileData = zip.files[string]._data; |
| 213 | let nativeObject = YAML.parse(fileData); |
| 214 | this.context.responseData = nativeObject; |
| 215 | callback(); |
| 216 | }); |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * @module ZipData |
| 221 | * @description Loads the json from zip file onto the context responseData for running check son the output |
| 222 | * @exampleFile Example_VSP.feature |
| 223 | * @step I want to load the json content of the entry {string} in the zip {string} to context |
| 224 | **/ |
| 225 | When('I want to load the json content of the entry {string} in the zip {string} to context', function (string, string2, callback) { |
| 226 | let zipFile = fs.readFileSync(string2, 'binary'); |
| 227 | let zip = new JSZip(zipFile, {base64: false, checkCRC32: true}); |
| 228 | let str = zip.files[string]._data; |
| 229 | this.context.responseData = JSON.parse(str); |
| 230 | callback(); |
| 231 | }); |