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.

SourceServiceTest.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.source;
  21. import com.google.common.collect.Lists;
  22. import java.util.Arrays;
  23. import java.util.HashSet;
  24. import java.util.List;
  25. import java.util.Optional;
  26. import java.util.Set;
  27. import org.junit.Before;
  28. import org.junit.Rule;
  29. import org.junit.Test;
  30. import org.junit.rules.ExpectedException;
  31. import org.sonar.api.utils.System2;
  32. import org.sonar.core.util.Uuids;
  33. import org.sonar.db.DbTester;
  34. import org.sonar.db.protobuf.DbFileSources;
  35. import org.sonar.db.source.FileSourceDto;
  36. import org.sonar.server.source.index.FileSourceTesting;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. import static org.mockito.Mockito.mock;
  39. import static org.mockito.Mockito.when;
  40. public class SourceServiceTest {
  41. public static final String FILE_UUID = "FILE_UUID";
  42. @Rule
  43. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  44. @Rule
  45. public ExpectedException expectedException = ExpectedException.none();
  46. HtmlSourceDecorator htmlDecorator = mock(HtmlSourceDecorator.class);
  47. SourceService underTest = new SourceService(dbTester.getDbClient(), htmlDecorator);
  48. @Before
  49. public void injectFakeLines() {
  50. FileSourceDto dto = new FileSourceDto();
  51. dto.setFileUuid(FILE_UUID).setUuid(Uuids.createFast()).setProjectUuid("PROJECT_UUID");
  52. dto.setSourceData(FileSourceTesting.newFakeData(10).build());
  53. dbTester.getDbClient().fileSourceDao().insert(dbTester.getSession(), dto);
  54. dbTester.commit();
  55. }
  56. @Test
  57. public void get_range_of_lines() {
  58. Set<Integer> lineNumbers = new HashSet<>(Arrays.asList(1, 5, 6));
  59. Optional<Iterable<DbFileSources.Line>> linesOpt = underTest.getLines(dbTester.getSession(), FILE_UUID, lineNumbers);
  60. assertThat(linesOpt.isPresent()).isTrue();
  61. List<DbFileSources.Line> lines = Lists.newArrayList(linesOpt.get());
  62. assertThat(lines).hasSize(3);
  63. assertThat(lines.get(0).getLine()).isEqualTo(1);
  64. assertThat(lines.get(1).getLine()).isEqualTo(5);
  65. assertThat(lines.get(2).getLine()).isEqualTo(6);
  66. }
  67. @Test
  68. public void get_set_of_lines() {
  69. Optional<Iterable<DbFileSources.Line>> linesOpt = underTest.getLines(dbTester.getSession(), FILE_UUID, 5, 7);
  70. assertThat(linesOpt.isPresent()).isTrue();
  71. List<DbFileSources.Line> lines = Lists.newArrayList(linesOpt.get());
  72. assertThat(lines).hasSize(3);
  73. assertThat(lines.get(0).getLine()).isEqualTo(5);
  74. assertThat(lines.get(1).getLine()).isEqualTo(6);
  75. assertThat(lines.get(2).getLine()).isEqualTo(7);
  76. }
  77. @Test
  78. public void get_range_of_lines_as_raw_text() {
  79. Optional<Iterable<String>> linesOpt = underTest.getLinesAsRawText(dbTester.getSession(), FILE_UUID, 5, 7);
  80. assertThat(linesOpt.isPresent()).isTrue();
  81. List<String> lines = Lists.newArrayList(linesOpt.get());
  82. assertThat(lines).containsExactly("SOURCE_5", "SOURCE_6", "SOURCE_7");
  83. }
  84. @Test
  85. public void get_range_of_lines_as_html() {
  86. when(htmlDecorator.getDecoratedSourceAsHtml("SOURCE_5", "HIGHLIGHTING_5", "SYMBOLS_5")).thenReturn("HTML_5");
  87. when(htmlDecorator.getDecoratedSourceAsHtml("SOURCE_6", "HIGHLIGHTING_6", "SYMBOLS_6")).thenReturn("HTML_6");
  88. when(htmlDecorator.getDecoratedSourceAsHtml("SOURCE_7", "HIGHLIGHTING_7", "SYMBOLS_7")).thenReturn("HTML_7");
  89. Optional<Iterable<String>> linesOpt = underTest.getLinesAsHtml(dbTester.getSession(), FILE_UUID, 5, 7);
  90. assertThat(linesOpt.isPresent()).isTrue();
  91. List<String> lines = Lists.newArrayList(linesOpt.get());
  92. assertThat(lines).containsExactly("HTML_5", "HTML_6", "HTML_7");
  93. }
  94. @Test
  95. public void getLines_fails_if_range_starts_at_zero() {
  96. expectedException.expect(IllegalArgumentException.class);
  97. expectedException.expectMessage("Line number must start at 1, got 0");
  98. underTest.getLines(dbTester.getSession(), FILE_UUID, 0, 2);
  99. }
  100. @Test
  101. public void getLines_fails_if_range_upper_bound_less_than_lower_bound() {
  102. expectedException.expect(IllegalArgumentException.class);
  103. expectedException.expectMessage("Line number must greater than or equal to 5, got 4");
  104. underTest.getLines(dbTester.getSession(), FILE_UUID, 5, 4);
  105. }
  106. @Test
  107. public void getLines_returns_empty_iterable_if_range_is_out_of_scope() {
  108. Optional<Iterable<DbFileSources.Line>> lines = underTest.getLines(dbTester.getSession(), FILE_UUID, 500, 510);
  109. assertThat(lines.isPresent()).isTrue();
  110. assertThat(lines.get()).isEmpty();
  111. }
  112. @Test
  113. public void getLines_file_does_not_exist() {
  114. Optional<Iterable<DbFileSources.Line>> lines = underTest.getLines(dbTester.getSession(), "FILE_DOES_NOT_EXIST", 1, 10);
  115. assertThat(lines.isPresent()).isFalse();
  116. }
  117. }