3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.qualityprofile.builtin;
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;
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;
32 public class BuiltInQPChangeNotificationTest {
34 private static final Random RANDOM = new Random();
38 public void serialize_and_parse_no_profile() {
39 Notification notification = new BuiltInQPChangeNotificationBuilder().build();
41 BuiltInQPChangeNotificationBuilder result = BuiltInQPChangeNotificationBuilder.parse(notification);
43 assertThat(result.getProfiles()).isEmpty();
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);
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)
69 BuiltInQPChangeNotificationBuilder result = BuiltInQPChangeNotificationBuilder.parse(notification);
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));
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);
86 BuiltInQPChangeNotification notification = new BuiltInQPChangeNotificationBuilder()
87 .addProfile(Profile.newBuilder()
88 .setProfileName(profileName1)
89 .setLanguageKey(languageKey1)
90 .setLanguageName(languageName1)
92 .addProfile(Profile.newBuilder()
93 .setProfileName(profileName2)
94 .setLanguageKey(languageKey2)
95 .setLanguageName(languageName2)
98 BuiltInQPChangeNotificationBuilder result = BuiltInQPChangeNotificationBuilder.parse(notification);
100 assertThat(result.getProfiles()).extracting(Profile::getProfileName, Profile::getLanguageKey, Profile::getLanguageName)
101 .containsExactlyInAnyOrder(tuple(profileName1, languageKey1, languageName1), tuple(profileName2, languageKey2, languageName2));
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;
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)
127 BuiltInQPChangeNotificationBuilder result = BuiltInQPChangeNotificationBuilder.parse(notification);
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));
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");