-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
RuleActivatorMediumTest.java
1182 lines (1010 loc) · 47.8 KB
/
RuleActivatorMediumTest.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
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact 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.server.qualityprofile;
import com.google.common.collect.ImmutableMap;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.server.rule.RuleParamType;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.ActiveRuleKey;
import org.sonar.db.qualityprofile.ActiveRuleParamDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.Message;
import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexer;
import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.search.QueryContext;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.singleton;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static org.sonar.api.rule.Severity.BLOCKER;
import static org.sonar.api.rule.Severity.CRITICAL;
import static org.sonar.api.rule.Severity.INFO;
import static org.sonar.api.rule.Severity.MAJOR;
import static org.sonar.api.rule.Severity.MINOR;
import static org.sonar.db.qualityprofile.ActiveRuleDto.INHERITED;
import static org.sonar.db.qualityprofile.ActiveRuleDto.OVERRIDES;
import static org.sonar.db.rule.RuleTesting.XOO_X1;
import static org.sonar.db.rule.RuleTesting.XOO_X2;
import static org.sonar.db.rule.RuleTesting.newCustomRule;
import static org.sonar.db.rule.RuleTesting.newDto;
import static org.sonar.db.rule.RuleTesting.newTemplateRule;
import static org.sonar.db.rule.RuleTesting.newXooX1;
import static org.sonar.db.rule.RuleTesting.newXooX2;
import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P1_KEY;
import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P2_KEY;
import static org.sonar.server.qualityprofile.QProfileTesting.XOO_P3_KEY;
// TODO Replace ServerTester by EsTester and DbTester
public class RuleActivatorMediumTest {
static final RuleKey TEMPLATE_RULE_KEY = RuleKey.of("xoo", "template1");
static final RuleKey CUSTOM_RULE_KEY = RuleKey.of("xoo", "custom1");
@ClassRule
public static ServerTester tester = new ServerTester().withEsIndexes();
@Rule
public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
DbClient db;
DbSession dbSession;
RuleActivator ruleActivator;
RuleIndexer ruleIndexer;
ActiveRuleIndex activeRuleIndex;
ActiveRuleIndexer activeRuleIndexer;
QualityProfileDto profileDto;
@Before
public void before() {
tester.clearDbAndIndexes();
db = tester.get(DbClient.class);
dbSession = db.openSession(false);
ruleActivator = tester.get(RuleActivator.class);
activeRuleIndex = tester.get(ActiveRuleIndex.class);
activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
activeRuleIndexer.setEnabled(true);
ruleIndexer = tester.get(RuleIndexer.class);
ruleIndexer.setEnabled(true);
// create pre-defined rules
RuleDto javaRule = newDto(RuleKey.of("squid", "j1"))
.setSeverity("MAJOR").setLanguage("java");
RuleDto xooRule1 = newXooX1().setSeverity("MINOR");
RuleDto xooRule2 = newXooX2().setSeverity("INFO");
RuleDto xooTemplateRule1 = newTemplateRule(TEMPLATE_RULE_KEY)
.setSeverity("MINOR").setLanguage("xoo");
db.ruleDao().insert(dbSession, javaRule);
db.ruleDao().insert(dbSession, xooRule1);
db.ruleDao().insert(dbSession, xooRule2);
db.ruleDao().insert(dbSession, xooTemplateRule1);
db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
.setName("max").setDefaultValue("10").setType(RuleParamType.INTEGER.type()));
db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
.setName("min").setType(RuleParamType.INTEGER.type()));
db.ruleDao().insertRuleParam(dbSession, xooTemplateRule1, RuleParamDto.createFor(xooTemplateRule1)
.setName("format").setType(RuleParamType.STRING.type()));
RuleDto xooCustomRule1 = newCustomRule(xooTemplateRule1).setRuleKey(CUSTOM_RULE_KEY.rule())
.setSeverity("MINOR").setLanguage("xoo");
db.ruleDao().insert(dbSession, xooCustomRule1);
db.ruleDao().insertRuleParam(dbSession, xooCustomRule1, RuleParamDto.createFor(xooTemplateRule1)
.setName("format").setDefaultValue("txt").setType(RuleParamType.STRING.type()));
// create pre-defined profile P1
profileDto = QProfileTesting.newXooP1();
db.qualityProfileDao().insert(dbSession, profileDto);
dbSession.commit();
dbSession.clearCache();
ruleIndexer.index();
}
@After
public void after() {
dbSession.close();
}
@Test
public void activate() {
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activation.setParameter("min", "3");
List<ActiveRuleChange> changes = ruleActivator.activate(dbSession, activation, XOO_P1_KEY);
dbSession.commit();
dbSession.clearCache();
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), BLOCKER, null,
ImmutableMap.of("max", "7", "min", "3"));
assertThat(changes).hasSize(1);
assertThat(changes.get(0).getType()).isEqualTo(ActiveRuleChange.Type.ACTIVATED);
}
@Test
public void automatic_activation_does_not_update_intended_column() {
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activation.setParameter("min", "3");
List<ActiveRuleChange> changes = ruleActivator.activate(dbSession, activation, XOO_P1_KEY);
dbSession.commit();
dbSession.clearCache();
userSessionRule.anonymous();
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), BLOCKER, null,
ImmutableMap.of("max", "7", "min", "3"));
assertThat(changes).hasSize(1);
assertThat(changes.get(0).getType()).isEqualTo(ActiveRuleChange.Type.ACTIVATED);
assertProfileHasBeenUpdatedAutomatically(XOO_P1_KEY);
}
@Test
public void activate_with_profile_dto() {
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activation.setParameter("min", "3");
userSessionRule.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
List<ActiveRuleChange> changes = ruleActivator.activate(dbSession, activation, profileDto);
dbSession.commit();
dbSession.clearCache();
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), BLOCKER, null,
ImmutableMap.of("max", "7", "min", "3"));
assertThat(changes).hasSize(1);
assertThat(changes.get(0).getType()).isEqualTo(ActiveRuleChange.Type.ACTIVATED);
assertProfileHasBeenUpdatedManually(profileDto.getKey());
}
@Test
public void activate_with_default_severity_and_parameter() {
activate(new RuleActivation(XOO_X1), XOO_P1_KEY);
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), MINOR, null,
ImmutableMap.of("max", "10"));
}
/**
* SONAR-5841
*/
@Test
public void activate_with_empty_parameter_having_no_default_value() {
activate(new RuleActivation(XOO_X1)
.setParameter("min", ""),
XOO_P1_KEY);
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), MINOR, null,
// Max should be set to default value, min has not value it should be ignored
ImmutableMap.of("max", "10"));
}
/**
* SONAR-5841
*/
@Test
public void activate_with_empty_parameters() {
activate(new RuleActivation(XOO_X1)
.setParameters(ImmutableMap.of("max", "", "min", "")),
XOO_P1_KEY);
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
// Max should be set to default value, min has not value it should be ignored
verifyHasActiveRuleInDb(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), MINOR, null,
ImmutableMap.of("max", "10"));
}
/**
* SONAR-5840
*/
@Test
public void activate_rule_with_negative_integer_value_on_parameter_having_no_default_value() {
activate(new RuleActivation(XOO_X1)
.setParameter("min", "-10"),
XOO_P1_KEY);
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
// Max should be set to default value, min should be set to -10
verifyHasActiveRuleInDb(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), MINOR, null,
ImmutableMap.of("max", "10", "min", "-10"));
}
@Test
public void activation_ignores_unsupported_parameters() {
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setParameter("xxx", "yyy");
activate(activation, XOO_P1_KEY);
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), MINOR, null, ImmutableMap.of("max", "10"));
}
@Test
public void update_activation_severity_and_parameters() {
// initial activation
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activate(activation, XOO_P1_KEY);
// update
RuleActivation update = new RuleActivation(XOO_X1);
update.setSeverity(CRITICAL);
update.setParameter("max", "42");
List<ActiveRuleChange> changes = activate(update, XOO_P1_KEY);
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), CRITICAL, null, ImmutableMap.of("max", "42"));
assertThat(changes).hasSize(1);
assertThat(changes.get(0).getType()).isEqualTo(ActiveRuleChange.Type.UPDATED);
}
@Test
public void update_activation_with_parameter_without_default_value() {
// initial activation -> param "max" has a default value
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activate(activation, XOO_P1_KEY);
verifyHasActiveRuleInDb(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), BLOCKER, null,
ImmutableMap.of("max", "10"));
// update param "min", which has no default value
RuleActivation update = new RuleActivation(XOO_X1);
update.setParameter("min", "3");
List<ActiveRuleChange> changes = activate(update, XOO_P1_KEY);
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1), BLOCKER, null,
ImmutableMap.of("min", "3", "max", "10"));
assertThat(changes).hasSize(1);
assertThat(changes.get(0).getType()).isEqualTo(ActiveRuleChange.Type.UPDATED);
}
@Test
public void update_activation_but_new_parameter() {
// initial activation
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(XOO_P1_KEY, XOO_X1);
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activate(activation, XOO_P1_KEY);
assertThat(db.activeRuleDao().selectParamByKeyAndName(activeRuleKey, "max", dbSession)).isNotNull();
db.activeRuleDao().deleteParamByKeyAndName(dbSession, activeRuleKey, "max");
dbSession.commit();
assertThat(db.activeRuleDao().selectParamByKeyAndName(activeRuleKey, "max", dbSession)).isNull();
dbSession.clearCache();
// update
RuleActivation update = new RuleActivation(XOO_X1);
update.setSeverity(CRITICAL);
update.setParameter("max", "42");
// contrary to activerule, the param 'max' is supposed to be inserted but not updated
activate(update, XOO_P1_KEY);
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(activeRuleKey, CRITICAL, null, ImmutableMap.of("max", "42"));
}
@Test
public void ignore_activation_without_changes() {
// initial activation
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activate(activation, XOO_P1_KEY);
// update with exactly the same severity and params
RuleActivation update = new RuleActivation(XOO_X1);
update.setSeverity(BLOCKER);
List<ActiveRuleChange> changes = activate(update, XOO_P1_KEY);
assertThat(changes).isEmpty();
}
@Test
public void do_not_change_severity_and_params_if_unset_and_already_activated() {
// initial activation
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(XOO_P1_KEY, XOO_X1);
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activate(activation, XOO_P1_KEY);
// update without any severity or params => keep
RuleActivation update = new RuleActivation(XOO_X1);
activate(update, XOO_P1_KEY);
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(activeRuleKey, BLOCKER, null, ImmutableMap.of("max", "7"));
}
@Test
public void revert_activation_to_default_severity_and_parameters() {
// initial activation
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(XOO_P1_KEY, XOO_X1);
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activation.setParameter("min", "3");
activate(activation, XOO_P1_KEY);
// update without any severity or params = reset
RuleActivation update = new RuleActivation(XOO_X1).setReset(true);
activate(update, XOO_P1_KEY);
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(activeRuleKey, MINOR, null,
// only default values
ImmutableMap.of("max", "10"));
}
@Test
public void ignore_parameters_when_activating_custom_rule() {
// initial activation
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(XOO_P1_KEY, CUSTOM_RULE_KEY);
RuleActivation activation = new RuleActivation(CUSTOM_RULE_KEY);
activate(activation, XOO_P1_KEY);
// update
RuleActivation update = new RuleActivation(CUSTOM_RULE_KEY)
.setParameter("format", "xls");
activate(update, XOO_P1_KEY);
assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
verifyHasActiveRuleInDb(activeRuleKey, MINOR, null, ImmutableMap.of("format", "txt"));
}
@Test
public void fail_to_activate_if_template() {
RuleActivation activation = new RuleActivation(TEMPLATE_RULE_KEY);
try {
activate(activation, XOO_P1_KEY);
fail();
} catch (BadRequestException e) {
assertThat(e).hasMessage("Rule template can't be activated on a Quality profile: xoo:template1");
verifyZeroActiveRules(XOO_P1_KEY);
}
}
@Test
public void fail_to_activate_if_different_languages() {
// profile and rule have different languages
RuleActivation activation = new RuleActivation(RuleKey.of("squid", "j1"));
try {
activate(activation, XOO_P1_KEY);
fail();
} catch (BadRequestException e) {
assertThat(e).hasMessage("Rule squid:j1 and profile XOO_P1 have different languages");
verifyZeroActiveRules(XOO_P1_KEY);
}
}
@Test
public void fail_to_activate_if_unknown_rule() {
// profile and rule have different languages
RuleActivation activation = new RuleActivation(RuleKey.of("xoo", "x3"));
try {
activate(activation, XOO_P1_KEY);
fail();
} catch (BadRequestException e) {
assertThat(e).hasMessage("Rule not found: xoo:x3");
verifyZeroActiveRules(XOO_P1_KEY);
}
}
@Test
public void fail_to_activate_if_rule_with_removed_status() {
RuleDto ruleDto = db.ruleDao().selectOrFailByKey(dbSession, XOO_X1);
ruleDto.setStatus(RuleStatus.REMOVED);
db.ruleDao().update(dbSession, ruleDto);
dbSession.commit();
dbSession.clearCache();
RuleActivation activation = new RuleActivation(XOO_X1);
try {
activate(activation, XOO_P1_KEY);
fail();
} catch (BadRequestException e) {
assertThat(e).hasMessage("Rule was removed: xoo:x1");
verifyZeroActiveRules(XOO_P1_KEY);
}
}
@Test
public void fail_to_activate_if_unknown_profile() {
try {
activate(new RuleActivation(XOO_X1), "unknown");
fail();
} catch (BadRequestException e) {
assertThat(e).hasMessage("Quality profile not found: unknown");
}
}
@Test
public void fail_to_activate_if_invalid_parameter() {
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setParameter("max", "foo");
try {
activate(activation, XOO_P1_KEY);
fail();
} catch (BadRequestException e) {
Message msg = e.errors().messages().get(0);
assertThat(msg.getKey()).isEqualTo("errors.type.notInteger");
verifyZeroActiveRules(XOO_P1_KEY);
}
}
@Test
public void deactivate() {
// activation
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
userSessionRule.login();
activate(activation, XOO_P1_KEY);
// deactivation
ruleActivator.deactivate(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1));
verifyZeroActiveRules(XOO_P1_KEY);
assertProfileHasBeenUpdatedManually(XOO_P1_KEY);
}
@Test
public void ignore_deactivation_if_rule_not_activated() {
// deactivation
ActiveRuleKey key = ActiveRuleKey.of(XOO_P1_KEY, XOO_X1);
ruleActivator.deactivate(key);
verifyZeroActiveRules(XOO_P1_KEY);
}
@Test
public void deactivation_fails_if_rule_not_found() {
ActiveRuleKey key = ActiveRuleKey.of(XOO_P1_KEY, RuleKey.of("xoo", "x3"));
try {
ruleActivator.deactivate(key);
fail();
} catch (BadRequestException e) {
assertThat(e).hasMessage("Rule not found: xoo:x3");
verifyZeroActiveRules(XOO_P1_KEY);
}
}
@Test
public void deactivation_fails_if_profile_not_found() {
ActiveRuleKey key = ActiveRuleKey.of("unknown", XOO_X1);
try {
ruleActivator.deactivate(key);
fail();
} catch (BadRequestException e) {
assertThat(e).hasMessage("Quality profile not found: unknown");
}
}
@Test
public void allow_to_deactivate_removed_rule() {
// activation
RuleActivation activation = new RuleActivation(XOO_X1);
activate(activation, XOO_P1_KEY);
// set rule as removed
RuleDto rule = db.ruleDao().selectOrFailByKey(dbSession, XOO_X1);
rule.setStatus(RuleStatus.REMOVED);
db.ruleDao().update(dbSession, rule);
dbSession.commit();
dbSession.clearCache();
// deactivation
ruleActivator.deactivate(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1));
verifyZeroActiveRules(XOO_P1_KEY);
}
// INHERITANCE OF PROFILES
@Test
public void activate_on_child_profile_but_not_on_parent() {
createChildProfiles();
// activate on child profile, but not on root
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activate(activation, XOO_P2_KEY);
verifyZeroActiveRules(XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
// update severity on child
activation = new RuleActivation(XOO_X1);
activation.setSeverity(MINOR);
activation.setParameter("max", "77");
activate(activation, XOO_P2_KEY);
verifyZeroActiveRules(XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, MINOR, null, ImmutableMap.of("max", "77"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, MINOR, INHERITED, ImmutableMap.of("max", "77"));
}
@Test
public void propagate_activation_on_child_profiles() {
createChildProfiles();
// activate on root profile
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
List<ActiveRuleChange> changes = activate(activation, XOO_P1_KEY);
assertThat(changes).hasSize(3);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
}
@Test
public void propagate_activation_update_on_child_profiles() {
createChildProfiles();
userSessionRule.login();
// activate on root profile
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
// update on parent
activation = new RuleActivation(XOO_X1);
activation.setSeverity(INFO);
activation.setParameter("max", "8");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, INFO, null, ImmutableMap.of("max", "8"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, INFO, INHERITED, ImmutableMap.of("max", "8"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, INFO, INHERITED, ImmutableMap.of("max", "8"));
// update on child -> propagate on grand child only
activation = new RuleActivation(XOO_X1);
activation.setSeverity(MINOR);
activation.setParameter("max", "9");
activate(activation, XOO_P2_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, INFO, null, ImmutableMap.of("max", "8"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, MINOR, OVERRIDES, ImmutableMap.of("max", "9"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, MINOR, INHERITED, ImmutableMap.of("max", "9"));
// update on grand child
activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "10");
activate(activation, XOO_P3_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, INFO, null, ImmutableMap.of("max", "8"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, MINOR, OVERRIDES, ImmutableMap.of("max", "9"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, OVERRIDES, ImmutableMap.of("max", "10"));
assertProfileHasBeenUpdatedManually(XOO_P1_KEY);
assertProfileHasBeenUpdatedManually(XOO_P2_KEY);
assertProfileHasBeenUpdatedManually(XOO_P3_KEY);
}
@Test
public void do_not_propagate_activation_update_on_child_overrides() {
createChildProfiles();
// activate on root profile P1
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(INFO);
activation.setParameter("max", "7");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, INFO, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, INFO, INHERITED, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, INFO, INHERITED, ImmutableMap.of("max", "7"));
// override on child P2
activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "8");
activate(activation, XOO_P2_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, INFO, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, OVERRIDES, ImmutableMap.of("max", "8"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "8"));
// change on parent -> do not propagate on children because they're overriding values
activation = new RuleActivation(XOO_X1);
activation.setSeverity(CRITICAL);
activation.setParameter("max", "9");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, CRITICAL, null, ImmutableMap.of("max", "9"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, OVERRIDES, ImmutableMap.of("max", "8"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "8"));
// reset on parent (use default severity and params) -> do not propagate on children because they're overriding values
activation = new RuleActivation(XOO_X1).setReset(true);
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, MINOR, null, ImmutableMap.of("max", "10"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, OVERRIDES, ImmutableMap.of("max", "8"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "8"));
}
@Test
public void active_on_parent_a_rule_already_activated_on_child() {
createChildProfiles();
// activate on child profile
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(INFO);
activation.setParameter("max", "7");
activate(activation, XOO_P2_KEY);
verifyZeroActiveRules(XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, INFO, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, INFO, INHERITED, ImmutableMap.of("max", "7"));
// active the same rule on root profile -> mark the child profile as OVERRIDES
activation = new RuleActivation(XOO_X1);
activation.setSeverity(MAJOR);
activation.setParameter("max", "8");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, MAJOR, null, ImmutableMap.of("max", "8"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, INFO, OVERRIDES, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, INFO, INHERITED, ImmutableMap.of("max", "7"));
}
@Test
public void do_not_override_on_child_if_same_values() {
createChildProfiles();
// activate on root profile
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(INFO);
activation.setParameter("max", "7");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, INFO, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, INFO, INHERITED, ImmutableMap.of("max", "7"));
// override on child P2 with same severity and params -> do nothing (still INHERITED but not OVERRIDDEN)
activation = new RuleActivation(XOO_X1);
activation.setSeverity(INFO);
activation.setParameter("max", "7");
activate(activation, XOO_P2_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, INFO, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, INFO, INHERITED, ImmutableMap.of("max", "7"));
}
@Test
public void propagate_deactivation_on_child_profiles() {
createChildProfiles();
// activate on root profile
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
// deactivate on root
ruleActivator.deactivate(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1));
verifyZeroActiveRules(XOO_P1_KEY);
verifyZeroActiveRules(XOO_P2_KEY);
verifyZeroActiveRules(XOO_P3_KEY);
}
@Test
public void propagate_deactivation_even_on_child_overrides() {
createChildProfiles();
// activate on root profile
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(INFO);
activation.setParameter("max", "7");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, INFO, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, INFO, INHERITED, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, INFO, INHERITED, ImmutableMap.of("max", "7"));
// override on child
activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "8");
activate(activation, XOO_P2_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, INFO, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, OVERRIDES, ImmutableMap.of("max", "8"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "8"));
// deactivate on parent -> do not propagate on children because they're overriding values
ruleActivator.deactivate(ActiveRuleKey.of(XOO_P1_KEY, XOO_X1));
dbSession.clearCache();
verifyZeroActiveRules(XOO_P1_KEY);
verifyZeroActiveRules(XOO_P2_KEY);
verifyZeroActiveRules(XOO_P3_KEY);
}
@Test
public void do_not_deactivate_inherited_or_overridden_rule() {
createChildProfiles();
// activate on root profile
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
// try to deactivate on child
try {
ruleActivator.deactivate(ActiveRuleKey.of(XOO_P2_KEY, XOO_X1));
fail();
} catch (BadRequestException e) {
assertThat(e).hasMessage("Cannot deactivate inherited rule 'xoo:x1'");
}
}
@Test
public void reset_child_profile() {
createChildProfiles();
// activate on root profile
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
// override
activation = new RuleActivation(XOO_X1);
activation.setSeverity(INFO);
activation.setParameter("max", "10");
activate(activation, XOO_P2_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, INFO, OVERRIDES, ImmutableMap.of("max", "10"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, INFO, INHERITED, ImmutableMap.of("max", "10"));
// reset -> remove overridden values
activation = new RuleActivation(XOO_X1).setReset(true);
activate(activation, XOO_P2_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
}
@Test
public void reset_is_not_propagated_to_child_overrides() {
createChildProfiles();
// activate on root profile
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity(BLOCKER);
activation.setParameter("max", "7");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
// override on child
activation = new RuleActivation(XOO_X1);
activation.setSeverity(INFO);
activation.setParameter("max", "10");
activate(activation, XOO_P2_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, INFO, OVERRIDES, ImmutableMap.of("max", "10"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, INFO, INHERITED, ImmutableMap.of("max", "10"));
// override on grand child
activation = new RuleActivation(XOO_X1);
activation.setSeverity(MINOR);
activation.setParameter("max", "20");
activate(activation, XOO_P3_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, INFO, OVERRIDES, ImmutableMap.of("max", "10"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, MINOR, OVERRIDES, ImmutableMap.of("max", "20"));
// reset child P2 -> keep the overridden grand-child P3
activation = new RuleActivation(XOO_X1).setReset(true);
activate(activation, XOO_P2_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, INHERITED, ImmutableMap.of("max", "7"));
verifyOneActiveRuleInDb(XOO_P3_KEY, XOO_X1, MINOR, OVERRIDES, ImmutableMap.of("max", "20"));
}
@Test
public void ignore_reset_if_not_activated() {
createChildProfiles();
RuleActivation activation = new RuleActivation(XOO_X1).setReset(true);
activate(activation, XOO_P1_KEY);
verifyZeroActiveRules(XOO_P1_KEY);
verifyZeroActiveRules(XOO_P2_KEY);
}
@Test
public void bulk_activation() {
// Generate more rules than the search's max limit
int bulkSize = QueryContext.MAX_LIMIT + 10;
for (int i = 0; i < bulkSize; i++) {
db.ruleDao().insert(dbSession, newDto(RuleKey.of("bulk", "r_" + i)).setLanguage("xoo"));
}
dbSession.commit();
ruleIndexer.index();
// 0. No active rules so far (base case) and plenty rules available
verifyZeroActiveRules(XOO_P1_KEY);
assertThat(tester.get(RuleIndex.class)
.search(new RuleQuery().setRepositories(Arrays.asList("bulk")), new SearchOptions()).getTotal())
.isEqualTo(bulkSize);
// 1. bulk activate all the rules
BulkChangeResult result = ruleActivator.bulkActivate(
new RuleQuery().setRepositories(Arrays.asList("bulk")), XOO_P1_KEY, "MINOR");
// 2. assert that all activation has been commit to DB and ES
dbSession.clearCache();
assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(bulkSize);
assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(bulkSize);
assertThat(result.countSucceeded()).isEqualTo(bulkSize);
assertThat(result.countFailed()).isEqualTo(0);
}
@Test
public void bulk_activation_ignores_errors() {
// 1. bulk activate all the rules, even non xoo-rules and xoo templates
BulkChangeResult result = ruleActivator.bulkActivate(new RuleQuery(), XOO_P1_KEY, "MINOR");
// 2. assert that all activations have been commit to DB and ES
// -> xoo rules x1, x2 and custom1
dbSession.clearCache();
assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(3);
assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(3);
assertThat(result.countSucceeded()).isEqualTo(3);
assertThat(result.countFailed()).isGreaterThan(0);
}
@Test
public void set_and_unset_parent_profile() {
// x1 is activated on the "future parent" P1
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity("MAJOR");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, MAJOR, null, ImmutableMap.of("max", "10"));
// create profile P2 with x2
db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP2());
activation = new RuleActivation(XOO_X2);
activation.setSeverity("MAJOR");
activate(activation, XOO_P2_KEY);
// set parent -> child profile inherits rule x1 and still has x2
ruleActivator.setParent(XOO_P2_KEY, XOO_P1_KEY);
dbSession.clearCache();
assertThat(db.qualityProfileDao().selectByKey(dbSession, XOO_P2_KEY).getParentKee()).isEqualTo(XOO_P1_KEY);
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X1), MAJOR, INHERITED, ImmutableMap.of("max", "10"));
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X2), MAJOR, null, Collections.emptyMap());
// unset parent
dbSession.clearCache();
ruleActivator.setParent(XOO_P2_KEY, null);
assertThat(countActiveRules(XOO_P2_KEY)).isEqualTo(1);
assertThat(db.qualityProfileDao().selectByKey(dbSession, XOO_P2_KEY).getParentKee()).isNull();
verifyHasActiveRuleInDbAndIndex(ActiveRuleKey.of(XOO_P2_KEY, XOO_X2), MAJOR, null, Collections.emptyMap());
}
@Test
public void unset_no_parent_does_not_fail() {
// P1 has no parent !
ruleActivator.setParent(XOO_P1_KEY, null);
assertThat(db.qualityProfileDao().selectByKey(dbSession, XOO_P1_KEY).getParentKee()).isNull();
}
@Test
public void fail_if_set_child_as_parent() {
createChildProfiles();
try {
ruleActivator.setParent(XOO_P1_KEY, XOO_P3_KEY);
fail();
} catch (BadRequestException e) {
assertThat(e).hasMessage("Descendant profile 'XOO_P3' can not be selected as parent of 'XOO_P1'");
}
}
@Test
public void keep_overridden_rules_when_unsetting_parent() {
// x1 is activated on the "future parent"
RuleActivation activation = new RuleActivation(XOO_X1);
activation.setSeverity("MAJOR");
activate(activation, XOO_P1_KEY);
verifyOneActiveRuleInDb(XOO_P1_KEY, XOO_X1, MAJOR, null, ImmutableMap.of("max", "10"));
// create empty profile P2
db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP2());
dbSession.commit();
dbSession.clearCache();
// set parent -> child profile inherits rule x1
ruleActivator.setParent(XOO_P2_KEY, XOO_P1_KEY);
verifyOneActiveRuleInDbAndIndex(XOO_P2_KEY, XOO_X1, MAJOR, INHERITED, ImmutableMap.of("max", "10"));
// override x1
activation = new RuleActivation(XOO_X1);
activation.setSeverity("BLOCKER").setParameter("max", "333");
activate(activation, XOO_P2_KEY);
verifyOneActiveRuleInDb(XOO_P2_KEY, XOO_X1, BLOCKER, OVERRIDES, ImmutableMap.of("max", "333"));
// unset parent -> keep x1
ruleActivator.setParent(XOO_P2_KEY, null);
dbSession.clearCache();
assertThat(db.qualityProfileDao().selectByKey(dbSession, XOO_P2_KEY).getParentKee()).isNull();
verifyOneActiveRuleInDbAndIndex(XOO_P2_KEY, XOO_X1, BLOCKER, null, ImmutableMap.of("max", "333"));
}