@reportportal/client-javascript 5.0.5 → 5.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,126 +5,128 @@ const helpers = require('./helpers');
5
5
  const RestClient = require('./rest');
6
6
  const Analytics = require('../analytics/analytics');
7
7
  const { RP_STATUSES } = require('./constants/statuses');
8
- const { CLIENT_JAVASCRIPT_EVENTS, getAgentEventLabel } = require('./../analytics/events');
8
+ const { CLIENT_JAVASCRIPT_EVENTS, getAgentEventLabel } = require('../analytics/events');
9
9
 
10
10
  const MULTIPART_BOUNDARY = Math.floor(Math.random() * 10000000000).toString();
11
11
 
12
12
  class RPClient {
13
- /**
14
- * Create a client for RP.
15
- * @param {Object} params - config object.
16
- * params should look like this
17
- * {
18
- * token: "00000000-0000-0000-0000-000000000000",
19
- * endpoint: "http://localhost:8080/api/v1",
20
- * launch: "YOUR LAUNCH NAME",
21
- * project: "PROJECT NAME",
22
- * }
23
- *
24
- * @param {Object} agentParams - agent's info object.
25
- * agentParams should look like this
26
- * {
27
- * name: "AGENT NAME",
28
- * version: "AGENT VERSION",
29
- * }
30
- */
31
- constructor(params, agentParams) {
32
- this.debug = params.debug;
33
- this.isLaunchMergeRequired = params.isLaunchMergeRequired === undefined ? false : params.isLaunchMergeRequired;
34
- this.map = {};
35
- this.baseURL = [params.endpoint, params.project].join('/');
36
- const headers = Object.assign({
37
- 'User-Agent': 'NodeJS',
38
- Authorization: `bearer ${params.token}`,
39
- }, params.headers || {});
40
- this.headers = headers;
41
- this.token = params.token;
42
- this.config = params;
43
- this.helpers = helpers;
44
- this.restClient = new RestClient({
45
- baseURL: this.baseURL,
46
- headers: this.headers,
47
- restClientConfig: params.restClientConfig,
48
- });
49
- this.analytics = new Analytics(agentParams);
50
- this.launchUuid = '';
51
- this.nonRetriedItemMap = new Map();
52
- }
53
-
54
- // eslint-disable-next-line valid-jsdoc
55
- /**
13
+ /**
14
+ * Create a client for RP.
15
+ * @param {Object} params - config object.
16
+ * params should look like this
17
+ * {
18
+ * token: "00000000-0000-0000-0000-000000000000",
19
+ * endpoint: "http://localhost:8080/api/v1",
20
+ * launch: "YOUR LAUNCH NAME",
21
+ * project: "PROJECT NAME",
22
+ * }
23
+ *
24
+ * @param {Object} agentParams - agent's info object.
25
+ * agentParams should look like this
26
+ * {
27
+ * name: "AGENT NAME",
28
+ * version: "AGENT VERSION",
29
+ * }
30
+ */
31
+ constructor(params, agentParams) {
32
+ this.debug = params.debug;
33
+ this.isLaunchMergeRequired =
34
+ params.isLaunchMergeRequired === undefined ? false : params.isLaunchMergeRequired;
35
+ this.map = {};
36
+ this.baseURL = [params.endpoint, params.project].join('/');
37
+ const headers = {
38
+ 'User-Agent': 'NodeJS',
39
+ Authorization: `bearer ${params.token}`,
40
+ ...(params.headers || {}),
41
+ };
42
+ this.headers = headers;
43
+ this.token = params.token;
44
+ this.config = params;
45
+ this.helpers = helpers;
46
+ this.restClient = new RestClient({
47
+ baseURL: this.baseURL,
48
+ headers: this.headers,
49
+ restClientConfig: params.restClientConfig,
50
+ });
51
+ this.analytics = new Analytics(agentParams);
52
+ this.launchUuid = '';
53
+ this.nonRetriedItemMap = new Map();
54
+ }
55
+
56
+ // eslint-disable-next-line valid-jsdoc
57
+ /**
56
58
  *
57
59
  * @Private
58
60
  */
59
- logDebug(msg) {
60
- if (this.debug) {
61
- console.log(msg);
62
- }
61
+ logDebug(msg, dataMsg = '') {
62
+ if (this.debug) {
63
+ console.log(msg, dataMsg);
63
64
  }
64
-
65
- calculateNonRetriedItemMapKey(launchId, parentId, name, itemId = '') {
66
- return `${launchId}__${parentId}__${name}__${itemId}`;
67
- }
68
-
69
- getUniqId() {
70
- return UniqId();
71
- }
72
-
73
- getRejectAnswer(tempId, error) {
74
- return {
75
- tempId,
76
- promise: Promise.reject(error),
77
- };
78
- }
79
-
80
- getNewItemObj(startPromiseFunc) {
81
- let resolveFinish;
82
- let rejectFinish;
83
- const obj = {
84
- promiseStart: (new Promise(startPromiseFunc)),
85
- realId: '',
86
- children: [],
87
- finishSend: false,
88
- promiseFinish: (new Promise((resolve, reject) => {
89
- resolveFinish = resolve;
90
- rejectFinish = reject;
91
- })),
92
- };
93
- obj.resolveFinish = resolveFinish;
94
- obj.rejectFinish = rejectFinish;
95
- return obj;
96
- }
97
-
98
- // eslint-disable-next-line valid-jsdoc
99
- /**
65
+ }
66
+
67
+ calculateNonRetriedItemMapKey(launchId, parentId, name, itemId = '') {
68
+ return `${launchId}__${parentId}__${name}__${itemId}`;
69
+ }
70
+
71
+ getUniqId() {
72
+ return UniqId();
73
+ }
74
+
75
+ getRejectAnswer(tempId, error) {
76
+ return {
77
+ tempId,
78
+ promise: Promise.reject(error),
79
+ };
80
+ }
81
+
82
+ getNewItemObj(startPromiseFunc) {
83
+ let resolveFinish;
84
+ let rejectFinish;
85
+ const obj = {
86
+ promiseStart: new Promise(startPromiseFunc),
87
+ realId: '',
88
+ children: [],
89
+ finishSend: false,
90
+ promiseFinish: new Promise((resolve, reject) => {
91
+ resolveFinish = resolve;
92
+ rejectFinish = reject;
93
+ }),
94
+ };
95
+ obj.resolveFinish = resolveFinish;
96
+ obj.rejectFinish = rejectFinish;
97
+ return obj;
98
+ }
99
+
100
+ // eslint-disable-next-line valid-jsdoc
101
+ /**
100
102
  *
101
103
  * @Private
102
104
  */
103
- cleanMap(ids) {
104
- ids.forEach((id) => {
105
- delete this.map[id];
106
- });
105
+ cleanMap(ids) {
106
+ ids.forEach((id) => {
107
+ delete this.map[id];
108
+ });
109
+ }
110
+
111
+ checkConnect() {
112
+ const url = [this.config.endpoint.replace('/v2', '/v1'), 'user'].join('/');
113
+ return RestClient.request('GET', url, {}, { headers: this.headers });
114
+ }
115
+
116
+ triggerAnalyticsEvent() {
117
+ if (process.env.REPORTPORTAL_CLIENT_JS_NO_ANALYTICS) {
118
+ return;
107
119
  }
108
120
 
109
- checkConnect() {
110
- const url = [this.config.endpoint.replace('/v2', '/v1'), 'user'].join('/');
111
- return RestClient.request('GET', url, {}, { headers: this.headers });
121
+ if (this.analytics.agentParams) {
122
+ const label = getAgentEventLabel(this.analytics.agentParams);
123
+ this.analytics.trackEvent(Object.assign(CLIENT_JAVASCRIPT_EVENTS.START_LAUNCH, { label }));
124
+ } else {
125
+ this.analytics.trackEvent(CLIENT_JAVASCRIPT_EVENTS.START_LAUNCH);
112
126
  }
127
+ }
113
128
 
114
- triggerAnalyticsEvent() {
115
- if (process.env.REPORTPORTAL_CLIENT_JS_NO_ANALYTICS) {
116
- return;
117
- }
118
-
119
- if (this.analytics.agentParams) {
120
- const label = getAgentEventLabel(this.analytics.agentParams);
121
- this.analytics.trackEvent(Object.assign(CLIENT_JAVASCRIPT_EVENTS.START_LAUNCH, { label }));
122
- } else {
123
- this.analytics.trackEvent(CLIENT_JAVASCRIPT_EVENTS.START_LAUNCH);
124
- }
125
- }
126
-
127
- /**
129
+ /**
128
130
  * Start launch and report it.
129
131
  * @param {Object} launchDataRQ - request object.
130
132
  * launchDataRQ should look like this
@@ -163,167 +165,191 @@ class RPClient {
163
165
  * or it would throw an error.
164
166
  * @Returns {Object} - an object which contains a tempID and a promise
165
167
  */
166
- startLaunch(launchDataRQ) {
167
- const tempId = this.getUniqId();
168
-
169
- if (launchDataRQ.id) {
170
- this.map[tempId] = this.getNewItemObj(resolve => resolve(launchDataRQ));
171
- this.map[tempId].realId = launchDataRQ.id;
172
- this.launchUuid = launchDataRQ.id;
173
- } else {
174
- const systemAttr = helpers.getSystemAttribute();
175
- const attributes = Array.isArray(launchDataRQ.attributes)
176
- ? launchDataRQ.attributes.concat(systemAttr)
177
- : systemAttr;
178
- const launchData = Object.assign(
179
- { name: this.config.launch || 'Test launch name', startTime: this.helpers.now() },
180
- launchDataRQ,
181
- { attributes },
182
- );
183
-
184
- this.map[tempId] = this.getNewItemObj((resolve, reject) => {
185
- const url = 'launch';
186
- this.logDebug(`Start launch ${tempId}`);
187
- this.restClient.create(url, launchData, { headers: this.headers })
188
- .then((response) => {
189
- this.map[tempId].realId = response.id;
190
- this.launchUuid = response.id;
191
-
192
- if (this.isLaunchMergeRequired) {
193
- helpers.saveLaunchIdToFile(response.id);
194
- }
195
-
196
- resolve(response);
197
- }, (error) => {
198
- console.dir(error);
199
- reject(error);
200
- });
201
- });
202
- }
203
- this.triggerAnalyticsEvent();
204
- return {
205
- tempId,
206
- promise: this.map[tempId].promiseStart,
207
- };
208
- }
209
-
210
- /**
211
- * Finish launch.
212
- * @param {string} launchTempId - temp launch id (returned in the query "startLaunch").
213
- * @param {Object} finishExecutionRQ - finish launch info should include time and status.
214
- * finishExecutionRQ should look like this
215
- * {
216
- * "endTime": this.helper.now(),
217
- * "status": "passed" or one of ‘passed’, ‘failed’, ‘stopped’, ‘skipped’, ‘interrupted’, ‘cancelled’
218
- * }
219
- * @Returns {Object} - an object which contains a tempID and a promise
220
- */
221
- finishLaunch(launchTempId, finishExecutionRQ) {
222
- const launchObj = this.map[launchTempId];
223
- if (!launchObj) {
224
- return this.getRejectAnswer(launchTempId, new Error(`Launch "${launchTempId}" not found`));
225
- }
226
-
227
- const finishExecutionData = Object.assign({ endTime: this.helpers.now() }, finishExecutionRQ);
228
-
229
- launchObj.finishSend = true;
230
- Promise.all(launchObj.children.map(itemId => this.map[itemId].promiseFinish))
231
- .then(() => {
232
- launchObj.promiseStart
233
- .then(() => {
234
- this.logDebug(`Finish launch ${launchTempId}`);
235
- const url = ['launch', launchObj.realId, 'finish'].join('/');
236
- this.restClient.update(url, finishExecutionData, { headers: this.headers })
237
- .then((response) => {
238
- this.logDebug(`Success finish launch ${launchTempId}`);
239
- launchObj.resolveFinish(response);
240
- }, (error) => {
241
- this.logDebug(`Error finish launch ${launchTempId}`);
242
- console.dir(error);
243
- launchObj.rejectFinish(error);
244
- });
245
- }, (error) => {
246
- launchObj.rejectFinish(error);
247
- });
248
- }, (error) => {
249
- launchObj.rejectFinish(error);
250
- });
251
-
252
- return {
253
- tempId: launchTempId,
254
- promise: launchObj.promiseFinish,
255
- };
256
- }
168
+ startLaunch(launchDataRQ) {
169
+ const tempId = this.getUniqId();
170
+
171
+ if (launchDataRQ.id) {
172
+ this.map[tempId] = this.getNewItemObj((resolve) => resolve(launchDataRQ));
173
+ this.map[tempId].realId = launchDataRQ.id;
174
+ this.launchUuid = launchDataRQ.id;
175
+ } else {
176
+ const systemAttr = helpers.getSystemAttribute();
177
+ const attributes = Array.isArray(launchDataRQ.attributes)
178
+ ? launchDataRQ.attributes.concat(systemAttr)
179
+ : systemAttr;
180
+ const launchData = {
181
+ name: this.config.launch || 'Test launch name',
182
+ startTime: this.helpers.now(),
183
+ ...launchDataRQ,
184
+ attributes,
185
+ };
186
+
187
+ this.map[tempId] = this.getNewItemObj((resolve, reject) => {
188
+ const url = 'launch';
189
+ this.logDebug(`Start launch with tempId ${tempId}`, launchDataRQ);
190
+ this.restClient.create(url, launchData, { headers: this.headers }).then(
191
+ (response) => {
192
+ this.map[tempId].realId = response.id;
193
+ this.launchUuid = response.id;
194
+
195
+ if (this.isLaunchMergeRequired) {
196
+ helpers.saveLaunchIdToFile(response.id);
197
+ }
257
198
 
258
- /*
259
- * This method is used to create data object for merge request to ReportPortal.
260
- *
261
- * @Returns {Object} - an object which contains a data for merge launches in ReportPortal.
262
- */
263
- getMergeLaunchesRequest(launchIds) {
264
- return {
265
- description: "Merged launch",
266
- endTime: this.helpers.now(),
267
- extendSuitesDescription: true,
268
- launches: launchIds,
269
- mergeType: "BASIC",
270
- mode: "DEFAULT",
271
- name: this.config.launch || 'Test launch name',
272
- attributes: this.config.attributes,
273
- };
199
+ this.logDebug(`Success start launch with tempId ${tempId}`, response);
200
+ resolve(response);
201
+ },
202
+ (error) => {
203
+ this.logDebug(`Error start launch with tempId ${tempId}`, error);
204
+ console.dir(error);
205
+ reject(error);
206
+ },
207
+ );
208
+ });
274
209
  }
275
-
276
- /*
277
- * This method is used for merge launches in ReportPortal.
278
- *
279
- * Please, keep in mind that this method is work only in case
280
- * the option isLaunchMergeRequired is true.
281
- */
282
- mergeLaunches() {
283
- if (this.isLaunchMergeRequired) {
284
- const launchUUIds = helpers.readLaunchesFromFile();
285
- const params = new URLSearchParams({
286
- 'filter.in.uuid': launchUUIds,
287
- });
288
- const launchSearchUrl = `launch?${params.toString()}`;
289
- return this.restClient.retrieveSyncAPI(launchSearchUrl, { headers: this.headers })
290
- .then((response) => {
291
- const launchIds = response.content.map(launch => launch.id);
292
- this.logDebug(`Found launches: ${launchIds}`);
293
- return launchIds;
294
- }, (error) => {
295
- console.dir(error);
296
- })
297
- .then((launchIds) => {
298
- const request = this.getMergeLaunchesRequest(launchIds);
299
- const mergeURL = 'launch/merge';
300
- return this.restClient.create(mergeURL, request, { headers: this.headers });
301
- })
302
- .then(() => {
303
- this.logDebug(`Launches successfully merged!`);
304
- })
305
- .catch((error) => {
306
- this.logDebug('ERROR');
307
- this.logDebug(error);
308
- });
309
- }
310
- this.logDebug('Option isLaunchMergeRequired is false');
210
+ this.triggerAnalyticsEvent();
211
+ return {
212
+ tempId,
213
+ promise: this.map[tempId].promiseStart,
214
+ };
215
+ }
216
+
217
+ /**
218
+ * Finish launch.
219
+ * @param {string} launchTempId - temp launch id (returned in the query "startLaunch").
220
+ * @param {Object} finishExecutionRQ - finish launch info should include time and status.
221
+ * finishExecutionRQ should look like this
222
+ * {
223
+ * "endTime": this.helper.now(),
224
+ * "status": "passed" or one of ‘passed’, ‘failed’, ‘stopped’, ‘skipped’, ‘interrupted’, ‘cancelled’
225
+ * }
226
+ * @Returns {Object} - an object which contains a tempID and a promise
227
+ */
228
+ finishLaunch(launchTempId, finishExecutionRQ) {
229
+ const launchObj = this.map[launchTempId];
230
+ if (!launchObj) {
231
+ return this.getRejectAnswer(
232
+ launchTempId,
233
+ new Error(`Launch with tempId "${launchTempId}" not found`),
234
+ );
311
235
  }
312
236
 
313
- /*
314
- * This method is used for frameworks as Jasmine. There is problem when
315
- * it doesn't wait for promise resolve and stop the process. So it better to call
316
- * this method at the spec's function as @afterAll() and manually resolve this promise.
317
- *
318
- * @return Promise
319
- */
320
- getPromiseFinishAllItems(launchTempId) {
321
- const launchObj = this.map[launchTempId];
322
- return Promise.all(launchObj.children.map(itemId => this.map[itemId].promiseFinish));
237
+ const finishExecutionData = { endTime: this.helpers.now(), ...finishExecutionRQ };
238
+
239
+ launchObj.finishSend = true;
240
+ Promise.all(launchObj.children.map((itemId) => this.map[itemId].promiseFinish)).then(
241
+ () => {
242
+ launchObj.promiseStart.then(
243
+ () => {
244
+ this.logDebug(`Finish launch with tempId ${launchTempId}`, finishExecutionData);
245
+ const url = ['launch', launchObj.realId, 'finish'].join('/');
246
+ this.restClient.update(url, finishExecutionData, { headers: this.headers }).then(
247
+ (response) => {
248
+ this.logDebug(`Success finish launch with tempId ${launchTempId}`, response);
249
+ launchObj.resolveFinish(response);
250
+ },
251
+ (error) => {
252
+ this.logDebug(`Error finish launch with tempId ${launchTempId}`, error);
253
+ console.dir(error);
254
+ launchObj.rejectFinish(error);
255
+ },
256
+ );
257
+ },
258
+ (error) => {
259
+ console.dir(error);
260
+ launchObj.rejectFinish(error);
261
+ },
262
+ );
263
+ },
264
+ (error) => {
265
+ console.dir(error);
266
+ launchObj.rejectFinish(error);
267
+ },
268
+ );
269
+
270
+ return {
271
+ tempId: launchTempId,
272
+ promise: launchObj.promiseFinish,
273
+ };
274
+ }
275
+
276
+ /*
277
+ * This method is used to create data object for merge request to ReportPortal.
278
+ *
279
+ * @Returns {Object} - an object which contains a data for merge launches in ReportPortal.
280
+ */
281
+ getMergeLaunchesRequest(launchIds) {
282
+ return {
283
+ description: 'Merged launch',
284
+ endTime: this.helpers.now(),
285
+ extendSuitesDescription: true,
286
+ launches: launchIds,
287
+ mergeType: 'BASIC',
288
+ mode: 'DEFAULT',
289
+ name: this.config.launch || 'Test launch name',
290
+ attributes: this.config.attributes,
291
+ };
292
+ }
293
+
294
+ /*
295
+ * This method is used for merge launches in ReportPortal.
296
+ *
297
+ * Please, keep in mind that this method is work only in case
298
+ * the option isLaunchMergeRequired is true.
299
+ */
300
+ mergeLaunches() {
301
+ if (this.isLaunchMergeRequired) {
302
+ const launchUUIds = helpers.readLaunchesFromFile();
303
+ const params = new URLSearchParams({
304
+ 'filter.in.uuid': launchUUIds,
305
+ });
306
+ const launchSearchUrl = `launch?${params.toString()}`;
307
+ this.logDebug(`Find launches with UUIDs to merge: ${launchUUIds}`);
308
+ return this.restClient
309
+ .retrieveSyncAPI(launchSearchUrl, { headers: this.headers })
310
+ .then(
311
+ (response) => {
312
+ const launchIds = response.content.map((launch) => launch.id);
313
+ this.logDebug(`Found launches: ${launchIds}`, response.content);
314
+ return launchIds;
315
+ },
316
+ (error) => {
317
+ this.logDebug(`Error during launches search with UUIDs: ${launchUUIds}`, error);
318
+ console.dir(error);
319
+ },
320
+ )
321
+ .then((launchIds) => {
322
+ const request = this.getMergeLaunchesRequest(launchIds);
323
+ this.logDebug(`Merge launches with ids: ${launchIds}`, request);
324
+ const mergeURL = 'launch/merge';
325
+ return this.restClient.create(mergeURL, request, { headers: this.headers });
326
+ })
327
+ .then(() => {
328
+ this.logDebug(`Launches with UUIDs: ${launchUUIds} were successfully merged!`);
329
+ })
330
+ .catch((error) => {
331
+ this.logDebug(`Error merging launches with UUIDs: ${launchUUIds}`, error);
332
+ console.dir(error);
333
+ });
323
334
  }
335
+ this.logDebug(
336
+ 'Option isLaunchMergeRequired is false, merge process cannot be done as no launch UUIDs where saved.',
337
+ );
338
+ }
339
+
340
+ /*
341
+ * This method is used for frameworks as Jasmine. There is problem when
342
+ * it doesn't wait for promise resolve and stop the process. So it better to call
343
+ * this method at the spec's function as @afterAll() and manually resolve this promise.
344
+ *
345
+ * @return Promise
346
+ */
347
+ getPromiseFinishAllItems(launchTempId) {
348
+ const launchObj = this.map[launchTempId];
349
+ return Promise.all(launchObj.children.map((itemId) => this.map[itemId].promiseFinish));
350
+ }
324
351
 
325
-
326
- /**
352
+ /**
327
353
  * Update launch.
328
354
  * @param {string} launchTempId - temp launch id (returned in the query "startLaunch").
329
355
  * @param {Object} launchData - new launch data
@@ -343,37 +369,48 @@ class RPClient {
343
369
  }
344
370
  * @Returns {Object} - an object which contains a tempId and a promise
345
371
  */
346
- updateLaunch(launchTempId, launchData) {
347
- const launchObj = this.map[launchTempId];
348
- if (!launchObj) {
349
- return this.getRejectAnswer(launchTempId, new Error(`Launch "${launchTempId}" not found`));
350
- }
351
- let resolvePromise;
352
- let rejectPromise;
353
- const promise = new Promise((resolve, reject) => {
354
- resolvePromise = resolve;
355
- rejectPromise = reject;
356
- });
357
-
358
- launchObj.promiseFinish.then(() => {
359
- const url = ['launch', launchObj.realId, 'update'].join('/');
360
- this.restClient.update(url, launchData, { headers: this.headers })
361
- .then((response) => {
362
- resolvePromise(response);
363
- }, (error) => {
364
- rejectPromise(error);
365
- });
366
- }, (error) => {
367
- rejectPromise(error);
368
- });
369
- return {
370
- tempId: launchTempId,
371
- promise,
372
- };
372
+ updateLaunch(launchTempId, launchData) {
373
+ const launchObj = this.map[launchTempId];
374
+ if (!launchObj) {
375
+ return this.getRejectAnswer(
376
+ launchTempId,
377
+ new Error(`Launch with tempId "${launchTempId}" not found`),
378
+ );
373
379
  }
374
-
375
-
376
- /**
380
+ let resolvePromise;
381
+ let rejectPromise;
382
+ const promise = new Promise((resolve, reject) => {
383
+ resolvePromise = resolve;
384
+ rejectPromise = reject;
385
+ });
386
+
387
+ launchObj.promiseFinish.then(
388
+ () => {
389
+ const url = ['launch', launchObj.realId, 'update'].join('/');
390
+ this.logDebug(`Update launch with tempId ${launchTempId}`, launchData);
391
+ this.restClient.update(url, launchData, { headers: this.headers }).then(
392
+ (response) => {
393
+ this.logDebug(`Launch with tempId ${launchTempId} were successfully updated`, response);
394
+ resolvePromise(response);
395
+ },
396
+ (error) => {
397
+ this.logDebug(`Error when updating launch with tempId ${launchTempId}`, error);
398
+ console.dir(error);
399
+ rejectPromise(error);
400
+ },
401
+ );
402
+ },
403
+ (error) => {
404
+ rejectPromise(error);
405
+ },
406
+ );
407
+ return {
408
+ tempId: launchTempId,
409
+ promise,
410
+ };
411
+ }
412
+
413
+ /**
377
414
  * If there is no parentItemId starts Suite, else starts test or item.
378
415
  * @param {Object} testItemDataRQ - object with item parameters
379
416
  * testItemDataRQ should look like this
@@ -400,83 +437,100 @@ class RPClient {
400
437
  * @param {string} parentTempId (optional) - temp item id (returned in the query "startTestItem").
401
438
  * @Returns {Object} - an object which contains a tempId and a promise
402
439
  */
403
- startTestItem(testItemDataRQ, launchTempId, parentTempId) {
404
- let parentMapId = launchTempId;
405
- const launchObj = this.map[launchTempId];
406
- if (!launchObj) {
407
- return this.getRejectAnswer(launchTempId, new Error(`Launch "${launchTempId}" not found`));
408
- }
409
- if (launchObj.finishSend) {
410
- const err = new Error(`Launch "${launchTempId}" is already finished, you can not add an item to it`);
411
- return this.getRejectAnswer(launchTempId, err);
412
- }
413
-
414
- const testCaseId = testItemDataRQ.testCaseId
415
- || helpers.generateTestCaseId(testItemDataRQ.codeRef, testItemDataRQ.parameters);
416
- const testItemData = Object.assign({
417
- startTime: this.helpers.now(),
418
- }, testItemDataRQ, testCaseId && { testCaseId });
419
-
420
- let parentPromise = launchObj.promiseStart;
421
- if (parentTempId) {
422
- parentMapId = parentTempId;
423
- const parentObj = this.map[parentTempId];
424
- if (!parentObj) {
425
- return this.getRejectAnswer(launchTempId, new Error(`Item "${parentTempId}" not found`));
426
- }
427
- parentPromise = parentObj.promiseStart;
428
- }
440
+ startTestItem(testItemDataRQ, launchTempId, parentTempId) {
441
+ let parentMapId = launchTempId;
442
+ const launchObj = this.map[launchTempId];
443
+ if (!launchObj) {
444
+ return this.getRejectAnswer(
445
+ launchTempId,
446
+ new Error(`Launch with tempId "${launchTempId}" not found`),
447
+ );
448
+ }
449
+ // TODO: Allow items reporting to finished launch
450
+ if (launchObj.finishSend) {
451
+ const err = new Error(
452
+ `Launch with tempId "${launchTempId}" is already finished, you can not add an item to it`,
453
+ );
454
+ return this.getRejectAnswer(launchTempId, err);
455
+ }
429
456
 
430
- const itemKey = this.calculateNonRetriedItemMapKey(
431
- launchTempId,
432
- parentTempId,
433
- testItemDataRQ.name,
434
- testItemDataRQ.uniqueId,
457
+ const testCaseId =
458
+ testItemDataRQ.testCaseId ||
459
+ helpers.generateTestCaseId(testItemDataRQ.codeRef, testItemDataRQ.parameters);
460
+ const testItemData = {
461
+ startTime: this.helpers.now(),
462
+ ...testItemDataRQ,
463
+ ...(testCaseId && { testCaseId }),
464
+ };
465
+
466
+ let parentPromise = launchObj.promiseStart;
467
+ if (parentTempId) {
468
+ parentMapId = parentTempId;
469
+ const parentObj = this.map[parentTempId];
470
+ if (!parentObj) {
471
+ return this.getRejectAnswer(
472
+ launchTempId,
473
+ new Error(`Item with tempId "${parentTempId}" not found`),
435
474
  );
436
- const firstNonRetriedItemPromise = testItemDataRQ.retry && this.nonRetriedItemMap.get(itemKey);
437
- if (firstNonRetriedItemPromise) {
438
- parentPromise = Promise.all([parentPromise, firstNonRetriedItemPromise]);
439
- }
475
+ }
476
+ parentPromise = parentObj.promiseStart;
477
+ }
440
478
 
441
- const tempId = this.getUniqId();
442
- this.map[tempId] = this.getNewItemObj((resolve, reject) => {
443
- parentPromise.then(() => {
444
- const realLaunchId = this.map[launchTempId].realId;
445
- let url = 'item/';
446
- if (parentTempId) {
447
- const realParentId = this.map[parentTempId].realId;
448
- url += `${realParentId}`;
449
- }
450
- testItemData.launchUuid = realLaunchId;
451
- this.logDebug(`Start test item ${tempId}`);
452
- this.restClient.create(url, testItemData, { headers: this.headers })
453
- .then((response) => {
454
- this.logDebug(`Success start item ${tempId}`);
455
- this.map[tempId].realId = response.id;
456
- this.nonRetriedItemMap.delete(itemKey);
457
- resolve(response);
458
- }, (error) => {
459
- this.logDebug(`Error start item ${tempId}:`);
460
- console.dir(error);
461
- reject(error);
462
- });
463
- }, (error) => {
464
- reject(error);
465
- });
466
- });
467
- this.map[parentMapId].children.push(tempId);
479
+ const itemKey = this.calculateNonRetriedItemMapKey(
480
+ launchTempId,
481
+ parentTempId,
482
+ testItemDataRQ.name,
483
+ testItemDataRQ.uniqueId,
484
+ );
485
+ const firstNonRetriedItemPromise = testItemDataRQ.retry && this.nonRetriedItemMap.get(itemKey);
486
+ if (firstNonRetriedItemPromise) {
487
+ parentPromise = Promise.all([parentPromise, firstNonRetriedItemPromise]);
488
+ }
468
489
 
469
- if (!testItemDataRQ.retry) {
470
- this.nonRetriedItemMap.set(itemKey, this.map[tempId].promiseStart);
471
- }
490
+ const tempId = this.getUniqId();
491
+ this.map[tempId] = this.getNewItemObj((resolve, reject) => {
492
+ parentPromise.then(
493
+ () => {
494
+ const realLaunchId = this.map[launchTempId].realId;
495
+ let url = 'item/';
496
+ if (parentTempId) {
497
+ const realParentId = this.map[parentTempId].realId;
498
+ url += `${realParentId}`;
499
+ }
500
+ testItemData.launchUuid = realLaunchId;
501
+ this.logDebug(`Start test item with tempId ${tempId}`, testItemData);
502
+ this.restClient.create(url, testItemData, { headers: this.headers }).then(
503
+ (response) => {
504
+ this.logDebug(`Success start item with tempId ${tempId}`, response);
505
+ this.map[tempId].realId = response.id;
506
+ this.nonRetriedItemMap.delete(itemKey);
507
+ resolve(response);
508
+ },
509
+ (error) => {
510
+ this.logDebug(`Error start item with tempId ${tempId}`, error);
511
+ console.dir(error);
512
+ reject(error);
513
+ },
514
+ );
515
+ },
516
+ (error) => {
517
+ reject(error);
518
+ },
519
+ );
520
+ });
521
+ this.map[parentMapId].children.push(tempId);
472
522
 
473
- return {
474
- tempId,
475
- promise: this.map[tempId].promiseStart,
476
- };
523
+ if (!testItemDataRQ.retry) {
524
+ this.nonRetriedItemMap.set(itemKey, this.map[tempId].promiseStart);
477
525
  }
478
526
 
479
- /**
527
+ return {
528
+ tempId,
529
+ promise: this.map[tempId].promiseStart,
530
+ };
531
+ }
532
+
533
+ /**
480
534
  * Finish Suite or Step level.
481
535
  * @param {string} itemTempId - temp item id (returned in the query "startTestItem").
482
536
  * @param {Object} finishTestItemRQ - object with item parameters.
@@ -500,112 +554,142 @@ class RPClient {
500
554
  }
501
555
  * @Returns {Object} - an object which contains a tempId and a promise
502
556
  */
503
- finishTestItem(itemTempId, finishTestItemRQ) {
504
- const itemObj = this.map[itemTempId];
505
- if (!itemObj) {
506
- return this.getRejectAnswer(itemTempId, new Error(`Item "${itemTempId}" not found`));
507
- }
508
-
509
- const finishTestItemData = Object.assign({
510
- endTime: this.helpers.now(),
511
- }, itemObj.children.length ? {} : { status: RP_STATUSES.PASSED }, finishTestItemRQ);
512
-
513
- itemObj.finishSend = true;
514
- Promise.all(itemObj.children.map(itemId => this.map[itemId] && this.map[itemId].promiseFinish))
515
- .then(() => {
516
- this.cleanMap(itemObj.children);
517
- this.finishTestItemPromiseStart(itemObj,
518
- itemTempId,
519
- Object.assign(finishTestItemData, { launchUuid: this.launchUuid }));
520
- }, () => {
521
- this.cleanMap(itemObj.children);
522
- this.logDebug(`Error finish children of test item ${itemTempId}`);
523
- this.logDebug(`Finish test item ${itemTempId}`);
524
- this.finishTestItemPromiseStart(itemObj,
525
- itemTempId,
526
- Object.assign(finishTestItemData, { launchUuid: this.launchUuid }));
527
- });
528
-
529
- return {
530
- tempId: itemTempId,
531
- promise: itemObj.promiseFinish,
532
- };
557
+ finishTestItem(itemTempId, finishTestItemRQ) {
558
+ const itemObj = this.map[itemTempId];
559
+ if (!itemObj) {
560
+ return this.getRejectAnswer(
561
+ itemTempId,
562
+ new Error(`Item with tempId "${itemTempId}" not found`),
563
+ );
533
564
  }
534
565
 
535
- saveLog(itemObj, requestPromiseFunc) {
536
- const tempId = this.getUniqId();
537
- this.map[tempId] = this.getNewItemObj((resolve, reject) => {
538
- itemObj.promiseStart.then(() => {
539
- this.logDebug(`Save log ${tempId}`);
540
- requestPromiseFunc(itemObj.realId, this.launchUuid).then((response) => {
541
- this.logDebug(`Successfully save log ${tempId}`);
542
- resolve(response);
543
- }, (error) => {
544
- this.logDebug(`Error finish log: ${error}`);
545
- reject(error);
546
- });
547
- }, (error) => {
548
- reject(error);
549
- });
550
- });
551
- itemObj.children.push(tempId);
552
-
553
- const logObj = this.map[tempId];
554
- logObj.finishSend = true;
555
- logObj.promiseStart.then(response => logObj.resolveFinish(response), error => logObj.rejectFinish(error));
556
-
557
- return {
558
- tempId,
559
- promise: this.map[tempId].promiseFinish,
560
- };
561
- }
562
-
563
- sendLog(itemTempId, saveLogRQ, fileObj) {
564
- const saveLogData = Object.assign({
565
- time: this.helpers.now(),
566
- message: '',
567
- level: '',
568
- }, saveLogRQ);
569
-
570
- if (fileObj) {
571
- return this.sendLogWithFile(itemTempId, saveLogData, fileObj);
572
- }
573
- return this.sendLogWithoutFile(itemTempId, saveLogData);
566
+ const finishTestItemData = {
567
+ endTime: this.helpers.now(),
568
+ ...(itemObj.children.length ? {} : { status: RP_STATUSES.PASSED }),
569
+ ...finishTestItemRQ,
570
+ };
571
+
572
+ itemObj.finishSend = true;
573
+ this.logDebug(`Finish all children for test item with tempId ${itemTempId}`);
574
+ Promise.all(
575
+ itemObj.children.map((itemId) => this.map[itemId] && this.map[itemId].promiseFinish),
576
+ ).then(
577
+ () => {
578
+ this.cleanMap(itemObj.children);
579
+ this.logDebug(
580
+ `All children for test item with tempId ${itemTempId} successfully finished.`,
581
+ );
582
+ this.logDebug(`Finish test item with tempId ${itemTempId}`, finishTestItemRQ);
583
+ this.finishTestItemPromiseStart(
584
+ itemObj,
585
+ itemTempId,
586
+ Object.assign(finishTestItemData, { launchUuid: this.launchUuid }),
587
+ );
588
+ },
589
+ () => {
590
+ this.cleanMap(itemObj.children);
591
+ this.logDebug(`Error finish children of test item with tempId ${itemTempId}`);
592
+ this.logDebug(`Finish test item with tempId ${itemTempId}`, finishTestItemRQ);
593
+ this.finishTestItemPromiseStart(
594
+ itemObj,
595
+ itemTempId,
596
+ Object.assign(finishTestItemData, { launchUuid: this.launchUuid }),
597
+ );
598
+ },
599
+ );
600
+
601
+ return {
602
+ tempId: itemTempId,
603
+ promise: itemObj.promiseFinish,
604
+ };
605
+ }
606
+
607
+ saveLog(itemObj, requestPromiseFunc) {
608
+ const tempId = this.getUniqId();
609
+ this.map[tempId] = this.getNewItemObj((resolve, reject) => {
610
+ itemObj.promiseStart.then(
611
+ () => {
612
+ this.logDebug(`Save log with tempId ${tempId}`, itemObj);
613
+ requestPromiseFunc(itemObj.realId, this.launchUuid).then(
614
+ (response) => {
615
+ this.logDebug(`Successfully save log with tempId ${tempId}`, response);
616
+ resolve(response);
617
+ },
618
+ (error) => {
619
+ this.logDebug(`Error save log with tempId ${tempId}`, error);
620
+ console.dir(error);
621
+ reject(error);
622
+ },
623
+ );
624
+ },
625
+ (error) => {
626
+ reject(error);
627
+ },
628
+ );
629
+ });
630
+ itemObj.children.push(tempId);
631
+
632
+ const logObj = this.map[tempId];
633
+ logObj.finishSend = true;
634
+ logObj.promiseStart.then(
635
+ (response) => logObj.resolveFinish(response),
636
+ (error) => logObj.rejectFinish(error),
637
+ );
638
+
639
+ return {
640
+ tempId,
641
+ promise: this.map[tempId].promiseFinish,
642
+ };
643
+ }
644
+
645
+ sendLog(itemTempId, saveLogRQ, fileObj) {
646
+ const saveLogData = {
647
+ time: this.helpers.now(),
648
+ message: '',
649
+ level: '',
650
+ ...saveLogRQ,
651
+ };
652
+
653
+ if (fileObj) {
654
+ return this.sendLogWithFile(itemTempId, saveLogData, fileObj);
574
655
  }
575
-
576
- /**
577
- * Send log of test results.
578
- * @param {string} itemTempId - temp item id (returned in the query "startTestItem").
579
- * @param {Object} saveLogRQ - object with data of test result.
580
- * saveLogRQ should look like this
581
- * {
582
- * level: 'error' or one of 'trace', 'debug', 'info', 'warn', 'error', '',
583
- * message: 'string' (support markdown),
584
- * time: this.helpers.now()
585
- * }
586
- * @Returns {Object} - an object which contains a tempId and a promise
587
- */
588
- sendLogWithoutFile(itemTempId, saveLogRQ) {
589
- const itemObj = this.map[itemTempId];
590
- if (!itemObj) {
591
- return this.getRejectAnswer(itemTempId, new Error(`Item "${itemTempId}" not found`));
592
- }
593
-
594
- const requestPromise = (itemUuid, launchUuid) => {
595
- const url = 'log';
596
- const isItemUuid = itemUuid !== launchUuid;
597
- return this.restClient.create(
598
- url,
599
- Object.assign(saveLogRQ,
600
- { launchUuid },
601
- isItemUuid && { itemUuid }),
602
- { headers: this.headers },
603
- );
604
- };
605
- return this.saveLog(itemObj, requestPromise);
656
+ return this.sendLogWithoutFile(itemTempId, saveLogData);
657
+ }
658
+
659
+ /**
660
+ * Send log of test results.
661
+ * @param {string} itemTempId - temp item id (returned in the query "startTestItem").
662
+ * @param {Object} saveLogRQ - object with data of test result.
663
+ * saveLogRQ should look like this
664
+ * {
665
+ * level: 'error' or one of 'trace', 'debug', 'info', 'warn', 'error', '',
666
+ * message: 'string' (support markdown),
667
+ * time: this.helpers.now()
668
+ * }
669
+ * @Returns {Object} - an object which contains a tempId and a promise
670
+ */
671
+ sendLogWithoutFile(itemTempId, saveLogRQ) {
672
+ const itemObj = this.map[itemTempId];
673
+ if (!itemObj) {
674
+ return this.getRejectAnswer(
675
+ itemTempId,
676
+ new Error(`Item with tempId "${itemTempId}" not found`),
677
+ );
606
678
  }
607
679
 
608
- /**
680
+ const requestPromise = (itemUuid, launchUuid) => {
681
+ const url = 'log';
682
+ const isItemUuid = itemUuid !== launchUuid;
683
+ return this.restClient.create(
684
+ url,
685
+ Object.assign(saveLogRQ, { launchUuid }, isItemUuid && { itemUuid }),
686
+ { headers: this.headers },
687
+ );
688
+ };
689
+ return this.saveLog(itemObj, requestPromise);
690
+ }
691
+
692
+ /**
609
693
  * Send log of test results with file.
610
694
  * @param {string} itemTempId - temp item id (returned in the query "startTestItem").
611
695
  * @param {Object} saveLogRQ - object with data of test result.
@@ -626,89 +710,118 @@ class RPClient {
626
710
  * }
627
711
  * @Returns {Object} - an object which contains a tempId and a promise
628
712
  */
629
- sendLogWithFile(itemTempId, saveLogRQ, fileObj) {
630
- const itemObj = this.map[itemTempId];
631
- if (!itemObj) {
632
- return this.getRejectAnswer(itemTempId, new Error(`Item "${itemTempId}" not found`));
633
- }
634
- // eslint-disable-next-line max-len
635
- const requestPromise = (itemUuid, launchUuid) => {
636
- const isItemUuid = itemUuid !== launchUuid;
637
-
638
- // eslint-disable-next-line max-len
639
- return this.getRequestLogWithFile(Object.assign(saveLogRQ, { launchUuid }, isItemUuid && { itemUuid }), fileObj);
640
- };
641
-
642
- return this.saveLog(itemObj, requestPromise);
713
+ sendLogWithFile(itemTempId, saveLogRQ, fileObj) {
714
+ const itemObj = this.map[itemTempId];
715
+ if (!itemObj) {
716
+ return this.getRejectAnswer(
717
+ itemTempId,
718
+ new Error(`Item with tempId "${itemTempId}" not found`),
719
+ );
643
720
  }
644
721
 
645
- getRequestLogWithFile(saveLogRQ, fileObj) {
646
- const url = 'log';
647
- // eslint-disable-next-line no-param-reassign
648
- saveLogRQ.file = { name: fileObj.name };
649
- return this.restClient.create(
650
- url,
651
- this.buildMultiPartStream([saveLogRQ], fileObj, MULTIPART_BOUNDARY),
652
- {
653
- headers: Object.assign({}, this.headers, {
654
- 'Content-Type': `multipart/form-data; boundary=${MULTIPART_BOUNDARY}`,
655
- }),
656
- },
657
- )
658
- .then(response => response)
659
- .catch((error) => {
660
- this.logDebug('ERROR');
661
- this.logDebug(error);
662
- });
663
- }
664
-
665
- // eslint-disable-next-line valid-jsdoc
666
- /**
722
+ const requestPromise = (itemUuid, launchUuid) => {
723
+ const isItemUuid = itemUuid !== launchUuid;
724
+
725
+ return this.getRequestLogWithFile(
726
+ Object.assign(saveLogRQ, { launchUuid }, isItemUuid && { itemUuid }),
727
+ fileObj,
728
+ );
729
+ };
730
+
731
+ return this.saveLog(itemObj, requestPromise);
732
+ }
733
+
734
+ getRequestLogWithFile(saveLogRQ, fileObj) {
735
+ const url = 'log';
736
+ // eslint-disable-next-line no-param-reassign
737
+ saveLogRQ.file = { name: fileObj.name };
738
+ this.logDebug(`Save log with file: ${fileObj.name}`, saveLogRQ);
739
+ return this.restClient
740
+ .create(url, this.buildMultiPartStream([saveLogRQ], fileObj, MULTIPART_BOUNDARY), {
741
+ headers: {
742
+ ...this.headers,
743
+ 'Content-Type': `multipart/form-data; boundary=${MULTIPART_BOUNDARY}`,
744
+ },
745
+ })
746
+ .then((response) => {
747
+ this.logDebug(`Success save log with file: ${fileObj.name}`, response);
748
+ return response;
749
+ })
750
+ .catch((error) => {
751
+ this.logDebug(`Error save log with file: ${fileObj.name}`, error);
752
+ console.dir(error);
753
+ });
754
+ }
755
+
756
+ // eslint-disable-next-line valid-jsdoc
757
+ /**
667
758
  *
668
759
  * @Private
669
760
  */
670
- buildMultiPartStream(jsonPart, filePart, boundary) {
671
- const eol = "\r\n";
672
- const bx = `--${boundary}`;
673
- const buffers = [
674
- // eslint-disable-next-line function-paren-newline
675
- Buffer.from(
676
- // eslint-disable-next-line prefer-template
677
- bx + eol + "Content-Disposition: form-data; name=\"json_request_part\""
678
- + eol + "Content-Type: application/json" + eol
679
- + eol + eol + JSON.stringify(jsonPart) + eol,
680
- ),
681
- // eslint-disable-next-line function-paren-newline
682
- Buffer.from(
683
- // eslint-disable-next-line prefer-template
684
- bx + eol + "Content-Disposition: form-data; name=\"file\"; filename=\"" + filePart.name + "\"" + eol
685
- + "Content-Type: " + filePart.type + eol + eol,
686
- ),
687
- Buffer.from(filePart.content, 'base64'),
688
- Buffer.from(`${eol + bx}--${eol}`),
689
- ];
690
- return Buffer.concat(buffers);
691
- }
692
-
693
- finishTestItemPromiseStart(itemObj, itemTempId, finishTestItemData) {
694
- itemObj.promiseStart
695
- .then(() => {
696
- const url = ['item', itemObj.realId].join('/');
697
- this.logDebug(`Finish test item ${itemTempId}`);
698
- // eslint-disable-next-line max-len
699
- this.restClient.update(url, Object.assign(finishTestItemData, { launchUuid: this.launchUuid }), { headers: this.headers })
700
- .then((response) => {
701
- this.logDebug(`Success finish item ${itemTempId}`);
702
- itemObj.resolveFinish(response);
703
- }, (error) => {
704
- this.logDebug(`Error finish test item ${itemTempId}`);
705
- console.dir(error);
706
- itemObj.rejectFinish(error);
707
- });
708
- }, (error) => {
709
- itemObj.rejectFinish(error);
710
- });
711
- }
761
+ buildMultiPartStream(jsonPart, filePart, boundary) {
762
+ const eol = '\r\n';
763
+ const bx = `--${boundary}`;
764
+ const buffers = [
765
+ // eslint-disable-next-line function-paren-newline
766
+ Buffer.from(
767
+ // eslint-disable-next-line prefer-template
768
+ bx +
769
+ eol +
770
+ 'Content-Disposition: form-data; name="json_request_part"' +
771
+ eol +
772
+ 'Content-Type: application/json' +
773
+ eol +
774
+ eol +
775
+ eol +
776
+ JSON.stringify(jsonPart) +
777
+ eol,
778
+ ),
779
+ // eslint-disable-next-line function-paren-newline
780
+ Buffer.from(
781
+ // eslint-disable-next-line prefer-template
782
+ bx +
783
+ eol +
784
+ 'Content-Disposition: form-data; name="file"; filename="' +
785
+ filePart.name +
786
+ '"' +
787
+ eol +
788
+ 'Content-Type: ' +
789
+ filePart.type +
790
+ eol +
791
+ eol,
792
+ ),
793
+ Buffer.from(filePart.content, 'base64'),
794
+ Buffer.from(`${eol + bx}--${eol}`),
795
+ ];
796
+ return Buffer.concat(buffers);
797
+ }
798
+
799
+ finishTestItemPromiseStart(itemObj, itemTempId, finishTestItemData) {
800
+ itemObj.promiseStart.then(
801
+ () => {
802
+ const url = ['item', itemObj.realId].join('/');
803
+ this.logDebug(`Finish test item with tempId ${itemTempId}`, itemObj);
804
+ this.restClient
805
+ .update(url, Object.assign(finishTestItemData, { launchUuid: this.launchUuid }), {
806
+ headers: this.headers,
807
+ })
808
+ .then(
809
+ (response) => {
810
+ this.logDebug(`Success finish item with tempId ${itemTempId}`, response);
811
+ itemObj.resolveFinish(response);
812
+ },
813
+ (error) => {
814
+ this.logDebug(`Error finish test item with tempId ${itemTempId}`, error);
815
+ console.dir(error);
816
+ itemObj.rejectFinish(error);
817
+ },
818
+ );
819
+ },
820
+ (error) => {
821
+ itemObj.rejectFinish(error);
822
+ },
823
+ );
824
+ }
712
825
  }
713
826
 
714
827
  module.exports = RPClient;