@adobe/acc-js-sdk 1.1.3 → 1.1.6

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.
@@ -1097,7 +1097,6 @@ describe('Application', () => {
1097
1097
  </GetEntityIfMoreRecentResponse>
1098
1098
  </SOAP-ENV:Body>
1099
1099
  </SOAP-ENV:Envelope>`));
1100
-
1101
1100
  const nodes = await link.joinNodes();
1102
1101
  expect(nodes).toMatchObject([]);
1103
1102
  const reverseLink = await link.reverseLink();
@@ -2108,6 +2107,7 @@ describe('Application', () => {
2108
2107
  const application = client.application;
2109
2108
  expect(application).not.toBeNull();
2110
2109
  expect(application.buildNumber).toBeUndefined();
2110
+ expect(application.version).toBeUndefined();
2111
2111
  expect(application.instanceName).toBeUndefined();
2112
2112
  expect(application.operator).toBeUndefined();
2113
2113
  expect(application.package).toBeUndefined();
@@ -2145,4 +2145,15 @@ describe('Application', () => {
2145
2145
  expect(schema2).toBeNull();
2146
2146
  });
2147
2147
  });
2148
- });
2148
+
2149
+ describe("Version", () => {
2150
+ it("Should get proper version information", async () => {
2151
+ const client = await Mock.makeClient();
2152
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
2153
+ await client.NLWS.xtkSession.logon();
2154
+ expect(client.application.buildNumber).toBe('9219');
2155
+ expect(client.application.version).toBe('6.7.0');
2156
+ })
2157
+ })
2158
+ });
2159
+
@@ -0,0 +1,338 @@
1
+ /*
2
+ Copyright 2022 Adobe. All rights reserved.
3
+ This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License. You may obtain a copy
5
+ of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software distributed under
8
+ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ OF ANY KIND, either express or implied. See the License for the specific language
10
+ governing permissions and limitations under the License.
11
+ */
12
+
13
+
14
+ /**********************************************************************************
15
+ *
16
+ * Unit tests for the cache refresher
17
+ *
18
+ *********************************************************************************/
19
+
20
+
21
+ const sdk = require('../src/index.js');
22
+ const { Cache } = require('../src/cache.js');
23
+ const Mock = require('./mock.js').Mock;
24
+ const CacheRefresher = require('../src/cacheRefresher.js').CacheRefresher;
25
+
26
+
27
+ describe("CacheRefresher cache", function () {
28
+
29
+ it('Should call refresh', async () => {
30
+ const client = await Mock.makeClient();
31
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
32
+
33
+ await client.NLWS.xtkSession.logon();
34
+ const cache = new Cache();
35
+ const cacheRefresher = new CacheRefresher(cache, client, "xtk:schema", "rootkey");
36
+
37
+ client._transport.mockReturnValueOnce(Mock.GETMODIFIEDENTITIES_CLEAR_RESPONSE);
38
+ await cacheRefresher._callAndRefresh();
39
+ expect(cacheRefresher._refresherStateCache.get("buildNumber")).toBe("9469");
40
+ expect(cacheRefresher._refresherStateCache.get("time")).toBe("2022-07-28T14:38:55.766Z");
41
+
42
+ client._transport.mockReturnValueOnce(Mock.GETMODIFIEDENTITIES_CLEAR_RESPONSE);
43
+ await cacheRefresher._callAndRefresh();
44
+ expect(cacheRefresher._refresherStateCache.get("buildNumber")).toBe("9469");
45
+ expect(cacheRefresher._refresherStateCache.get("time")).toBe("2022-07-28T14:38:55.766Z");
46
+
47
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
48
+ await client.NLWS.xtkSession.logoff();
49
+ });
50
+
51
+ it('Should call refresh after 1 seconds', async () => {
52
+ const connectionParameters = sdk.ConnectionParameters.ofUserAndPassword("http://acc-sdk:8080", "admin", "admin");
53
+ const client = await sdk.init(connectionParameters);
54
+ client._transport = jest.fn();
55
+
56
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
57
+
58
+ await client.NLWS.xtkSession.logon();
59
+ expect(client.isLogged()).toBeTruthy();
60
+ const cache = new Cache();
61
+
62
+ const cacheRefresher = new CacheRefresher(cache, client, "xtk:schema", "rootkey");
63
+
64
+ expect(cacheRefresher._refresherStateCache.get("buildNumber")).toBeUndefined();
65
+ expect(cacheRefresher._refresherStateCache.get("time")).toBeUndefined();
66
+ client._transport.mockReturnValue(Promise.resolve(Mock.GETMODIFIEDENTITIES_CLEAR_RESPONSE));
67
+ jest.useFakeTimers();
68
+ cacheRefresher.startAutoRefresh(5000);
69
+ jest.advanceTimersByTime(6000);
70
+ jest.useRealTimers();
71
+
72
+ // to allow soap call to finish
73
+ await new Promise(process.nextTick);
74
+
75
+ expect(cacheRefresher._refresherStateCache.get("buildNumber")).toBe("9469");
76
+ expect(cacheRefresher._refresherStateCache.get("time")).toBe("2022-07-28T14:38:55.766Z");
77
+
78
+ cacheRefresher.stopAutoRefresh();
79
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
80
+ await client.NLWS.xtkSession.logoff();
81
+ });
82
+
83
+ it('Should send buildNumber when call refresh', async () => {
84
+ const client = await Mock.makeClient();
85
+ const logs = await Mock.withMockConsole(async () => {
86
+ client.traceAPICalls(true);
87
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
88
+
89
+ await client.NLWS.xtkSession.logon();
90
+
91
+ const cache = new Cache();
92
+
93
+ const cacheRefresher = new CacheRefresher(cache, client, "xtk:schema", "rootkey");
94
+ cacheRefresher._refresherStateCache.put("buildNumber", "9469");
95
+ cacheRefresher._refresherStateCache.put("time", "2022-07-28T14:38:55.766Z");
96
+
97
+ client._transport.mockReturnValueOnce(Mock.GETMODIFIEDENTITIES_CLEAR_RESPONSE);
98
+ await cacheRefresher._callAndRefresh();
99
+ expect(cacheRefresher._refresherStateCache.get("buildNumber")).toBe("9469");
100
+ expect(cacheRefresher._refresherStateCache.get("time")).toBe("2022-07-28T14:38:55.766Z");
101
+
102
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
103
+
104
+
105
+ await client.NLWS.xtkSession.logoff();
106
+
107
+ })
108
+ expect(logs.length).toBe(6);
109
+ expect(logs[0]).toMatch(/SOAP.*request.*Logon/is)
110
+ expect(logs[1]).toMatch(/SOAP.*response.*LogonResponse/is)
111
+ expect(logs[2]).toMatch(/SOAP.*request.*buildNumber.*9469.*2022-07-28T14:38:55.766Z.*GetModifiedEntities*/is)
112
+ expect(logs[3]).toMatch(/SOAP.*response.*GetModifiedEntitiesResponse/is)
113
+ expect(logs[4]).toMatch(/SOAP.*request.*Logoff/is)
114
+ expect(logs[5]).toMatch(/SOAP.*response.*LogoffResponse/is)
115
+ });
116
+
117
+ it('Should refresh cache', async () => {
118
+ const client = await Mock.makeClient();
119
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
120
+
121
+ await client.NLWS.xtkSession.logon();
122
+ const cache = new Cache();
123
+ const cacheRefresher = new CacheRefresher(cache, client, "xtk:schema", "rootkey");
124
+
125
+ cache.put("xtk:schema|nms:recipient", "<content recipient>");
126
+ cache.put("xtk:schema|nms:replicationStrategy", "<content xtk:schema|nms:replicationStrategy>");
127
+ cache.put("xtk:schema|nms:operation", "<content xtk:schema|nms:operation>");
128
+ expect(cache.get("xtk:schema|nms:recipient")).toBe("<content recipient>");
129
+ expect(cache.get("xtk:schema|nms:replicationStrategy")).toBe("<content xtk:schema|nms:replicationStrategy>");
130
+ expect(cache.get("xtk:schema|nms:operation")).toBe("<content xtk:schema|nms:operation>");
131
+
132
+ client._transport.mockReturnValueOnce(Mock.GETMODIFIEDENTITIES_SCHEMA_RESPONSE);
133
+ await cacheRefresher._callAndRefresh();
134
+ expect(cacheRefresher._refresherStateCache.get("buildNumber")).toBe("9469");
135
+ expect(cacheRefresher._refresherStateCache.get("time")).toBe("2022-07-28T15:32:00.785Z");
136
+ expect(cache.get("xtk:schema|nms:recipient")).toBeUndefined();
137
+ expect(cache.get("xtk:schema|nms:replicationStrategy")).toBeUndefined();
138
+ expect(cache.get("xtk:schema|nms:operation")).toBe("<content xtk:schema|nms:operation>");
139
+
140
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
141
+ await client.NLWS.xtkSession.logoff();
142
+ });
143
+
144
+ it('Should stop refresh if method not exist', async () => {
145
+ const client = await Mock.makeClient();
146
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
147
+
148
+ await client.NLWS.xtkSession.logon();
149
+ const cache = new Cache();
150
+ const cacheRefresher = new CacheRefresher(cache, client, "xtk:schema", "rootkey");
151
+
152
+ cacheRefresher.startAutoRefresh();
153
+
154
+ client._transport.mockReturnValueOnce(Mock.GETMODIFIEDENTITIES_UNDEFINED_RESPONSE);
155
+ await cacheRefresher._callAndRefresh();
156
+ expect(cacheRefresher._refresherStateCache.get("buildNumber")).toBeUndefined();
157
+ expect(cacheRefresher._refresherStateCache.get("time")).toBeUndefined();
158
+ expect(cacheRefresher._intervalId).toBeNull();
159
+
160
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
161
+ await client.NLWS.xtkSession.logoff();
162
+ });
163
+
164
+ it('Should not stop refresh if error different from undefined', async () => {
165
+ const client = await Mock.makeClient();
166
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
167
+
168
+ await client.NLWS.xtkSession.logon();
169
+ const cache = new Cache();
170
+ const cacheRefresher = new CacheRefresher(cache, client, "xtk:schema", "rootkey");
171
+
172
+ cacheRefresher.startAutoRefresh();
173
+
174
+ client._transport.mockReturnValueOnce(Mock.GETMODIFIEDENTITIES_ERROR_RESPONSE);
175
+ try {
176
+ await cacheRefresher._callAndRefresh();
177
+ fail('exception is expected');
178
+ } catch (e) {
179
+ expect(e).not.toBeNull();
180
+ }
181
+ expect(cacheRefresher._refresherStateCache.get("buildNumber")).toBeUndefined();
182
+ expect(cacheRefresher._refresherStateCache.get("time")).toBeUndefined();
183
+ expect(cacheRefresher._intervalId).not.toBeNull();
184
+
185
+ cacheRefresher.stopAutoRefresh();
186
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
187
+ await client.NLWS.xtkSession.logoff();
188
+ });
189
+
190
+ it('Should be able to call start refresh twice', async () => {
191
+ const client = await Mock.makeClient();
192
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
193
+
194
+ await client.NLWS.xtkSession.logon();
195
+ const cache = new Cache();
196
+ const cacheRefresher = new CacheRefresher(cache, client, "xtk:schema", "rootkey");
197
+
198
+ jest.useFakeTimers();
199
+ cacheRefresher.startAutoRefresh(100000);
200
+ expect(cacheRefresher._intervalId).not.toBeNull();
201
+ const firstIntervalId = cacheRefresher._intervalId;
202
+ cacheRefresher.startAutoRefresh(5000);
203
+ expect(cacheRefresher._intervalId).not.toBeNull();
204
+ expect(cacheRefresher._intervalId != firstIntervalId);
205
+
206
+ client._transport.mockReturnValueOnce(Mock.GETMODIFIEDENTITIES_CLEAR_RESPONSE);
207
+
208
+ jest.advanceTimersByTime(6000);
209
+ jest.useRealTimers();
210
+
211
+ // to allow soap call to finish
212
+ await new Promise(process.nextTick);
213
+
214
+ expect(cacheRefresher._refresherStateCache.get("buildNumber")).toBe("9469");
215
+ expect(cacheRefresher._refresherStateCache.get("time")).toBe("2022-07-28T14:38:55.766Z");
216
+
217
+ cacheRefresher.stopAutoRefresh();
218
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
219
+ await client.NLWS.xtkSession.logoff();
220
+ });
221
+
222
+ it('Should notify when refresh cache', async () => {
223
+ const client = await Mock.makeClient();
224
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
225
+
226
+
227
+ class Listener {
228
+ constructor() {
229
+ this._schemas = {};
230
+ }
231
+ add(schemaId) {
232
+ this._schemas[schemaId] = "1";
233
+ }
234
+
235
+ invalidateCacheItem(schemaId) {
236
+ this._schemas[schemaId] = undefined;
237
+ }
238
+ getSchema(schemaId) {
239
+ return this._schemas[schemaId];
240
+ }
241
+ }
242
+
243
+ let listener = new Listener();
244
+ client._registerCacheChangeListener(listener);
245
+
246
+ await client.NLWS.xtkSession.logon();
247
+ const cache = new Cache();
248
+ const cacheRefresher = new CacheRefresher(cache, client, "xtk:schema", "rootkey");
249
+
250
+ cache.put("xtk:schema|nms:recipient", "<content recipient>");
251
+ cache.put("xtk:schema|nms:replicationStrategy", "<content xtk:schema|nms:replicationStrategy>");
252
+ cache.put("xtk:schema|nms:operation", "<content xtk:schema|nms:operation>");
253
+
254
+ listener.add("nms:recipient");
255
+
256
+ client._transport.mockReturnValueOnce(Mock.GETMODIFIEDENTITIES_SCHEMA_RESPONSE);
257
+ await cacheRefresher._callAndRefresh();
258
+
259
+ expect(listener.getSchema("nms:recipient")).toBeUndefined();
260
+
261
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
262
+ await client.NLWS.xtkSession.logoff();
263
+ client._unregisterCacheChangeListener(listener);
264
+ });
265
+
266
+ it('Should protect callAndRefresh from re-entrance', async () => {
267
+ const client = await Mock.makeClient();
268
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
269
+ await client.NLWS.xtkSession.logon();
270
+ const cache = new Cache();
271
+ const cacheRefresher = new CacheRefresher(cache, client, "xtk:schema", "rootkey");
272
+ let count = 0;
273
+ cacheRefresher._callAndRefresh = jest.fn(() => { count = count + 1 });
274
+ expect(cacheRefresher._running).toBe(false);
275
+ await cacheRefresher._callAndRefresh();
276
+ expect(count).toBe(1);
277
+ expect(cacheRefresher._running).toBe(false);
278
+ await cacheRefresher._safeCallAndRefresh();
279
+ expect(count).toBe(2);
280
+ expect(cacheRefresher._running).toBe(false);
281
+
282
+ cacheRefresher._running = true;
283
+ await cacheRefresher._safeCallAndRefresh();
284
+ expect(count).toBe(2); // should not have been called since already executing
285
+ })
286
+
287
+ it('Throw CampaignException when calling _callAndRefresh without logon', async () => {
288
+ const client = await Mock.makeClient();
289
+ const cache = new Cache();
290
+ const cacheRefresher = new CacheRefresher(cache, client, "xtk:schema", "rootkey");
291
+
292
+ try {
293
+ await cacheRefresher._callAndRefresh();
294
+ fail('exception is expected');
295
+ } catch (e) {
296
+ expect(e.name).toBe("CampaignException");
297
+ expect(e.errorCode).toBe("SDK-000010");
298
+ }
299
+ });
300
+
301
+ it('Ignore error when calling _safeCallAndRefresh without logon', async () => {
302
+ const client = await Mock.makeClient();
303
+ const cache = new Cache();
304
+ const cacheRefresher = new CacheRefresher(cache, client, "xtk:schema", "rootkey");
305
+
306
+ try {
307
+ await cacheRefresher._safeCallAndRefresh();
308
+ } catch (e) {
309
+ fail('exception is not expected');
310
+ }
311
+ });
312
+
313
+ it('Catch error in soap call GetModifiedEntities and display a warning', async () => {
314
+ const client = await Mock.makeClient();
315
+ client._transport.mockReturnValueOnce(Mock.LOGON_RESPONSE);
316
+
317
+ await client.NLWS.xtkSession.logon();
318
+ const cache = new Cache();
319
+ const cacheRefresher = new CacheRefresher(cache, client, "xtk:schema", "rootkey");
320
+
321
+ client._transport.mockReturnValueOnce(Mock.GETMODIFIEDENTITIES_ERROR_RESPONSE);
322
+ try {
323
+ jest.useFakeTimers();
324
+ cacheRefresher.startAutoRefresh(5000);
325
+ jest.advanceTimersByTime(6000);
326
+ jest.useRealTimers();
327
+
328
+ // to allow soap call to finish
329
+ await new Promise(process.nextTick);
330
+ } catch (e) {
331
+ fail('exception is not expected');
332
+ }
333
+
334
+ cacheRefresher.stopAutoRefresh();
335
+ client._transport.mockReturnValueOnce(Mock.LOGOFF_RESPONSE);
336
+ await client.NLWS.xtkSession.logoff();
337
+ });
338
+ });
@@ -54,7 +54,22 @@ describe('Caches', function() {
54
54
  cache.clear();
55
55
  expect(cache.get("Hello")).toBeUndefined();
56
56
  })
57
- })
57
+
58
+ it("Should remove key in cache", () => {
59
+ const cache = new Cache();
60
+ cache.put("Hello", "World");
61
+ cache.put("Hi", "A");
62
+ expect(cache.get("Hello")).toBe("World");
63
+ expect(cache.get("Hi")).toBe("A");
64
+ cache.remove("Hello");
65
+ expect(cache.get("Hello")).toBeUndefined();
66
+ expect(cache.get("Hi")).toBe("A");
67
+ // should support removing a key which has already been removed
68
+ cache.remove("Hello");
69
+ expect(cache.get("Hello")).toBeUndefined();
70
+ expect(cache.get("Hi")).toBe("A");
71
+ })
72
+ });
58
73
 
59
74
  describe("Entity cache", function() {
60
75
  it("Should cache value", function() {