docker for cucumber BDD
[sdc.git] / cucumber-js-test-apis-ci / stepDefinitions / Utils.js
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 request = require('request');
17 const fs = require('fs');
18 require('node-zip');
19
20 function _request(context, method, path, data, isBinary=false, type='onboarding') {
21         let server = context.getUrlForType(type);
22
23         let options = {
24                 method: method,
25                 url: server + path,
26                 headers: context.headers[type]
27         };
28         console.log('--> Calling REST ' + options.method +' url: ' + options.url);
29
30         return new Promise(function (resolve, reject) {
31                 if (method === 'POST' || method === 'PUT') {
32                         if (isBinary) {
33                                 var formData = {
34                                         upload: fs.createReadStream(data),
35                                 };
36                                 options.formData = formData;
37                         } else {
38                                 options.json = data;
39                         }
40                 }
41                 request(options, function (err, result, data) {
42                         context.inputData = null;
43                         if (err) {
44                                 console.error('Request URL: ' + result.request.uri.href);
45                                 console.error('Request Method: ' + result.request.method);
46                                 console.error('Response Status Code: ' +result.statusCode);
47                                 console.log(err);
48                                 reject(err);
49                         } else {
50                                 let isExpected = (context.shouldFail) ? (result.statusCode != 200 && result.statusCode != 201) : (result.statusCode == 200 || result.statusCode == 201);
51                                 if (!isExpected) {
52                                         console.error('Request URL: ' + result.request.uri.href);
53                                         console.error('Request Method: ' + result.request.method);
54                                         console.error('Response Status Code: ' +result.statusCode);
55                                         console.error(result.body);
56                                         reject('Status Code was ' + result.statusCode);
57                                 }
58                                 if (context.shouldFail && context.errorCode) {
59                     if (typeof data === 'string' && data) {
60                         data = JSON.parse(data);
61                     }
62                                         let errorCode = data.errorCode;
63                                         let contextErrorCode = context.errorCode;
64                                         context.errorCode = null;
65                                         if (errorCode !== contextErrorCode) {
66                                                 reject('Error Code was ' + errorCode + ' instead of ' + contextErrorCode);
67                                         }
68                                 }
69                                 if (context.shouldFail && context.errorMessage) {
70                                     if (typeof data === 'string' && data) {
71                         data = JSON.parse(data);
72                     }
73                     let errorMessage = data.message;
74                     let contextErrorMessage = context.errorMessage;
75                     context.errorMessage = null;
76                     if (errorMessage !== contextErrorMessage) {
77                         reject('Error Message was ' + errorMessage + ' instead of ' + contextErrorMessage);
78                     }
79                 }
80                                 if (context.shouldFail) {
81                                         context.shouldFail = false;
82                                         resolve({statusCode: result.statusCode, data: {}});
83                                         return;
84                                 }
85                                 if (method === 'GET' && isBinary) {
86                                         // downloading (NetworkPackage) files
87                                         return ({
88                                                 blob: blobUtil.createBlob([data], {type: 'text/plain'}),
89                                                 headers: result.headers
90                                         });
91                                 } else {
92                                         if (typeof data === 'string' && data) {
93                                                 data = JSON.parse(data);
94                                         }
95                                         context.responseData = data;
96                                         context.inputData = data;
97                                         resolve({statusCode: result.statusCode, data: data});
98                                 }
99                         }
100                 });
101         });
102 }
103
104 function download(context, path, filePath,  callback, type='onboarding') {
105         let server = context.getUrlForType(type);
106         let options = {
107                         method: 'GET',
108                         url: server + path,
109                         headers: context.headers[type]
110                 };
111         console.log('--> Calling REST download url: ' + options.url);
112
113         var file = fs.createWriteStream(filePath);
114                 var r = request(options).pipe(file);
115                 r.on('error', function (err) {
116                         console.log(err);
117                         callback(err);
118                 });
119                 r.on('finish', function () {
120                         file.close();
121                         let zipFile = fs.readFileSync(filePath, 'binary');
122                         let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
123                         if (zip.files['MANIFEST.json']) {
124                                 let manifestData = zip.files['MANIFEST.json']._data;
125                                 manifestData = manifestData.replace(/\\n/g, '');
126                                 context.responseData = JSON.parse(manifestData);
127                         }
128                         callback();
129                 });
130
131 };
132
133 function _random() {
134         let d = new Date();
135         return d.getTime().toString().split('').reverse().join('');
136 }
137
138 function _getJSONFromFile(file) {
139         return JSON.parse(fs.readFileSync(file, 'utf8'));
140 }
141
142
143 module.exports = {
144         request: _request,
145         random : _random,
146         getJSONFromFile: _getJSONFromFile,
147         download: download
148 };