SyncronisationJobServerTest.java
11.7 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* com.day.cq.wcm.api.Page
* com.day.cq.wcm.api.PageManager
* com.day.cq.wcm.api.WCMException
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.nodetype.NodeType
* org.apache.sling.api.resource.LoginException
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ResourceResolverFactory
* org.apache.sling.commons.json.JSONArray
* org.apache.sling.commons.json.JSONException
* org.apache.sling.commons.json.JSONObject
* org.apache.sling.event.jobs.JobManager
* org.apache.sling.junit.annotations.SlingAnnotationsTestRunner
* org.apache.sling.junit.annotations.TestReference
* org.junit.After
* org.junit.Assert
* org.junit.Before
* org.junit.Test
* org.junit.runner.RunWith
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.cq.aam.client.testing;
import com.adobe.cq.aam.client.AudienceManagerFolderList;
import com.adobe.cq.aam.client.TestData;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.WCMException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.nodetype.NodeType;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.json.JSONArray;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.JSONObject;
import org.apache.sling.event.jobs.JobManager;
import org.apache.sling.junit.annotations.SlingAnnotationsTestRunner;
import org.apache.sling.junit.annotations.TestReference;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RunWith(value=SlingAnnotationsTestRunner.class)
public class SyncronisationJobServerTest {
private static final int ONE_SECOND = 1000;
private static final int TWO_SECONDS = 2000;
private static final int TEN_SECONDS = 10000;
private static final long ONE_TWENTY_SECONDS = 120000;
private static final long THIRTY_SECONDS = 30000;
private static final Logger LOGGER = LoggerFactory.getLogger(SyncronisationJobServerTest.class);
@TestReference
private ResourceResolverFactory resourceResolverFactory;
@TestReference
private JobManager jobManager;
private Long instanceId;
private String configNodePath;
private ResourceResolver resourceResolver;
private Session session;
private boolean notOnServer = false;
private boolean cleanup;
private String baseSegmetnationPath;
private PageManager pageManager;
@Before
public void setup() throws LoginException, InterruptedException, RepositoryException, WCMException {
if (this.resourceResolverFactory == null) {
this.notOnServer = true;
return;
}
boolean running = false;
try {
this.createInstanceId();
this.resourceResolver = this.resourceResolverFactory.getAdministrativeResourceResolver(null);
this.pageManager = (PageManager)this.resourceResolver.adaptTo(PageManager.class);
running = true;
}
catch (Exception e) {
LOGGER.error(e.getMessage(), (Throwable)e);
throw new RuntimeException(e.getMessage(), e);
}
finally {
if (!running) {
try {
this.resourceResolver.close();
this.resourceResolver = null;
this.session = null;
}
catch (Exception e) {
LOGGER.debug(e.getMessage(), (Throwable)e);
}
}
}
}
@After
public void teardown() throws RepositoryException {
try {
if (this.notOnServer) {
return;
}
if (this.cleanup) {
for (String path : new String[]{this.configNodePath, this.baseSegmetnationPath}) {
if (!this.session.itemExists(path)) continue;
this.session.removeItem(path);
}
}
this.session.save();
}
catch (Exception e) {
LOGGER.error(e.getMessage(), (Throwable)e);
throw new RuntimeException(e.getMessage(), e);
}
finally {
try {
this.resourceResolver.close();
}
catch (Exception e) {
LOGGER.debug(e.getMessage(), (Throwable)e);
}
this.deleteInstanceId();
}
}
private synchronized void createInstanceId() throws InterruptedException {
while (this.instanceId != null) {
this.wait();
}
this.instanceId = System.currentTimeMillis();
}
private synchronized void deleteInstanceId() {
this.instanceId = null;
this.notify();
}
@Test
public void testStandardSegmentSet() throws InterruptedException, RepositoryException {
try {
if (this.notOnServer) {
return;
}
this.runTestSet("com/adobe/cq/aam/client/segment", 30000);
}
catch (Exception e) {
LOGGER.error(e.getMessage(), (Throwable)e);
throw new RuntimeException(e.getMessage(), e);
}
}
@Test
public void testLargeSegmentSet() throws InterruptedException, RepositoryException {
try {
if (this.notOnServer) {
return;
}
this.runTestSet("com/adobe/cq/aam/client/1000_gen", 120000);
}
catch (Exception e) {
LOGGER.error(e.getMessage(), (Throwable)e);
throw new RuntimeException(e.getMessage(), e);
}
}
private void runTestSet(String testSet, long timeout) throws WCMException, RepositoryException, JSONException, IOException, InterruptedException {
String[] setupData = this.createConfigNode(testSet, String.valueOf(this.instanceId));
this.configNodePath = setupData[0];
this.baseSegmetnationPath = setupData[1] + "_0";
int expected = this.countExpectedNodes(testSet);
long tupdate = System.currentTimeMillis();
this.waitForCreateTest(expected, timeout);
LOGGER.info("Found all expected nodes in tree, sleeping to allow sync Job to complete. ", (Object)expected);
Thread.sleep(2000);
tupdate = System.currentTimeMillis() - tupdate;
this.waitForUpdateTest(expected, tupdate + 10000);
LOGGER.info("The correct number of nodes was found after an update sync: Test passed Ok ");
}
private String[] createConfigNode(String segmentset, String testId) throws WCMException, RepositoryException {
Page testConfig = this.pageManager.create("/etc/cloudservices/audiencemanager", testId, "/libs/cq/personalization/audiencemanager/templates/audiencemanager", "Test Config " + testId);
Resource testConfigResource = testConfig.getContentResource();
Node configNode = (Node)testConfigResource.adaptTo(Node.class);
JcrUtil.setProperty((Node)configNode, (String)"partner", (Object)String.valueOf(this.instanceId));
JcrUtil.setProperty((Node)configNode, (String)"container", (Object)"0");
JcrUtil.setProperty((Node)configNode, (String)"offlineMode", (Object)true);
JcrUtil.setProperty((Node)configNode, (String)"offlineSet", (Object)segmentset);
JcrUtil.setProperty((Node)configNode, (String)"oauthExpiresEpoch", (Object)(System.currentTimeMillis() + 3600000));
JcrUtil.setProperty((Node)configNode, (String)"oauthAccessToken", (Object)"dummyToken");
this.session = configNode.getSession();
this.session.save();
return new String[]{configNode.getPath(), "/etc/segmentation/aam/" + testId};
}
private void waitForCreateTest(int expected, long timeout) throws RepositoryException, InterruptedException {
this.cleanup = false;
int n = 0;
long eta = System.currentTimeMillis() + timeout;
while (System.currentTimeMillis() < eta) {
Thread.sleep(1000);
if (this.session.nodeExists(this.baseSegmetnationPath) && (n = this.countNodes(this.session.getNode(this.baseSegmetnationPath), "cq:Page")) >= expected) {
this.cleanup = true;
return;
}
LOGGER.info("Found {} or {} nodes in segmentation tree ", (Object)n, (Object)expected);
}
Assert.fail((String)("Timeout or not enough nodes, visual inspection of " + this.baseSegmetnationPath + " required once activity dies down"));
}
private void waitForUpdateTest(int expected, long sleepFor) throws InterruptedException, RepositoryException {
for (Node n : new Iterable<Node>(){
@Override
public Iterator<Node> iterator() {
try {
return SyncronisationJobServerTest.this.session.getNode(SyncronisationJobServerTest.this.baseSegmetnationPath).getNodes();
}
catch (Exception e) {
return null;
}
}
}) {
if (!"cq:Page".equals(n.getPrimaryNodeType().getName())) continue;
n.remove();
}
LOGGER.info("Simulating rapid job triggers, whcih should not cause a problem");
JcrUtil.setProperty((Node)this.session.getNode(this.configNodePath), (String)"container", (Object)null);
this.session.save();
JcrUtil.setProperty((Node)this.session.getNode(this.configNodePath), (String)"container", (Object)"0");
this.session.save();
Thread.sleep(sleepFor);
this.cleanup = false;
Assert.assertEquals((String)("The Number of cq:PageNodes was not correct, please check " + this.baseSegmetnationPath), (long)expected, (long)this.countNodes(this.session.getNode(this.baseSegmetnationPath), "cq:Page"));
this.cleanup = true;
}
private int countExpectedNodes(String testset) throws JSONException, IOException {
return this.countFolders(testset) + this.countSegments(testset);
}
private int countSegments(String testset) throws JSONException, IOException {
return new JSONObject(TestData.loadTestData(testset + "_folder_page0.json")).getInt("total");
}
private int countFolders(String testset) throws JSONException, IOException {
AudienceManagerFolderList folders = new AudienceManagerFolderList(new JSONArray(TestData.loadTestData(testset + "_folders.json")), "none");
return folders.size();
}
private int countNodes(final Node node, String type) {
int c = 0;
for (Node n : new Iterable<Node>(){
@Override
public Iterator<Node> iterator() {
try {
return node.getNodes();
}
catch (RepositoryException e) {
return new ArrayList().iterator();
}
}
}) {
try {
if (type.equals(n.getPrimaryNodeType().getName())) {
++c;
}
}
catch (RepositoryException e) {
LOGGER.error(e.getMessage(), (Throwable)e);
}
c += this.countNodes(n, type);
}
return c;
}
}