Add activity spec code
[sdc/sdc-workflow-designer.git] / workflow-bdd / 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='workflow') {
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.responseStatusCode) {
59                     if (result.statusCode !== context.responseStatusCode) {
60                         reject('Response Status Code was ' + result.statusCode + ' instead of ' + context.responseStatusCode);
61                     }
62                 }
63                                 if (context.shouldFail && context.errorCode) {
64                     if (typeof data === 'string' && data) {
65                         data = JSON.parse(data);
66                     }
67                                         let errorCode = data.errorCode;
68                                         let contextErrorCode = context.errorCode;
69                                         context.errorCode = null;
70                                         if (errorCode !== contextErrorCode) {
71                                                 reject('Error Code was ' + errorCode + ' instead of ' + contextErrorCode);
72                                         }
73                                 }
74                                 if (context.shouldFail && context.errorMessage) {
75                                     if (typeof data === 'string' && data) {
76                         data = JSON.parse(data);
77                     }
78                     let errorMessage = data.message;
79                     let contextErrorMessage = context.errorMessage;
80                     context.errorMessage = null;
81                     if (errorMessage !== contextErrorMessage) {
82                         reject('Error Message was ' + errorMessage + ' instead of ' + contextErrorMessage);
83                     }
84                 }
85                                 if (context.shouldFail) {
86                                         context.shouldFail = false;
87                                         resolve({statusCode: result.statusCode, data: {}});
88                                         return;
89                                 }
90                                 if (method === 'GET' && isBinary) {
91                                         // downloading (NetworkPackage) files
92                                         return ({
93                                                 blob: blobUtil.createBlob([data], {type: 'text/plain'}),
94                                                 headers: result.headers
95                                         });
96                                 } else {
97                                         if (typeof data === 'string' && data) {
98                                                 data = JSON.parse(data);
99                                         }
100                                         context.responseData = data;
101                                         context.inputData = data;
102                                         resolve({statusCode: result.statusCode, data: data});
103                                 }
104                         }
105                 });
106         });
107 }
108
109 function download(context, path, filePath, callback, type='workflow') {
110         let server = context.getUrlForType(type);
111         let options = {
112                         method: 'GET',
113                         url: server + path,
114                         headers: context.headers[type]
115                 };
116         console.log('--> Calling REST download url: ' + options.url);
117
118         var file = fs.createWriteStream(filePath);
119                 var r = request(options).pipe(file);
120                 r.on('error', function (err) {
121                         console.log(err);
122                         callback(err);
123                 });
124                 r.on('finish', function () {
125                         file.close();
126                         let zipFile = fs.readFileSync(filePath, 'binary');
127                         let zip = new JSZip(zipFile, {base64: false, checkCRC32: true});
128                         if (zip.files['MANIFEST.json']) {
129                                 let manifestData = zip.files['MANIFEST.json']._data;
130                                 manifestData = manifestData.replace(/\\n/g, '');
131                                 context.responseData = JSON.parse(manifestData);
132                         }
133                         callback();
134                 });
135
136 };
137
138 function _random() {
139         let d = new Date();
140         return d.getTime().toString().split('').reverse().join('');
141 }
142
143 function _getJSONFromFile(file) {
144         return JSON.parse(fs.readFileSync(file, 'utf8'));
145 }
146
147
148 module.exports = {
149         request: _request,
150         random : _random,
151         getJSONFromFile: _getJSONFromFile,
152         download: download
153 };