aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/test/java/org/sonar/db/source/FileSourceDtoTest.java
blob: c158e726ad2d39c15a0b2ab3683ea16e798563fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.db.source;

import com.google.common.base.Joiner;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
import org.sonar.db.protobuf.DbFileSources;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class FileSourceDtoTest {
  private static final String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ac magna libero. " +
    "Integer eu quam vulputate, interdum ante quis, sodales mauris. Nam mollis ornare dolor at maximus. Cras pharetra aliquam fringilla. " +
    "Nunc hendrerit, elit eu mattis fermentum, ligula metus malesuada nunc, non fermentum augue tellus eu odio. Praesent ut vestibulum " +
    "nibh. " +
    "Curabitur sit amet dignissim magna, at efficitur dolor. Ut non felis aliquam justo euismod gravida. Morbi eleifend vitae ante eu " +
    "pulvinar. " +
    "Aliquam rhoncus magna quis lorem posuere semper.";


  @Test
  void getSourceData_throws_ISE_with_id_fileUuid_and_projectUuid_in_message_when_data_cant_be_read() {
    String uuid = "uuid";
    String fileUuid = "file uuid";
    String projectUuid = "project uuid";
    FileSourceDto underTest = new FileSourceDto()
      .setBinaryData(new byte[]{1, 2, 3, 4, 5})
      .setUuid(uuid)
      .setFileUuid(fileUuid)
      .setProjectUuid(projectUuid);

    assertThatThrownBy(underTest::getSourceData)
      .isInstanceOf(IllegalStateException.class)
      .hasMessage("Fail to decompress and deserialize source data [uuid=" + uuid + ",fileUuid=" + fileUuid + ",projectUuid=" + projectUuid + "]");
  }

  @Test
  void getSourceData_reads_Data_object_bigger_than_default_size_limit() {
    DbFileSources.Data build = createOver64MBDataStructure();
    byte[] bytes = FileSourceDto.encodeSourceData(build);

    DbFileSources.Data data = new FileSourceDto().decodeSourceData(bytes);
    assertThat(data.getLinesCount()).isEqualTo(build.getLinesCount());
  }

  private static DbFileSources.Data createOver64MBDataStructure() {
    DbFileSources.Data.Builder dataBuilder = DbFileSources.Data.newBuilder();
    DbFileSources.Line.Builder lineBuilder = DbFileSources.Line.newBuilder();
    for (int i = 0; i < 199999; i++) {
      dataBuilder.addLines(
        lineBuilder.setSource(LOREM_IPSUM)
          .setLine(i)
          .build());
    }
    return dataBuilder.build();
  }

  @Test
  void new_FileSourceDto_as_lineCount_0_and_rawLineHashes_to_null() {
    FileSourceDto underTest = new FileSourceDto();

    assertThat(underTest.getLineCount()).isZero();
    assertThat(underTest.getLineHashes()).isEmpty();
    assertThat(underTest.getRawLineHashes()).isNull();
  }

  @Test
  void setLineHashes_null_sets_lineCount_to_0_and_rawLineHashes_to_null() {
    FileSourceDto underTest = new FileSourceDto();
    underTest.setLineHashes(null);

    assertThat(underTest.getLineCount()).isZero();
    assertThat(underTest.getLineHashes()).isEmpty();
    assertThat(underTest.getRawLineHashes()).isNull();
  }

  @Test
  void setLineHashes_empty_sets_lineCount_to_1_and_rawLineHashes_to_null() {
    FileSourceDto underTest = new FileSourceDto();
    underTest.setLineHashes(Collections.emptyList());

    assertThat(underTest.getLineCount()).isOne();
    assertThat(underTest.getLineHashes()).isEmpty();
    assertThat(underTest.getRawLineHashes()).isNull();
  }

  @Test
  void setLineHashes_sets_lineCount_to_size_of_list_and_rawLineHashes_to_join_by_line_return() {
    FileSourceDto underTest = new FileSourceDto();
    int expected = 1 + new Random().nextInt(96);
    List<String> lineHashes = IntStream.range(0, expected).mapToObj(String::valueOf).toList();
    underTest.setLineHashes(lineHashes);

    assertThat(underTest.getLineCount()).isEqualTo(expected);
    assertThat(underTest.getRawLineHashes()).isEqualTo(Joiner.on('\n').join(lineHashes));
  }
}