-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon.js
177 lines (144 loc) · 5.37 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Common utility functions. Probably in the future this would need a bit of refactoring
*
* Copyright (c) 2018 FIWARE Foundation e.V.
*
*
*/
const endpoint = process.env.TEST_ENDPOINT || 'http://localhost:1026';
const accEndpoint = process.env.ACC_ENDPOINT || 'http://localhost:8085';
//const notifyEndpoint = process.env.NOTIFY_ENDPOINT || 'http://host.docker.internal:8085/acc';
const notifyEndpoint = process.env.NOTIFY_ENDPOINT || 'http://accumulator:8085/acc';
const ngsild = 'ngsi-ld/v1';
const testedResource = endpoint + '/' + ngsild;
// Regular expression for matching the link header pointing to the JSON-LD @context
const JSON_LD_CONTEXT_HEADER = /<.+>;\s+rel="http:\/\/www\.w3\.org\/ns\/json-ld#context";\s+type="application\/ld\+json"/;
const JSON_TYPE = /application\/json(;.*)?/;
const emptyArray = [];
const arrayWithAnEmptyObj = [{}];
const ACCEPTABLE_EMPTY_RESULTS = [emptyArray, arrayWithAnEmptyObj];
function acceptableContexts(entity, contexts) {
const entities = [];
contexts.forEach((context, index) => {
const obj = JSON.parse(JSON.stringify(entity));
obj['@context'] = context;
entities.push(obj);
});
return entities;
}
function assertCreated(response, id, resource) {
const resourceTest = resource || '/entities/';
expect(response).toHaveProperty('statusCode', 201);
expect(response.headers).toHaveProperty('location', '/' + ngsild + resourceTest + id);
}
function assertSubscriptionCreated(response, id) {
assertCreated(response, id, '/subscriptions/');
}
function assertRegistrationCreated(response, id) {
assertCreated(response, id, '/csourceRegistrations/');
}
function assertResponse(response, mimeType) {
const mType = mimeType || JSON_TYPE;
expect(response.response).toHaveProperty('statusCode', 200);
expect(response.response.headers['content-type']).toMatch(mType);
// response.response.headers['link'] =
// '<http://json-ld.org/contexts/person.jsonld>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"';
if (mType === JSON_TYPE) {
expect(response.response.headers.link).toBeDefined();
const linkHeader = response.response.headers.link;
expect(linkHeader).toMatch(JSON_LD_CONTEXT_HEADER);
}
}
function assertRetrievedAlternatives(response, entity, mimeType, contexts) {
const entities = acceptableContexts(entity, contexts);
assertResponse(response, mimeType);
expect(entities).toContainEqual(response.body);
}
function assertRetrieved(response, entity, mimeType) {
assertResponse(response, mimeType);
expect(response.body).toEqual(entity);
}
function assertRetrievedQueryAlternatives(response, entity, mimeType, contexts) {
const entities = acceptableContexts(entity, contexts);
assertResponse(response, mimeType);
expect(response.body).toBeDefined();
// Check first query result
expect(response.body[0]).toBeDefined();
expect(entities).toContainEqual(response.body[0]);
}
function assertRetrievedQuery(response, entity, mimeType) {
assertResponse(response, mimeType);
expect(response.body).toBeDefined();
// Check first query result
expect(response.body[0]).toBeDefined();
expect(response.body[0]).toEqual(entity);
}
function assertNoResultsQuery(response, mimeType) {
const checkedMimeType = mimeType || 'application/json';
assertResponse(response, checkedMimeType);
expect(response.body).toBeDefined();
// Check first query result
expect(ACCEPTABLE_EMPTY_RESULTS).toContainEqual(response.body);
// expect(response.body.length).toBe(0);
}
function assertResultsQuery(response, numResults) {
expect(response.body).toBeDefined();
expect(response.body.length).toBe(numResults);
}
function assertBatchOperation(response, success, errors) {
expect(typeof response.body).toBe('object');
const respSuccess = response.body.success;
const respErrors = response.body.errors;
expect(respSuccess.length).toBe(success.length);
expect(respErrors.length).toBe(errors.length);
for (let j = 0; j < success.length; j++) {
expect(respSuccess[j]).toBe(success[j]);
}
for (let j = 0; j < errors.length; j++) {
expect(respErrors[j].entityId).toBe(errors[j]);
}
}
function serializeParams(query) {
let out = '';
Object.keys(query).forEach(function(key) {
out += key + '=' + encodeURIComponent(query[key]);
out += '&';
});
return out.substring(0, out.length - 1);
}
// Patches the object and returns a new copy of the patched object
// For some reason when invoking this function from the tests the JSON
// global object is not found
// TECHNICAL DEBT
function patchObj(target, patch) {
const copy = JSON.parse(JSON.stringify(target));
return Object.assign(copy, patch);
}
function sleep(milliseconds) {
/*eslint no-unused-vars: "off"*/
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, milliseconds);
});
}
module.exports = {
testedResource,
accEndpoint,
notifyEndpoint,
assertCreated,
assertSubscriptionCreated,
assertRegistrationCreated,
assertRetrieved,
assertRetrievedAlternatives,
assertRetrievedQueryAlternatives,
assertRetrievedQuery,
assertResultsQuery,
assertNoResultsQuery,
serializeParams,
assertBatchOperation,
// TECHNICAL DEBT
patchObj,
sleep,
assertResponse
};