aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-ce-task/src/it/java/org/sonar/ce/task/log/CeTaskMessagesImplIT.java
blob: d3f91862a057b65d272fae3533f2de2836fc4e99 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * 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.ce.task.log;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.sonar.api.utils.System2;
import org.sonar.ce.task.CeTask;
import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbClient;
import org.sonar.db.DbTester;

import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.RandomStringUtils.secure;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

public class CeTaskMessagesImplIT {
  @Rule
  public DbTester dbTester = DbTester.create(System2.INSTANCE);

  private DbClient dbClient = dbTester.getDbClient();
  private UuidFactory uuidFactory = mock(UuidFactory.class);
  private String taskUuid = secure().nextAlphabetic(12);

  private CeTask ceTask = new CeTask.Builder()
    .setUuid(taskUuid)
    .setType(secure().nextAlphabetic(5))
    .build();

  private CeTaskMessagesImpl underTest = new CeTaskMessagesImpl(dbClient, uuidFactory, ceTask);

  @Test
  public void add_fails_with_NPE_if_arg_is_null() {
    assertThatThrownBy(() -> underTest.add(null))
      .isInstanceOf(NullPointerException.class)
      .hasMessage("message can't be null");
  }

  @Test
  public void add_persist_message_to_DB() {
    CeTaskMessages.Message message = new CeTaskMessages.Message(secure().nextAlphabetic(20), 2_999L);
    String uuid = secure().nextAlphanumeric(40);
    when(uuidFactory.create()).thenReturn(uuid);

    underTest.add(message);

    assertThat(dbTester.select("select uuid as \"UUID\", task_uuid as \"TASK_UUID\", message as \"MESSAGE\", created_at as \"CREATED_AT\" from ce_task_message"))
      .extracting(t -> t.get("UUID"), t -> t.get("TASK_UUID"), t -> t.get("MESSAGE"), t -> t.get("CREATED_AT"))
      .containsOnly(tuple(uuid, taskUuid, message.getText(), message.getTimestamp()));
  }

  @Test
  public void addAll_fails_with_NPE_if_arg_is_null() {
    assertThatThrownBy(() -> underTest.addAll(null))
      .isInstanceOf(NullPointerException.class);
  }

  @Test
  public void addAll_fails_with_NPE_if_any_message_in_list_is_null() {
    Random random = new Random();
    List<CeTaskMessages.Message> messages = Stream.of(
      // some (or none) non null Message before null one
      IntStream.range(0, random.nextInt(5)).mapToObj(i -> new CeTaskMessages.Message(secure().nextAlphabetic(3) + "_i", 1_999L + i)),
      Stream.of((CeTaskMessages.Message) null),
      // some (or none) non null Message after null one
      IntStream.range(0, random.nextInt(5)).mapToObj(i -> new CeTaskMessages.Message(secure().nextAlphabetic(3) + "_i", 1_999L + i)))
      .flatMap(t -> t)
      .collect(toList());

    assertThatThrownBy(() -> underTest.addAll(messages))
      .isInstanceOf(NullPointerException.class)
      .hasMessage("message can't be null");
  }

  @Test
  public void addAll_has_no_effect_if_arg_is_empty() {
    DbClient dbClientMock = mock(DbClient.class);
    UuidFactory uuidFactoryMock = mock(UuidFactory.class);
    CeTask ceTaskMock = mock(CeTask.class);
    CeTaskMessagesImpl underTest = new CeTaskMessagesImpl(dbClientMock, uuidFactoryMock, ceTaskMock);

    underTest.addAll(Collections.emptyList());

    verifyNoInteractions(dbClientMock, uuidFactoryMock, ceTaskMock);
  }

  @Test
  public void addAll_persists_all_messages_to_DB() {
    int messageCount = 5;
    String[] uuids = IntStream.range(0, messageCount).mapToObj(i -> "UUId_" + i).toArray(String[]::new);
    CeTaskMessages.Message[] messages = IntStream.range(0, messageCount)
      .mapToObj(i -> new CeTaskMessages.Message("message_" + i, 2_999L + i))
      .toArray(CeTaskMessages.Message[]::new);
    when(uuidFactory.create()).thenAnswer(new Answer<String>() {
      int i = 0;

      @Override
      public String answer(InvocationOnMock invocation) {
        return uuids[i++];
      }
    });

    underTest.addAll(Arrays.stream(messages).toList());

    assertThat(dbTester.select("select uuid as \"UUID\", task_uuid as \"TASK_UUID\", message as \"MESSAGE\", created_at as \"CREATED_AT\" from ce_task_message"))
      .extracting(t -> t.get("UUID"), t -> t.get("TASK_UUID"), t -> t.get("MESSAGE"), t -> t.get("CREATED_AT"))
      .containsOnly(
        tuple(uuids[0], taskUuid, messages[0].getText(), messages[0].getTimestamp()),
        tuple(uuids[1], taskUuid, messages[1].getText(), messages[1].getTimestamp()),
        tuple(uuids[2], taskUuid, messages[2].getText(), messages[2].getTimestamp()),
        tuple(uuids[3], taskUuid, messages[3].getText(), messages[3].getTimestamp()),
        tuple(uuids[4], taskUuid, messages[4].getText(), messages[4].getTimestamp()));
  }
}