aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/it/java/org/sonar/db/user/UserDismissedMessagesDaoIT.java
blob: c31b66be9277228dd20c1f6898ec234ad9401b71 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * 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.user;

import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonar.api.utils.System2;
import org.sonar.core.util.Uuids;
import org.sonar.db.DbTester;
import org.sonar.db.dismissmessage.MessageType;
import org.sonar.db.project.ProjectDto;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.db.dismissmessage.MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE;

class UserDismissedMessagesDaoIT {

  @RegisterExtension
  private final DbTester db = DbTester.create(System2.INSTANCE);

  private final UserDismissedMessagesDao underTest = db.getDbClient().userDismissedMessagesDao();

  @Test
  void insert_user_dismissed_message() {
    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
    UserDto user = db.users().insertUser();
    UserDismissedMessageDto dto = newDto(project, user);

    underTest.insert(db.getSession(), dto);

    Optional<UserDismissedMessageDto> dtoFromDb = underTest.selectByUserAndProjectAndMessageType(db.getSession(), user.getUuid(), project
      , dto.getMessageType());
    assertThat(dtoFromDb).isPresent();
    assertThat(dtoFromDb.get().getUuid()).isEqualTo(dto.getUuid());
    assertThat(dtoFromDb.get().getUserUuid()).isEqualTo(dto.getUserUuid());
    assertThat(dtoFromDb.get().getProjectUuid()).isEqualTo(dto.getProjectUuid());
    assertThat(dtoFromDb.get().getMessageType()).isEqualTo(dto.getMessageType());
    assertThat(dtoFromDb.get().getMessageType().isDismissible()).isEqualTo(dto.getMessageType().isDismissible());
    assertThat(dtoFromDb.get().getCreatedAt()).isEqualTo(dto.getCreatedAt());
  }

  @Test
  void selectByUserAndProjectAndMessageType_returns_object_if_record_found() {
    UserDto user = db.users().insertUser();
    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
    db.users().insertUserDismissedMessageOnProject(user, project, MessageType.GENERIC);

    Optional<UserDismissedMessageDto> result = underTest.selectByUserAndProjectAndMessageType(db.getSession(), user.getUuid(), project,
      MessageType.GENERIC);

    assertThat(result).isPresent();
    assertThat(result.get().getUserUuid()).isEqualTo(user.getUuid());
    assertThat(result.get().getProjectUuid()).isEqualTo(project.getUuid());
    assertThat(result.get().getMessageType()).isEqualTo(MessageType.GENERIC);
  }

  @Test
  void selectByUserAndMessageType_returns_object_if_record_found() {
    UserDto user = db.users().insertUser();
    db.users().insertUserDismissedMessageOnInstance(user, MessageType.GENERIC);

    Optional<UserDismissedMessageDto> result = underTest.selectByUserUuidAndMessageType(db.getSession(), user.getUuid(),
      MessageType.GENERIC);

    assertThat(result).isPresent();
    assertThat(result.get().getUserUuid()).isEqualTo(user.getUuid());
    assertThat(result.get().getProjectUuid()).isNull();
    assertThat(result.get().getMessageType()).isEqualTo(MessageType.GENERIC);
  }

  @Test
  void selectByUserAndProjectAndMessageType_returns_absent_if_no_record_found() {
    UserDto user = db.users().insertUser();
    ProjectDto project = db.components().insertPrivateProject().getProjectDto();
    db.users().insertUserDismissedMessageOnProject(user, project, MessageType.GENERIC);

    Optional<UserDismissedMessageDto> result = underTest.selectByUserAndProjectAndMessageType(db.getSession(), user.getUuid(), project,
      SUGGEST_DEVELOPER_EDITION_UPGRADE);

    assertThat(result).isNotPresent();
  }

