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
|
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.db.component;
import com.google.common.base.Optional;
import java.util.Collections;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.ExpectedException;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.RowNotFoundException;
import org.sonar.test.DbTests;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Sets.newHashSet;
import static java.util.Collections.singleton;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
import static org.sonar.db.component.ComponentTesting.newDeveloper;
import static org.sonar.db.component.ComponentTesting.newFileDto;
import static org.sonar.db.component.ComponentTesting.newModuleDto;
import static org.sonar.db.component.ComponentTesting.newProjectDto;
import static org.sonar.db.component.ComponentTesting.newView;
@Category(DbTests.class)
public class ComponentDaoTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
ComponentDbTester componentDb = new ComponentDbTester(db);
DbSession dbSession = db.getSession();
ComponentDao underTest = new ComponentDao();
@Test
public void get_by_uuid() {
db.prepareDbUnit(getClass(), "shared.xml");
ComponentDto result = underTest.selectByUuid(dbSession, "KLMN").get();
assertThat(result).isNotNull();
assertThat(result.uuid()).isEqualTo("KLMN");
assertThat(result.moduleUuid()).isEqualTo("EFGH");
assertThat(result.moduleUuidPath()).isEqualTo(".ABCD.EFGH.");
assertThat(result.parentProjectId()).isEqualTo(2);
assertThat(result.projectUuid()).isEqualTo("ABCD");
assertThat(result.key()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
assertThat(result.path()).isEqualTo("src/org/struts/RequestContext.java");
assertThat(result.name()).isEqualTo("RequestContext.java");
assertThat(result.longName()).isEqualTo("org.struts.RequestContext");
assertThat(result.qualifier()).isEqualTo("FIL");
assertThat(result.scope()).isEqualTo("FIL");
assertThat(result.language()).isEqualTo("java");
assertThat(result.getCopyResourceId()).isNull();
assertThat(underTest.selectByUuid(dbSession, "UNKNOWN")).isAbsent();
}
@Test
public void get_by_uuid_on_technical_project_copy() {
db.prepareDbUnit(getClass(), "shared.xml");
ComponentDto result = underTest.selectByUuid(dbSession, "STUV").get();
assertThat(result).isNotNull();
assertThat(result.uuid()).isEqualTo("STUV");
assertThat(result.moduleUuid()).isEqualTo("OPQR");
assertThat(result.moduleUuidPath()).isEqualTo(".OPQR.");
assertThat(result.parentProjectId()).isEqualTo(11);
assertThat(result.projectUuid()).isEqualTo("OPQR");
assertThat(result.key()).isEqualTo("DEV:anakin@skywalker.name:org.struts:struts");
assertThat(result.path()).isNull();
assertThat(result.name()).isEqualTo("Apache Struts");
assertThat(result.longName()).isEqualTo("Apache Struts");
assertThat(result.qualifier()).isEqualTo("DEV_PRJ");
assertThat(result.scope()).isEqualTo("PRJ");
assertThat(result.language()).isNull();
assertThat(result.getCopyResourceId()).isEqualTo(1L);
}
@Test
public void get_by_uuid_on_developer_project_copy() {
db.prepareDbUnit(getClass(), "shared.xml");
ComponentDto result = underTest.selectByUuid(dbSession, "STUV").get();
assertThat(result.getDeveloperId()).isEqualTo(11L);
}
@Test
public void get_by_uuid_on_disabled_component() {
db.prepareDbUnit(getClass(), "shared.xml");
ComponentDto result = underTest.selectByUuid(dbSession, "DCBA").get();
assertThat(result).isNotNull();
assertThat(result.isEnabled()).isFalse();
}
@Test
public void fail_to_get_by_uuid_when_component_not_found() {
thrown.expect(RowNotFoundException.class);
db.prepareDbUnit(getClass(), "shared.xml");
underTest.selectOrFailByUuid(dbSession, "unknown");
}
@Test
public void get_by_key() {
db.prepareDbUnit(getClass(), "shared.xml");
Optional<ComponentDto> optional = underTest.selectByKey(dbSession, "org.struts:struts-core:src/org/struts/RequestContext.java");
assertThat(optional).isPresent();
ComponentDto result = optional.get();
assertThat(result.key()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
assertThat(result.path()).isEqualTo("src/org/struts/RequestContext.java");
assertThat(result.name()).isEqualTo("RequestContext.java");
assertThat(result.longName()).isEqualTo("org.struts.RequestContext");
assertThat(result.qualifier()).isEqualTo("FIL");
assertThat(result.scope()).isEqualTo("FIL");
assertThat(result.language()).isEqualTo("java");
assertThat(result.parentProjectId()).isEqualTo(2);
assertThat(underTest.selectByKey(dbSession, "unknown")).isAbsent();
}
@Test
public void fail_to_get_by_key_when_component_not_found() {
thrown.expect(RowNotFoundException.class);
db.prepareDbUnit(getClass(), "shared.xml");
underTest.selectOrFailByKey(dbSession, "unknown");
}
@Test
public void get_by_key_on_disabled_component() {
db.prepareDbUnit(getClass(), "shared.xml");
ComponentDto result = underTest.selectOrFailByKey(dbSession, "org.disabled.project");
assertThat(result.isEnabled()).isFalse();
}
@Test
public void get_by_key_on_a_root_project() {
db.prepareDbUnit(getClass(), "shared.xml");
ComponentDto result = underTest.selectOrFailByKey(dbSession, "org.struts:struts");
assertThat(result.key()).isEqualTo("org.struts:struts");
assertThat(result.deprecatedKey()).isEqualTo("org.struts:struts");
assertThat(result.path()).isNull();
assertThat(result.name()).isEqualTo("Struts");
assertThat(result.longName()).isEqualTo("Apache Struts");
assertThat(result.description()).isEqualTo("the description");
assertThat(result.qualifier()).isEqualTo("TRK");
assertThat(result.scope()).isEqualTo("PRJ");
assertThat(result.language()).isNull();
assertThat(result.parentProjectId()).isNull();
assertThat(result.getAuthorizationUpdatedAt()).isEqualTo(123456789L);
}
@Test
public void get_by_keys() {
db.prepareDbUnit(getClass(), "shared.xml");
List<ComponentDto> results = underTest.selectByKeys(dbSession, singletonList("org.struts:struts-core:src/org/struts/RequestContext.java"));
assertThat(results).hasSize(1);
ComponentDto result = results.get(0);
assertThat(result).isNotNull();
assertThat(result.key()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
assertThat(result.path()).isEqualTo("src/org/struts/RequestContext.java");
assertThat(result.name()).isEqualTo("RequestContext.java");
assertThat(result.longName()).isEqualTo("org.struts.RequestContext");
assertThat(result.qualifier()).isEqualTo("FIL");
assertThat(result.scope()).isEqualTo("FIL");
assertThat(result.language()).isEqualTo("java");
assertThat(result.parentProjectId()).isEqualTo(2);
assertThat(underTest.selectByKeys(dbSession, singletonList("unknown"))).isEmpty();
}
@Test
public void get_by_ids() {
db.prepareDbUnit(getClass(), "shared.xml");
List<ComponentDto> results = underTest.selectByIds(dbSession, newArrayList(4L));
assertThat(results).hasSize(1);
ComponentDto result = results.get(0);
assertThat(result).isNotNull();
assertThat(result.key()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
assertThat(result.path()).isEqualTo("src/org/struts/RequestContext.java");
assertThat(result.name()).isEqualTo("RequestContext.java");
assertThat(result.longName()).isEqualTo("org.struts.RequestContext");
assertThat(result.qualifier()).isEqualTo("FIL");
assertThat(result.scope()).isEqualTo("FIL");
assertThat(result.language()).isEqualTo("java");
assertThat(result.parentProjectId()).isEqualTo(2);
assertThat(underTest.selectByIds(dbSession, newArrayList(555L))).isEmpty();
}
@Test
public void get_by_uuids() {
db.prepareDbUnit(getClass(), "shared.xml");
List<ComponentDto> results = underTest.selectByUuids(dbSession, newArrayList("KLMN"));
assertThat(results).hasSize(1);
ComponentDto result = results.get(0);
assertThat(result).isNotNull();
assertThat(result.uuid()).isEqualTo("KLMN");
assertThat(result.moduleUuid()).isEqualTo("EFGH");
assertThat(result.moduleUuidPath()).isEqualTo(".ABCD.EFGH.");
assertThat(result.parentProjectId()).isEqualTo(2);
assertThat(result.projectUuid()).isEqualTo("ABCD");
assertThat(result.key()).isEqualTo("org.struts:struts-core:src/org/struts/RequestContext.java");
assertThat(result.path()).isEqualTo("src/org/struts/RequestContext.java");
assertThat(result.name()).isEqualTo("RequestContext.java");
assertThat(result.longName()).isEqualTo("org.struts.RequestContext");
assertThat(result.qualifier()).isEqualTo("FIL");
assertThat(result.scope()).isEqualTo("FIL");
assertThat(result.language()).isEqualTo("java");
assertThat(underTest.selectByUuids(dbSession, newArrayList("unknown"))).isEmpty();
}
@Test
public void get_by_uuids_on_removed_components() {
db.prepareDbUnit(getClass(), "shared.xml");
List<ComponentDto> results = underTest.selectByUuids(dbSession, newArrayList("DCBA"));
assertThat(results).hasSize(1);
ComponentDto result = results.get(0);
assertThat(result).isNotNull();
assertThat(result.isEnabled()).isFalse();
}
@Test
public void select_existing_uuids() {
db.prepareDbUnit(getClass(), "shared.xml");
List<String> results = underTest.selectExistingUuids(dbSession, newArrayList("KLMN"));
assertThat(results).containsOnly("KLMN");
assertThat(underTest.selectExistingUuids(dbSession, newArrayList("KLMN", "unknown"))).hasSize(1);
assertThat(underTest.selectExistingUuids(dbSession, newArrayList("unknown"))).isEmpty();
}
@Test
public void get_by_id() {
db.prepareDbUnit(getClass(), "shared.xml");
assertThat(underTest.selectOrFailById(dbSession, 4L)).isNotNull();
}
@Test
public void get_by_id_on_disabled_component() {
db.prepareDbUnit(getClass(), "shared.xml");
Optional<ComponentDto> result = underTest.selectById(dbSession, 10L);
assertThat(result).isPresent();
assertThat(result.get().isEnabled()).isFalse();
}
@Test(expected = RowNotFoundException.class)
public void fail_to_get_by_id_when_project_not_found() {
db.prepareDbUnit(getClass(), "shared.xml");
underTest.selectOrFailById(dbSession, 111L);
}
@Test
public void get_nullable_by_id() {
db.prepareDbUnit(getClass(), "shared.xml");
assertThat(underTest.selectById(dbSession, 4L)).isPresent();
assertThat(underTest.selectById(dbSession, 111L)).isAbsent();
}
@Test
public void count_by_id() {
db.prepareDbUnit(getClass(), "shared.xml");
assertThat(underTest.existsById(4L, dbSession)).isTrue();
assertThat(underTest.existsById(111L, dbSession)).isFalse();
}
@Test
public void select_component_keys_by_qualifiers() {
db.prepareDbUnit(getClass(), "shared.xml");
assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("TRK"))).extracting("kee").containsOnly("org.struts:struts", "org.disabled.project");
assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("BRC"))).extracting("kee").containsOnly("org.struts:struts-core");
assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("DIR"))).extracting("kee").containsOnly("org.struts:struts-core:src/org/struts");
assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("FIL"))).extracting("kee").containsOnly("org.struts:struts-core:src/org/struts/RequestContext.java");
assertThat(underTest.selectComponentsByQualifiers(dbSession, newHashSet("unknown"))).isEmpty();
}
@Test
public void fail_with_IAE_select_component_keys_by_qualifiers_on_empty_qualifier() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Qualifiers cannot be empty");
db.prepareDbUnit(getClass(), "shared.xml");
underTest.selectComponentsByQualifiers(dbSession, Collections.<String>emptySet());
}
@Test
public void find_sub_projects_by_component_keys() {
db.prepareDbUnit(getClass(), "multi-modules.xml");
// Sub project of a file
List<ComponentDto> results = underTest.selectSubProjectsByComponentUuids(dbSession, newArrayList("HIJK"));
assertThat(results).hasSize(1);
assertThat(results.get(0).getKey()).isEqualTo("org.struts:struts-data");
// Sub project of a directory
results = underTest.selectSubProjectsByComponentUuids(dbSession, newArrayList("GHIJ"));
assertThat(results).hasSize(1);
assertThat(results.get(0).getKey()).isEqualTo("org.struts:struts-data");
// Sub project of a sub module
results = underTest.selectSubProjectsByComponentUuids(dbSession, newArrayList("FGHI"));
assertThat(results).hasSize(1);
assertThat(results.get(0).getKey()).isEqualTo("org.struts:struts");
// Sub project of a module
results = underTest.selectSubProjectsByComponentUuids(dbSession, newArrayList("EFGH"));
assertThat(results).hasSize(1);
assertThat(results.get(0).getKey()).isEqualTo("org.struts:struts");
// Sub project of a project
assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, newArrayList("ABCD"))).isEmpty();
// SUb projects of a component and a sub module
assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, newArrayList("HIJK", "FGHI"))).hasSize(2);
assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, newArrayList("unknown"))).isEmpty();
assertThat(underTest.selectSubProjectsByComponentUuids(dbSession, Collections.<String>emptyList())).isEmpty();
}
@Test
public void select_enabled_modules_tree() {
db.prepareDbUnit(getClass(), "multi-modules.xml");
// From root project
List<ComponentDto> modules = underTest.selectEnabledDescendantModules(dbSession, "ABCD");
assertThat(modules).extracting("uuid").containsOnly("ABCD", "EFGH", "FGHI");
// From module
modules = underTest.selectEnabledDescendantModules(dbSession, "EFGH");
assertThat(modules).extracting("uuid").containsOnly("EFGH", "FGHI");
// From sub module
modules = underTest.selectEnabledDescendantModules(dbSession, "FGHI");
assertThat(modules).extracting("uuid").containsOnly("FGHI");
// Folder
assertThat(underTest.selectEnabledDescendantModules(dbSession, "GHIJ")).isEmpty();
assertThat(underTest.selectEnabledDescendantModules(dbSession, "unknown")).isEmpty();
}
@Test
public void select_all_modules_tree() {
db.prepareDbUnit(getClass(), "multi-modules.xml");
// From root project, disabled sub module is returned
List<ComponentDto> modules = underTest.selectDescendantModules(dbSession, "ABCD");
assertThat(modules).extracting("uuid").containsOnly("ABCD", "EFGH", "FGHI", "IHGF");
// From module, disabled sub module is returned
modules = underTest.selectDescendantModules(dbSession, "EFGH");
assertThat(modules).extracting("uuid").containsOnly("EFGH", "FGHI", "IHGF");
// From removed sub module -> should not be returned
assertThat(underTest.selectDescendantModules(dbSession, "IHGF")).isEmpty();
}
@Test
public void select_enabled_module_files_tree_from_module() {
db.prepareDbUnit(getClass(), "select_module_files_tree.xml");
// From root project
List<FilePathWithHashDto> files = underTest.selectEnabledDescendantFiles(dbSession, "ABCD");
assertThat(files).extracting("uuid").containsOnly("EFGHI", "HIJK");
assertThat(files).extracting("moduleUuid").containsOnly("EFGH", "FGHI");
assertThat(files).extracting("srcHash").containsOnly("srcEFGHI", "srcHIJK");
assertThat(files).extracting("path").containsOnly("src/org/struts/pom.xml", "src/org/struts/RequestContext.java");
assertThat(files).extracting("revision").containsOnly("123456789");
// From module
files = underTest.selectEnabledDescendantFiles(dbSession, "EFGH");
assertThat(files).extracting("uuid").containsOnly("EFGHI", "HIJK");
assertThat(files).extracting("moduleUuid").containsOnly("EFGH", "FGHI");
assertThat(files).extracting("srcHash").containsOnly("srcEFGHI", "srcHIJK");
assertThat(files).extracting("path").containsOnly("src/org/struts/pom.xml", "src/org/struts/RequestContext.java");
assertThat(files).extracting("revision").containsOnly("123456789");
// From sub module
files = underTest.selectEnabledDescendantFiles(dbSession, "FGHI");
assertThat(files).extracting("uuid").containsOnly("HIJK");
assertThat(files).extracting("moduleUuid").containsOnly("FGHI");
assertThat(files).extracting("srcHash").containsOnly("srcHIJK");
assertThat(files).extracting("path").containsOnly("src/org/struts/RequestContext.java");
assertThat(files).extracting("revision").containsOnly("123456789");
// From directory
assertThat(underTest.selectEnabledDescendantFiles(dbSession, "GHIJ")).isEmpty();
assertThat(underTest.selectEnabledDescendantFiles(dbSession, "unknown")).isEmpty();
}
@Test
public void select_enabled_module_files_tree_from_project() {
db.prepareDbUnit(getClass(), "select_module_files_tree.xml");
// From root project
List<FilePathWithHashDto> files = underTest.selectEnabledFilesFromProject(dbSession, "ABCD");
assertThat(files).extracting("uuid").containsOnly("EFGHI", "HIJK");
assertThat(files).extracting("moduleUuid").containsOnly("EFGH", "FGHI");
assertThat(files).extracting("srcHash").containsOnly("srcEFGHI", "srcHIJK");
assertThat(files).extracting("path").containsOnly("src/org/struts/pom.xml", "src/org/struts/RequestContext.java");
// From module
assertThat(underTest.selectEnabledFilesFromProject(dbSession, "EFGH")).isEmpty();
// From sub module
assertThat(underTest.selectEnabledFilesFromProject(dbSession, "FGHI")).isEmpty();
// From directory
assertThat(underTest.selectEnabledFilesFromProject(dbSession, "GHIJ")).isEmpty();
assertThat(underTest.selectEnabledFilesFromProject(dbSession, "unknown")).isEmpty();
}
@Test
public void select_all_components_from_project() {
db.prepareDbUnit(getClass(), "multi-modules.xml");
List<ComponentDto> components = underTest.selectAllComponentsFromProjectKey(dbSession, "org.struts:struts");
// Removed components are included
assertThat(components).hasSize(8);
assertThat(underTest.selectAllComponentsFromProjectKey(dbSession, "UNKNOWN")).isEmpty();
}
@Test
public void select_modules_from_project() {
db.prepareDbUnit(getClass(), "multi-modules.xml");
List<ComponentDto> components = underTest.selectEnabledModulesFromProjectKey(dbSession, "org.struts:struts");
assertThat(components).hasSize(3);
assertThat(underTest.selectEnabledModulesFromProjectKey(dbSession, "UNKNOWN")).isEmpty();
}
@Test
public void select_views_and_sub_views() {
db.prepareDbUnit(getClass(), "shared_views.xml");
assertThat(underTest.selectAllViewsAndSubViews(dbSession)).extracting("uuid").containsOnly("ABCD", "EFGH", "FGHI", "IJKL");
assertThat(underTest.selectAllViewsAndSubViews(dbSession)).extracting("projectUuid").containsOnly("ABCD", "EFGH", "IJKL");
}
@Test
public void select_projects_from_view() {
db.prepareDbUnit(getClass(), "shared_views.xml");
assertThat(underTest.selectProjectsFromView(dbSession, "ABCD", "ABCD")).containsOnly("JKLM");
assertThat(underTest.selectProjectsFromView(dbSession, "EFGH", "EFGH")).containsOnly("KLMN", "JKLM");
assertThat(underTest.selectProjectsFromView(dbSession, "FGHI", "EFGH")).containsOnly("JKLM");
assertThat(underTest.selectProjectsFromView(dbSession, "IJKL", "IJKL")).isEmpty();
assertThat(underTest.selectProjectsFromView(dbSession, "Unknown", "Unknown")).isEmpty();
}
@Test
public void select_projects() {
db.prepareDbUnit(getClass(), "select_provisioned_projects.xml");
List<ComponentDto> result = underTest.selectProjects(dbSession);
assertThat(result).extracting("id").containsOnly(42L, 1L);
}
@Test
public void select_provisioned_projects() {
db.prepareDbUnit(getClass(), "select_provisioned_projects.xml");
List<ComponentDto> result = underTest.selectProvisionedProjects(dbSession, 0, 10, null);
ComponentDto project = result.get(0);
assertThat(result).hasSize(1);
assertThat(project.getKey()).isEqualTo("org.provisioned.project");
}
@Test
public void count_provisioned_projects() {
db.prepareDbUnit(getClass(), "select_provisioned_projects.xml");
int numberOfProjects = underTest.countProvisionedProjects(dbSession, null);
assertThat(numberOfProjects).isEqualTo(1);
}
@Test
public void select_ghost_projects() {
db.prepareDbUnit(getClass(), "select_ghost_projects.xml");
List<ComponentDto> result = underTest.selectGhostProjects(dbSession, 0, 10, null);
assertThat(result).hasSize(1);
assertThat(result.get(0).key()).isEqualTo("org.ghost.project");
assertThat(underTest.countGhostProjects(dbSession, null)).isEqualTo(1);
}
@Test
public void selectResourcesByRootId() {
db.prepareDbUnit(getClass(), "shared.xml");
List<ComponentDto> resources = underTest.selectByProjectUuid("ABCD", dbSession);
assertThat(resources).extracting("id").containsOnly(1l, 2l, 3l, 4l);
}
@Test
public void insert() {
db.prepareDbUnit(getClass(), "empty.xml");
ComponentDto componentDto = new ComponentDto()
.setId(1L)
.setUuid("GHIJ")
.setProjectUuid("ABCD")
.setModuleUuid("EFGH")
.setModuleUuidPath(".ABCD.EFGH.")
.setKey("org.struts:struts-core:src/org/struts/RequestContext.java")
.setDeprecatedKey("org.struts:struts-core:src/org/struts/RequestContext.java")
.setName("RequestContext.java")
.setLongName("org.struts.RequestContext")
.setQualifier("FIL")
.setScope("FIL")
.setLanguage("java")
.setDescription("description")
.setPath("src/org/struts/RequestContext.java")
.setParentProjectId(3L)
.setCopyResourceId(5L)
.setDeveloperId(7L)
.setEnabled(true)
.setCreatedAt(DateUtils.parseDate("2014-06-18"))
.setAuthorizationUpdatedAt(123456789L);
underTest.insert(dbSession, componentDto);
dbSession.commit();
assertThat(componentDto.getId()).isNotNull();
db.assertDbUnit(getClass(), "insert-result.xml", "projects");
}
@Test
public void insert_disabled_component() {
db.prepareDbUnit(getClass(), "empty.xml");
ComponentDto componentDto = new ComponentDto()
.setId(1L)
.setUuid("GHIJ")
.setProjectUuid("ABCD")
.setModuleUuid("EFGH")
.setModuleUuidPath(".ABCD.EFGH.")
.setKey("org.struts:struts-core:src/org/struts/RequestContext.java")
.setName("RequestContext.java")
.setLongName("org.struts.RequestContext")
.setQualifier("FIL")
.setScope("FIL")
.setLanguage("java")
.setPath("src/org/struts/RequestContext.java")
.setParentProjectId(3L)
.setEnabled(false)
.setCreatedAt(DateUtils.parseDate("2014-06-18"))
.setAuthorizationUpdatedAt(123456789L);
underTest.insert(dbSession, componentDto);
dbSession.commit();
assertThat(componentDto.getId()).isNotNull();
db.assertDbUnit(getClass(), "insert_disabled_component-result.xml", "projects");
}
@Test
public void update() {
db.prepareDbUnit(getClass(), "update.xml");
ComponentDto componentDto = new ComponentDto()
.setUuid("GHIJ")
.setProjectUuid("DCBA")
.setModuleUuid("HGFE")
.setModuleUuidPath(".DCBA.HGFE.")
.setKey("org.struts:struts-core:src/org/struts/RequestContext2.java")
.setDeprecatedKey("org.struts:struts-core:src/org/struts/RequestContext2.java")
.setName("RequestContext2.java")
.setLongName("org.struts.RequestContext2")
.setQualifier("LIF")
.setScope("LIF")
.setLanguage("java2")
.setDescription("description2")
.setPath("src/org/struts/RequestContext2.java")
.setParentProjectId(4L)
.setCopyResourceId(6L)
.setDeveloperId(9L)
.setEnabled(false)
.setAuthorizationUpdatedAt(12345678910L);
underTest.update(dbSession, componentDto);
dbSession.commit();
db.assertDbUnit(getClass(), "update-result.xml", "projects");
}
@Test
public void delete() throws Exception {
ComponentDto project1 = componentDb.insertComponent(newProjectDto().setKey("PROJECT_1"));
componentDb.insertComponent(newProjectDto().setKey("PROJECT_2"));
underTest.delete(dbSession, project1.getId());
dbSession.commit();
assertThat(underTest.selectByKey(dbSession, "PROJECT_1")).isAbsent();
assertThat(underTest.selectByKey(dbSession, "PROJECT_2")).isPresent();
}
@Test
public void select_components_with_paging_query_and_qualifiers() {
underTest.insert(dbSession, newProjectDto().setName("aaaa-name"));
underTest.insert(dbSession, newView());
underTest.insert(dbSession, newDeveloper("project-name"));
for (int i = 9; i >= 1; i--) {
underTest.insert(dbSession, newProjectDto().setName("project-" + i));
}
List<ComponentDto> result = underTest.selectComponents(dbSession, singleton(Qualifiers.PROJECT), 1, 3, "project");
assertThat(result).hasSize(3);
assertThat(result).extracting("name").containsExactly("project-2", "project-3", "project-4");
}
@Test
public void select_by_query_with_paging_query_and_qualifiers() {
componentDb.insertProjectAndSnapshot(newProjectDto().setName("aaaa-name"));
componentDb.insertProjectAndSnapshot(newView());
componentDb.insertProjectAndSnapshot(newDeveloper("project-name"));
for (int i = 9; i >= 1; i--) {
componentDb.insertProjectAndSnapshot(newProjectDto().setName("project-" + i));
}
db.commit();
componentDb.indexProjects();
ComponentQuery query = new ComponentQuery("oJect", null, Qualifiers.PROJECT);
List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 1, 3);
assertThat(result).hasSize(3);
assertThat(underTest.countByQuery(dbSession, query)).isEqualTo(9);
assertThat(result).extracting("name").containsExactly("project-2", "project-3", "project-4");
}
@Test
public void select_by_query_name_with_special_characters() {
componentDb.insertProjectAndSnapshot(newProjectDto().setName("project-\\_%/-name"));
db.commit();
componentDb.indexProjects();
ComponentQuery query = new ComponentQuery("-\\_%/-", null, Qualifiers.PROJECT);
List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
assertThat(result).hasSize(1);
assertThat(result.get(0).name()).isEqualTo("project-\\_%/-name");
}
@Test
public void select_by_query_key_with_special_characters() {
componentDb.insertProjectAndSnapshot(newProjectDto()
.setKey("project-_%-key"));
db.commit();
componentDb.indexProjects();
ComponentQuery query = new ComponentQuery("project-_%-", null, Qualifiers.PROJECT);
List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
assertThat(result).hasSize(1);
assertThat(result.get(0).key()).isEqualTo("project-_%-key");
}
@Test
public void select_by_query_filter_on_language() {
componentDb.insertComponent(newProjectDto().setKey("java-project-key").setLanguage("java"));
componentDb.insertComponent(newProjectDto().setKey("cpp-project-key").setLanguage("cpp"));
db.commit();
ComponentQuery query = new ComponentQuery(null, "java", Qualifiers.PROJECT);
List<ComponentDto> result = underTest.selectByQuery(dbSession, query, 0, 10);
assertThat(result).hasSize(1);
assertThat(result.get(0).key()).isEqualTo("java-project-key");
}
@Test
public void select_direct_children_of_a_project() {
ComponentDto project = newProjectDto().setKey("project-key").setUuid("project-uuid");
SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(project);
SnapshotDto moduleSnapshot = componentDb.insertComponentAndSnapshot(newModuleDto("module-1-uuid", project), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-1-uuid"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-2-uuid"), moduleSnapshot);
db.commit();
componentDb.indexProjects();
ComponentTreeQuery query = newTreeQuery(projectSnapshot).build();
List<ComponentDto> result = underTest.selectDirectChildren(dbSession, query);
int count = underTest.countDirectChildren(dbSession, query);
assertThat(count).isEqualTo(2);
assertThat(result).extracting("uuid").containsExactly("file-1-uuid", "module-1-uuid");
}
@Test
public void select_direct_children_with_name_query() {
ComponentDto project = newProjectDto().setKey("project-key").setUuid("project-uuid");
SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(project);
SnapshotDto moduleSnapshot = componentDb.insertComponentAndSnapshot(newModuleDto("module-1-uuid", project), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-1-uuid").setName("file-name-1"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-2-uuid").setName("file-name-2"), moduleSnapshot);
db.commit();
componentDb.indexProjects();
ComponentTreeQuery query = newTreeQuery(projectSnapshot)
.setNameOrKeyQuery("file-name").build();
List<ComponentDto> result = underTest.selectDirectChildren(dbSession, query);
int count = underTest.countDirectChildren(dbSession, query);
assertThat(count).isEqualTo(1);
assertThat(result).extracting("uuid").containsExactly("file-1-uuid");
}
@Test
public void select_direct_children_with_key_query() {
ComponentDto project = newProjectDto().setKey("project-key").setUuid("project-uuid");
SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(project);
SnapshotDto moduleSnapshot = componentDb.insertComponentAndSnapshot(newModuleDto("module-1-uuid", project), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-1-uuid").setKey("file-key-1"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-2-uuid").setKey("file-key-2"), moduleSnapshot);
db.commit();
componentDb.indexProjects();
ComponentTreeQuery query = newTreeQuery(projectSnapshot)
.setNameOrKeyQuery("file-key").build();
List<ComponentDto> result = underTest.selectDirectChildren(dbSession, query);
int count = underTest.countDirectChildren(dbSession, query);
assertThat(count).isEqualTo(1);
assertThat(result).extracting("uuid").containsExactly("file-1-uuid");
}
@Test
public void select_direct_children_with_pagination() {
ComponentDto project = newProjectDto().setKey("project-key").setUuid("project-uuid");
SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(project);
for (int i = 1; i <= 9; i++) {
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-uuid-" + i), projectSnapshot);
}
db.commit();
componentDb.indexProjects();
ComponentTreeQuery query = newTreeQuery(projectSnapshot)
.setPage(2)
.setPageSize(3)
.setAsc(false)
.build();
List<ComponentDto> result = underTest.selectDirectChildren(dbSession, query);
int count = underTest.countDirectChildren(dbSession, query);
assertThat(count).isEqualTo(9);
assertThat(result).extracting("uuid").containsExactly("file-uuid-6", "file-uuid-5", "file-uuid-4");
}
@Test
public void select_direct_children_with_order_by_path() {
ComponentDto project = newProjectDto();
SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(project);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-uuid-1").setName("file-name-1").setPath("3"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-uuid-2").setName("file-name-2").setPath("2"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-uuid-3").setName("file-name-3").setPath("1"), projectSnapshot);
db.commit();
componentDb.indexProjects();
ComponentTreeQuery query = newTreeQuery(projectSnapshot)
.setSortFields(singletonList("path"))
.setAsc(true)
.build();
List<ComponentDto> result = underTest.selectDirectChildren(dbSession, query);
assertThat(result).extracting("uuid").containsExactly("file-uuid-3", "file-uuid-2", "file-uuid-1");
}
@Test
public void select_direct_children_of_a_module() {
ComponentDto project = newProjectDto();
SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(project);
SnapshotDto moduleSnapshot = componentDb.insertComponentAndSnapshot(newModuleDto("module-1-uuid", project), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-1-uuid"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-2-uuid"), moduleSnapshot);
db.commit();
componentDb.indexProjects();
ComponentTreeQuery query = newTreeQuery(moduleSnapshot).build();
List<ComponentDto> result = underTest.selectDirectChildren(dbSession, query);
assertThat(result).extracting("uuid").containsOnly("file-2-uuid");
}
@Test
public void select_all_children_of_a_project() {
ComponentDto project = newProjectDto().setKey("project-key").setUuid("project-uuid");
SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(project);
SnapshotDto moduleSnapshot = componentDb.insertComponentAndSnapshot(newModuleDto("module-1-uuid", project), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-1-uuid"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-2-uuid"), moduleSnapshot);
db.commit();
componentDb.indexProjects();
ComponentTreeQuery query = newTreeQuery(projectSnapshot).build();
List<ComponentDto> result = underTest.selectAllChildren(dbSession, query);
int count = underTest.countAllChildren(dbSession, query);
assertThat(count).isEqualTo(3);
assertThat(result).extracting("uuid").containsExactly("file-1-uuid", "file-2-uuid", "module-1-uuid");
}
@Test
public void select_all_files_of_a_project_paginated_and_ordered() {
ComponentDto project = newProjectDto().setKey("project-key").setUuid("project-uuid");
SnapshotDto projectSnapshot = componentDb.insertProjectAndSnapshot(project);
SnapshotDto moduleSnapshot = componentDb.insertComponentAndSnapshot(newModuleDto("module-1-uuid", project), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-uuid-1").setName("file-name-1"), projectSnapshot);
componentDb.insertComponentAndSnapshot(newFileDto(project, "another-uuid"), projectSnapshot);
for (int i = 2; i <= 9; i++) {
componentDb.insertComponentAndSnapshot(newFileDto(project, "file-uuid-" + i).setName("file-name-" + i), moduleSnapshot);
}
db.commit();
componentDb.indexProjects();
ComponentTreeQuery query = newTreeQuery(projectSnapshot)
.setQualifiers(newArrayList(Qualifiers.FILE))
.setPage(2)
.setPageSize(3)
.setNameOrKeyQuery("file-name")
.setSortFields(singletonList("name"))
.setAsc(false)
.build();
List<ComponentDto> result = underTest.selectAllChildren(dbSession, query);
int count = underTest.countAllChildren(dbSession, query);
assertThat(count).isEqualTo(9);
assertThat(result).extracting("uuid").containsExactly("file-uuid-6", "file-uuid-5", "file-uuid-4");
}
private static ComponentTreeQuery.Builder newTreeQuery(SnapshotDto baseSnapshot) {
return ComponentTreeQuery.builder()
.setPage(1)
.setPageSize(500)
.setBaseSnapshot(baseSnapshot)
.setSortFields(singletonList("name"))
.setAsc(true)
.setQualifiers(newArrayList(Qualifiers.FILE, Qualifiers.MODULE, Qualifiers.DIRECTORY, Qualifiers.PROJECT));
}
}
|