]> source.dussan.org Git - sonarqube.git/blob
bacbaa97af5eca71cce8cf819d0cd568cf1481a3
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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
21 package org.sonar.server.qualityprofile;
22
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.notifications.Notification;
27 import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile;
28
29 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.assertj.core.api.Assertions.tuple;
32 import static org.sonar.server.qualityprofile.BuiltInQualityProfilesNotificationSender.BUILT_IN_QUALITY_PROFILES;
33
34 public class BuiltInQualityProfilesNotificationTest {
35
36   @Rule
37   public ExpectedException expectedException = ExpectedException.none();
38
39   @Test
40   public void serialize_and_parse_no_profile() {
41     Notification notification = new BuiltInQualityProfilesNotification().serialize();
42
43     BuiltInQualityProfilesNotification result = BuiltInQualityProfilesNotification.parse(notification);
44
45     assertThat(result.getProfiles()).isEmpty();
46   }
47
48   @Test
49   public void serialize_and_parse_single_profile() {
50     String profileName = randomAlphanumeric(20);
51     String languageKey = randomAlphanumeric(20);
52     String languageName = randomAlphanumeric(20);
53
54     Notification notification = new BuiltInQualityProfilesNotification()
55       .addProfile(Profile.newBuilder()
56         .setProfileName(profileName)
57         .setLanguageKey(languageKey)
58         .setLanguageName(languageName)
59         .setNewRules(3)
60         .setUpdatedRules(5)
61         .setRemovedRules(7)
62         .build())
63       .serialize();
64     BuiltInQualityProfilesNotification result = BuiltInQualityProfilesNotification.parse(notification);
65
66     assertThat(result.getProfiles()).extracting(Profile::getProfileName, Profile::getLanguageKey, Profile::getLanguageName,
67       Profile::getNewRules, Profile::getUpdatedRules, Profile::getRemovedRules)
68       .containsExactlyInAnyOrder(tuple(profileName, languageKey, languageName, 3, 5, 7));
69   }
70
71   @Test
72   public void serialize_and_parse_multiple_profiles() {
73     String profileName1 = randomAlphanumeric(20);
74     String languageKey1 = randomAlphanumeric(20);
75     String languageName1 = randomAlphanumeric(20);
76     String profileName2 = randomAlphanumeric(20);
77     String languageKey2 = randomAlphanumeric(20);
78     String languageName2 = randomAlphanumeric(20);
79
80     Notification notification = new BuiltInQualityProfilesNotification()
81       .addProfile(Profile.newBuilder()
82         .setProfileName(profileName1)
83         .setLanguageKey(languageKey1)
84         .setLanguageName(languageName1)
85         .build())
86       .addProfile(Profile.newBuilder()
87         .setProfileName(profileName2)
88         .setLanguageKey(languageKey2)
89         .setLanguageName(languageName2)
90         .build())
91       .serialize();
92     BuiltInQualityProfilesNotification result = BuiltInQualityProfilesNotification.parse(notification);
93
94     assertThat(result.getProfiles()).extracting(Profile::getProfileName, Profile::getLanguageKey, Profile::getLanguageName)
95       .containsExactlyInAnyOrder(tuple(profileName1, languageKey1, languageName1), tuple(profileName2, languageKey2, languageName2));
96   }
97
98   @Test
99   public void fail_with_ISE_when_parsing_empty_notification() {
100     expectedException.expect(IllegalStateException.class);
101     expectedException.expectMessage("Could not read the built-in quality profile notification");
102
103     BuiltInQualityProfilesNotification.parse(new Notification(BUILT_IN_QUALITY_PROFILES));
104   }
105 }