  @Test
  void selectByUserUuid_returns_all_dismissed_messages_of_a_user() {
    UserDto user1 = db.users().insertUser();
    UserDto user2 = db.users().insertUser();
    ProjectDto project1 = db.components().insertPrivateProject().getProjectDto();
    ProjectDto project2 = db.components().insertPrivateProject().getProjectDto();
    db.users().insertUserDismissedMessageOnProject(user1, project1, MessageType.GENERIC);
    db.users().insertUserDismissedMessageOnProject(user1, project2, MessageType.GENERIC);
    UserDismissedMessageDto dto1 = db.users().insertUserDismissedMessageOnProject(user2, project1, MessageType.GENERIC);
    UserDismissedMessageDto dto2 = db.users().insertUserDismissedMessageOnProject(user2, project2, MessageType.GENERIC);

    List<UserDismissedMessageDto> result = underTest.selectByUser(db.getSession(), user2);

    assertThat(result).hasSize(2);
    assertThat(result).extracting(UserDismissedMessageDto::getUuid, UserDismissedMessageDto::getUserUuid,
        UserDismissedMessageDto::getProjectUuid,
        UserDismissedMessageDto::getMessageType)
      .containsExactlyInAnyOrder(
        tuple(dto1.getUuid(), user2.getUuid(), project1.getUuid(), MessageType.GENERIC),
        tuple(dto2.getUuid(), user2.getUuid(), project2.getUuid(), MessageType.GENERIC));
  }

  @Test
  void deleteByUserUuid_removes_dismiss_warning_data_of_a_user() {
    UserDto user1 = db.users().insertUser();
    UserDto user2 = db.users().insertUser();
    ProjectDto project1 = db.components().insertPrivateProject().getProjectDto();
    ProjectDto project2 = db.components().insertPrivateProject().getProjectDto();
    db.users().insertUserDismissedMessageOnProject(user1, project1, MessageType.GENERIC);
    db.users().insertUserDismissedMessageOnProject(user1, project2, MessageType.GENERIC);
    db.users().insertUserDismissedMessageOnProject(user2, project1, MessageType.GENERIC);
    db.users().insertUserDismissedMessageOnProject(user2, project2, MessageType.GENERIC);

    underTest.deleteByUser(db.getSession(), user2);

    assertThat(underTest.selectByUser(db.getSession(), user1)).hasSize(2);
    assertThat(underTest.selectByUser(db.getSession(), user2)).isEmpty();
  }

  @Test
  void deleteByUserUuid_removes_dismissed_messages_of_that_type() {
    UserDto user1 = db.users().insertUser();
    UserDto user2 = db.users().insertUser();
    ProjectDto project1 = db.components().insertPrivateProject().getProjectDto();
    ProjectDto project2 = db.components().insertPrivateProject().getProjectDto();
    UserDismissedMessageDto dto1 = db.users().insertUserDismissedMessageOnProject(user1, project1, MessageType.GENERIC);
    db.users().insertUserDismissedMessageOnProject(user1, project2, SUGGEST_DEVELOPER_EDITION_UPGRADE);
    db.users().insertUserDismissedMessageOnProject(user2, project1, SUGGEST_DEVELOPER_EDITION_UPGRADE);
    UserDismissedMessageDto dto2 = db.users().insertUserDismissedMessageOnProject(user2, project2, MessageType.GENERIC);

    underTest.deleteByType(db.getSession(), SUGGEST_DEVELOPER_EDITION_UPGRADE);

    assertThat(underTest.selectByUser(db.getSession(), user1))
      .extracting(UserDismissedMessageDto::getUuid)
      .containsExactly(dto1.getUuid());
    assertThat(underTest.selectByUser(db.getSession(), user2))
      .extracting(UserDismissedMessageDto::getUuid)
      .containsExactly(dto2.getUuid());
  }

  static UserDismissedMessageDto newDto(ProjectDto project, UserDto user) {
    return new UserDismissedMessageDto()
      .setUuid(Uuids.createFast())
      .setMessageType(MessageType.GENERIC)
      .setUserUuid(user.getUuid())
      .setProjectUuid(project.getUuid());
  }
}