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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
|
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.property;
import com.google.common.collect.ImmutableMap;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.organization.OrganizationTesting;
import org.sonar.db.user.UserDto;
import org.sonar.db.user.UserTesting;
import static com.google.common.collect.Sets.newHashSet;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.groups.Tuple.tuple;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.sonar.db.property.PropertyTesting.newComponentPropertyDto;
import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto;
import static org.sonar.db.property.PropertyTesting.newUserPropertyDto;
@RunWith(DataProviderRunner.class)
public class PropertiesDaoTest {
private static final String VALUE_SIZE_4000 = String.format("%1$4000.4000s", "*");
private static final String VALUE_SIZE_4001 = VALUE_SIZE_4000 + "P";
private static final long DATE_1 = 1_555_000L;
private static final long DATE_2 = 1_666_000L;
private static final long DATE_3 = 1_777_000L;
private static final long DATE_4 = 1_888_000L;
private static final long DATE_5 = 1_999_000L;
private System2 system2 = mock(System2.class);
@Rule
public ExpectedException thrown = ExpectedException.none();
@Rule
public DbTester dbTester = DbTester.create(system2);
private DbClient dbClient = dbTester.getDbClient();
private DbSession session = dbTester.getSession();
private PropertiesDao underTest = dbTester.getDbClient().propertiesDao();
@Test
public void shouldFindUsersForNotification() throws SQLException {
ComponentDto project1 = insertProject("uuid_45");
ComponentDto project2 = insertProject("uuid_56");
int userId1 = insertUser("user1");
int userId2 = insertUser("user2");
int userId3 = insertUser("user3");
insertProperty("notification.NewViolations.Email", "true", project1.getId(), userId2);
insertProperty("notification.NewViolations.Twitter", "true", null, userId3);
insertProperty("notification.NewViolations.Twitter", "true", project2.getId(), userId1);
insertProperty("notification.NewViolations.Twitter", "true", project2.getId(), userId3);
assertThat(underTest.selectUsersForNotification("NewViolations", "Email", null))
.isEmpty();
assertThat(underTest.selectUsersForNotification("NewViolations", "Email", "uuid_78"))
.isEmpty();
assertThat(underTest.selectUsersForNotification("NewViolations", "Email", "uuid_45"))
.hasSize(1).containsOnly("user2");
assertThat(underTest.selectUsersForNotification("NewViolations", "Twitter", null))
.hasSize(1)
.containsOnly("user3");
assertThat(underTest.selectUsersForNotification("NewViolations", "Twitter", "uuid_78"))
.isEmpty();
assertThat(underTest.selectUsersForNotification("NewViolations", "Twitter", "uuid_56"))
.hasSize(2)
.containsOnly("user1", "user3");
}
@Test
public void findNotificationSubscribers() throws SQLException {
int userId1 = insertUser("user1");
int userId2 = insertUser("user2");
ComponentDto projectDto = insertProject("PROJECT_A");
long projectId = projectDto.getId();
String projectKey = projectDto.key();
// global subscription
insertProperty("notification.DispatcherWithGlobalSubscribers.Email", "true", null, userId2);
// project subscription
insertProperty("notification.DispatcherWithProjectSubscribers.Email", "true", projectId, userId1);
insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", 56L, userId1);
insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", projectId, userId1);
// global subscription
insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", null, userId2);
// Nobody is subscribed
assertThat(underTest.selectNotificationSubscribers("NotSexyDispatcher", "Email", projectKey))
.isEmpty();
// Global subscribers
assertThat(underTest.selectNotificationSubscribers("DispatcherWithGlobalSubscribers", "Email", projectKey))
.containsOnly("user2");
assertThat(underTest.selectNotificationSubscribers("DispatcherWithGlobalSubscribers", "Email", null))
.containsOnly("user2");
// Project subscribers
assertThat(underTest.selectNotificationSubscribers("DispatcherWithProjectSubscribers", "Email", projectKey))
.containsOnly("user1");
// Global + Project subscribers
assertThat(underTest.selectNotificationSubscribers("DispatcherWithGlobalAndProjectSubscribers", "Email", projectKey))
.containsOnly("user1", "user2");
}
@Test
public void hasNotificationSubscribers() throws SQLException {
int userId1 = insertUser("user1");
int userId2 = insertUser("user2");
Long projectId = insertProject("PROJECT_A").getId();
// global subscription
insertProperty("notification.DispatcherWithGlobalSubscribers.Email", "true", null, userId2);
// project subscription
insertProperty("notification.DispatcherWithProjectSubscribers.Email", "true", projectId, userId1);
insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", 56L, userId1);
insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", projectId, userId1);
// global subscription
insertProperty("notification.DispatcherWithGlobalAndProjectSubscribers.Email", "true", null, userId2);
// Nobody is subscribed
assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", singletonList("NotSexyDispatcher")))
.isFalse();
// Global subscribers
assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", singletonList("DispatcherWithGlobalSubscribers")))
.isTrue();
// Project subscribers
assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", singletonList("DispatcherWithProjectSubscribers")))
.isTrue();
assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_B", singletonList("DispatcherWithProjectSubscribers")))
.isFalse();
// Global + Project subscribers
assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_A", singletonList("DispatcherWithGlobalAndProjectSubscribers")))
.isTrue();
assertThat(underTest.hasProjectNotificationSubscribersForDispatchers("PROJECT_B", singletonList("DispatcherWithGlobalAndProjectSubscribers")))
.isTrue();
}
@Test
public void selectGlobalProperties() throws SQLException {
// global
long id1 = insertProperty("global.one", "one", null, null);
long id2 = insertProperty("global.two", "two", null, null);
List<PropertyDto> properties = underTest.selectGlobalProperties();
assertThat(properties.size())
.isEqualTo(2);
assertThatDto(findByKey(properties, "global.one"))
.hasKey("global.one")
.hasNoUserId()
.hasNoResourceId()
.hasValue("one");
assertThatDto(findByKey(properties, "global.two"))
.hasKey("global.two")
.hasNoResourceId()
.hasNoUserId()
.hasValue("two");
}
@Test
@UseDataProvider("allValuesForSelect")
public void selectGlobalProperties_supports_all_values(String dbValue, String expected) throws SQLException {
insertProperty("global.one", dbValue, null, null);
List<PropertyDto> dtos = underTest.selectGlobalProperties();
assertThat(dtos)
.hasSize(1);
assertThatDto(dtos.iterator().next())
.hasKey("global.one")
.hasNoResourceId()
.hasNoUserId()
.hasValue(expected);
}
@Test
public void selectGlobalProperty() throws SQLException {
// global
insertProperty("global.one", "one", null, null);
insertProperty("global.two", "two", null, null);
// project
insertProperty("project.one", "one", 10L, null);
// user
insertProperty("user.one", "one", null, 100);
assertThatDto(underTest.selectGlobalProperty("global.one"))
.hasNoResourceId()
.hasNoUserId()
.hasValue("one");
assertThat(underTest.selectGlobalProperty("project.one")).isNull();
assertThat(underTest.selectGlobalProperty("user.one")).isNull();
assertThat(underTest.selectGlobalProperty("unexisting")).isNull();
}
@Test
@UseDataProvider("allValuesForSelect")
public void selectGlobalProperty_supports_all_values(String dbValue, String expected) throws SQLException {
insertProperty("global.one", dbValue, null, null);
assertThatDto(underTest.selectGlobalProperty("global.one"))
.hasNoResourceId()
.hasNoUserId()
.hasValue(expected);
}
@Test
public void selectProjectProperties() throws SQLException {
ComponentDto projectDto = insertProject("A");
long projectId = projectDto.getId();
// global
insertProperty("global.one", "one", null, null);
insertProperty("global.two", "two", null, null);
// project
insertProperty("project.one", "Pone", projectId, null);
insertProperty("project.two", "Ptwo", projectId, null);
List<PropertyDto> dtos = underTest.selectProjectProperties(projectDto.key());
assertThat(dtos)
.hasSize(2);
assertThatDto(findByKey(dtos, "project.one"))
.hasKey("project.one")
.hasResourceId(projectId)
.hasValue("Pone");
assertThatDto(findByKey(dtos, "project.two"))
.hasKey("project.two")
.hasResourceId(projectId)
.hasValue("Ptwo");
}
@Test
@UseDataProvider("allValuesForSelect")
public void selectProjectProperties_supports_all_values(String dbValue, String expected) throws SQLException {
ComponentDto projectDto = insertProject("A");
insertProperty("project.one", dbValue, projectDto.getId(), null);
List<PropertyDto> dtos = underTest.selectProjectProperties(projectDto.key());
assertThat(dtos).hasSize(1);
assertThatDto(dtos.iterator().next())
.hasKey("project.one")
.hasResourceId(projectDto.getId())
.hasValue(expected);
}
@DataProvider
public static Object[][] allValuesForSelect() {
return new Object[][] {
{null, ""},
{"", ""},
{"some value", "some value"},
{VALUE_SIZE_4000, VALUE_SIZE_4000},
{VALUE_SIZE_4001, VALUE_SIZE_4001}
};
}
@Test
public void selectProjectProperty() throws SQLException {
insertProperty("project.one", "one", 10L, null);
PropertyDto property = underTest.selectProjectProperty(10L, "project.one");
assertThatDto(property)
.hasKey("project.one")
.hasResourceId(10L)
.hasNoUserId()
.hasValue("one");
}
@Test
public void selectEnabledDescendantModuleProperties() {
dbTester.prepareDbUnit(getClass(), "select_module_properties_tree.xml");
List<PropertyDto> properties = underTest.selectEnabledDescendantModuleProperties("ABCD", dbTester.getSession());
assertThat(properties.size()).isEqualTo(4);
assertThat(properties).extracting("key").containsOnly("struts.one", "core.one", "core.two", "data.one");
assertThat(properties).extracting("value").containsOnly("one", "two");
properties = underTest.selectEnabledDescendantModuleProperties("EFGH", dbTester.getSession());
assertThat(properties.size()).isEqualTo(3);
assertThat(properties).extracting("key").containsOnly("core.one", "core.two", "data.one");
properties = underTest.selectEnabledDescendantModuleProperties("FGHI", dbTester.getSession());
assertThat(properties.size()).isEqualTo(1);
assertThat(properties).extracting("key").containsOnly("data.one");
assertThat(underTest.selectEnabledDescendantModuleProperties("unknown-result.xml", dbTester.getSession()).size()).isEqualTo(0);
}
@Test
@UseDataProvider("allValuesForSelect")
public void selectEnabledDescendantModuleProperties_supports_all_values(String dbValue, String expected) throws SQLException {
String projectUuid = "A";
ComponentDto project = ComponentTesting.newProjectDto(OrganizationTesting.newOrganizationDto(), projectUuid);
dbClient.componentDao().insert(session, project);
long projectId = project.getId();
insertProperty("project.one", dbValue, projectId, null);
List<PropertyDto> dtos = underTest.selectEnabledDescendantModuleProperties(projectUuid, dbTester.getSession());
assertThat(dtos)
.hasSize(1);
assertThatDto(dtos.iterator().next())
.hasKey("project.one")
.hasResourceId(projectId)
.hasNoUserId()
.hasValue(expected);
}
@Test
public void select_by_query() throws SQLException {
// global
insertProperty("global.one", "one", null, null);
insertProperty("global.two", "two", null, null);
// struts
insertProperty("struts.one", "one", 10L, null);
// commons
insertProperty("commonslang.one", "one", 11L, null);
// user
insertProperty("user.one", "one", null, 100);
insertProperty("user.two", "two", 10L, 100);
// other
insertProperty("other.one", "one", 12L, null);
List<PropertyDto> results = underTest.selectByQuery(PropertyQuery.builder().setKey("user.two").setComponentId(10L).setUserId(100).build(), dbTester.getSession());
assertThat(results).hasSize(1);
assertThat(results.get(0).getValue()).isEqualTo("two");
results = underTest.selectByQuery(PropertyQuery.builder().setKey("user.one").setUserId(100).build(), dbTester.getSession());
assertThat(results).hasSize(1);
assertThat(results.get(0).getValue()).isEqualTo("one");
}
@Test
public void select_global_properties_by_keys() throws Exception {
insertProject("A");
int userId = insertUser("B");
String key = "key";
String anotherKey = "anotherKey";
insertProperty(key, "value", null, null);
insertProperty(key, "value", 10L, null);
insertProperty(key, "value", null, userId);
insertProperty(anotherKey, "value", null, null);
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key)))
.extracting("key")
.containsOnly(key);
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key, anotherKey)))
.extracting("key")
.containsOnly(key, anotherKey);
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet(key, anotherKey, "unknown")))
.extracting("key")
.containsOnly(key, anotherKey);
assertThat(underTest.selectGlobalPropertiesByKeys(session, newHashSet("unknown")))
.isEmpty();
}
@Test
public void select_component_properties_by_keys() throws Exception {
ComponentDto project = dbTester.components().insertProject();
UserDto user = dbTester.users().insertUser();
String key = "key";
String anotherKey = "anotherKey";
insertProperties(
newGlobalPropertyDto().setKey(key),
newComponentPropertyDto(project).setKey(key),
newUserPropertyDto(user).setKey(key),
newComponentPropertyDto(project).setKey(anotherKey));
assertThat(underTest.selectPropertiesByKeysAndComponentId(session, newHashSet(key), project.getId())).extracting("key").containsOnly(key);
assertThat(underTest.selectPropertiesByKeysAndComponentId(session, newHashSet(key, anotherKey), project.getId())).extracting("key").containsOnly(key, anotherKey);
assertThat(underTest.selectPropertiesByKeysAndComponentId(session, newHashSet(key, anotherKey, "unknown"), project.getId())).extracting("key").containsOnly(key, anotherKey);
assertThat(underTest.selectPropertiesByKeysAndComponentId(session, newHashSet("unknown"), project.getId())).isEmpty();
assertThat(underTest.selectPropertiesByKeysAndComponentId(session, newHashSet(key), 123456789L)).isEmpty();
}
@Test
public void select_component_properties_by_ids() throws Exception {
ComponentDto project = dbTester.components().insertProject();
ComponentDto project2 = dbTester.components().insertProject();
UserDto user = UserTesting.newUserDto();
dbClient.userDao().insert(session, user);
String key = "key";
String anotherKey = "anotherKey";
insertProperties(
newGlobalPropertyDto().setKey(key),
newComponentPropertyDto(project).setKey(key),
newComponentPropertyDto(project2).setKey(key),
newComponentPropertyDto(project2).setKey(anotherKey),
newUserPropertyDto(user).setKey(key));
assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(project.getId())))
.extracting("key", "resourceId").containsOnly(tuple(key, project.getId()));
assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(project.getId(), project2.getId())))
.extracting("key", "resourceId").containsOnly(
tuple(key, project.getId()),
tuple(key, project2.getId()),
tuple(anotherKey, project2.getId())
);
assertThat(underTest.selectPropertiesByComponentIds(session, newHashSet(123456789L))).isEmpty();
}
@Test
public void select_properties_by_keys_and_component_ids() throws Exception {
ComponentDto project = dbTester.components().insertProject();
ComponentDto project2 = dbTester.components().insertProject();
UserDto user = UserTesting.newUserDto();
dbClient.userDao().insert(session, user);
String key = "key";
String anotherKey = "anotherKey";
insertProperties(
newGlobalPropertyDto().setKey(key),
newComponentPropertyDto(project).setKey(key),
newComponentPropertyDto(project2).setKey(key),
newComponentPropertyDto(project2).setKey(anotherKey),
newUserPropertyDto(user).setKey(key));
assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key), newHashSet(project.getId())))
.extracting("key", "resourceId").containsOnly(tuple(key, project.getId()));
assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key), newHashSet(project.getId(), project2.getId())))
.extracting("key", "resourceId").containsOnly(
tuple(key, project.getId()),
tuple(key, project2.getId()));
assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet(key, anotherKey), newHashSet(project.getId(), project2.getId())))
.extracting("key", "resourceId").containsOnly(
tuple(key, project.getId()),
tuple(key, project2.getId()),
tuple(anotherKey, project2.getId()));
assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet("unknown"), newHashSet(project.getId()))).isEmpty();
assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet("key"), newHashSet(123456789L))).isEmpty();
assertThat(underTest.selectPropertiesByKeysAndComponentIds(session, newHashSet("unknown"), newHashSet(123456789L))).isEmpty();
}
@Test
public void saveProperty_inserts_global_properties_when_they_do_not_exist_in_db() {
when(system2.now()).thenReturn(DATE_1, DATE_2, DATE_3, DATE_4, DATE_5);
underTest.saveProperty(new PropertyDto().setKey("global.null").setValue(null));
underTest.saveProperty(new PropertyDto().setKey("global.empty").setValue(""));
underTest.saveProperty(new PropertyDto().setKey("global.text").setValue("some text"));
underTest.saveProperty(new PropertyDto().setKey("global.4000").setValue(VALUE_SIZE_4000));
underTest.saveProperty(new PropertyDto().setKey("global.clob").setValue(VALUE_SIZE_4001));
assertThatPropertiesRow("global.null")
.hasNoResourceId()
.hasNoUserId()
.isEmpty()
.hasCreatedAt(DATE_1);
assertThatPropertiesRow("global.empty")
.hasNoResourceId()
.hasNoUserId()
.isEmpty()
.hasCreatedAt(DATE_2);
assertThatPropertiesRow("global.text")
.hasNoResourceId()
.hasNoUserId()
.hasTextValue("some text")
.hasCreatedAt(DATE_3);
assertThatPropertiesRow("global.4000")
.hasNoResourceId()
.hasNoUserId()
.hasTextValue(VALUE_SIZE_4000)
.hasCreatedAt(DATE_4);
assertThatPropertiesRow("global.clob")
.hasNoResourceId()
.hasNoUserId()
.hasClobValue(VALUE_SIZE_4001)
.hasCreatedAt(DATE_5);
}
@Test
public void saveProperty_inserts_component_properties_when_they_do_not_exist_in_db() {
when(system2.now()).thenReturn(DATE_1, DATE_2, DATE_3, DATE_4, DATE_5);
long resourceId = 12;
underTest.saveProperty(new PropertyDto().setKey("component.null").setResourceId(resourceId).setValue(null));
underTest.saveProperty(new PropertyDto().setKey("component.empty").setResourceId(resourceId).setValue(""));
underTest.saveProperty(new PropertyDto().setKey("component.text").setResourceId(resourceId).setValue("some text"));
underTest.saveProperty(new PropertyDto().setKey("component.4000").setResourceId(resourceId).setValue(VALUE_SIZE_4000));
underTest.saveProperty(new PropertyDto().setKey("component.clob").setResourceId(resourceId).setValue(VALUE_SIZE_4001));
assertThatPropertiesRow("component.null")
.hasResourceId(resourceId)
.hasNoUserId()
.isEmpty()
.hasCreatedAt(DATE_1);
assertThatPropertiesRow("component.empty")
.hasResourceId(resourceId)
.hasNoUserId()
.isEmpty()
.hasCreatedAt(DATE_2);
assertThatPropertiesRow("component.text")
.hasResourceId(resourceId)
.hasNoUserId()
.hasTextValue("some text")
.hasCreatedAt(DATE_3);
assertThatPropertiesRow("component.4000")
.hasResourceId(resourceId)
.hasNoUserId()
.hasTextValue(VALUE_SIZE_4000)
.hasCreatedAt(DATE_4);
assertThatPropertiesRow("component.clob")
.hasResourceId(resourceId)
.hasNoUserId()
.hasClobValue(VALUE_SIZE_4001)
.hasCreatedAt(DATE_5);
}
@Test
public void saveProperty_inserts_user_properties_when_they_do_not_exist_in_db() {
when(system2.now()).thenReturn(DATE_1, DATE_2, DATE_3, DATE_4, DATE_5);
int userId = 100;
underTest.saveProperty(new PropertyDto().setKey("user.null").setUserId(userId).setValue(null));
underTest.saveProperty(new PropertyDto().setKey("user.empty").setUserId(userId).setValue(""));
underTest.saveProperty(new PropertyDto().setKey("user.text").setUserId(userId).setValue("some text"));
underTest.saveProperty(new PropertyDto().setKey("user.4000").setUserId(userId).setValue(VALUE_SIZE_4000));
underTest.saveProperty(new PropertyDto().setKey("user.clob").setUserId(userId).setValue(VALUE_SIZE_4001));
assertThatPropertiesRow("user.null")
.hasNoResourceId()
.hasUserId(userId)
.isEmpty()
.hasCreatedAt(DATE_1);
assertThatPropertiesRow("user.empty")
.hasNoResourceId()
.hasUserId(userId)
.isEmpty()
.hasCreatedAt(DATE_2);
assertThatPropertiesRow("user.text")
.hasNoResourceId()
.hasUserId(userId)
.hasTextValue("some text")
.hasCreatedAt(DATE_3);
assertThatPropertiesRow("user.4000")
.hasNoResourceId()
.hasUserId(userId)
.hasTextValue(VALUE_SIZE_4000)
.hasCreatedAt(DATE_4);
assertThatPropertiesRow("user.clob")
.hasNoResourceId()
.hasUserId(userId)
.hasClobValue(VALUE_SIZE_4001)
.hasCreatedAt(DATE_5);
}
@Test
@UseDataProvider("valueUpdatesDataProvider")
public void saveProperty_deletes_then_inserts_global_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) throws SQLException {
long id = insertProperty("global", oldValue, null, null, DATE_1);
when(system2.now()).thenReturn(DATE_4);
underTest.saveProperty(new PropertyDto().setKey("global").setValue(newValue));
assertThatPropertiesRow(id)
.doesNotExist();
PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global")
.hasNoResourceId()
.hasNoUserId()
.hasCreatedAt(DATE_4);
if (newValue == null || newValue.isEmpty()) {
propertiesRowAssert.isEmpty();
} else if (newValue.length() > 4000) {
propertiesRowAssert.hasClobValue(newValue);
} else {
propertiesRowAssert.hasTextValue(newValue);
}
}
@Test
@UseDataProvider("valueUpdatesDataProvider")
public void saveProperty_deletes_then_inserts_component_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) throws SQLException {
long resourceId = 999L;
long id = insertProperty("global", oldValue, resourceId, null, DATE_1);
when(system2.now()).thenReturn(DATE_4);
underTest.saveProperty(new PropertyDto().setKey("global").setResourceId(resourceId).setValue(newValue));
assertThatPropertiesRow(id)
.doesNotExist();
PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global")
.hasResourceId(resourceId)
.hasNoUserId()
.hasCreatedAt(DATE_4);
if (newValue == null || newValue.isEmpty()) {
propertiesRowAssert.isEmpty();
} else if (newValue.length() > 4000) {
propertiesRowAssert.hasClobValue(newValue);
} else {
propertiesRowAssert.hasTextValue(newValue);
}
}
@Test
@UseDataProvider("valueUpdatesDataProvider")
public void saveProperty_deletes_then_inserts_user_properties_when_they_exist_in_db(@Nullable String oldValue, @Nullable String newValue) throws SQLException {
int userId = 90;
long id = insertProperty("global", oldValue, null, userId, DATE_1);
when(system2.now()).thenReturn(DATE_4);
underTest.saveProperty(new PropertyDto().setKey("global").setUserId(userId).setValue(newValue));
assertThatPropertiesRow(id)
.doesNotExist();
PropertiesRowAssert propertiesRowAssert = assertThatPropertiesRow("global")
.hasNoResourceId()
.hasUserId(userId)
.hasCreatedAt(DATE_4);
if (newValue == null || newValue.isEmpty()) {
propertiesRowAssert.isEmpty();
} else if (newValue.length() > 4000) {
propertiesRowAssert.hasClobValue(newValue);
} else {
propertiesRowAssert.hasTextValue(newValue);
}
}
@DataProvider
public static Object[][] valueUpdatesDataProvider() {
return new Object[][] {
{null, null},
{null, ""},
{null, "some value"},
{null, VALUE_SIZE_4000},
{null, VALUE_SIZE_4001},
{"", null},
{"", ""},
{"", "some value"},
{"", VALUE_SIZE_4000},
{"", VALUE_SIZE_4001},
{"a value", null},
{"a value", ""},
{"a value", "a value"},
{"a value", "some value"},
{"a value", VALUE_SIZE_4000},
{"a value", VALUE_SIZE_4001},
{VALUE_SIZE_4000, null},
{VALUE_SIZE_4000, ""},
{VALUE_SIZE_4000, "a value"},
{VALUE_SIZE_4000, VALUE_SIZE_4000},
{VALUE_SIZE_4000, VALUE_SIZE_4000.substring(1) + "a"},
{VALUE_SIZE_4000, VALUE_SIZE_4001},
{VALUE_SIZE_4001, null},
{VALUE_SIZE_4001, ""},
{VALUE_SIZE_4001, "a value"},
{VALUE_SIZE_4001, VALUE_SIZE_4000},
{VALUE_SIZE_4001, VALUE_SIZE_4001},
{VALUE_SIZE_4001, VALUE_SIZE_4001 + "dfsdfs"},
};
}
@Test
public void delete_project_property() throws SQLException {
long projectId1 = insertProject("A").getId();
long projectId2 = insertProject("B").getId();
long projectId3 = insertProject("C").getId();
long id1 = insertProperty("global.one", "one", null, null);
long id2 = insertProperty("global.two", "two", null, null);
long id3 = insertProperty("struts.one", "one", projectId1, null);
long id4 = insertProperty("commonslang.one", "one", projectId2, null);
long id5 = insertProperty("user.one", "one", null, 100);
long id6 = insertProperty("user.two", "two", null, 100);
long id7 = insertProperty("other.one", "one", projectId3, null);
underTest.deleteProjectProperty("struts.one", projectId1);
assertThatPropertiesRow(id1)
.hasKey("global.one")
.hasNoResourceId()
.hasNoUserId()
.hasTextValue("one");
assertThatPropertiesRow(id2)
.hasKey("global.two")
.hasNoResourceId()
.hasNoUserId()
.hasTextValue("two");
assertThatPropertiesRow(id3)
.doesNotExist();
assertThatPropertiesRow(id4)
.hasKey("commonslang.one")
.hasResourceId(projectId2)
.hasNoUserId()
.hasTextValue("one");
assertThatPropertiesRow(id5)
.hasKey("user.one")
.hasNoResourceId()
.hasUserId(100)
.hasTextValue("one");
assertThatPropertiesRow(id6)
.hasKey("user.two")
.hasNoResourceId()
.hasUserId(100)
.hasTextValue("two");
assertThatPropertiesRow(id7)
.hasKey("other.one")
.hasResourceId(projectId3)
.hasNoUserId()
.hasTextValue("one");
}
@Test
public void delete_project_properties() throws SQLException {
long id1 = insertProperty("sonar.profile.java", "Sonar Way", 1L, null);
long id2 = insertProperty("sonar.profile.java", "Sonar Way", 2L, null);
long id3 = insertProperty("sonar.profile.java", "Sonar Way", null, null);
long id4 = insertProperty("sonar.profile.js", "Sonar Way", 1L, null);
long id5 = insertProperty("sonar.profile.js", "Sonar Way", 2L, null);
long id6 = insertProperty("sonar.profile.js", "Sonar Way", null, null);
underTest.deleteProjectProperties("sonar.profile.java", "Sonar Way");
assertThatPropertiesRow(id1)
.doesNotExist();
assertThatPropertiesRow(id2)
.doesNotExist();
assertThatPropertiesRow(id3)
.hasKey("sonar.profile.java")
.hasNoResourceId()
.hasNoUserId()
.hasTextValue("Sonar Way");
assertThatPropertiesRow(id4)
.hasKey("sonar.profile.js")
.hasResourceId(1)
.hasNoUserId()
.hasTextValue("Sonar Way");
assertThatPropertiesRow(id5)
.hasKey("sonar.profile.js")
.hasResourceId(2)
.hasNoUserId()
.hasTextValue("Sonar Way");
assertThatPropertiesRow(id6)
.hasKey("sonar.profile.js")
.hasNoResourceId()
.hasNoUserId()
.hasTextValue("Sonar Way");
}
@Test
public void deleteGlobalProperty() throws SQLException {
// global
long id1 = insertProperty("global.key", "new_global", null, null);
long id2 = insertProperty("to_be_deleted", "xxx", null, null);
// project - do not delete this project property that has the same key
long id3 = insertProperty("to_be_deleted", "new_project", 10L, null);
// user
long id4 = insertProperty("user.key", "new_user", null, 100);
underTest.deleteGlobalProperty("to_be_deleted");
assertThatPropertiesRow(id1)
.hasKey("global.key")
.hasNoUserId()
.hasNoResourceId()
.hasTextValue("new_global");
assertThatPropertiesRow(id2)
.doesNotExist();
assertThatPropertiesRow("to_be_deleted", null, null)
.doesNotExist();
assertThatPropertiesRow(id3)
.hasKey("to_be_deleted")
.hasResourceId(10)
.hasNoUserId()
.hasTextValue("new_project");
assertThatPropertiesRow(id4)
.hasKey("user.key")
.hasNoResourceId()
.hasUserId(100)
.hasTextValue("new_user");
}
@Test
public void saveGlobalProperties_insert_property_if_does_not_exist_in_db() {
when(system2.now()).thenReturn(DATE_1, DATE_2, DATE_3, DATE_4, DATE_5);
underTest.saveGlobalProperties(mapOf(
"null_value_property", null,
"empty_value_property", "",
"text_value_property", "dfdsfsd",
"4000_char_value_property", VALUE_SIZE_4000,
"clob_value_property", VALUE_SIZE_4001));
assertThatPropertiesRow("null_value_property")
.hasNoResourceId()
.hasNoUserId()
.isEmpty()
.hasCreatedAt(DATE_1);
assertThatPropertiesRow("empty_value_property")
.hasNoResourceId()
.hasNoUserId()
.isEmpty()
.hasCreatedAt(DATE_2);
assertThatPropertiesRow("text_value_property")
.hasNoResourceId()
.hasNoUserId()
.hasTextValue("dfdsfsd")
.hasCreatedAt(DATE_3);
assertThatPropertiesRow("4000_char_value_property")
.hasNoResourceId()
.hasNoUserId()
.hasTextValue(VALUE_SIZE_4000)
.hasCreatedAt(DATE_4);
assertThatPropertiesRow("clob_value_property")
.hasNoResourceId()
.hasNoUserId()
.hasClobValue(VALUE_SIZE_4001)
.hasCreatedAt(DATE_5);
}
@Test
public void saveGlobalProperties_delete_and_insert_new_value_when_property_exists_in_db() throws SQLException {
long id = insertProperty("to_be_updated", "old_value", null, null, DATE_1);
when(system2.now()).thenReturn(DATE_3);
underTest.saveGlobalProperties(ImmutableMap.of("to_be_updated", "new value"));
assertThatPropertiesRow(id)
.doesNotExist();
assertThatPropertiesRow("to_be_updated")
.hasNoResourceId()
.hasNoUserId()
.hasTextValue("new value")
.hasCreatedAt(DATE_3);
}
private static Map<String, String> mapOf(String... values) {
// use LinkedHashMap to keep order of array
Map<String, String> res = new LinkedHashMap<>(values.length / 2);
Iterator<String> iterator = Arrays.asList(values).iterator();
while (iterator.hasNext()) {
res.put(iterator.next(), iterator.next());
}
return res;
}
@Test
public void renamePropertyKey_updates_global_component_and_user_properties() throws SQLException {
long id1 = insertProperty("foo", "bar", null, null, DATE_1);
long id2 = insertProperty("old_name", "doc1", null, null, DATE_1);
long id3 = insertProperty("old_name", "doc2", 15L, null, DATE_1);
long id4 = insertProperty("old_name", "doc3", 16L, null, DATE_1);
long id5 = insertProperty("old_name", "doc4", null, 100, DATE_1);
long id6 = insertProperty("old_name", "doc5", null, 101, DATE_1);
underTest.renamePropertyKey("old_name", "new_name");
assertThatPropertiesRow(id1)
.hasKey("foo")
.hasNoUserId()
.hasNoResourceId()
.hasTextValue("bar")
.hasCreatedAt(DATE_1);
assertThatPropertiesRow(id2)
.hasKey("new_name")
.hasNoResourceId()
.hasNoUserId()
.hasTextValue("doc1")
.hasCreatedAt(DATE_1);
assertThatPropertiesRow(id3)
.hasKey("new_name")
.hasResourceId(15)
.hasNoUserId()
.hasTextValue("doc2")
.hasCreatedAt(DATE_1);
assertThatPropertiesRow(id4)
.hasKey("new_name")
.hasResourceId(16)
.hasNoUserId()
.hasTextValue("doc3")
.hasCreatedAt(DATE_1);
assertThatPropertiesRow(id5)
.hasKey("new_name")
.hasNoResourceId()
.hasUserId(100)
.hasTextValue("doc4")
.hasCreatedAt(DATE_1);
assertThatPropertiesRow(id6)
.hasKey("new_name")
.hasNoResourceId()
.hasUserId(101)
.hasTextValue("doc5")
.hasCreatedAt(DATE_1);
}
@Test
public void rename_to_same_key_has_no_effect() throws SQLException {
long now = 1_890_999L;
long id = insertProperty("foo", "bar", null, null, now);
assertThatPropertiesRow(id)
.hasCreatedAt(now);
underTest.renamePropertyKey("foo", "foo");
assertThatPropertiesRow(id)
.hasKey("foo")
.hasNoUserId()
.hasNoResourceId()
.hasTextValue("bar")
.hasCreatedAt(now);
}
@Test
public void should_not_rename_with_empty_key() {
thrown.expect(IllegalArgumentException.class);
underTest.renamePropertyKey("foo", "");
}
@Test
public void should_not_rename_an_empty_key() {
thrown.expect(IllegalArgumentException.class);
underTest.renamePropertyKey(null, "foo");
}
private PropertyDto findByKey(List<PropertyDto> properties, String key) {
for (PropertyDto property : properties) {
if (key.equals(property.getKey())) {
return property;
}
}
return null;
}
private void insertProperties(PropertyDto... properties) {
for (PropertyDto propertyDto : properties) {
underTest.saveProperty(session, propertyDto);
}
session.commit();
}
private long insertProperty(String key, @Nullable String value, @Nullable Long resourceId, @Nullable Integer userId, long createdAt) throws SQLException {
when(system2.now()).thenReturn(createdAt);
return insertProperty(key, value, resourceId, userId);
}
private long insertProperty(String key, @Nullable String value, @Nullable Long resourceId, @Nullable Integer userId) throws SQLException {
DbSession session = dbTester.getSession();
PropertyDto dto = new PropertyDto().setKey(key)
.setResourceId(resourceId == null ? null : resourceId.longValue())
.setUserId(userId == null ? null : userId)
.setValue(value);
dbTester.getDbClient().propertiesDao().saveProperty(session, dto);
session.commit();
return (long) dbTester.selectFirst(session, "select id as \"id\" from properties" +
" where prop_key='" + key + "'" +
" and user_id" + (userId == null ? " is null" : "='" + userId + "'") +
" and resource_id" + (resourceId == null ? " is null" : "='" + resourceId + "'")).get("id");
}
private ComponentDto insertProject(String uuid) {
String key = "project" + uuid;
ComponentDto project = ComponentTesting.newProjectDto(dbTester.getDefaultOrganization(), uuid).setKey(key);
dbClient.componentDao().insert(session, project);
dbTester.commit();
return project;
}
private int insertUser(String login) {
UserDto dto = new UserDto().setLogin(login);
DbSession session = dbTester.getSession();
dbClient.userDao().insert(session, dto);
session.commit();
return dto.getId();
}
private static PropertyDtoAssert assertThatDto(@Nullable PropertyDto dto) {
return new PropertyDtoAssert(dto);
}
private PropertiesRowAssert assertThatPropertiesRow(String key, @Nullable Integer userId, @Nullable Integer componentId) {
return new PropertiesRowAssert(dbTester, key, userId, componentId);
}
private PropertiesRowAssert assertThatPropertiesRow(String key) {
return new PropertiesRowAssert(dbTester, key);
}
private PropertiesRowAssert assertThatPropertiesRow(long id) {
return new PropertiesRowAssert(dbTester, id);
}
}
|