Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AnalysisPropertiesDaoTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.db.component;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. import java.util.Random;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.junit.rules.ExpectedException;
  27. import org.sonar.api.utils.System2;
  28. import org.sonar.api.impl.utils.TestSystem2;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.DbTester;
  31. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. public class AnalysisPropertiesDaoTest {
  34. private static final long NOW = 1_000L;
  35. @Rule
  36. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  37. @Rule
  38. public ExpectedException expectedException = ExpectedException.none();
  39. private final System2 system2 = new TestSystem2().setNow(NOW);
  40. private final DbSession dbSession = dbTester.getSession();
  41. private final AnalysisPropertiesDao underTest = new AnalysisPropertiesDao(system2);
  42. private final Random random = new Random();
  43. @Test
  44. public void insert_with_null_uuid_throws_NPE() {
  45. AnalysisPropertyDto analysisPropertyDto = new AnalysisPropertyDto()
  46. .setSnapshotUuid(randomAlphanumeric(10))
  47. .setKey(randomAlphanumeric(10))
  48. .setValue(randomAlphanumeric(10));
  49. expectedException.expect(NullPointerException.class);
  50. expectedException.expectMessage("uuid cannot be null");
  51. underTest.insert(dbSession, analysisPropertyDto);
  52. }
  53. @Test
  54. public void insert_with_null_key_throws_NPE() {
  55. AnalysisPropertyDto analysisPropertyDto = new AnalysisPropertyDto()
  56. .setSnapshotUuid(randomAlphanumeric(10))
  57. .setUuid(randomAlphanumeric(10))
  58. .setValue(randomAlphanumeric(10));
  59. expectedException.expect(NullPointerException.class);
  60. expectedException.expectMessage("key cannot be null");
  61. underTest.insert(dbSession, analysisPropertyDto);
  62. }
  63. @Test
  64. public void insert_with_null_snapshot_uuid_throws_NPE() {
  65. AnalysisPropertyDto analysisPropertyDto = new AnalysisPropertyDto()
  66. .setUuid(randomAlphanumeric(10))
  67. .setKey(randomAlphanumeric(10))
  68. .setValue(randomAlphanumeric(10));
  69. expectedException.expect(NullPointerException.class);
  70. expectedException.expectMessage("snapshot uuid cannot be null");
  71. underTest.insert(dbSession, analysisPropertyDto);
  72. }
  73. @Test
  74. public void insert_with_null_value_throws_NPE() {
  75. AnalysisPropertyDto analysisPropertyDto = new AnalysisPropertyDto()
  76. .setSnapshotUuid(randomAlphanumeric(10))
  77. .setUuid(randomAlphanumeric(10))
  78. .setKey(randomAlphanumeric(10));
  79. expectedException.expect(NullPointerException.class);
  80. expectedException.expectMessage("value cannot be null");
  81. underTest.insert(dbSession, analysisPropertyDto);
  82. }
  83. @Test
  84. public void insert_as_empty() {
  85. AnalysisPropertyDto analysisPropertyDto = insertAnalysisPropertyDto(0);
  86. assertThat(dbTester.countRowsOfTable(dbSession, "ANALYSIS_PROPERTIES")).isEqualTo(1);
  87. compareFirstValueWith(analysisPropertyDto);
  88. }
  89. @Test
  90. public void insert_as_text() {
  91. AnalysisPropertyDto analysisPropertyDto = insertAnalysisPropertyDto(1 + random.nextInt(3999));
  92. assertThat(dbTester.countRowsOfTable(dbSession, "ANALYSIS_PROPERTIES")).isEqualTo(1);
  93. compareFirstValueWith(analysisPropertyDto);
  94. }
  95. @Test
  96. public void insert_as_clob() {
  97. AnalysisPropertyDto analysisPropertyDto = insertAnalysisPropertyDto(4000 + random.nextInt(100));
  98. assertThat(dbTester.countRowsOfTable(dbSession, "ANALYSIS_PROPERTIES")).isEqualTo(1);
  99. compareFirstValueWith(analysisPropertyDto);
  100. }
  101. @Test
  102. public void insert_a_list() {
  103. List<AnalysisPropertyDto> propertyDtos = Arrays.asList(
  104. newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)),
  105. newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)),
  106. newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)),
  107. newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)),
  108. newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)),
  109. newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)),
  110. newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)),
  111. newAnalysisPropertyDto(random.nextInt(8000), randomAlphanumeric(40)));
  112. underTest.insert(dbSession, propertyDtos);
  113. assertThat(dbTester.countRowsOfTable(dbSession, "ANALYSIS_PROPERTIES")).isEqualTo(propertyDtos.size());
  114. }
  115. @Test
  116. public void selectByAnalysisUuid_should_return_correct_values() {
  117. String snapshotUuid = randomAlphanumeric(40);
  118. List<AnalysisPropertyDto> propertyDtos = Arrays.asList(
  119. newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid),
  120. newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid),
  121. newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid),
  122. newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid),
  123. newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid),
  124. newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid),
  125. newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid),
  126. newAnalysisPropertyDto(random.nextInt(8000), snapshotUuid));
  127. underTest.insert(dbSession, propertyDtos);
  128. assertThat(dbTester.countRowsOfTable(dbSession, "ANALYSIS_PROPERTIES")).isEqualTo(propertyDtos.size());
  129. List<AnalysisPropertyDto> result = underTest.selectBySnapshotUuid(dbSession, snapshotUuid);
  130. assertThat(result).containsExactlyInAnyOrder(propertyDtos.toArray(new AnalysisPropertyDto[0]));
  131. }
  132. private AnalysisPropertyDto insertAnalysisPropertyDto(int valueLength) {
  133. AnalysisPropertyDto analysisPropertyDto = newAnalysisPropertyDto(valueLength, randomAlphanumeric(40));
  134. underTest.insert(dbSession, analysisPropertyDto);
  135. return analysisPropertyDto;
  136. }
  137. private AnalysisPropertyDto newAnalysisPropertyDto(int valueLength, String snapshotUuid) {
  138. return new AnalysisPropertyDto()
  139. .setSnapshotUuid(snapshotUuid)
  140. .setKey(randomAlphanumeric(512))
  141. .setUuid(randomAlphanumeric(40))
  142. .setValue(randomAlphanumeric(valueLength))
  143. .setCreatedAt( 1_000L);
  144. }
  145. private void compareFirstValueWith(AnalysisPropertyDto analysisPropertyDto) {
  146. AnalysisPropertyDto dtoFromDatabase = underTest.selectBySnapshotUuid(dbSession, analysisPropertyDto.getSnapshotUuid()).get(0);
  147. assertThat(dtoFromDatabase).isEqualTo(analysisPropertyDto);
  148. }
  149. }