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.

QProfileCopierTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.io.IOException;
  22. import java.io.Reader;
  23. import java.io.Writer;
  24. import java.util.Collection;
  25. import javax.annotation.Nullable;
  26. import org.apache.commons.io.IOUtils;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.ExpectedException;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
  32. import org.sonar.api.utils.internal.JUnitTempFolder;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.DbTester;
  35. import org.sonar.db.organization.OrganizationDto;
  36. import org.sonar.db.qualityprofile.QProfileDto;
  37. import org.sonar.db.qualityprofile.QualityProfileTesting;
  38. import static org.assertj.core.api.Assertions.assertThat;
  39. import static org.junit.Assert.fail;
  40. public class QProfileCopierTest {
  41. private static final String BACKUP = "<backup/>";
  42. private System2 system2 = new AlwaysIncreasingSystem2();
  43. @Rule
  44. public ExpectedException expectedException = ExpectedException.none();
  45. @Rule
  46. public DbTester db = DbTester.create(system2);
  47. @Rule
  48. public JUnitTempFolder temp = new JUnitTempFolder();
  49. private DummyProfileFactory profileFactory = new DummyProfileFactory();
  50. private DummyBackuper backuper = new DummyBackuper();
  51. private QProfileCopier underTest = new QProfileCopier(db.getDbClient(), profileFactory, backuper, temp);
  52. @Test
  53. public void create_target_profile_and_copy_rules() {
  54. OrganizationDto organization = db.organizations().insert();
  55. QProfileDto source = db.qualityProfiles().insert(organization);
  56. QProfileDto target = underTest.copyToName(db.getSession(), source, "foo");
  57. assertThat(target.getLanguage()).isEqualTo(source.getLanguage());
  58. assertThat(target.getName()).isEqualTo("foo");
  59. assertThat(target.getParentKee()).isNull();
  60. assertThat(backuper.backuped).isSameAs(source);
  61. assertThat(backuper.restored.getName()).isEqualTo("foo");
  62. assertThat(backuper.restoredBackup).isEqualTo(BACKUP);
  63. }
  64. @Test
  65. public void create_target_profile_with_same_parent_than_source_profile() {
  66. OrganizationDto organization = db.organizations().insert();
  67. QProfileDto parent = db.qualityProfiles().insert(organization);
  68. QProfileDto source = db.qualityProfiles().insert(organization, p -> p.setParentKee(parent.getKee()));
  69. QProfileDto target = underTest.copyToName(db.getSession(), source, "foo");
  70. assertThat(target.getLanguage()).isEqualTo(source.getLanguage());
  71. assertThat(target.getName()).isEqualTo("foo");
  72. assertThat(target.getParentKee()).isEqualTo(parent.getKee());
  73. }
  74. @Test
  75. public void fail_to_copy_on_self() {
  76. OrganizationDto organization = db.organizations().insert();
  77. QProfileDto source = db.qualityProfiles().insert(organization);
  78. try {
  79. underTest.copyToName(db.getSession(), source, source.getName());
  80. fail();
  81. } catch (IllegalArgumentException e) {
  82. assertThat(e).hasMessage("Source and target profiles are equal: " + source.getName());
  83. assertThat(backuper.backuped).isNull();
  84. assertThat(backuper.restored).isNull();
  85. }
  86. }
  87. @Test
  88. public void copy_to_existing_profile() {
  89. OrganizationDto organization = db.organizations().insert();
  90. QProfileDto profile1 = db.qualityProfiles().insert(organization);
  91. QProfileDto profile2 = db.qualityProfiles().insert(organization, p -> p.setLanguage(profile1.getLanguage()));
  92. QProfileDto target = underTest.copyToName(db.getSession(), profile1, profile2.getName());
  93. assertThat(profileFactory.createdProfile).isNull();
  94. assertThat(target.getLanguage()).isEqualTo(profile2.getLanguage());
  95. assertThat(target.getName()).isEqualTo(profile2.getName());
  96. assertThat(backuper.backuped).isSameAs(profile1);
  97. assertThat(backuper.restored.getName()).isEqualTo(profile2.getName());
  98. assertThat(backuper.restoredBackup).isEqualTo(BACKUP);
  99. }
  100. private static class DummyProfileFactory implements QProfileFactory {
  101. private QProfileDto createdProfile;
  102. @Override
  103. public QProfileDto getOrCreateCustom(DbSession dbSession, OrganizationDto organization, QProfileName key) {
  104. throw new UnsupportedOperationException();
  105. }
  106. @Override
  107. public QProfileDto checkAndCreateCustom(DbSession dbSession, OrganizationDto organization, QProfileName key) {
  108. createdProfile = QualityProfileTesting.newQualityProfileDto()
  109. .setOrganizationUuid(organization.getUuid())
  110. .setLanguage(key.getLanguage())
  111. .setName(key.getName());
  112. return createdProfile;
  113. }
  114. @Override
  115. public void delete(DbSession dbSession, Collection<QProfileDto> profiles) {
  116. throw new UnsupportedOperationException();
  117. }
  118. }
  119. private static class DummyBackuper implements QProfileBackuper {
  120. private QProfileDto backuped;
  121. private String restoredBackup;
  122. private QProfileDto restored;
  123. @Override
  124. public void backup(DbSession dbSession, QProfileDto profile, Writer backupWriter) {
  125. this.backuped = profile;
  126. try {
  127. backupWriter.write(BACKUP);
  128. } catch (IOException e) {
  129. throw new IllegalStateException(e);
  130. }
  131. }
  132. @Override
  133. public QProfileRestoreSummary restore(DbSession dbSession, Reader backup, OrganizationDto organization, @Nullable String overriddenProfileName) {
  134. throw new UnsupportedOperationException();
  135. }
  136. @Override
  137. public QProfileRestoreSummary restore(DbSession dbSession, Reader backup, QProfileDto profile) {
  138. try {
  139. this.restoredBackup = IOUtils.toString(backup);
  140. this.restored = profile;
  141. return null;
  142. } catch (IOException e) {
  143. throw new IllegalStateException(e);
  144. }
  145. }
  146. }
  147. }