You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BuiltInQualityProfilesNotificationTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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;
  21. import java.util.Random;
  22. import org.junit.Rule;
  23. import org.junit.Test;
  24. import org.junit.rules.ExpectedException;
  25. import org.sonar.api.notifications.Notification;
  26. import org.sonar.server.qualityprofile.BuiltInQualityProfilesNotification.Profile;
  27. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  28. import static org.assertj.core.api.Assertions.assertThat;
  29. import static org.assertj.core.api.Assertions.tuple;
  30. import static org.sonar.server.qualityprofile.BuiltInQualityProfilesUpdateListener.BUILT_IN_QUALITY_PROFILES;
  31. public class BuiltInQualityProfilesNotificationTest {
  32. private static final Random RANDOM = new Random();
  33. @Rule
  34. public ExpectedException expectedException = ExpectedException.none();
  35. @Test
  36. public void serialize_and_parse_no_profile() {
  37. Notification notification = new BuiltInQualityProfilesNotification().serialize();
  38. BuiltInQualityProfilesNotification result = BuiltInQualityProfilesNotification.parse(notification);
  39. assertThat(result.getProfiles()).isEmpty();
  40. }
  41. @Test
  42. public void serialize_and_parse_single_profile() {
  43. String profileName = randomAlphanumeric(20);
  44. String languageKey = randomAlphanumeric(20);
  45. String languageName = randomAlphanumeric(20);
  46. int newRules = RANDOM.nextInt(5000);
  47. int updatedRules = RANDOM.nextInt(5000);
  48. int removedRules = RANDOM.nextInt(5000);
  49. long startDate = RANDOM.nextInt(5000);
  50. long endDate = startDate + RANDOM.nextInt(5000);
  51. Notification notification = new BuiltInQualityProfilesNotification()
  52. .addProfile(Profile.newBuilder()
  53. .setProfileName(profileName)
  54. .setLanguageKey(languageKey)
  55. .setLanguageName(languageName)
  56. .setNewRules(newRules)
  57. .setUpdatedRules(updatedRules)
  58. .setRemovedRules(removedRules)
  59. .setStartDate(startDate)
  60. .setEndDate(endDate)
  61. .build())
  62. .serialize();
  63. BuiltInQualityProfilesNotification result = BuiltInQualityProfilesNotification.parse(notification);
  64. assertThat(result.getProfiles())
  65. .extracting(Profile::getProfileName, Profile::getLanguageKey, Profile::getLanguageName, Profile::getNewRules, Profile::getUpdatedRules, Profile::getRemovedRules,
  66. Profile::getStartDate, Profile::getEndDate)
  67. .containsExactlyInAnyOrder(tuple(profileName, languageKey, languageName, newRules, updatedRules, removedRules, startDate, endDate));
  68. }
  69. @Test
  70. public void serialize_and_parse_multiple_profiles() {
  71. String profileName1 = randomAlphanumeric(20);
  72. String languageKey1 = randomAlphanumeric(20);
  73. String languageName1 = randomAlphanumeric(20);
  74. String profileName2 = randomAlphanumeric(20);
  75. String languageKey2 = randomAlphanumeric(20);
  76. String languageName2 = randomAlphanumeric(20);
  77. Notification notification = new BuiltInQualityProfilesNotification()
  78. .addProfile(Profile.newBuilder()
  79. .setProfileName(profileName1)
  80. .setLanguageKey(languageKey1)
  81. .setLanguageName(languageName1)
  82. .build())
  83. .addProfile(Profile.newBuilder()
  84. .setProfileName(profileName2)
  85. .setLanguageKey(languageKey2)
  86. .setLanguageName(languageName2)
  87. .build())
  88. .serialize();
  89. BuiltInQualityProfilesNotification result = BuiltInQualityProfilesNotification.parse(notification);
  90. assertThat(result.getProfiles()).extracting(Profile::getProfileName, Profile::getLanguageKey, Profile::getLanguageName)
  91. .containsExactlyInAnyOrder(tuple(profileName1, languageKey1, languageName1), tuple(profileName2, languageKey2, languageName2));
  92. }
  93. @Test
  94. public void serialize_and_parse_max_values() {
  95. String profileName = randomAlphanumeric(20);
  96. String languageKey = randomAlphanumeric(20);
  97. String languageName = randomAlphanumeric(20);
  98. int newRules = Integer.MAX_VALUE;
  99. int updatedRules = Integer.MAX_VALUE;
  100. int removedRules = Integer.MAX_VALUE;
  101. long startDate = Long.MAX_VALUE;
  102. long endDate = Long.MAX_VALUE;
  103. Notification notification = new BuiltInQualityProfilesNotification()
  104. .addProfile(Profile.newBuilder()
  105. .setProfileName(profileName)
  106. .setLanguageKey(languageKey)
  107. .setLanguageName(languageName)
  108. .setNewRules(newRules)
  109. .setUpdatedRules(updatedRules)
  110. .setRemovedRules(removedRules)
  111. .setStartDate(startDate)
  112. .setEndDate(endDate)
  113. .build())
  114. .serialize();
  115. BuiltInQualityProfilesNotification result = BuiltInQualityProfilesNotification.parse(notification);
  116. assertThat(result.getProfiles())
  117. .extracting(Profile::getProfileName, Profile::getLanguageKey, Profile::getLanguageName, Profile::getNewRules, Profile::getUpdatedRules, Profile::getRemovedRules,
  118. Profile::getStartDate, Profile::getEndDate)
  119. .containsExactlyInAnyOrder(tuple(profileName, languageKey, languageName, newRules, updatedRules, removedRules, startDate, endDate));
  120. }
  121. @Test
  122. public void fail_with_ISE_when_parsing_empty_notification() {
  123. expectedException.expect(IllegalStateException.class);
  124. expectedException.expectMessage("Could not read the built-in quality profile notification");
  125. BuiltInQualityProfilesNotification.parse(new Notification(BUILT_IN_QUALITY_PROFILES));
  126. }
  127. }