-
Notifications
You must be signed in to change notification settings - Fork 23
/
common.js
90 lines (82 loc) · 2.64 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// common.js
// ------------------------------------------------------------------
//
// Copyright 2017-2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// created: Sun Apr 30 19:30:27 2017
// last saved: <2022-April-01 13:57:27>
/* global exports, global */
const chai = require('chai');
const assert = chai.assert;
const path = require('path');
const apigeejs = require('../index.js');
const faker = require('faker');
//var sprintf = require('sprintf-js').sprintf;
const chaiAsPromised = require("chai-as-promised");
chai.use(chaiAsPromised);
var config = require('../testConfig.json');
// testConfig.json ought to look something like this:
//
// {
// "mgmtServer" : "https://api.enterprise.apigee.com",
// "org" : "my-apigee-org-name",
// "netrc" : true
// }
//
// or
// {
// "mgmtServer" : "https://api.enterprise.apigee.com",
// "org" : "my-apigee-org-name",
// "user" : "[email protected]"
// "password" : "Secret-BB208846F523"
// }
//
// It can also have a verbosity flag. (truthy/falsy)
//...which you can override here:
//config.verbosity = true;
// export some global vars for all tests
global.assert = assert;
global.path = path;
global.aej = apigeejs;
global.config = config;
global.faker = faker;
//global.sprintf = sprintf;
global.apigeejs = apigeejs;
function connectApigee(cb) {
let options = Object.assign({}, config);
// for verbose logging, uncomment the following.
//options.verbosity = 1;
if (cb) {
return apigeejs.apigee.connect(options, function(e, org){
assert.isNull(e, JSON.stringify(e));
cb(org);
});
}
else {
return apigeejs.apigee.connect(options);
}
}
function selectNRandom(list, N, promiseFn, done) {
function reducer(promise, num) {
let ix = Math.floor(Math.random() * list.length);
return promise.then( () => promiseFn(list[ix], ix, list))
.then( () => (1+num >= N) ? done(): {});
}
Array.from(Array(N).keys()).reduce(reducer, Promise.resolve());
}
exports.selectNRandom = selectNRandom;
exports.connectApigee = connectApigee;
exports.testTimeout = config.timeout || 35000;
exports.slowThreshold = config.slowThreshold || 5000;