blob: 13c03f7848c7b439d272932efd779b707ee0f235 [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');
23const 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 **/
31Given('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 **/
44Given('Response Data:', function (docString) {
45 this.context.responseData = JSON.parse(docString);
46});
47
48/**
49 * @module ContextData
ilanap637206b2018-02-04 17:06:22 +020050 * @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 **/
53Then('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 **/
64Then('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 **/
74Then('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 **/
84Then('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
89 * @description Will check the output data for a property and a integer. property can be a path (example: results[0].id)
90 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
91 * @step I want to check property {string} for value {int}
92 **/
93Then('I want to check property {string} for value {int}', function(string, int) {
94 assert.equal(_.get(this.context.responseData, string), int);
95});
96/**
97 * @module ResponseData
98 * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id)
99 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
100 * @step I want to check property {string} to be "True/False"
101 **/
102Then('I want to check property {string} to be {string}', function(string, string2) {
103 assert.equal(_.get(this.context.responseData, string), string2.toLowerCase());
104});
105/**
106 * @module ResponseData
107 * @description Will check the output data for a property and a boolean. property can be a path (example: results[0].id)
108 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
109 * @step I want to check property {string} to have length {int}
110 **/
111Then('I want to check property {string} to have length {int}', function(string, intLength) {
112 let arrayProp = _.get(this.context.responseData, string);
113 assert.equal(arrayProp.length, intLength);
114});
115/**
116 * @module ResponseData
117 * @description Will check the output data for a property and make sure it exists
118 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
119 * @step I want to check property {string} exists
120 **/
121Then('I want to check property {string} exists', function(string) {
122 assert.equal(_.has(this.context.responseData, string), true);
123});
124/**
125 * @module ResponseData
126 * @description Will check the output data for a property and make sure it does not exist
127 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
128 * @step I want to check property {string} does not exist
129 **/
130Then('I want to check property {string} does not exist', function(string) {
131 assert.equal(_.has(this.context.responseData, string), false);
132});
133
134/**
135* @module ContextData
136* @description Use during development to see what is on the context
137 * @exampleFile Example_ResponseData_CheckAndManipulation.feature
138* @step I want to print context data
139**/
140Then('I want to print the context data', function() {
141 console.log('------------ context ---------------');
142 console.log(JSON.stringify(this.context, null, 2));
143 console.log('--------------------------------------');
144});
145/**
146 * @module ContextData
147 * @description Set this in order to check that the following Rest call will not have response code 200
148 * @exampleFile Example_Rest_Calls.feature
149 * @step I want the following to fail
150 **/
151Then('I want the following to fail', function() {
152 this.context.shouldFail = true;
153});
154
155/**
156 * @module ContextData
157 * @description Set this in order to check that the following Rest call will have the error code on the return data
158 * @exampleFile Example_VSP.feature
159 * @step I want the following to fail with error code {string}
160 **/
161Then('I want the following to fail with error code {string}', function(string) {
162 this.context.shouldFail = true;
163 this.context.errorCode = string;
164});
165
ayalabenaad27812018-02-18 10:03:22 +0200166
167/**
168 * @module ContextData
169 * @description Set this in order to check that the following Rest call will have the error message on the return data
170 * @exampleFile DeleteVLMCertified.feature
171 * @step I want the following to fail with error message {string}
172 **/
173Then('I want the following to fail with error message {string}', function(string) {
174 this.context.shouldFail = true;
175 this.context.errorMessage = string;
176});
177
ilanap637206b2018-02-04 17:06:22 +0200178/**
179 * @module ZipData
180 * @description Use this in order to extract a file from a zip file and to compare it to a local file (string comparison).
181 * @exampleFile Example_VSP.feature
182 * @step I want to compare the content of the entry {string} in the zip {string} with file {string}
183 **/
184Then ('I want to compare the content of the entry {string} in the zip {string} with file {string}', function (string, string2, string3) {
185 let zipFile = fs.readFileSync(string2, 'binary');
186 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
187 let fileData = zip.files[string]._data;
188 let compareFileData = fs.readFileSync(string3, {encoding: 'ascii'});
189 assert.equal(normalizeNewline(compareFileData), normalizeNewline(fileData));
190});
191
192/**
193 * @module ZipData
194 * @description Loads the yaml from zip file onto the context responseData as JSON for running checks on the output
195 * @exampleFile Example_VSP.feature
196 * @step I want to load the yaml content of the entry {string} in the zip {string} to context
197 **/
198Then ('I want to load the yaml content of the entry {string} in the zip {string} to context', function (string, string2, callback) {
199 let zipFile = fs.readFileSync(string2, 'binary');
200 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
201 let fileData = zip.files[string]._data;
202 let nativeObject = YAML.parse(fileData);
203 this.context.responseData = nativeObject;
204 callback();
205});
206
207
208/**
209 * @module ZipData
210 * @description Loads the json from zip file onto the context responseData for running check son the output
211 * @exampleFile Example_VSP.feature
212 * @step I want to load the json content of the entry {string} in the zip {string} to context
213 **/
214When('I want to load the json content of the entry {string} in the zip {string} to context', function (string, string2, callback) {
215 let zipFile = fs.readFileSync(string2, 'binary');
216 let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
217 let str = zip.files[string]._data;
218 this.context.responseData = JSON.parse(str);
219 callback();
220});