-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityTest.java
More file actions
524 lines (473 loc) · 23.8 KB
/
EntityTest.java
File metadata and controls
524 lines (473 loc) · 23.8 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
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
package com.contentgrid.appserver.application.model;
import static org.junit.jupiter.api.Assertions.*;
import com.contentgrid.appserver.application.model.attributes.Attribute;
import com.contentgrid.appserver.application.model.attributes.CompositeAttribute;
import com.contentgrid.appserver.application.model.attributes.CompositeAttributeImpl;
import com.contentgrid.appserver.application.model.attributes.ContentAttribute;
import com.contentgrid.appserver.application.model.attributes.SimpleAttribute;
import com.contentgrid.appserver.application.model.attributes.SimpleAttribute.Type;
import com.contentgrid.appserver.application.model.attributes.flags.ReadOnlyFlag;
import com.contentgrid.appserver.application.model.exceptions.AttributeNotFoundException;
import com.contentgrid.appserver.application.model.exceptions.DuplicateElementException;
import com.contentgrid.appserver.application.model.exceptions.InvalidArgumentModelException;
import com.contentgrid.appserver.application.model.exceptions.InvalidAttributeTypeException;
import com.contentgrid.appserver.application.model.exceptions.MissingFlagException;
import com.contentgrid.appserver.application.model.i18n.UserLocales;
import com.contentgrid.appserver.application.model.searchfilters.AttributeSearchFilter;
import com.contentgrid.appserver.application.model.searchfilters.AttributeSearchFilter.Operation;
import com.contentgrid.appserver.application.model.searchfilters.FullTextSearchContentAttributeSearchFilter;
import com.contentgrid.appserver.application.model.searchfilters.SearchFilter;
import com.contentgrid.appserver.application.model.sortable.SortableField;
import com.contentgrid.appserver.application.model.values.ApplicationName;
import com.contentgrid.appserver.application.model.values.AttributeName;
import com.contentgrid.appserver.application.model.values.ColumnName;
import com.contentgrid.appserver.application.model.values.EntityName;
import com.contentgrid.appserver.application.model.values.FilterName;
import com.contentgrid.appserver.application.model.values.LinkName;
import com.contentgrid.appserver.application.model.values.PathSegmentName;
import com.contentgrid.appserver.application.model.values.PropertyPath;
import com.contentgrid.appserver.application.model.values.SortableName;
import com.contentgrid.appserver.application.model.values.TableName;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class EntityTest {
private static final SimpleAttribute PRIMARY_KEY = SimpleAttribute.builder().name(AttributeName.of("id")).column(
ColumnName.of("id")).type(Type.UUID).flag(ReadOnlyFlag.INSTANCE).build();
private static final SimpleAttribute ATTRIBUTE1 = SimpleAttribute.builder().name(AttributeName.of("attribute1")).column(ColumnName.of("column1")).type(Type.TEXT).build();
private static final SimpleAttribute ATTRIBUTE2 = SimpleAttribute.builder().name(AttributeName.of("attribute2")).column(ColumnName.of("column2")).type(Type.BOOLEAN).build();
private static final ContentAttribute CONTENT1 = ContentAttribute.builder()
.name(AttributeName.of("content1"))
.pathSegment(PathSegmentName.of("content1"))
.linkName(LinkName.of("content1"))
.idColumn(ColumnName.of("content1__id"))
.filenameColumn(ColumnName.of("content1__filename"))
.mimetypeColumn(ColumnName.of("content1__mimetype"))
.lengthColumn(ColumnName.of("content1__length"))
.build();
private static final SimpleAttribute NESTED_ATTRIBUTE = SimpleAttribute.builder()
.name(AttributeName.of("nested_attribute"))
.column(ColumnName.of("nested_attr"))
.type(Type.TEXT)
.build();
private static final CompositeAttribute COMPOSITE = CompositeAttributeImpl.builder()
.name(AttributeName.of("composite"))
.attribute(NESTED_ATTRIBUTE)
.build();
private static final SearchFilter FILTER1 = AttributeSearchFilter.builder()
.operation(Operation.PREFIX)
.name(FilterName.of("filter1"))
.attribute(ATTRIBUTE1)
.build();
private static final SearchFilter FILTER2 = AttributeSearchFilter.builder()
.operation(Operation.EXACT)
.name(FilterName.of("filter2"))
.attribute(ATTRIBUTE2)
.build();
private static final SortableField SORTABLE1 = SortableField.builder().name(SortableName.of("sortable1")).propertyPath(PropertyPath.of(ATTRIBUTE1.getName())).build();
private static final SortableField SORTABLE3 = SortableField.builder().name(SortableName.of("sortable3"))
.propertyPath(PropertyPath.of(CONTENT1.getName(), AttributeName.of("length"))).build();
@Test
void entityTest() {
var entity = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.description("entity description")
.primaryKey(PRIMARY_KEY)
.attribute(ATTRIBUTE1)
.attribute(ATTRIBUTE2)
.attribute(CONTENT1)
.attribute(COMPOSITE)
.searchFilter(FILTER1)
.searchFilter(FILTER2)
.sortableField(SORTABLE1)
.sortableField(SORTABLE3)
.build();
assertEquals(EntityName.of("entity"), entity.getName());
assertEquals(PathSegmentName.of("segment"), entity.getPathSegment());
assertEquals(LinkName.of("link"), entity.getLinkName());
assertEquals(TableName.of("table"), entity.getTable());
assertEquals("entity description", entity.getDescription());
// getAttributeByName
var foundAttribute = entity.getAttributeByName(AttributeName.of("attribute1")).orElseThrow();
assertEquals(AttributeName.of("attribute1"), foundAttribute.getName());
assertEquals(List.of(ColumnName.of("column1")), foundAttribute.getColumns());
var simpleAttribute = assertInstanceOf(SimpleAttribute.class, foundAttribute);
assertEquals(Type.TEXT, simpleAttribute.getType());
// getFilterByName
var filter = entity.getFilterByName(FilterName.of("filter1")).orElseThrow();
assertEquals(FilterName.of("filter1"), filter.getName());
var attrSearchFilter = assertInstanceOf(AttributeSearchFilter.class, filter);
assertEquals(Operation.PREFIX, attrSearchFilter.getOperation());
assertEquals(AttributeName.of("attribute1"), attrSearchFilter.getAttributePath().getFirst());
// getContentByPathSegment
var content = entity.getContentByPathSegment(PathSegmentName.of("content1")).orElseThrow();
assertEquals(AttributeName.of("content1"), content.getName());
assertEquals(LinkName.of("content1"), content.getLinkName());
// Can not use column name or filter name for finding attribute by name
assertTrue(entity.getAttributeByName(AttributeName.of("column1")).isEmpty());
assertTrue(entity.getAttributeByName(AttributeName.of("filter1")).isEmpty());
var attributes = entity.getAttributes();
var attribute3 = SimpleAttribute.builder().name(AttributeName.of("attribute3")).column(ColumnName.of("column3")).type(SimpleAttribute.Type.TEXT).build();
// validate that the list of attributes is immutable
assertThrows(
UnsupportedOperationException.class,
() -> attributes.add(attribute3)
);
var filters = entity.getSearchFilters();
var filter3 = AttributeSearchFilter.builder()
.operation(Operation.EXACT)
.name(FilterName.of("filter3"))
.attribute(ATTRIBUTE1)
.build();
// validate that the list of search filters is immutable
assertThrows(
UnsupportedOperationException.class,
() -> filters.add(filter3)
);
var sortables = entity.getSortableFields();
var sortable2 = SortableField.builder().name(SortableName.of("sortable2")).propertyPath(PropertyPath.of(ATTRIBUTE2.getName())).build();
// validate that the list of sortable fields is immutable
assertThrows(
UnsupportedOperationException.class,
() -> sortables.add(sortable2)
);
var contentAttributes = entity.getContentAttributes();
var content3 = ContentAttribute.builder()
.name(AttributeName.of("content3"))
.pathSegment(PathSegmentName.of("content3"))
.linkName(LinkName.of("content3"))
.idColumn(ColumnName.of("content3__id"))
.filenameColumn(ColumnName.of("content3__filename"))
.mimetypeColumn(ColumnName.of("content3__mimetype"))
.lengthColumn(ColumnName.of("content3__length"))
.build();
// validate that list of content attributes is immutable
assertThrows(
UnsupportedOperationException.class,
() -> contentAttributes.add(content3)
);
}
@Test
void entity_defaultPrimaryKey() {
var entity = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(ATTRIBUTE1)
.attribute(ATTRIBUTE2)
.searchFilter(FILTER1)
.searchFilter(FILTER2)
.sortableField(SORTABLE1)
.build();
assertEquals(PRIMARY_KEY, entity.getPrimaryKey());
}
@Test
void entity_differentPrimaryKey() {
var primaryKey = SimpleAttribute.builder().name(AttributeName.of("entity-id")).column(ColumnName.of("entity_id")).type(Type.UUID).flag(ReadOnlyFlag.INSTANCE).build();
var entity = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.primaryKey(primaryKey)
.attribute(ATTRIBUTE1)
.attribute(ATTRIBUTE2)
.searchFilter(FILTER1)
.searchFilter(FILTER2)
.sortableField(SORTABLE1)
.build();
assertEquals(primaryKey, entity.getPrimaryKey());
}
@ParameterizedTest
@ValueSource(strings = {"TEXT", "LONG", "DOUBLE", "BOOLEAN", "DATETIME"})
void entity_invalidPrimaryKey(Type type) {
var primaryKey = SimpleAttribute.builder().name(AttributeName.of("entity-id")).column(ColumnName.of("entity_id")).type(type).flag(ReadOnlyFlag.INSTANCE).build();
var builder = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.primaryKey(primaryKey)
.attribute(ATTRIBUTE1)
.attribute(ATTRIBUTE2)
.searchFilter(FILTER1)
.searchFilter(FILTER2)
.sortableField(SORTABLE1);
assertThrows(InvalidAttributeTypeException.class, builder::build);
}
@Test
void entity_invalidPrimaryKey_missingFlag() {
var primaryKey = SimpleAttribute.builder().name(AttributeName.of("entity-id")).column(ColumnName.of("entity_id")).type(Type.UUID).build();
var builder = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.primaryKey(primaryKey)
.attribute(ATTRIBUTE1)
.attribute(ATTRIBUTE2)
.searchFilter(FILTER1)
.searchFilter(FILTER2)
.sortableField(SORTABLE1);
assertThrows(MissingFlagException.class, builder::build);
}
@Test
void entity_duplicateAttributeName() {
// normal attribute
var duplicate1 = SimpleAttribute.builder().name(ATTRIBUTE1.getName()).column(ColumnName.of("other_column")).type(Type.TEXT).build();
var builder1 = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(ATTRIBUTE1)
.attribute(duplicate1)
.searchFilter(FILTER1)
.sortableField(SORTABLE1);
assertThrows(DuplicateElementException.class, builder1::build);
// primary key attribute
var duplicate2 = SimpleAttribute.builder().name(AttributeName.of("id")).column(ColumnName.of("other_column")).type(Type.TEXT).build();
var builder2 = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(ATTRIBUTE1)
.attribute(duplicate2)
.searchFilter(FILTER1)
.sortableField(SORTABLE1);
assertThrows(DuplicateElementException.class, builder2::build);
}
@Test
void entity_duplicateColumnName() {
// normal column
var duplicate1 = SimpleAttribute.builder().name(AttributeName.of("other_name")).column(ATTRIBUTE1.getColumn()).type(Type.TEXT).build();
var builder1 = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(ATTRIBUTE1)
.attribute(duplicate1)
.searchFilter(FILTER1)
.sortableField(SORTABLE1);
assertThrows(DuplicateElementException.class, builder1::build);
// primary key column
var duplicate2 = SimpleAttribute.builder().name(AttributeName.of("other_name")).column(ColumnName.of("id")).type(Type.TEXT).build();
var builder2 = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(ATTRIBUTE1)
.attribute(duplicate2)
.searchFilter(FILTER1)
.sortableField(SORTABLE1);
assertThrows(DuplicateElementException.class, builder2::build);
}
@Test
void entity_duplicateFilterName() {
var duplicate = AttributeSearchFilter.builder()
.operation(Operation.EXACT)
.name(FILTER1.getName())
.attribute(ATTRIBUTE2)
.build();
var builder = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(ATTRIBUTE1)
.attribute(ATTRIBUTE2)
.searchFilter(FILTER1)
.searchFilter(duplicate);
assertThrows(DuplicateElementException.class, builder::build);
}
@Test
void entity_filterOnMissingAttribute() {
var entity = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(ATTRIBUTE1)
.searchFilter(FILTER1)
.searchFilter(FILTER2) // on missing ATTRIBUTE2
.build();
var applicationBuilder = Application.builder()
.name(ApplicationName.of("testApp"))
.entity(entity);
assertThrows(AttributeNotFoundException.class, applicationBuilder::build);
}
@Test
void entity_filterOnComposite() {
Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(COMPOSITE)
.searchFilter(AttributeSearchFilter.builder()
.operation(Operation.EXACT)
.name(FilterName.of("filter"))
.attributePath(PropertyPath.of(COMPOSITE.getName(), NESTED_ATTRIBUTE.getName()))
.build()
).build();
}
@Test
void entity_filterOnCompositePointsToSimple() {
var entity = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(ATTRIBUTE1)
.searchFilter(AttributeSearchFilter.builder()
.operation(Operation.EXACT)
.name(FilterName.of("filter"))
.attributePath(PropertyPath.of(ATTRIBUTE1.getName(), AttributeName.of("foo")))
.build()
)
.build();
var applicationBuilder = Application.builder()
.name(ApplicationName.of("testApp"))
.entity(entity);
assertThrows(AttributeNotFoundException.class, applicationBuilder::build);
}
@Test
void entity_filterOnSimplePointsToComposite() {
var entity = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(COMPOSITE)
.searchFilter(AttributeSearchFilter.builder()
.operation(Operation.EXACT)
.name(FilterName.of("filter"))
.attributePath(PropertyPath.of(COMPOSITE.getName()))
.build()
)
.build();
var applicationBuilder = Application.builder()
.name(ApplicationName.of("testApp"))
.entity(entity);
assertThrows(InvalidArgumentModelException.class, applicationBuilder::build);
}
@Test
void entity_duplicateContentPathSegment() {
var duplicate = ContentAttribute.builder()
.name(AttributeName.of("content3"))
.pathSegment(CONTENT1.getPathSegment())
.linkName(LinkName.of("content3"))
.idColumn(ColumnName.of("content3__id"))
.filenameColumn(ColumnName.of("content3__filename"))
.mimetypeColumn(ColumnName.of("content3__mimetype"))
.lengthColumn(ColumnName.of("content3__length"))
.build();
var builder = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(CONTENT1)
.attribute(duplicate);
assertThrows(DuplicateElementException.class, builder::build);
}
@Test
void entity_duplicateContentLinkName() {
var duplicate = ContentAttribute.builder()
.name(AttributeName.of("content3"))
.pathSegment(PathSegmentName.of("content3"))
.linkName(CONTENT1.getLinkName())
.idColumn(ColumnName.of("content3__id"))
.filenameColumn(ColumnName.of("content3__filename"))
.mimetypeColumn(ColumnName.of("content3__mimetype"))
.lengthColumn(ColumnName.of("content3__length"))
.build();
var builder = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(CONTENT1)
.attribute(duplicate);
assertThrows(DuplicateElementException.class, builder::build);
}
@Test
void entity_duplicateSortableName() {
var duplicate = SortableField.builder().name(SORTABLE1.getName()).propertyPath(PropertyPath.of(ATTRIBUTE2.getName())).build();
var builder = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(ATTRIBUTE1)
.attribute(ATTRIBUTE2)
.searchFilter(FILTER1)
.sortableField(SORTABLE1)
.sortableField(duplicate);
assertThrows(DuplicateElementException.class, builder::build);
}
@Test
void entity_sortableOnMissingAttribute() {
var sortable2 = SortableField.builder().name(SortableName.of("sortable2")).propertyPath(PropertyPath.of(ATTRIBUTE2.getName())).build();
var builder = Entity.builder()
.name(EntityName.of("entity"))
.pathSegment(PathSegmentName.of("segment"))
.linkName(LinkName.of("link"))
.table(TableName.of("table"))
.attribute(ATTRIBUTE1)
// No attribute 2 here
.searchFilter(FILTER1)
.sortableField(SORTABLE1)
.sortableField(sortable2);
assertThrows(InvalidArgumentModelException.class, builder::build);
}
@Test
void entity_translations() {
var entity = Entity.builder()
.name(EntityName.of("color"))
.pathSegment(PathSegmentName.of("colors"))
.linkName(LinkName.of("color"))
.table(TableName.of("color"))
.translationsBy(Locale.ENGLISH, t -> t.withSingularName("Color").withPluralName("Colors"))
.translationsBy(Locale.UK, t -> t.withSingularName("Colour").withPluralName("Colours"))
.translationsBy(Locale.FRENCH, t -> t.withSingularName("Couleur"))
.build();
assertEquals("Colour", entity.getTranslations(Locale.UK).getSingularName());
assertEquals("Colours", entity.getTranslations(Locale.UK).getPluralName());
assertEquals("Color", entity.getTranslations(UserLocales.defaults()).getSingularName());
assertEquals("Colors", entity.getTranslations(UserLocales.defaults()).getPluralName());
assertEquals("Couleur", entity.getTranslations(Locale.FRENCH).getSingularName());
assertNull(entity.getTranslations(Locale.FRENCH).getPluralName());
}
@Test
void entity_createsHiddenTextAttribute() {
FullTextSearchContentAttributeSearchFilter filter = FullTextSearchContentAttributeSearchFilter
.builder()
.name(FilterName.of("file-fts-content"))
.locale(Locale.ENGLISH)
.attribute(CONTENT1)
.build();
var entity = Entity.builder()
.name(EntityName.of("file"))
.pathSegment(PathSegmentName.of("files"))
.linkName(LinkName.of("file"))
.table(TableName.of("file"))
.attribute(CONTENT1)
.searchFilter(filter)
.build();
String attrName = filter.getHiddenTextAttributeFormattedName();
Optional<Attribute> attribute = entity.getAttributeByName(AttributeName.of(attrName));
assertTrue(attribute.isPresent());
assertTrue(attribute.get().isIgnored());
}
}