/
ImmutableThingFromScratchBuilderTest.java
executable file
·757 lines (598 loc) · 24.8 KB
/
ImmutableThingFromScratchBuilderTest.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.model.things;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.eclipse.ditto.model.things.TestConstants.Feature.FLUX_CAPACITOR;
import static org.eclipse.ditto.model.things.TestConstants.Feature.FLUX_CAPACITOR_ID;
import static org.eclipse.ditto.model.things.TestConstants.Feature.FLUX_CAPACITOR_PROPERTIES;
import static org.eclipse.ditto.model.things.TestConstants.Thing.ATTRIBUTES;
import static org.eclipse.ditto.model.things.assertions.DittoThingsAssertions.assertThat;
import java.util.Collections;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.model.base.exceptions.DittoJsonException;
import org.junit.Before;
import org.junit.Test;
/**
* Unit test for {@link ImmutableThingFromScratchBuilder}.
*/
public final class ImmutableThingFromScratchBuilderTest {
private static final JsonPointer ATTRIBUTE_PATH = JsonFactory.newPointer("location/longitude");
private static final JsonValue ATTRIBUTE_VALUE = JsonFactory.newValue(42);
private static final JsonPointer PROPERTY_PATH = JsonFactory.newPointer("target_year_1");
private static final JsonValue PROPERTY_VALUE = JsonFactory.newValue(1337);
private ImmutableThingFromScratchBuilder underTest = null;
@Before
public void setUp() {
underTest = ImmutableThingFromScratchBuilder.newInstance();
}
@Test
public void createEmptyThing() {
final Thing thing = underTest.build();
assertThat(thing)
.hasNoId()
.hasNoNamespace()
.hasNoAcl()
.hasNoAttributes()
.hasNoFeatures()
.hasNoRevision()
.hasNoLifecycle()
.hasNoModified();
}
@Test(expected = NullPointerException.class)
public void tryToSetNullAttributes() {
underTest.setAttributes((Attributes) null);
}
@Test
public void setAttributes() {
underTest.setAttributes(ATTRIBUTES);
final Thing thing = underTest.build();
assertThat(thing).hasAttributes(ATTRIBUTES);
}
@Test(expected = NullPointerException.class)
public void tryToSetAttributesFromNullJsonObject() {
underTest.setAttributes((JsonObject) null);
}
@Test
public void setAttributesFromJsonObject() {
final JsonObject attributesJsonObject = ATTRIBUTES.toJson();
underTest.setAttributes(attributesJsonObject);
final Thing thing = underTest.build();
assertThat(thing).hasAttributes(ATTRIBUTES);
}
@Test
public void setAttributesFromSemanticNullJsonObject() {
underTest.setAttributes(JsonFactory.nullObject());
final Thing thing = underTest.build();
assertThat(thing).hasAttributes(ThingsModelFactory.nullAttributes());
}
@Test
public void setAttributesFromSemanticNullJsonString() {
underTest.setAttributes("null");
final Thing thing = underTest.build();
assertThat(thing).hasAttributes(ThingsModelFactory.nullAttributes());
}
@Test
public void setEmptyAttributes() {
underTest.setEmptyAttributes();
final Thing thing = underTest.build();
assertThat(thing).hasAttributes(ThingsModelFactory.emptyAttributes());
}
@Test
public void setAttributesWithEmptyAttributes() {
underTest.setAttributes(ThingsModelFactory.emptyAttributes());
final Thing thing = underTest.build();
assertThat(thing).hasAttributes(ThingsModelFactory.emptyAttributes());
}
@Test
public void setNullAttributes() {
underTest.setNullAttributes();
final Thing thing = underTest.build();
assertThat(thing).hasAttributes(ThingsModelFactory.nullAttributes());
}
@Test(expected = DittoJsonException.class)
public void tryToSetAttributesFromNullJsonString() {
underTest.setAttributes((String) null);
}
@Test(expected = DittoJsonException.class)
public void tryToSetAttributesFromEmptyJsonString() {
underTest.setAttributes("");
}
@Test
public void setAttributesFromJsonString() {
final String attributesJsonString = ATTRIBUTES.toJsonString();
underTest.setAttributes(attributesJsonString);
final Thing thing = underTest.build();
assertThat(thing).hasAttributes(ATTRIBUTES);
}
@Test
public void removeAllAttributes() {
underTest.setAttributes(ATTRIBUTES);
underTest.removeAllAttributes();
final Thing thing = underTest.build();
assertThat(thing).hasNoAttributes();
}
@Test(expected = NullPointerException.class)
public void tryToSetAttributeWithNullPath() {
underTest.setAttribute(null, ATTRIBUTE_VALUE);
}
@Test(expected = NullPointerException.class)
public void tryToSetAttributeWithNullValue() {
underTest.setAttribute(ATTRIBUTE_PATH, null);
}
@Test
public void setAttribute() {
underTest.setAttribute(ATTRIBUTE_PATH, ATTRIBUTE_VALUE);
final Thing thing = underTest.build();
assertThat(thing).hasAttribute(ATTRIBUTE_PATH, ATTRIBUTE_VALUE);
}
@Test(expected = NullPointerException.class)
public void tryToRemoveAttributeWithNullPath() {
underTest.setAttributes(ATTRIBUTES);
underTest.removeAttribute(null);
}
@Test
public void removeAttribute() {
underTest.setAttributes(ATTRIBUTES);
underTest.removeAttribute(ATTRIBUTE_PATH);
final Thing thing = underTest.build();
assertThat(thing).hasNotAttribute(ATTRIBUTE_PATH);
}
@Test(expected = NullPointerException.class)
public void tryToSetNullFeature() {
underTest.setFeature((Feature) null);
}
@Test
public void setFeature() {
underTest.setFeature(FLUX_CAPACITOR);
final Thing thing = underTest.build();
assertThat(thing).hasFeature(FLUX_CAPACITOR);
}
@Test
public void setNullFeature() {
final String nullFeatureId = "schroedinger";
underTest.setFeature(FLUX_CAPACITOR);
underTest.setFeature(ThingsModelFactory.nullFeature(nullFeatureId));
final Thing thing = underTest.build();
assertThat(thing)
.hasFeature(FLUX_CAPACITOR)
.hasFeatureWithId(nullFeatureId);
}
@Test
public void tryToSetFeatureWithNullId() {
assertThatNullPointerException()
.isThrownBy(() -> underTest.setFeature((String) null))
.withMessage("The %s must not be null!", "ID of the Feature")
.withNoCause();
}
@Test
public void setFeatureById() {
underTest.setFeature(FLUX_CAPACITOR_ID);
final Thing thing = underTest.build();
assertThat(thing).hasFeatureWithId(FLUX_CAPACITOR_ID);
}
@Test
public void tryToSetFeatureWithPropertiesWithNullId() {
assertThatNullPointerException()
.isThrownBy(() -> underTest.setFeature(null, TestConstants.Feature.FLUX_CAPACITOR_PROPERTIES))
.withMessage("The %s must not be null!", "ID of the Feature")
.withNoCause();
}
@Test
public void setFeatureWithProperties() {
underTest.setFeature(FLUX_CAPACITOR_ID,
TestConstants.Feature.FLUX_CAPACITOR_DEFINITION, TestConstants.Feature.FLUX_CAPACITOR_PROPERTIES);
final Thing thing = underTest.build();
assertThat(thing).hasFeature(FLUX_CAPACITOR);
}
@Test
public void tryToRemoveFeatureWithNullId() {
assertThatNullPointerException()
.isThrownBy(() -> underTest.removeFeature(null))
.withMessage("The %s must not be null!", "identifier of the feature to be removed")
.withNoCause();
}
@Test
public void removeFeature() {
underTest.setFeature(FLUX_CAPACITOR);
underTest.removeFeature(FLUX_CAPACITOR_ID);
final Thing thing = underTest.build();
assertThat(thing).hasNoFeatures();
}
@Test
public void tryToSetFetFeaturePropertyForNullFeatureId() {
assertThatNullPointerException()
.isThrownBy(() -> underTest.setFeatureProperty(null, PROPERTY_PATH, PROPERTY_VALUE))
.withMessage("The %s must not be null!", "ID of the Feature")
.withNoCause();
}
@Test(expected = NullPointerException.class)
public void tryToSetFeaturePropertyWithNullPath() {
underTest.setFeatureProperty(FLUX_CAPACITOR_ID, null, PROPERTY_VALUE);
}
@Test(expected = NullPointerException.class)
public void tryToSetFeaturePropertyWithNullValue() {
underTest.setFeatureProperty(FLUX_CAPACITOR_ID, PROPERTY_PATH, null);
}
@Test
public void setFeaturePropertyOnEmptyBuilder() {
underTest.setFeatureProperty(FLUX_CAPACITOR_ID, PROPERTY_PATH, PROPERTY_VALUE);
final Thing thing = underTest.build();
assertThat(thing).hasFeatureProperty(FLUX_CAPACITOR_ID, PROPERTY_PATH, PROPERTY_VALUE);
}
@Test
public void setFeaturePropertyOnBuilderWithFeatures() {
underTest.setFeatures(TestConstants.Feature.FEATURES);
underTest.setFeatureProperty(FLUX_CAPACITOR_ID, PROPERTY_PATH, PROPERTY_VALUE);
final Thing thing = underTest.build();
assertThat(thing).hasFeatureProperty(FLUX_CAPACITOR_ID, PROPERTY_PATH, PROPERTY_VALUE);
}
@Test(expected = NullPointerException.class)
public void tryToRemoveFeaturePropertyForNullFeatureId() {
underTest.removeFeatureProperty(null, PROPERTY_PATH);
}
@Test(expected = NullPointerException.class)
public void tryToRemoveFeaturePropertyWithNullPath() {
underTest.removeFeatureProperty(FLUX_CAPACITOR_ID, null);
}
@Test
public void removeFeatureProperty() {
underTest.setFeature(FLUX_CAPACITOR);
underTest.removeFeatureProperty(FLUX_CAPACITOR_ID, PROPERTY_PATH);
final Thing thing = underTest.build();
assertThat(thing).hasNotFeatureProperty(FLUX_CAPACITOR_ID, PROPERTY_PATH);
}
@Test(expected = NullPointerException.class)
public void tryToSetFeaturesWithNullIterable() {
underTest.setFeatures((Iterable<Feature>) null);
}
@Test
public void setFeatures() {
underTest.setFeatures(TestConstants.Feature.FEATURES);
final Thing thing = underTest.build();
assertThat(thing).hasFeatures(TestConstants.Feature.FEATURES);
}
@Test(expected = NullPointerException.class)
public void tryToSetFeaturesFromNullJsonObject() {
underTest.setFeatures((JsonObject) null);
}
@Test
public void setFeaturesFromSemanticNullJsonObject() {
underTest.setFeatures(JsonFactory.nullObject());
final Thing thing = underTest.build();
assertThat(thing).hasFeatures(ThingsModelFactory.nullFeatures());
}
@Test
public void setEmptyFeatures() {
underTest.setEmptyFeatures();
final Thing thing = underTest.build();
assertThat(thing).hasFeatures(ThingsModelFactory.emptyFeatures());
}
@Test
public void setNullFeatures() {
underTest.setNullFeatures();
final Thing thing = underTest.build();
assertThat(thing).hasFeatures(ThingsModelFactory.nullFeatures());
}
@Test
public void setFeaturesWithEmptyFeatures() {
underTest.setFeatures(ThingsModelFactory.emptyFeatures());
final Thing thing = underTest.build();
assertThat(thing).hasFeatures(ThingsModelFactory.emptyFeatures());
}
@Test(expected = DittoJsonException.class)
public void tryToSetFeaturesFromNullJsonString() {
underTest.setFeatures((String) null);
}
@Test(expected = DittoJsonException.class)
public void tryToSetFeaturesFromEmptyJsonString() {
underTest.setFeatures("");
}
@Test
public void setFeaturesFromJsonString() {
final String featuresJsonString = TestConstants.Feature.FEATURES.toJsonString();
underTest.setFeatures(featuresJsonString);
final Thing thing = underTest.build();
assertThat(thing).hasFeatures(TestConstants.Feature.FEATURES);
}
@Test
public void setFeaturesFromSemanticNullJsonString() {
final Features nullFeatures = ThingsModelFactory.nullFeatures();
final String featuresJsonString = nullFeatures.toJsonString();
underTest.setFeatures(featuresJsonString);
final Thing thing = underTest.build();
assertThat(thing).hasFeatures(nullFeatures);
}
@Test
public void removeAllFeatures() {
underTest.setFeatures(TestConstants.Feature.FEATURES);
underTest.removeAllFeatures();
final Thing thing = underTest.build();
assertThat(thing).hasNoFeatures();
}
@Test
public void setLifecycle() {
underTest.setLifecycle(TestConstants.Thing.LIFECYCLE);
final Thing thing = underTest.build();
assertThat(thing).hasLifecycle(TestConstants.Thing.LIFECYCLE);
}
@Test
public void setNullLifecycle() {
underTest.setLifecycle(TestConstants.Thing.LIFECYCLE);
underTest.setLifecycle(null);
final Thing thing = underTest.build();
assertThat(thing).hasNoLifecycle();
}
@Test
public void setRevision() {
underTest.setRevision(TestConstants.Thing.REVISION);
final Thing thing = underTest.build();
assertThat(thing).hasRevision(TestConstants.Thing.REVISION);
}
@Test
public void setNullRevision() {
underTest.setRevision(TestConstants.Thing.REVISION);
underTest.setRevision(null);
final Thing thing = underTest.build();
assertThat(thing).hasNoRevision();
}
@Test
public void setRevisionByNumber() {
underTest.setRevision(TestConstants.Thing.REVISION_NUMBER);
final Thing thing = underTest.build();
assertThat(thing).hasRevision(TestConstants.Thing.REVISION);
}
@Test
public void setModified() {
underTest.setModified(TestConstants.Thing.MODIFIED);
final Thing thing = underTest.build();
assertThat(thing).hasModified(TestConstants.Thing.MODIFIED);
}
@Test
public void setNullModified() {
underTest.setModified(TestConstants.Thing.MODIFIED);
underTest.setModified(null);
final Thing thing = underTest.build();
assertThat(thing).hasNoModified();
}
@Test(expected = NullPointerException.class)
public void tryToSetPermissionsWithNullJsonObject() {
underTest.setPermissions((JsonObject) null);
}
@Test
public void setPermissionsWithSemanticNullJsonObjectOnEmptyBuilder() {
underTest.setPermissions(JsonFactory.nullObject());
final Thing thing = underTest.build();
assertThat(thing).hasAcl(ThingsModelFactory.emptyAcl());
}
@Test
public void setPermissionsWithSemanticNullJsonObjectOnBuilderWithAcl() {
underTest.setPermissions(TestConstants.Thing.ACL);
underTest.setPermissions(JsonFactory.nullObject());
final Thing thing = underTest.build();
assertThat(thing).hasAcl(TestConstants.Thing.ACL);
}
@Test(expected = DittoJsonException.class)
public void tryToSetPermissionsWithNullJsonString() {
underTest.setPermissions((String) null);
}
@Test(expected = DittoJsonException.class)
public void tryToSetPermissionsWithEmptyJsonString() {
underTest.setPermissions("");
}
@Test(expected = DittoJsonException.class)
public void tryToSetPermissionsWithArrayJsonString() {
underTest.setPermissions("[\"a\",\"b\"]");
}
@Test(expected = NullPointerException.class)
public void tryToSetSinglePermissionsWithNullAuthorizationSubject() {
underTest.setPermissions(null, Permission.READ, Permission.WRITE);
}
@Test(expected = NullPointerException.class)
public void tryToSetSinglePermissionsWithNullPermission() {
underTest.setPermissions(TestConstants.Authorization.AUTH_SUBJECT_GRIMES, null);
}
@Test
public void setSinglePermissions() {
underTest.setPermissions(TestConstants.Authorization.AUTH_SUBJECT_GRIMES, Permission.READ, Permission.WRITE);
final Thing thing = underTest.build();
assertThat(thing)
.hasAclEntry(TestConstants.Authorization.AUTH_SUBJECT_GRIMES, Permission.READ, Permission.WRITE);
}
@Test(expected = NullPointerException.class)
public void tryToSetMultiplePermissionsWithNullAuthorizationSubject() {
underTest.setPermissions(null, ThingsModelFactory.allPermissions());
}
@Test(expected = NullPointerException.class)
public void tryToSetMultiplePermissionsWithNullPermissions() {
underTest.setPermissions(TestConstants.Authorization.AUTH_SUBJECT_GRIMES, null);
}
@Test
public void setMultiplePermissions() {
underTest.setPermissions(TestConstants.Authorization.AUTH_SUBJECT_GRIMES, ThingsModelFactory.allPermissions());
final Thing thing = underTest.build();
assertThat(thing)
.hasAclEntry(TestConstants.Authorization.AUTH_SUBJECT_GRIMES, ThingsModelFactory.allPermissions());
}
@Test(expected = NullPointerException.class)
public void tryToSetPermissionsWithNullAclEntry() {
underTest.setPermissions((AclEntry) null);
}
@Test(expected = NullPointerException.class)
public void tryToSetPermissionsWithNullFurtherAclEntries() {
underTest.setPermissions(TestConstants.Authorization.ACL_ENTRY_GRIMES, null);
}
@Test
public void setPermissionsBySingleAclEntries() {
underTest.setPermissions(
TestConstants.Authorization.ACL_ENTRY_GRIMES, TestConstants.Authorization.ACL_ENTRY_OLDMAN);
final Thing thing = underTest.build();
assertThat(thing)
.hasAclEntry(TestConstants.Authorization.ACL_ENTRY_GRIMES)
.hasAclEntry(TestConstants.Authorization.ACL_ENTRY_OLDMAN);
}
@Test(expected = NullPointerException.class)
public void tryToSetPermissionsWithNullAclEntryIterable() {
underTest.setPermissions((Iterable<AclEntry>) null);
}
@Test
public void setPermissionsWithEmptyAclEntryIterable() {
underTest.setPermissions(Collections.emptySet());
final Thing thing = underTest.build();
assertThat(thing).hasAcl(ThingsModelFactory.emptyAcl());
}
@Test
public void setPermissionsFromExistingAcl() {
underTest.setPermissions(TestConstants.Thing.ACL);
final Thing thing = underTest.build();
assertThat(thing).hasAcl(TestConstants.Thing.ACL);
}
@Test(expected = NullPointerException.class)
public void tryToRemovePermissionsOfNullAuthorizationSubject() {
underTest.removePermissionsOf(null);
}
@Test
public void removePermissionsOfAuthorizationSubjectFromEmptyBuilder() {
underTest.removePermissionsOf(TestConstants.Authorization.AUTH_SUBJECT_GRIMES);
final Thing thing = underTest.build();
assertThat(thing).hasNoAcl();
}
@Test
public void removePermissionsOfAuthorizationSubjectFromExistingAcl() {
underTest.setPermissions(TestConstants.Thing.ACL);
underTest.removePermissionsOf(TestConstants.Authorization.AUTH_SUBJECT_OLDMAN);
final Thing thing = underTest.build();
assertThat(thing).hasAcl(
TestConstants.Thing.ACL.removeAllPermissionsOf(TestConstants.Authorization.AUTH_SUBJECT_OLDMAN));
}
@Test
public void removeAllPermissions() {
underTest.setPermissions(TestConstants.Thing.ACL);
underTest.removeAllPermissions();
final Thing thing = underTest.build();
assertThat(thing).hasNoAcl();
}
@Test(expected = ThingIdInvalidException.class)
public void tryToSetEmptyThingId() {
underTest.setId("");
}
@Test(expected = ThingIdInvalidException.class)
public void tryToSetIdWithMissingNamespace() {
underTest.setId("foobar2000");
}
@Test(expected = ThingIdInvalidException.class)
public void tryToSetIdWithInvalidCharactersInNamespace() {
underTest.setId("foo-bar:foobar2000");
}
@Test(expected = ThingIdInvalidException.class)
public void tryToSetIdWithInvalidCharactersInNamespace2() {
underTest.setId("foo.bar%bum:foobar2000");
}
@Test(expected = ThingIdInvalidException.class)
public void tryToSetIdWithNamespaceStartingWithPeriod() {
underTest.setId(".namespace:foobar2000");
}
@Test(expected = ThingIdInvalidException.class)
public void tryToSetIdWithNamespaceEndingWithPeriod() {
underTest.setId("namespace.:foobar2000");
}
@Test(expected = ThingIdInvalidException.class)
public void tryToSetIdWithTwoSubsequentPeriodsInNamespace() {
underTest.setId("namespace..invalid:foobar2000");
}
@Test(expected = ThingIdInvalidException.class)
public void tryToSetIdWithNamespaceWithNumberAfterPeriod() {
underTest.setId("namespace.42:foobar2000");
}
@Test(expected = ThingIdInvalidException.class)
public void tryToSetIdWithEmptyNamespace() { underTest.setId(":foobar2000"); }
@Test
public void setIdWithNamespace() {
final String thingId = "foo.a42:foobar2000";
underTest.setId(thingId);
final Thing thing = underTest.build();
assertThat(thing)
.hasId(thingId)
.hasNamespace("foo.a42");
}
@Test
public void setIdWithNamespace2() {
final String thingId = "ad:foobar2000";
underTest.setId(thingId);
final Thing thing = underTest.build();
assertThat(thing)
.hasId(thingId)
.hasNamespace("ad");
}
@Test
public void setIdWithNamespace3() {
final String thingId = "da23:foobar2000";
underTest.setId(thingId);
final Thing thing = underTest.build();
assertThat(thing)
.hasId(thingId)
.hasNamespace("da23");
}
@Test
public void setGeneratedId() {
underTest.setGeneratedId();
final Thing thing = underTest.build();
assertThat(thing.getId()).isPresent();
}
@Test
public void tryToSetNullFeatureDefinition() {
assertThatNullPointerException()
.isThrownBy(() -> underTest.setFeatureDefinition(FLUX_CAPACITOR_ID, null))
.withMessage("The %s must not be null!", "Feature Definition to be set")
.withNoCause();
}
@Test
public void setFeatureDefinitionCreatesFeatureIfNecessary() {
final FeatureDefinition definition = TestConstants.Feature.FLUX_CAPACITOR_DEFINITION;
final Feature expected = ThingsModelFactory.newFeature(FLUX_CAPACITOR_ID, definition);
underTest.setFeatureDefinition(FLUX_CAPACITOR_ID, definition);
final Thing thing = underTest.build();
assertThat(thing).hasFeature(expected);
}
@Test
public void setFeatureDefinitionExtendsAlreadySetFeature() {
final String featureId = FLUX_CAPACITOR_ID;
final FeatureDefinition featureDefinition = TestConstants.Feature.FLUX_CAPACITOR_DEFINITION;
final FeatureProperties featureProperties = FLUX_CAPACITOR_PROPERTIES;
final Feature featureWithoutDefinition = ThingsModelFactory.newFeature(featureId, featureProperties);
final Feature expected = ThingsModelFactory.newFeature(featureId, featureDefinition, featureProperties);
underTest.setFeature(featureWithoutDefinition);
underTest.setFeatureDefinition(featureId, featureDefinition);
final Thing thing = underTest.build();
assertThat(thing).hasFeature(expected);
}
@Test
public void removeFeatureDefinitionFromUnknownFeatureIdDoesNothing() {
underTest.removeFeatureDefinition(FLUX_CAPACITOR_ID);
final Thing thing = underTest.build();
assertThat(thing).hasNoFeatures();
}
@Test
public void removeFeatureDefinitionWorksAsExpected() {
final String featureId = FLUX_CAPACITOR_ID;
final FeatureDefinition featureDefinition = TestConstants.Feature.FLUX_CAPACITOR_DEFINITION;
final FeatureProperties featureProperties = FLUX_CAPACITOR_PROPERTIES;
final Feature expected = ThingsModelFactory.newFeature(featureId, featureProperties);
underTest.setFeature(ThingsModelFactory.newFeature(featureId, featureDefinition, featureProperties));
underTest.removeFeatureDefinition(featureId);
final Thing thing = underTest.build();
assertThat(thing).hasFeature(expected);
}
}