/
DistributionSetManagementTest.java
1338 lines (1105 loc) · 72.8 KB
/
DistributionSetManagementTest.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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.repository.jpa.management;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jakarta.validation.ConstraintViolationException;
import org.apache.commons.lang3.RandomStringUtils;
import org.assertj.core.api.Condition;
import org.eclipse.hawkbit.repository.DistributionSetManagement;
import org.eclipse.hawkbit.repository.RepositoryProperties;
import org.eclipse.hawkbit.repository.builder.DistributionSetCreate;
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetCreatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetTagCreatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.DistributionSetUpdatedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.SoftwareModuleCreatedEvent;
import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.exception.EntityReadOnlyException;
import org.eclipse.hawkbit.repository.exception.IncompleteDistributionSetException;
import org.eclipse.hawkbit.repository.exception.InvalidDistributionSetException;
import org.eclipse.hawkbit.repository.exception.LockedException;
import org.eclipse.hawkbit.repository.exception.UnsupportedSoftwareModuleForThisDistributionSetException;
import org.eclipse.hawkbit.repository.jpa.AbstractJpaIntegrationTest;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSetMetadata;
import org.eclipse.hawkbit.repository.model.Action.Status;
import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.DistributionSetFilter;
import org.eclipse.hawkbit.repository.model.DistributionSetFilter.DistributionSetFilterBuilder;
import org.eclipse.hawkbit.repository.model.DistributionSetInvalidation;
import org.eclipse.hawkbit.repository.model.DistributionSetInvalidation.CancelationType;
import org.eclipse.hawkbit.repository.model.DistributionSetMetadata;
import org.eclipse.hawkbit.repository.model.DistributionSetTag;
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.MetaData;
import org.eclipse.hawkbit.repository.model.NamedEntity;
import org.eclipse.hawkbit.repository.model.NamedVersionedEntity;
import org.eclipse.hawkbit.repository.model.Rollout;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
import org.eclipse.hawkbit.repository.model.Statistic;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.test.matcher.Expect;
import org.eclipse.hawkbit.repository.test.matcher.ExpectEvents;
import org.eclipse.hawkbit.repository.test.util.WithUser;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import io.qameta.allure.Description;
import io.qameta.allure.Feature;
import io.qameta.allure.Step;
import io.qameta.allure.Story;
/**
* {@link DistributionSetManagement} tests.
*
*/
@Feature("Component Tests - Repository")
@Story("DistributionSet Management")
class DistributionSetManagementTest extends AbstractJpaIntegrationTest {
public static final String TAG1_NAME = "Tag1";
@Test
@Description("Verifies that management get access react as specified on calls for non existing entities by means "
+ "of Optional not present.")
@ExpectEvents({ @Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = SoftwareModuleCreatedEvent.class, count = 3) })
void nonExistingEntityAccessReturnsNotPresent() {
final DistributionSet set = testdataFactory.createDistributionSet();
assertThat(distributionSetManagement.get(NOT_EXIST_IDL)).isNotPresent();
assertThat(distributionSetManagement.getWithDetails(NOT_EXIST_IDL)).isNotPresent();
assertThat(distributionSetManagement.getByNameAndVersion(NOT_EXIST_ID, NOT_EXIST_ID)).isNotPresent();
assertThat(distributionSetManagement.getMetaDataByDistributionSetId(set.getId(), NOT_EXIST_ID)).isNotPresent();
}
@Test
@Description("Verifies that management queries react as specfied on calls for non existing entities "
+ " by means of throwing EntityNotFoundException.")
@ExpectEvents({ @Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = DistributionSetTagCreatedEvent.class, count = 1),
@Expect(type = SoftwareModuleCreatedEvent.class, count = 4) })
void entityQueriesReferringToNotExistingEntitiesThrowsException() {
final DistributionSet set = testdataFactory.createDistributionSet();
final DistributionSetTag dsTag = testdataFactory.createDistributionSetTags(1).get(0);
final SoftwareModule module = testdataFactory.createSoftwareModuleApp();
verifyThrownExceptionBy(
() -> distributionSetManagement.assignSoftwareModules(NOT_EXIST_IDL, singletonList(module.getId())),
"DistributionSet");
verifyThrownExceptionBy(
() -> distributionSetManagement.assignSoftwareModules(set.getId(), singletonList(NOT_EXIST_IDL)),
"SoftwareModule");
verifyThrownExceptionBy(() -> distributionSetManagement.countByTypeId(NOT_EXIST_IDL), "DistributionSet");
verifyThrownExceptionBy(() -> distributionSetManagement.unassignSoftwareModule(NOT_EXIST_IDL, module.getId()),
"DistributionSet");
verifyThrownExceptionBy(() -> distributionSetManagement.unassignSoftwareModule(set.getId(), NOT_EXIST_IDL),
"SoftwareModule");
verifyThrownExceptionBy(() -> distributionSetManagement.assignTag(singletonList(set.getId()), NOT_EXIST_IDL),
"DistributionSetTag");
verifyThrownExceptionBy(() -> distributionSetManagement.assignTag(singletonList(NOT_EXIST_IDL), dsTag.getId()),
"DistributionSet");
verifyThrownExceptionBy(() -> distributionSetManagement.findByTag(PAGE, NOT_EXIST_IDL), "DistributionSetTag");
verifyThrownExceptionBy(() -> distributionSetManagement.findByRsqlAndTag(PAGE, "name==*", NOT_EXIST_IDL),
"DistributionSetTag");
verifyThrownExceptionBy(
() -> distributionSetManagement.toggleTagAssignment(singletonList(NOT_EXIST_IDL), dsTag.getName()),
"DistributionSet");
verifyThrownExceptionBy(
() -> distributionSetManagement.toggleTagAssignment(singletonList(set.getId()), NOT_EXIST_ID),
"DistributionSetTag");
verifyThrownExceptionBy(() -> distributionSetManagement.unassignTag(set.getId(), NOT_EXIST_IDL),
"DistributionSetTag");
verifyThrownExceptionBy(() -> distributionSetManagement.unassignTag(NOT_EXIST_IDL, dsTag.getId()),
"DistributionSet");
verifyThrownExceptionBy(
() -> distributionSetManagement
.create(entityFactory.distributionSet().create().name("xxx").type(NOT_EXIST_ID)),
"DistributionSetType");
verifyThrownExceptionBy(() -> distributionSetManagement.createMetaData(NOT_EXIST_IDL,
singletonList(entityFactory.generateDsMetadata("123", "123"))), "DistributionSet");
verifyThrownExceptionBy(() -> distributionSetManagement.delete(singletonList(NOT_EXIST_IDL)),
"DistributionSet");
verifyThrownExceptionBy(() -> distributionSetManagement.delete(NOT_EXIST_IDL), "DistributionSet");
verifyThrownExceptionBy(() -> distributionSetManagement.deleteMetaData(NOT_EXIST_IDL, "xxx"),
"DistributionSet");
verifyThrownExceptionBy(() -> distributionSetManagement.deleteMetaData(set.getId(), NOT_EXIST_ID),
"DistributionSetMetadata");
verifyThrownExceptionBy(() -> distributionSetManagement.getByAction(NOT_EXIST_IDL), "Action");
verifyThrownExceptionBy(() -> distributionSetManagement.getMetaDataByDistributionSetId(NOT_EXIST_IDL, "xxx"),
"DistributionSet");
verifyThrownExceptionBy(() -> distributionSetManagement.findMetaDataByDistributionSetId(PAGE, NOT_EXIST_IDL),
"DistributionSet");
verifyThrownExceptionBy(
() -> distributionSetManagement.findMetaDataByDistributionSetIdAndRsql(PAGE, NOT_EXIST_IDL, "name==*"),
"DistributionSet");
assertThatThrownBy(() -> distributionSetManagement.isInUse(NOT_EXIST_IDL))
.isInstanceOf(EntityNotFoundException.class).hasMessageContaining(NOT_EXIST_ID)
.hasMessageContaining("DistributionSet");
verifyThrownExceptionBy(
() -> distributionSetManagement.update(entityFactory.distributionSet().update(NOT_EXIST_IDL)),
"DistributionSet");
verifyThrownExceptionBy(() -> distributionSetManagement.updateMetaData(NOT_EXIST_IDL,
entityFactory.generateDsMetadata("xxx", "xxx")), "DistributionSet");
verifyThrownExceptionBy(() -> distributionSetManagement.updateMetaData(set.getId(),
entityFactory.generateDsMetadata(NOT_EXIST_ID, "xxx")), "DistributionSetMetadata");
verifyThrownExceptionBy(() -> distributionSetManagement.getOrElseThrowException(NOT_EXIST_IDL),
"DistributionSet");
verifyThrownExceptionBy(() -> distributionSetManagement.getValidAndComplete(NOT_EXIST_IDL), "DistributionSet");
verifyThrownExceptionBy(() -> distributionSetManagement.getValid(NOT_EXIST_IDL), "DistributionSet");
}
@Test
@Description("Verify that a DistributionSet with invalid properties cannot be created or updated")
@ExpectEvents({ @Expect(type = DistributionSetCreatedEvent.class, count = 1),
@Expect(type = SoftwareModuleCreatedEvent.class, count = 3),
@Expect(type = DistributionSetUpdatedEvent.class) })
void createAndUpdateDistributionSetWithInvalidFields() {
final DistributionSet set = testdataFactory.createDistributionSet();
createAndUpdateDistributionSetWithInvalidDescription(set);
createAndUpdateDistributionSetWithInvalidName(set);
createAndUpdateDistributionSetWithInvalidVersion(set);
}
@Step
private void createAndUpdateDistributionSetWithInvalidDescription(final DistributionSet set) {
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too long description should not be created")
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet().create().name("a")
.version("a").description(RandomStringUtils.randomAlphanumeric(513))));
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with invalid description should not be created")
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet().create().name("a")
.version("a").description(INVALID_TEXT_HTML)));
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too long description should not be updated")
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId())
.description(RandomStringUtils.randomAlphanumeric(513))));
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with invalid characters should not be updated").isThrownBy(() -> distributionSetManagement
.update(entityFactory.distributionSet().update(set.getId()).description(INVALID_TEXT_HTML)));
}
@Step
private void createAndUpdateDistributionSetWithInvalidName(final DistributionSet set) {
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too long name should not be created")
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet().create().version("a")
.name(RandomStringUtils.randomAlphanumeric(NamedEntity.NAME_MAX_SIZE + 1))));
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too short name should not be created").isThrownBy(() -> distributionSetManagement
.create(entityFactory.distributionSet().create().version("a").name("")));
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with invalid characters in name should not be created")
.isThrownBy(() -> distributionSetManagement
.create(entityFactory.distributionSet().create().version("a").name(INVALID_TEXT_HTML)));
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too long name should not be updated")
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId())
.name(RandomStringUtils.randomAlphanumeric(NamedEntity.NAME_MAX_SIZE + 1))));
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with invalid characters should not be updated").isThrownBy(() -> distributionSetManagement
.update(entityFactory.distributionSet().update(set.getId()).name(INVALID_TEXT_HTML)));
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too short name should not be updated").isThrownBy(() -> distributionSetManagement
.update(entityFactory.distributionSet().update(set.getId()).name("")));
}
@Step
private void createAndUpdateDistributionSetWithInvalidVersion(final DistributionSet set) {
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too long version should not be created")
.isThrownBy(() -> distributionSetManagement.create(entityFactory.distributionSet().create().name("a")
.version(RandomStringUtils.randomAlphanumeric(NamedVersionedEntity.VERSION_MAX_SIZE + 1))));
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too short version should not be created").isThrownBy(() -> distributionSetManagement
.create(entityFactory.distributionSet().create().name("a").version("")));
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too long version should not be updated")
.isThrownBy(() -> distributionSetManagement.update(entityFactory.distributionSet().update(set.getId())
.version(RandomStringUtils.randomAlphanumeric(NamedVersionedEntity.VERSION_MAX_SIZE + 1))));
assertThatExceptionOfType(ConstraintViolationException.class)
.as("entity with too short version should not be updated").isThrownBy(() -> distributionSetManagement
.update(entityFactory.distributionSet().update(set.getId()).version("")));
}
@Test
@Description("Ensures that it is not possible to create a DS that already exists (unique constraint is on name,version for DS).")
void createDuplicateDistributionSetsFailsWithException() {
testdataFactory.createDistributionSet("a");
assertThatThrownBy(() -> testdataFactory.createDistributionSet("a"))
.isInstanceOf(EntityAlreadyExistsException.class);
}
@Test
@Description("Verifies that a DS is of default type if not specified explicitly at creation time.")
void createDistributionSetWithImplicitType() {
final DistributionSet set = distributionSetManagement
.create(entityFactory.distributionSet().create().name("newtypesoft").version("1"));
assertThat(set.getType()).as("Type should be equal to default type of tenant")
.isEqualTo(systemManagement.getTenantMetadata().getDefaultDsType());
}
@Test
@Description("Verifies that a DS cannot be created if another DS with same name and version exists.")
void createDistributionSetWithDuplicateNameAndVersionFails() {
distributionSetManagement.create(entityFactory.distributionSet().create().name("newtypesoft").version("1"));
assertThatExceptionOfType(EntityAlreadyExistsException.class).isThrownBy(() -> distributionSetManagement
.create(entityFactory.distributionSet().create().name("newtypesoft").version("1")));
}
@Test
@Description("Verifies that multiple DS are of default type if not specified explicitly at creation time.")
void createMultipleDistributionSetsWithImplicitType() {
final List<DistributionSetCreate> creates = new ArrayList<>(10);
for (int i = 0; i < 10; i++) {
creates.add(entityFactory.distributionSet().create().name("newtypesoft" + i).version("1" + i));
}
final List<DistributionSet> sets = distributionSetManagement.create(creates);
assertThat(sets).as("Type should be equal to default type of tenant").are(new Condition<DistributionSet>() {
@Override
public boolean matches(final DistributionSet value) {
return value.getType().equals(systemManagement.getTenantMetadata().getDefaultDsType());
}
});
}
@Test
@Description("Checks that metadata for a distribution set can be created.")
void createDistributionSetMetadata() {
final String knownKey = "dsMetaKnownKey";
final String knownValue = "dsMetaKnownValue";
final DistributionSet ds = testdataFactory.createDistributionSet("testDs");
final DistributionSetMetadata metadata = new JpaDistributionSetMetadata(knownKey, ds, knownValue);
final JpaDistributionSetMetadata createdMetadata = (JpaDistributionSetMetadata) createDistributionSetMetadata(
ds.getId(), metadata);
assertThat(createdMetadata).isNotNull();
assertThat(createdMetadata.getId().getKey()).isEqualTo(knownKey);
assertThat(createdMetadata.getDistributionSet().getId()).isEqualTo(ds.getId());
assertThat(createdMetadata.getValue()).isEqualTo(knownValue);
}
@Test
@Description("Verifies the enforcement of the metadata quota per distribution set.")
void createDistributionSetMetadataUntilQuotaIsExceeded() {
// add meta data one by one
final DistributionSet ds1 = testdataFactory.createDistributionSet("ds1");
final int maxMetaData = quotaManagement.getMaxMetaDataEntriesPerDistributionSet();
for (int i = 0; i < maxMetaData; ++i) {
assertThat((JpaDistributionSetMetadata) createDistributionSetMetadata(ds1.getId(),
new JpaDistributionSetMetadata("k" + i, ds1, "v" + i))).isNotNull();
}
// quota exceeded
assertThatExceptionOfType(AssignmentQuotaExceededException.class)
.isThrownBy(() -> createDistributionSetMetadata(ds1.getId(), createDistributionSetMetadata(ds1.getId(),
new JpaDistributionSetMetadata("k" + maxMetaData, ds1, "v" + maxMetaData))));
// add multiple meta data entries at once
final DistributionSet ds2 = testdataFactory.createDistributionSet("ds2");
final List<MetaData> metaData2 = new ArrayList<>();
for (int i = 0; i < maxMetaData + 1; ++i) {
metaData2.add(new JpaDistributionSetMetadata("k" + i, ds2, "v" + i));
}
// verify quota is exceeded
assertThatExceptionOfType(AssignmentQuotaExceededException.class)
.isThrownBy(() -> createDistributionSetMetadata(ds2.getId(), metaData2));
// add some meta data entries
final DistributionSet ds3 = testdataFactory.createDistributionSet("ds3");
final int firstHalf = Math.round((maxMetaData) / 2.f);
for (int i = 0; i < firstHalf; ++i) {
createDistributionSetMetadata(ds3.getId(), new JpaDistributionSetMetadata("k" + i, ds3, "v" + i));
}
// add too many data entries
final int secondHalf = maxMetaData - firstHalf;
final List<MetaData> metaData3 = new ArrayList<>();
for (int i = 0; i < secondHalf + 1; ++i) {
metaData3.add(new JpaDistributionSetMetadata("kk" + i, ds3, "vv" + i));
}
// verify quota is exceeded
assertThatExceptionOfType(AssignmentQuotaExceededException.class)
.isThrownBy(() -> createDistributionSetMetadata(ds3.getId(), metaData3));
}
@Test
@Description("Ensures that distribution sets can assigned and unassigned to a distribution set tag.")
void assignAndUnassignDistributionSetToTag() {
final List<Long> assignDS = new ArrayList<>(4);
for (int i = 0; i < 4; i++) {
assignDS.add(testdataFactory.createDistributionSet("DS" + i, "1.0", Collections.emptyList()).getId());
}
final DistributionSetTag tag = distributionSetTagManagement
.create(entityFactory.tag().create().name(TAG1_NAME));
final List<DistributionSet> assignedDS = distributionSetManagement.assignTag(assignDS, tag.getId());
assertThat(assignedDS.size()).as("assigned ds has wrong size").isEqualTo(4);
assignedDS.stream().map(c -> (JpaDistributionSet) c)
.forEach(ds -> assertThat(ds.getTags().size()).as("ds has wrong tag size").isEqualTo(1));
final DistributionSetTag findDistributionSetTag = getOrThrow(distributionSetTagManagement.getByName(TAG1_NAME));
assertThat(assignedDS.size()).as("assigned ds has wrong size")
.isEqualTo(distributionSetManagement.findByTag(PAGE, tag.getId()).getNumberOfElements());
final JpaDistributionSet unAssignDS = (JpaDistributionSet) distributionSetManagement
.unassignTag(assignDS.get(0), findDistributionSetTag.getId());
assertThat(unAssignDS.getId()).as("unassigned ds is wrong").isEqualTo(assignDS.get(0));
assertThat(unAssignDS.getTags().size()).as("unassigned ds has wrong tag size").isZero();
assertThat(distributionSetTagManagement.getByName(TAG1_NAME)).isPresent();
assertThat(distributionSetManagement.findByTag(PAGE, tag.getId()).getNumberOfElements())
.as("ds tag ds has wrong ds size").isEqualTo(3);
assertThat(distributionSetManagement.findByRsqlAndTag(PAGE, "name==" + unAssignDS.getName(), tag.getId())
.getNumberOfElements()).as("ds tag ds has wrong ds size").isZero();
assertThat(distributionSetManagement.findByRsqlAndTag(PAGE, "name!=" + unAssignDS.getName(), tag.getId())
.getNumberOfElements()).as("ds tag ds has wrong ds size").isEqualTo(3);
}
@Test
@Description("Ensures that updates concerning the internal software structure of a DS are not possible if the DS is already assigned.")
void updateDistributionSetForbiddenWithIllegalUpdate() {
// prepare data
final Target target = testdataFactory.createTarget();
DistributionSet ds = testdataFactory.createDistributionSet("ds-1");
final SoftwareModule ah2 = testdataFactory.createSoftwareModuleApp();
final SoftwareModule os2 = testdataFactory.createSoftwareModuleOs();
// update is allowed as it is still not assigned to a target
ds = distributionSetManagement.assignSoftwareModules(ds.getId(), Set.of(ah2.getId()));
// assign target
assignDistributionSet(ds.getId(), target.getControllerId());
ds = getOrThrow(distributionSetManagement.getWithDetails(ds.getId()));
final Long dsId = ds.getId();
// not allowed as it is assigned now
assertThatThrownBy(() -> distributionSetManagement.assignSoftwareModules(dsId, Set.of(os2.getId())))
.isInstanceOf(EntityReadOnlyException.class);
// not allowed as it is assigned now
final Long appId = getOrThrow(ds.findFirstModuleByType(appType)).getId();
assertThatThrownBy(() -> distributionSetManagement.unassignSoftwareModule(dsId, appId))
.isInstanceOf(EntityReadOnlyException.class);
}
@Test
@Description("Ensures that it is not possible to add a software module that is not defined of the DS's type.")
void updateDistributionSetUnsupportedModuleFails() {
final DistributionSet set = distributionSetManagement
.create(entityFactory
.distributionSet().create().name("agent-hub2").version(
"1.0.5")
.type(distributionSetTypeManagement.create(entityFactory.distributionSetType().create()
.key("test").name("test").mandatory(singletonList(osType.getId()))).getKey()));
final SoftwareModule module = softwareModuleManagement.create(
entityFactory.softwareModule().create().name("agent-hub2").version("1.0.5").type(appType.getKey()));
// update data
assertThatThrownBy(
() -> distributionSetManagement.assignSoftwareModules(set.getId(), Set.of(module.getId())))
.isInstanceOf(UnsupportedSoftwareModuleForThisDistributionSetException.class);
}
@Test
@Description("Legal updates of a DS, e.g. name or description and module addition, removal while still unassigned.")
void updateDistributionSet() {
// prepare data
DistributionSet ds = testdataFactory.createDistributionSet("");
final SoftwareModule os = testdataFactory.createSoftwareModuleOs();
// update data
// legal update of module addition
distributionSetManagement.assignSoftwareModules(ds.getId(), Set.of(os.getId()));
ds = getOrThrow(distributionSetManagement.getWithDetails(ds.getId()));
assertThat(getOrThrow(ds.findFirstModuleByType(osType))).isEqualTo(os);
// legal update of module removal
distributionSetManagement.unassignSoftwareModule(ds.getId(),
getOrThrow(ds.findFirstModuleByType(appType)).getId());
ds = getOrThrow(distributionSetManagement.getWithDetails(ds.getId()));
assertThat(ds.findFirstModuleByType(appType)).isNotPresent();
// Update description
distributionSetManagement.update(entityFactory.distributionSet().update(ds.getId()).name("a new name")
.description("a new description").version("a new version").requiredMigrationStep(true));
ds = getOrThrow(distributionSetManagement.getWithDetails(ds.getId()));
assertThat(ds.getDescription()).isEqualTo("a new description");
assertThat(ds.getName()).isEqualTo("a new name");
assertThat(ds.getVersion()).isEqualTo("a new version");
assertThat(ds.isRequiredMigrationStep()).isTrue();
}
@Test
@Description("Verifies that an exception is thrown when trying to update an invalid distribution set")
void updateInvalidDistributionSet() {
final DistributionSet distributionSet = testdataFactory.createAndInvalidateDistributionSet();
assertThatExceptionOfType(InvalidDistributionSetException.class)
.as("Invalid distributionSet should throw an exception").isThrownBy(() -> distributionSetManagement
.update(entityFactory.distributionSet().update(distributionSet.getId()).name("new_name")));
}
@Test
@Description("Verifies the enforcement of the software module quota per distribution set.")
void assignSoftwareModulesUntilQuotaIsExceeded() {
// create some software modules
final int maxModules = quotaManagement.getMaxSoftwareModulesPerDistributionSet();
final List<Long> modules = new ArrayList<>();
for (int i = 0; i < maxModules + 1; ++i) {
modules.add(testdataFactory.createSoftwareModuleApp("sm" + i).getId());
}
// assign software modules one by one
final DistributionSet ds1 = testdataFactory.createDistributionSetWithNoSoftwareModules("ds1", "1.0");
for (int i = 0; i < maxModules; ++i) {
distributionSetManagement.assignSoftwareModules(ds1.getId(), singletonList(modules.get(i)));
}
// add one more to cause the quota to be exceeded
assertThatExceptionOfType(AssignmentQuotaExceededException.class).isThrownBy(() -> distributionSetManagement
.assignSoftwareModules(ds1.getId(), singletonList(modules.get(maxModules))));
// assign all software modules at once
final DistributionSet ds2 = testdataFactory.createDistributionSetWithNoSoftwareModules("ds2", "1.0");
// verify quota is exceeded
assertThatExceptionOfType(AssignmentQuotaExceededException.class)
.isThrownBy(() -> distributionSetManagement.assignSoftwareModules(ds2.getId(), modules));
// assign some software modules
final DistributionSet ds3 = testdataFactory.createDistributionSetWithNoSoftwareModules("ds3", "1.0");
final int firstHalf = Math.round((maxModules) / 2.f);
for (int i = 0; i < firstHalf; ++i) {
distributionSetManagement.assignSoftwareModules(ds3.getId(), singletonList(modules.get(i)));
}
// assign the remaining modules to cause the quota to be exceeded
assertThatExceptionOfType(AssignmentQuotaExceededException.class).isThrownBy(() -> distributionSetManagement
.assignSoftwareModules(ds3.getId(), modules.subList(firstHalf, modules.size())));
}
@Test
@Description("Verifies that an exception is thrown when trying to assign software modules to an invalidated distribution set.")
void verifyAssignSoftwareModulesToInvalidDistributionSet() {
final DistributionSet distributionSet = testdataFactory.createAndInvalidateDistributionSet();
final SoftwareModule softwareModule = testdataFactory.createSoftwareModuleOs();
assertThatExceptionOfType(InvalidDistributionSetException.class)
.as("Invalid distributionSet should throw an exception").isThrownBy(() -> distributionSetManagement
.assignSoftwareModules(distributionSet.getId(), singletonList(softwareModule.getId())));
}
@Test
@Description("Verifies that an exception is thrown when trying to unassign a software module from an invalidated distribution set.")
void verifyUnassignSoftwareModulesToInvalidDistributionSet() {
final DistributionSet distributionSet = testdataFactory.createDistributionSet();
final SoftwareModule softwareModule = testdataFactory.createSoftwareModuleOs();
distributionSetManagement.assignSoftwareModules(distributionSet.getId(), singletonList(softwareModule.getId()));
distributionSetInvalidationManagement.invalidateDistributionSet(
new DistributionSetInvalidation(singletonList(distributionSet.getId()), CancelationType.NONE, false));
assertThatExceptionOfType(InvalidDistributionSetException.class)
.as("Invalid distributionSet should throw an exception").isThrownBy(() -> distributionSetManagement
.unassignSoftwareModule(distributionSet.getId(), softwareModule.getId()));
}
@Test
@WithUser(allSpPermissions = true)
@Description("Checks that metadata for a distribution set can be updated.")
void updateDistributionSetMetadata() {
final String knownKey = "myKnownKey";
final String knownValue = "myKnownValue";
final String knownUpdateValue = "myNewUpdatedValue";
// create a DS
final DistributionSet ds = testdataFactory.createDistributionSet("testDs");
// initial opt lock revision must be zero
assertThat(ds.getOptLockRevision()).isEqualTo(1);
// create an DS meta data entry
createDistributionSetMetadata(ds.getId(), new JpaDistributionSetMetadata(knownKey, ds, knownValue));
final DistributionSet changedLockRevisionDS = getOrThrow(distributionSetManagement.get(ds.getId()));
assertThat(changedLockRevisionDS.getOptLockRevision()).isEqualTo(2);
// update the DS metadata
final JpaDistributionSetMetadata updated = (JpaDistributionSetMetadata) distributionSetManagement
.updateMetaData(ds.getId(), entityFactory.generateDsMetadata(knownKey, knownUpdateValue));
// we are updating the sw metadata so also modifying the base software
// module so opt lock revision must be three
final DistributionSet reloadedDS = getOrThrow(distributionSetManagement.get(ds.getId()));
assertThat(reloadedDS.getOptLockRevision()).isEqualTo(3);
assertThat(reloadedDS.getLastModifiedAt()).isPositive();
// verify updated meta data contains the updated value
assertThat(updated).isNotNull();
assertThat(updated.getValue()).isEqualTo(knownUpdateValue);
assertThat(updated.getId().getKey()).isEqualTo(knownKey);
assertThat(updated.getDistributionSet().getId()).isEqualTo(ds.getId());
}
@Test
@Description("Tests that a DS queue is possible where the result is ordered by the target assignment, i.e. assigned first in the list.")
void findDistributionSetsAllOrderedByLinkTarget() {
final List<DistributionSet> buildDistributionSets = testdataFactory.createDistributionSets("dsOrder", 10);
final List<Target> buildTargetFixtures = testdataFactory.createTargets(5, "tOrder", "someDesc");
final Iterator<DistributionSet> dsIterator = buildDistributionSets.iterator();
final Iterator<Target> tIterator = buildTargetFixtures.iterator();
final DistributionSet dsFirst = dsIterator.next();
final DistributionSet dsSecond = dsIterator.next();
final DistributionSet dsThree = dsIterator.next();
final DistributionSet dsFour = dsIterator.next();
final Target tFirst = tIterator.next();
final Target tSecond = tIterator.next();
// set assigned
assignDistributionSet(dsSecond.getId(), tSecond.getControllerId());
implicitLock(dsSecond);
assignDistributionSet(dsThree.getId(), tFirst.getControllerId());
implicitLock(dsThree);
// set installed
testdataFactory.sendUpdateActionStatusToTargets(Collections.singleton(tSecond), Status.FINISHED,
singletonList("some message"));
assignDistributionSet(dsFour.getId(), tSecond.getControllerId());
implicitLock(dsFour);
final DistributionSetFilter distributionSetFilter =
DistributionSetFilter.builder()
.isDeleted(false)
.isComplete(true)
.selectDSWithNoTag(Boolean.FALSE).build();
// target first only has an assigned DS-three so check order correct
final List<DistributionSet> tFirstPin = distributionSetManagement
.findByDistributionSetFilterOrderByLinkedTarget(PAGE, distributionSetFilter, tFirst.getControllerId())
.getContent();
assertThat(tFirstPin).hasSize(10);
// assigned
assertThat(tFirstPin.get(0)).isEqualTo(dsThree);
// remaining id:ASC
assertThat(tFirstPin.get(1)).isEqualTo(dsFirst);
assertThat(tFirstPin.get(2)).isEqualTo(dsSecond);
assertThat(tFirstPin.get(3)).isEqualTo(dsFour);
// target second has installed DS-2 and assigned DS-4 so check order
// correct
final List<DistributionSet> tSecondPin = distributionSetManagement
.findByDistributionSetFilterOrderByLinkedTarget(PAGE, distributionSetFilter, tSecond.getControllerId())
.getContent();
assertThat(tSecondPin).hasSize(10);
// installed
assertThat(tSecondPin.get(0)).isEqualTo(dsSecond);
// assigned
assertThat(tSecondPin.get(1)).isEqualTo(dsFour);
// remaining id:ASC
assertThat(tSecondPin.get(2)).isEqualTo(dsFirst);
assertThat(tSecondPin.get(3)).isEqualTo(dsThree);
// target second has installed DS-2 and assigned DS-4 so check order
// correct
final List<DistributionSet> tSecondPinOrderedByName = distributionSetManagement
.findByDistributionSetFilterOrderByLinkedTarget(
PageRequest.of(0, 500, Sort.by(Direction.DESC, "version")), distributionSetFilter,
tSecond.getControllerId())
.getContent();
assertThat(tSecondPinOrderedByName).hasSize(10);
// installed
assertThat(tSecondPinOrderedByName.get(0)).isEqualTo(buildDistributionSets.get(1));
// assigned
assertThat(tSecondPinOrderedByName.get(1)).isEqualTo(buildDistributionSets.get(3));
// remaining version:DESC
assertThat(tSecondPinOrderedByName.get(2)).isEqualTo(buildDistributionSets.get(9));
assertThat(tSecondPinOrderedByName.get(3)).isEqualTo(buildDistributionSets.get(8));
assertThat(tSecondPinOrderedByName.get(4)).isEqualTo(buildDistributionSets.get(7));
assertThat(tSecondPinOrderedByName.get(5)).isEqualTo(buildDistributionSets.get(6));
assertThat(tSecondPinOrderedByName.get(6)).isEqualTo(buildDistributionSets.get(5));
assertThat(tSecondPinOrderedByName.get(7)).isEqualTo(buildDistributionSets.get(4));
assertThat(tSecondPinOrderedByName.get(8)).isEqualTo(buildDistributionSets.get(2));
assertThat(tSecondPinOrderedByName.get(9)).isEqualTo(buildDistributionSets.get(0));
}
@Test
@Description("searches for distribution sets based on the various filter options, e.g. name, version, desc., tags.")
void searchDistributionSetsOnFilters() {
DistributionSetTag dsTagA = distributionSetTagManagement
.create(entityFactory.tag().create().name("DistributionSetTag-A"));
final DistributionSetTag dsTagB = distributionSetTagManagement
.create(entityFactory.tag().create().name("DistributionSetTag-B"));
final DistributionSetTag dsTagC = distributionSetTagManagement
.create(entityFactory.tag().create().name("DistributionSetTag-C"));
distributionSetTagManagement.create(entityFactory.tag().create().name("DistributionSetTag-D"));
List<DistributionSet> dsGroup1 = testdataFactory.createDistributionSets("", 5);
final String dsGroup2Prefix = "test";
List<DistributionSet> dsGroup2 = testdataFactory.createDistributionSets(dsGroup2Prefix, 5);
DistributionSet dsDeleted = testdataFactory.createDistributionSet("testDeleted");
final DistributionSet dsInComplete = distributionSetManagement.create(entityFactory.distributionSet().create()
.name("notcomplete").version("1").type(standardDsType.getKey()));
DistributionSetType newType = distributionSetTypeManagement
.create(entityFactory.distributionSetType().create().key("foo").name("bar").description("test"));
distributionSetTypeManagement.assignMandatorySoftwareModuleTypes(newType.getId(),
singletonList(osType.getId()));
newType = distributionSetTypeManagement.assignOptionalSoftwareModuleTypes(newType.getId(),
Arrays.asList(appType.getId(), runtimeType.getId()));
final DistributionSet dsNewType = distributionSetManagement.create(
entityFactory.distributionSet().create().name("newtype").version("1").type(newType.getKey()).modules(
dsDeleted.getModules().stream().map(SoftwareModule::getId).collect(Collectors.toList())));
assignDistributionSet(dsDeleted, testdataFactory.createTargets(5));
distributionSetManagement.delete(dsDeleted.getId());
dsDeleted = getOrThrow(distributionSetManagement.get(dsDeleted.getId()));
dsGroup1 = toggleTagAssignment(dsGroup1, dsTagA).getAssignedEntity();
dsTagA = getOrThrow(distributionSetTagRepository.findByNameEquals(dsTagA.getName()));
dsGroup1 = toggleTagAssignment(dsGroup1, dsTagB).getAssignedEntity();
dsTagA = getOrThrow(distributionSetTagRepository.findByNameEquals(dsTagA.getName()));
dsGroup2 = toggleTagAssignment(dsGroup2, dsTagA).getAssignedEntity();
dsTagA = getOrThrow(distributionSetTagRepository.findByNameEquals(dsTagA.getName()));
final List<DistributionSet> allDistributionSets = Stream
.of(dsGroup1, dsGroup2, Arrays.asList(dsDeleted, dsInComplete, dsNewType)).flatMap(Collection::stream)
.collect(Collectors.toList());
final List<DistributionSet> dsGroup1WithGroup2 = Stream.of(dsGroup1, dsGroup2).flatMap(Collection::stream)
.collect(Collectors.toList());
final int sizeOfAllDistributionSets = allDistributionSets.size();
// check setup
assertThat(distributionSetRepository.findAll()).hasSize(sizeOfAllDistributionSets);
validateFindAll(allDistributionSets);
validateDeleted(dsDeleted, sizeOfAllDistributionSets - 1);
validateCompleted(dsInComplete, sizeOfAllDistributionSets - 1);
validateType(newType, dsNewType, sizeOfAllDistributionSets - 1);
validateSearchText(allDistributionSets, dsGroup2Prefix);
validateTags(dsTagA, dsTagB, dsTagC, dsGroup1WithGroup2, dsGroup1);
validateDeletedAndCompleted(dsGroup1WithGroup2, dsNewType, dsDeleted);
validateDeletedAndCompletedAndType(dsGroup1WithGroup2, dsDeleted, newType, dsNewType);
validateDeletedAndCompletedAndTypeAndSearchText(dsGroup2, newType, dsGroup2Prefix);
validateDeletedAndCompletedAndTypeAndSearchText(dsGroup1WithGroup2, dsDeleted, dsInComplete, dsNewType, newType,
":1");
validateDeletedAndCompletedAndTypeAndSearchTextAndTag(dsGroup2, dsTagA, dsGroup2Prefix);
}
@Step
private void validateFindAll(final List<DistributionSet> expectedDistributionsets) {
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder(), expectedDistributionsets);
}
@Step
private void validateDeleted(final DistributionSet deletedDistributionSet, final int notDeletedSize) {
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().isDeleted(Boolean.TRUE),
singletonList(deletedDistributionSet));
assertThatFilterHasSizeAndDoesNotContainDistributionSet(
DistributionSetFilter.builder().isDeleted(Boolean.FALSE), notDeletedSize, deletedDistributionSet);
}
@Step
private void validateCompleted(final DistributionSet dsIncomplete, final int completedSize) {
assertThatFilterHasSizeAndDoesNotContainDistributionSet(
DistributionSetFilter.builder().isComplete(Boolean.TRUE), completedSize, dsIncomplete);
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().isComplete(Boolean.FALSE), singletonList(dsIncomplete));
}
@Step
private void validateType(final DistributionSetType newType, final DistributionSet dsNewType,
final int standardDsTypeSize) {
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().typeId(newType.getId()),
singletonList(dsNewType));
assertThatFilterHasSizeAndDoesNotContainDistributionSet(
DistributionSetFilter.builder().typeId(standardDsType.getId()), standardDsTypeSize, dsNewType);
}
@Step
private void validateSearchText(final List<DistributionSet> allDistributionSets, final String dsNamePrefix) {
final List<DistributionSet> withTestNamePrefix = allDistributionSets.stream()
.filter(ds -> ds.getName().startsWith(dsNamePrefix)).collect(Collectors.toList());
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().searchText(dsNamePrefix),
withTestNamePrefix);
final List<DistributionSet> withTestNameExact = withTestNamePrefix.stream()
.filter(ds -> ds.getName().equals(dsNamePrefix)).collect(Collectors.toList());
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().searchText(dsNamePrefix + ":"), withTestNameExact);
final List<DistributionSet> withTestNameExactAndVersionPrefix = withTestNameExact.stream()
.filter(ds -> ds.getVersion().startsWith("1")).collect(Collectors.toList());
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().searchText(dsNamePrefix + ":1"),
withTestNameExactAndVersionPrefix);
final List<DistributionSet> dsWithExactNameAndVersion = withTestNameExactAndVersionPrefix.stream()
.filter(ds -> ds.getVersion().equals("1.0.0")).collect(Collectors.toList());
assertThat(dsWithExactNameAndVersion).hasSize(1);
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().searchText(dsNamePrefix + ":1.0.0"), dsWithExactNameAndVersion);
final List<DistributionSet> withVersionPrefix = allDistributionSets.stream()
.filter(ds -> ds.getVersion().startsWith("1.0.")).collect(Collectors.toList());
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().searchText(":1.0."),
withVersionPrefix);
final List<DistributionSet> withVersionExact = withVersionPrefix.stream()
.filter(ds -> ds.getVersion().equals("1.0.0")).collect(Collectors.toList());
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().searchText(":1.0.0"),
withVersionExact);
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().searchText(":"),
allDistributionSets);
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().searchText(" : "),
allDistributionSets);
}
@Step
private void validateTags(final DistributionSetTag dsTagA, final DistributionSetTag dsTagB,
final DistributionSetTag dsTagC, final List<DistributionSet> dsWithTagA,
final List<DistributionSet> dsWithTagB) {
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().tagNames(singletonList(dsTagA.getName())), dsWithTagA);
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().tagNames(singletonList(dsTagB.getName())), dsWithTagB);
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().tagNames(Arrays.asList(dsTagA.getName(), dsTagB.getName())),
dsWithTagA);
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().tagNames(Arrays.asList(dsTagC.getName(), dsTagB.getName())),
dsWithTagB);
assertThatFilterDoesNotContainAnyDistributionSet(
DistributionSetFilter.builder().tagNames(singletonList(dsTagC.getName())));
}
@Step
private void validateDeletedAndCompleted(final List<DistributionSet> completedStandardType,
final DistributionSet dsNewType, final DistributionSet dsDeleted) {
final List<DistributionSet> completedNotDeleted = new ArrayList<>(completedStandardType);
completedNotDeleted.add(dsNewType);
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().isComplete(Boolean.TRUE).isDeleted(Boolean.FALSE),
completedNotDeleted);
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().isComplete(Boolean.TRUE).isDeleted(Boolean.TRUE),
singletonList(dsDeleted));
assertThatFilterDoesNotContainAnyDistributionSet(
DistributionSetFilter.builder().isComplete(Boolean.FALSE).isDeleted(Boolean.TRUE));
}
@Step
private void validateDeletedAndCompletedAndType(final List<DistributionSet> deletedAndCompletedAndStandardType,
final DistributionSet dsDeleted, final DistributionSetType newType, final DistributionSet dsNewType) {
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().isDeleted(Boolean.FALSE)
.isComplete(Boolean.TRUE).typeId(standardDsType.getId()), deletedAndCompletedAndStandardType);
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().isComplete(Boolean.TRUE)
.typeId(standardDsType.getId()).isDeleted(Boolean.TRUE), singletonList(dsDeleted));
assertThatFilterDoesNotContainAnyDistributionSet(DistributionSetFilter.builder().isDeleted(Boolean.TRUE)
.isComplete(Boolean.FALSE).typeId(standardDsType.getId()));
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().isComplete(Boolean.TRUE).typeId(newType.getId()),
singletonList(dsNewType));
}
@Step
private void validateDeletedAndCompletedAndTypeAndSearchText(
final List<DistributionSet> completedAndStandardTypeAndSearchText, final DistributionSetType newType,
final String text) {
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().isDeleted(Boolean.FALSE)
.isComplete(Boolean.TRUE).typeId(standardDsType.getId()).searchText(text),
completedAndStandardTypeAndSearchText);
assertThatFilterDoesNotContainAnyDistributionSet(DistributionSetFilter.builder().isComplete(Boolean.TRUE)
.isDeleted(Boolean.TRUE).typeId(standardDsType.getId()).searchText(text + ":"));
assertThatFilterDoesNotContainAnyDistributionSet(
DistributionSetFilter.builder().typeId(standardDsType.getId()).searchText(text)
.isComplete(Boolean.FALSE).isDeleted(Boolean.FALSE));
assertThatFilterDoesNotContainAnyDistributionSet(DistributionSetFilter.builder().typeId(newType.getId())
.searchText(text).isComplete(Boolean.TRUE).isDeleted(Boolean.FALSE));
}
@Step
private void validateDeletedAndCompletedAndTypeAndSearchText(
final List<DistributionSet> completedAndNotDeletedStandardTypeAndFilterString,
final DistributionSet dsDeleted, final DistributionSet dsInComplete, final DistributionSet dsNewType,
final DistributionSetType newType, final String filterString) {
final List<DistributionSet> completedAndStandardTypeAndFilterString = new ArrayList<>(
completedAndNotDeletedStandardTypeAndFilterString);
completedAndStandardTypeAndFilterString.add(dsDeleted);
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().isComplete(Boolean.TRUE)
.typeId(standardDsType.getId()).searchText(filterString),
completedAndStandardTypeAndFilterString);
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().isComplete(Boolean.TRUE).isDeleted(Boolean.FALSE)
.typeId(standardDsType.getId()).searchText(filterString),
completedAndNotDeletedStandardTypeAndFilterString);
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().isComplete(Boolean.TRUE)
.isDeleted(Boolean.TRUE).typeId(standardDsType.getId()).searchText(filterString),
singletonList(dsDeleted));
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().typeId(standardDsType.getId()).searchText(filterString)
.isComplete(Boolean.FALSE).isDeleted(Boolean.FALSE),
singletonList(dsInComplete));
assertThatFilterContainsOnlyGivenDistributionSets(DistributionSetFilter.builder().typeId(newType.getId())
.searchText(filterString).isComplete(Boolean.TRUE).isDeleted(Boolean.FALSE),
singletonList(dsNewType));
}
@Step
private void validateDeletedAndCompletedAndTypeAndSearchTextAndTag(
final List<DistributionSet> completedAndStandartTypeAndSearchTextAndTagA, final DistributionSetTag dsTagA,
final String text) {
assertThatFilterContainsOnlyGivenDistributionSets(
DistributionSetFilter.builder().isComplete(Boolean.TRUE).typeId(standardDsType.getId())
.searchText(text).tagNames(singletonList(dsTagA.getName())),
completedAndStandartTypeAndSearchTextAndTagA);
assertThatFilterDoesNotContainAnyDistributionSet(DistributionSetFilter.builder()
.typeId(standardDsType.getId()).searchText(text).tagNames(singletonList(dsTagA.getName()))
.isComplete(Boolean.FALSE).isDeleted(Boolean.FALSE));
}
private void assertThatFilterContainsOnlyGivenDistributionSets(final DistributionSetFilterBuilder filterBuilder,
final List<DistributionSet> distributionSets) {
final int expectedDsSize = distributionSets.size();
assertThat(distributionSetManagement.findByDistributionSetFilter(PAGE, filterBuilder.build()).getContent())
.hasSize(expectedDsSize).containsOnly(distributionSets.toArray(new DistributionSet[expectedDsSize]));
}
private void assertThatFilterDoesNotContainAnyDistributionSet(final DistributionSetFilterBuilder filterBuilder) {
assertThat(distributionSetManagement.findByDistributionSetFilter(PAGE, filterBuilder.build()).getContent())
.isEmpty();
}
private void assertThatFilterHasSizeAndDoesNotContainDistributionSet(
final DistributionSetFilterBuilder filterBuilder, final int size, final DistributionSet ds) {
assertThat(distributionSetManagement.findByDistributionSetFilter(PAGE, filterBuilder.build()).getContent())
.hasSize(size).doesNotContain(ds);
}
@Test
@Description("Simple DS load without the related data that should be loaded lazy.")
void findDistributionSetsWithoutLazy() {
testdataFactory.createDistributionSets(20);
assertThat(distributionSetManagement.findByCompleted(PAGE, true)).hasSize(20);
}
@Test