]> source.dussan.org Git - sonarqube.git/blob
a117b35432fccde52dda12a130c51577fc32e89b
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 3 of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 package org.sonar.server.qualityprofile.builtin;
21
22 import java.util.Random;
23 import org.junit.Test;
24 import org.sonar.api.notifications.Notification;
25 import org.sonar.server.qualityprofile.builtin.BuiltInQPChangeNotificationBuilder.Profile;
26
27 import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.assertj.core.api.Assertions.assertThatThrownBy;
30 import static org.assertj.core.api.Assertions.tuple;
31
32 public class BuiltInQPChangeNotificationTest {
33
34   private static final Random RANDOM = new Random();
35
36
37   @Test
38   public void serialize_and_parse_no_profile() {
39     Notification notification = new BuiltInQPChangeNotificationBuilder().build();
40
41     BuiltInQPChangeNotificationBuilder result = BuiltInQPChangeNotificationBuilder.parse(notification);
42
43     assertThat(result.getProfiles()).isEmpty();
44   }
45
46   @Test
47   public void serialize_and_parse_single_profile() {
48     String profileName = randomAlphanumeric(20);
49     String languageKey = randomAlphanumeric(20);
50     String languageName = randomAlphanumeric(20);
51     int newRules = RANDOM.nextInt(5000);
52     int updatedRules = RANDOM.nextInt(5000);
53     int removedRules = RANDOM.nextInt(5000);
54     long startDate = RANDOM.nextInt(5000);
55     long endDate = startDate + RANDOM.nextInt(5000);
56
57     BuiltInQPChangeNotification notification = new BuiltInQPChangeNotificationBuilder()
58       .addProfile(Profile.newBuilder()
59         .setProfileName(profileName)
60         .setLanguageKey(languageKey)
61         .setLanguageName(languageName)
62         .setNewRules(newRules)
63         .setUpdatedRules(updatedRules)
64         .setRemovedRules(removedRules)
65         .setStartDate(startDate)
66         .setEndDate(endDate)
67         .build())
68       .build();
69     BuiltInQPChangeNotificationBuilder result = BuiltInQPChangeNotificationBuilder.parse(notification);
70
71     assertThat(result.getProfiles())
72       .extracting(Profile::getProfileName, Profile::getLanguageKey, Profile::getLanguageName, Profile::getNewRules, Profile::getUpdatedRules, Profile::getRemovedRules,
73         Profile::getStartDate, Profile::getEndDate)
74       .containsExactlyInAnyOrder(tuple(profileName, languageKey, languageName, newRules, updatedRules, removedRules, startDate, endDate));
75   }
76
77   @Test
78   public void serialize_and_parse_multiple_profiles() {
79     String profileName1 = randomAlphanumeric(20);
80     String languageKey1 = randomAlphanumeric(20);
81     String languageName1 = randomAlphanumeric(20);
82     String profileName2 = randomAlphanumeric(20);
83     String languageKey2 = randomAlphanumeric(20);
84     String languageName2 = randomAlphanumeric(20);
85
86     BuiltInQPChangeNotification notification = new BuiltInQPChangeNotificationBuilder()
87       .addProfile(Profile.newBuilder()
88         .setProfileName(profileName1)
89         .setLanguageKey(languageKey1)
90         .setLanguageName(languageName1)
91         .build())
92       .addProfile(Profile.newBuilder()
93         .setProfileName(profileName2)
94         .setLanguageKey(languageKey2)
95         .setLanguageName(languageName2)
96         .build())
97       .build();
98     BuiltInQPChangeNotificationBuilder result = BuiltInQPChangeNotificationBuilder.parse(notification);
99
100     assertThat(result.getProfiles()).extracting(Profile::getProfileName, Profile::getLanguageKey, Profile::getLanguageName)
101       .containsExactlyInAnyOrder(tuple(profileName1, languageKey1, languageName1), tuple(profileName2, languageKey2, languageName2));
102   }
103
104   @Test
105   public void serialize_and_parse_max_values() {
106     String profileName = randomAlphanumeric(20);
107     String languageKey = randomAlphanumeric(20);
108     String languageName = randomAlphanumeric(20);
109     int newRules = Integer.MAX_VALUE;
110     int updatedRules = Integer.MAX_VALUE;
111     int removedRules = Integer.MAX_VALUE;
112     long startDate = Long.MAX_VALUE;
113     long endDate = Long.MAX_VALUE;
114
115     BuiltInQPChangeNotification notification = new BuiltInQPChangeNotificationBuilder()
116       .addProfile(Profile.newBuilder()
117         .setProfileName(profileName)
118         .setLanguageKey(languageKey)
119         .setLanguageName(languageName)
120         .setNewRules(newRules)
121         .setUpdatedRules(updatedRules)
122         .setRemovedRules(removedRules)
123         .setStartDate(startDate)
124         .setEndDate(endDate)
125         .build())
126       .build();
127     BuiltInQPChangeNotificationBuilder result = BuiltInQPChangeNotificationBuilder.parse(notification);
128
129     assertThat(result.getProfiles())
130       .extracting(Profile::getProfileName, Profile::getLanguageKey, Profile::getLanguageName, Profile::getNewRules, Profile::getUpdatedRules, Profile::getRemovedRules,
131         Profile::getStartDate, Profile::getEndDate)
132       .containsExactlyInAnyOrder(tuple(profileName, languageKey, languageName, newRules, updatedRules, removedRules, startDate, endDate));
133   }
134
135   @Test
136   public void fail_with_ISE_when_parsing_empty_notification() {
137     assertThatThrownBy(() -> BuiltInQPChangeNotificationBuilder.parse(new Notification(BuiltInQPChangeNotification.TYPE)))
138       .isInstanceOf(IllegalStateException.class)
139       .hasMessage("Could not read the built-in quality profile notification");
140   }
141 }