-
Notifications
You must be signed in to change notification settings - Fork 477
/
HibernateAbstractTest.java
566 lines (528 loc) · 24 KB
/
HibernateAbstractTest.java
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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
*
* http://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.
*/
package org.kaaproject.kaa.server.common.dao.impl.sql;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.kaaproject.kaa.common.dto.KaaAuthorityDto;
import org.kaaproject.kaa.common.dto.NotificationTypeDto;
import org.kaaproject.kaa.common.dto.TopicTypeDto;
import org.kaaproject.kaa.common.dto.UpdateStatus;
import org.kaaproject.kaa.common.dto.ctl.CTLSchemaScopeDto;
import org.kaaproject.kaa.common.dto.event.ApplicationEventAction;
import org.kaaproject.kaa.common.dto.event.EventClassType;
import org.kaaproject.kaa.server.common.core.schema.KaaSchemaFactoryImpl;
import org.kaaproject.kaa.server.common.dao.AbstractTest;
import org.kaaproject.kaa.server.common.dao.model.sql.Application;
import org.kaaproject.kaa.server.common.dao.model.sql.ApplicationEventFamilyMap;
import org.kaaproject.kaa.server.common.dao.model.sql.ApplicationEventMap;
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchema;
import org.kaaproject.kaa.server.common.dao.model.sql.CTLSchemaMetaInfo;
import org.kaaproject.kaa.server.common.dao.model.sql.Change;
import org.kaaproject.kaa.server.common.dao.model.sql.Configuration;
import org.kaaproject.kaa.server.common.dao.model.sql.ConfigurationSchema;
import org.kaaproject.kaa.server.common.dao.model.sql.EndpointGroup;
import org.kaaproject.kaa.server.common.dao.model.sql.EndpointProfileSchema;
import org.kaaproject.kaa.server.common.dao.model.sql.EventClass;
import org.kaaproject.kaa.server.common.dao.model.sql.EventClassFamily;
import org.kaaproject.kaa.server.common.dao.model.sql.EventClassFamilyVersion;
import org.kaaproject.kaa.server.common.dao.model.sql.History;
import org.kaaproject.kaa.server.common.dao.model.sql.LogAppender;
import org.kaaproject.kaa.server.common.dao.model.sql.LogSchema;
import org.kaaproject.kaa.server.common.dao.model.sql.NotificationSchema;
import org.kaaproject.kaa.server.common.dao.model.sql.ProfileFilter;
import org.kaaproject.kaa.server.common.dao.model.sql.SdkProfile;
import org.kaaproject.kaa.server.common.dao.model.sql.ServerProfileSchema;
import org.kaaproject.kaa.server.common.dao.model.sql.Tenant;
import org.kaaproject.kaa.server.common.dao.model.sql.Topic;
import org.kaaproject.kaa.server.common.dao.model.sql.User;
import org.kaaproject.kaa.server.common.dao.model.sql.UserVerifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class HibernateAbstractTest extends AbstractTest {
private static final Logger LOG = LoggerFactory.getLogger(HibernateAbstractTest.class);
protected Tenant generateTenant() {
LOG.debug("Generate tenant...");
Tenant tenant = new Tenant();
tenant.setName("Test tenant" + RANDOM.nextInt());
tenant = tenantDao.save(tenant);
LOG.debug("Generate tenant {}", tenant);
return tenant;
}
protected Change generateChange() {
LOG.debug("Generate change...");
Change change = new Change();
change.setConfigurationId(RANDOM.nextLong());
change.setConfigurationVersion(RANDOM.nextInt(3000));
change = historyDao.save(change, Change.class);
LOG.debug("Generated change {}", change);
return change;
}
protected List<History> generateHistory(Application app, int count) {
LOG.debug("Generate history...");
List<History> histories = new ArrayList<>();
if (app == null) {
app = generateApplication(null);
}
for (int i = 0; i < count; i++) {
History history = new History();
history.setApplication(app);
history.setLastModifyTime(System.currentTimeMillis());
history.setSequenceNumber(i + 1);
history.setChange(generateChange());
history = historyDao.save(history);
histories.add(history);
LOG.debug("Generated history {}", history);
}
return histories;
}
protected User generateUser(Tenant tenant, KaaAuthorityDto authority) {
LOG.debug("Generate user...");
if (tenant == null) {
tenant = generateTenant();
}
User user = new User();
user.setExternalUid(UUID.randomUUID().toString());
user.setTenant(tenant);
if (authority == null) {
authority = KaaAuthorityDto.KAA_ADMIN;
}
user.setAuthority(authority);
user.setUsername("TestUserName");
user = userDao.save(user);
LOG.debug("Generated user {}", user);
return user;
}
protected Application generateApplication(Tenant tenant) {
LOG.debug("Generate application...");
if (tenant == null) {
tenant = generateTenant();
}
Application app = new Application();
app.setName("Test app name" + UUID.randomUUID().toString());
app.setTenant(tenant);
app.setApplicationToken(UUID.randomUUID().toString());
app.setSequenceNumber(RANDOM.nextInt());
app = applicationDao.save(app);
LOG.debug("Generated application {}", app);
return app;
}
protected EndpointGroup generateEndpointGroup(Application app, Set<Topic> topics) {
EndpointGroup group = new EndpointGroup();
if (app == null) {
app = generateApplication(null);
}
group.setApplication(app);
group.setName("GROUP_ALL_" + RANDOM.nextInt());
group.setWeight(RANDOM.nextInt());
group.setTopics(topics);
return endpointGroupDao.save(group);
}
protected List<ConfigurationSchema> generateConfSchema(Application app, int count) {
List<ConfigurationSchema> schemas = Collections.emptyList();
try {
if (app == null) {
app = generateApplication(null);
}
ConfigurationSchema schema;
schemas = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
schema = new ConfigurationSchema();
schema.setApplication(app);
schema.setSchema(readSchemaFileAsString("dao/schema/testDataSchema.json"));
schema.setVersion(i + 1);
schema = configurationSchemaDao.save(schema);
Assert.assertNotNull(schema);
schemas.add(schema);
}
} catch (IOException e) {
LOG.error("Can't generate configuration schemas {}", e);
Assert.fail("Can't generate configuration schemas." + e.getMessage());
}
return schemas;
}
protected List<Configuration> generateConfiguration(ConfigurationSchema schema, EndpointGroup group, int count, UpdateStatus status) {
List<Configuration> configs = Collections.emptyList();
try {
if (schema == null) {
schema = generateConfSchema(null, 1).get(0);
}
if (group == null) {
group = generateEndpointGroup(schema.getApplication(), null);
}
Assert.assertNotNull(schema);
configs = new ArrayList<>();
for (int i = 0; i < count; i++) {
Configuration dto = new Configuration();
dto.setId(null);
dto.setStatus(status != null ? status : UpdateStatus.INACTIVE);
dto.setConfigurationBody(new byte[]{0, 2, 3, 4,});
dto.setConfigurationSchema(schema);
dto.setSequenceNumber(i);
dto.setSchemaVersion(i + 1);
dto.setApplication(schema.getApplication());
dto.setEndpointGroup(group);
Configuration saved = configurationDao.save(dto);
Assert.assertNotNull(saved);
configs.add(saved);
}
} catch (Exception e) {
LOG.error("Can't generate configs {}", e);
Assert.fail("Can't generate configurations." + e.getMessage());
}
return configs;
}
protected List<EndpointProfileSchema> generateProfSchema(Application app, int count) {
List<EndpointProfileSchema> schemas = Collections.emptyList();
try {
if (app == null) {
app = generateApplication(null);
}
CTLSchema ctlSchema = generateCTLSchema(DEFAULT_FQN, 1, app.getTenant(), null);
EndpointProfileSchema schemaDto;
schemas = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
schemaDto = new EndpointProfileSchema();
schemaDto.setApplication(app);
schemaDto.setCreatedUsername("Test User");
schemaDto.setCtlSchema(ctlSchema);
schemaDto.setVersion(i + 1);
schemaDto.setName("Test Name");
schemaDto = profileSchemaDao.save(schemaDto);
Assert.assertNotNull(schemaDto);
schemas.add(schemaDto);
}
} catch (Exception e) {
LOG.error("Can't generate profile schema {}", e);
Assert.fail("Can't generate profile schema." + e.getMessage());
}
return schemas;
}
protected CTLSchema generateCTLSchema(String fqn, int version, Tenant tenant, CTLSchemaScopeDto scope) {
if (scope == null) {
if (tenant == null) {
scope = CTLSchemaScopeDto.SYSTEM;
} else {
scope = CTLSchemaScopeDto.TENANT;
}
}
CTLSchemaMetaInfo metaInfo = new CTLSchemaMetaInfo();
metaInfo.setFqn(fqn);
metaInfo.setTenant(tenant);
metaInfo = ctlSchemaMetaInfoDao.save(metaInfo);
CTLSchema ctlSchema = new CTLSchema();
ctlSchema.setMetaInfo(metaInfo);
ctlSchema.setVersion(version);
ctlSchema.setBody(UUID.randomUUID().toString());
ctlSchema.setDependencySet(new HashSet<CTLSchema>());
ctlSchema = ctlSchemaDao.save(ctlSchema);
return ctlSchema;
}
protected List<NotificationSchema> generateNotificationSchema(Application app, int ctlVersion, int count, NotificationTypeDto type) {
List<NotificationSchema> schemas = Collections.emptyList();
try {
if (app == null) {
app = generateApplication(null);
}
CTLSchema ctlSchema = generateCTLSchema(DEFAULT_FQN, ctlVersion, app.getTenant(), null);
NotificationSchema schemaDto;
schemas = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
schemaDto = new NotificationSchema();
schemaDto.setApplication(app);
schemaDto.setCreatedUsername("Test User");
schemaDto.setCtlSchema(ctlSchema);
schemaDto.setVersion(i + 1);
schemaDto.setName("Test Name");
schemaDto.setType(type);
schemaDto = notificationSchemaDao.save(schemaDto);
Assert.assertNotNull(schemaDto);
schemas.add(schemaDto);
}
} catch (Exception e) {
LOG.error("Can't generate profile schema {}", e);
Assert.fail("Can't generate profile schema." + e.getMessage());
}
return schemas;
}
protected List<ProfileFilter> generateFilter(EndpointProfileSchema schema, ServerProfileSchema srvSchema, EndpointGroup group, int count, UpdateStatus status) {
return generateFilter(generateApplication(null), schema, srvSchema, group, count, status);
}
protected List<ProfileFilter> generateFilter(Application app, EndpointProfileSchema schema, ServerProfileSchema srvSchema, EndpointGroup group, int count, UpdateStatus status) {
if (schema == null) {
schema = generateProfSchema(app, 1).get(0);
}
if (srvSchema == null) {
srvSchema = new ServerProfileSchema(generateServerProfileSchema(app.getStringId(), app.getTenant().getStringId()));
}
if (group == null) {
group = generateEndpointGroup(app, null);
}
List<ProfileFilter> filters = new ArrayList<>();
for (int i = 0; i < count; i++) {
ProfileFilter dto = new ProfileFilter();
dto.setId(null);
dto.setStatus(status != null ? status : UpdateStatus.INACTIVE);
dto.setEndpointGroup(group);
dto.setEndpointProfileSchema(schema);
dto.setServerProfileSchema(srvSchema);
dto.setSequenceNumber(i);
dto.setApplication(app);
ProfileFilter saved = profileFilterDao.save(dto);
Assert.assertNotNull(saved);
filters.add(saved);
}
return filters;
}
protected List<ProfileFilter> generateFilterWithoutSchemaGeneration(EndpointProfileSchema schema, ServerProfileSchema srvSchema, EndpointGroup group, int count, UpdateStatus status) {
Application app = null;
if (schema != null) {
app = schema.getApplication();
} else if (srvSchema != null) {
app = srvSchema.getApplication();
}
if (group == null) {
group = generateEndpointGroup(app, null);
}
List<ProfileFilter> filters = new ArrayList<>();
for (int i = 0; i < count; i++) {
ProfileFilter dto = new ProfileFilter();
dto.setId(null);
dto.setStatus(status != null ? status : UpdateStatus.INACTIVE);
dto.setEndpointGroup(group);
dto.setEndpointProfileSchema(schema);
dto.setServerProfileSchema(srvSchema);
dto.setSequenceNumber(i);
dto.setApplication(app);
ProfileFilter saved = profileFilterDao.save(dto);
Assert.assertNotNull(saved);
filters.add(saved);
}
return filters;
}
protected Topic generateTopic(Application app, TopicTypeDto type, String topicName) {
Topic topic = new Topic();
if (topicName != null && !topicName.isEmpty()) {
topic.setName(topicName);
} else {
topic.setName("Generated Topic name");
}
if (app == null) {
app = generateApplication(null);
}
topic.setApplication(app);
if (type == null) {
type = TopicTypeDto.MANDATORY;
}
topic.setType(type);
return topicDao.save(topic);
}
protected LogAppender generateLogAppender(Application app) {
LogAppender appender = new LogAppender();
if (app == null) {
app = generateApplication(null);
}
appender.setApplication(app);
appender.setMinLogSchemaVersion(1);
appender.setMaxLogSchemaVersion(2);
return appenderDao.save(appender);
}
protected List<EventClassFamily> generateEventClassFamily(Tenant tenant, int count) {
int eventSchemaVersionsCount = 2;
if (tenant == null) {
tenant = generateTenant();
}
EventClassFamily eventClassFamily;
List<EventClassFamily> eventClassFamilies = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
eventClassFamily = new EventClassFamily();
eventClassFamily.setTenant(tenant);
eventClassFamily.setClassName("Test ClassName" + RANDOM.nextInt());
eventClassFamily.setCreatedTime(new Date().getTime());
eventClassFamily.setCreatedUsername("Test Username");
eventClassFamily.setDescription("Test Description");
eventClassFamily.setName("Test Name" + RANDOM.nextInt());
eventClassFamily.setNamespace("Test Namespace");
List<EventClassFamilyVersion> eventSchemaVersions = new ArrayList<>(eventSchemaVersionsCount);
for (int j = 0; j < eventSchemaVersionsCount; j++) {
EventClassFamilyVersion eventSchemaVersion = new EventClassFamilyVersion();
eventSchemaVersion.setCreatedTime(new Date().getTime());
eventSchemaVersion.setCreatedUsername("Test Username");
eventSchemaVersion.setVersion(1);
eventSchemaVersions.add(eventSchemaVersion);
}
eventClassFamily.setSchemas(eventSchemaVersions);
eventClassFamily = eventClassFamilyDao.save(eventClassFamily);
Assert.assertNotNull(eventClassFamily);
eventClassFamilies.add(eventClassFamily);
}
return eventClassFamilies;
}
protected List<EventClass> generateEventClass(Tenant tenant, EventClassFamily eventClassFamily, int count) {
if (tenant == null) {
tenant = generateTenant();
}
if (eventClassFamily == null) {
eventClassFamily = generateEventClassFamily(tenant, 1).get(0);
}
EventClass eventClass;
EventClassFamilyVersion eventClassFamilyVersion = eventClassFamily.getSchemas().get(0);
List<EventClass> records = eventClassFamilyVersion.getRecords();
if (records == null) {
records = new ArrayList<>();
}
for (int i = 0; i < count; i++) {
eventClass = new EventClass();
eventClass.setTenant(tenant);
Integer version = 1;
try {
version = ctlSchemaDao.find().stream().max((ctl1, ctl2) -> Integer.compare(ctl1.getVersion(), ctl2.getVersion())).get().getVersion()+1;
} catch (NoSuchElementException e) {}
eventClass.setCtlSchema(generateCTLSchema(DEFAULT_FQN, version, tenant, CTLSchemaScopeDto.TENANT));
eventClass.setEcf(eventClassFamilyVersion);
eventClass.setFqn("Test FQN" + RANDOM.nextInt());
eventClass.setType(EventClassType.EVENT);
eventClass.setVersion(1);
eventClass.setName("test schema name" + RANDOM.nextInt());
eventClass.setDescription("test schema description" + RANDOM.nextInt());
eventClass.setCreatedUsername("test createdUsername" + RANDOM.nextInt());
eventClass.setCreatedTime(new Date().getTime());
eventClass.setApplication(generateApplication(tenant));
records.add(eventClass);
}
eventClassFamilyVersion.setRecords(records);
eventClassFamilyDao.save(eventClassFamily);
eventClassFamily = eventClassFamilyDao.findById(eventClassFamily.getStringId());
Long ecfvId = eventClassFamilyVersion.getId();
eventClassFamilyVersion = eventClassFamily.getSchemas().stream().filter(ecfv -> ecfv.getId().equals(ecfvId)).collect(Collectors.toList()).get(0);
List<EventClass> eventClasses = new ArrayList<>(count);
eventClasses.addAll(eventClassFamilyVersion.getRecords());
return eventClasses;
}
protected List<ApplicationEventFamilyMap> generateApplicationEventFamilyMap(Tenant tenant, Application application,
EventClassFamily eventClassFamily, int count, boolean generateApplicationEventMaps) {
int applicationEventMapCount = 2;
if (tenant == null) {
tenant = generateTenant();
}
if (application == null) {
application = generateApplication(tenant);
}
if (eventClassFamily == null) {
eventClassFamily = generateEventClassFamily(tenant, 1).get(0);
}
ApplicationEventFamilyMap applicationEventFamilyMap;
List<ApplicationEventFamilyMap> applicationEventFamilyMaps = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
applicationEventFamilyMap = new ApplicationEventFamilyMap();
applicationEventFamilyMap.setApplication(application);
applicationEventFamilyMap.setCreatedTime(new Date().getTime());
applicationEventFamilyMap.setCreatedUsername("Test Username");
applicationEventFamilyMap.setEcf(eventClassFamily);
applicationEventFamilyMap.setVersion(1);
if (generateApplicationEventMaps) {
List<ApplicationEventMap> applicationEventMaps = new ArrayList<>(applicationEventMapCount);
for (int j = 0; j < applicationEventMapCount; j++) {
ApplicationEventMap applicationEventMap = new ApplicationEventMap();
applicationEventMap.setAction(ApplicationEventAction.BOTH);
applicationEventMap.setFqn("Test FQN" + RANDOM.nextInt());
applicationEventMap.setEventClass(generateEventClass(tenant, eventClassFamily, 1).get(0));
applicationEventMaps.add(applicationEventMap);
}
applicationEventFamilyMap.setEventMaps(applicationEventMaps);
}
applicationEventFamilyMap = applicationEventFamilyMapDao.save(applicationEventFamilyMap);
Assert.assertNotNull(applicationEventFamilyMap);
applicationEventFamilyMaps.add(applicationEventFamilyMap);
}
return applicationEventFamilyMaps;
}
protected List<LogSchema> generateLogSchema(Tenant tenant, Application application, int count) {
List<LogSchema> schemas = Collections.emptyList();
try {
if (application == null) {
application = generateApplication(tenant);
}
LogSchema schema;
schemas = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
schema = new LogSchema();
schema.setApplication(application);
schema.setSchema(new KaaSchemaFactoryImpl().createDataSchema(readSchemaFileAsString("dao/schema/testDataSchema.json")).getRawSchema());
schema.setCreatedUsername("Test User");
schema.setName("Test Name");
schema = logSchemaDao.save(schema);
Assert.assertNotNull(schema);
schemas.add(schema);
}
} catch (IOException e) {
LOG.error("Can't generate log schemas {}", e);
Assert.fail("Can't generate log schemas.");
}
return schemas;
}
protected String readSchemaFileAsString(String filePath) throws IOException {
try {
URL url = Thread.currentThread().getContextClassLoader().getResource(filePath);
if (url != null) {
Path path = Paths.get(url.toURI());
byte[] bytes = Files.readAllBytes(path);
return new String(bytes);
}
} catch (URISyntaxException e) {
LOG.error("Can't generate configs {}", e);
}
return null;
}
protected UserVerifier generateUserVerifier(Application app, String verifierToken) {
UserVerifier verifier = new UserVerifier();
verifier.setName("GENERATED test Verifier");
if (app == null) {
app = generateApplication(null);
}
verifier.setApplication(app);
if (verifierToken == null) {
verifierToken = "token";
}
verifier.setVerifierToken(verifierToken);
return verifierDao.save(verifier);
}
protected SdkProfile generateSdkProfile(Application application, String token) {
SdkProfile entity = new SdkProfile();
if (application == null) {
application = this.generateApplication(null);
}
entity.setApplication(application);
if (token == null) {
token = "token";
}
entity.setToken(token);
return sdkProfileDao.save(entity);
}
}