aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/it/java/org/sonar/db/property/PropertiesDaoWithPersisterIT.java
blob: f3bab89943e4367296d0b80f6cbb0c016a2571d8 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
 * 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.property;

import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.ArgumentCaptor;
import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
import org.sonar.db.component.ComponentQualifiers;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.audit.AuditPersister;
import org.sonar.db.audit.model.PropertyNewValue;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.user.UserDto;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

class PropertiesDaoWithPersisterIT {
  private static final String KEY = "key";
  private static final String ANOTHER_KEY = "another_key";
  private static final String PROJECT_KEY = "project_key";
  private static final String PROJECT_NAME = "project_name";
  private static final String PROJECT_UUID = "project_uuid";
  private static final String SECURED_KEY = "key.secured";
  private static final String USER_LOGIN = "user_login";
  private static final String USER_UUID = "user_uuid";
  private static final String VALUE = "value";

  private static final long INITIAL_DATE = 1_444_000L;

  private final AlwaysIncreasingSystem2 system2 = new AlwaysIncreasingSystem2(INITIAL_DATE, 1);

  private final AuditPersister auditPersister = mock(AuditPersister.class);
  private final ArgumentCaptor<PropertyNewValue> newValueCaptor = ArgumentCaptor.forClass(PropertyNewValue.class);

  @RegisterExtension
  private final DbTester db = DbTester.create(system2, auditPersister);

  private final DbClient dbClient = db.getDbClient();
  private final DbSession session = db.getSession();
  private final PropertiesDao underTest = dbClient.propertiesDao();

  @Test
  void saveGlobalTrackedPropertyIsPersisted() {
    when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
    underTest.saveProperty(new PropertyDto().setKey(KEY).setValue(VALUE));

    verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false));

    PropertyNewValue newValue = newValueCaptor.getValue();

    assertThat(newValue)
      .extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue,
        PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin)
      .containsExactly(KEY, VALUE, null, null);
    assertThat(newValue.toString()).doesNotContain("projectUuid");
  }

  @Test
  void saveGlobalTrackedAndSecuredPropertyIsPersisted() {
    when(auditPersister.isTrackedProperty(SECURED_KEY)).thenReturn(true);

    underTest.saveProperty(new PropertyDto().setKey(SECURED_KEY).setValue(VALUE));

    verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false));
    PropertyNewValue newValue = newValueCaptor.getValue();
    assertThat(newValue)
      .extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue,
        PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin)
      .containsExactly(SECURED_KEY, null, null, null);
    assertThat(newValue.toString()).doesNotContain("projectUuid");
  }

  @Test
  void saveProjectPropertyIsPersisted() {
    when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);

    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
    underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_KEY, PROJECT_NAME, ComponentQualifiers.PROJECT);

    verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false));
    PropertyNewValue newValue = newValueCaptor.getValue();
    assertThat(newValue)
      .extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue,
        PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin,
        PropertyNewValue::getComponentUuid, PropertyNewValue::getComponentKey,
        PropertyNewValue::getComponentName, PropertyNewValue::getQualifier)
      .containsExactly(propertyDto.getKey(), propertyDto.getValue(), propertyDto.getUserUuid(),
        USER_LOGIN, propertyDto.getEntityUuid(), PROJECT_KEY, PROJECT_NAME, "TRK");
    assertThat(newValue.toString()).contains("componentUuid");
  }

  @Test
  void saveApplicationTrackedPropertyIsPersisted() {
    when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);

    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
    underTest.saveProperty(session, propertyDto, USER_LOGIN, "app-key", "app-name", ComponentQualifiers.APP);

    verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false));
    PropertyNewValue newValue = newValueCaptor.getValue();
    assertThat(newValue)
      .extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue,
        PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin,
        PropertyNewValue::getComponentUuid, PropertyNewValue::getComponentKey,
        PropertyNewValue::getComponentName, PropertyNewValue::getQualifier)
      .containsExactly(propertyDto.getKey(), propertyDto.getValue(), propertyDto.getUserUuid(),
        USER_LOGIN, propertyDto.getEntityUuid(), "app-key", "app-name", "APP");
    assertThat(newValue.toString())
      .contains("componentUuid");
  }

  @Test
  void savePortfolioTrackedPropertyIsPersisted() {
    when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);

    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
    underTest.saveProperty(session, propertyDto, USER_LOGIN, "portfolio-key", "portfolio-name", ComponentQualifiers.VIEW);

    verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false));
    PropertyNewValue newValue = newValueCaptor.getValue();
    assertThat(newValue)
      .extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue,
        PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin,
        PropertyNewValue::getComponentUuid, PropertyNewValue::getComponentKey,
        PropertyNewValue::getComponentName, PropertyNewValue::getQualifier)
      .containsExactly(propertyDto.getKey(), propertyDto.getValue(), propertyDto.getUserUuid(),
        USER_LOGIN, propertyDto.getEntityUuid(), "portfolio-key", "portfolio-name", "VW");
    assertThat(newValue.toString())
      .contains("componentUuid");
  }

  @Test
  void saveProjectTrackedAndSecuredPropertyIsPersisted() {
    when(auditPersister.isTrackedProperty(SECURED_KEY)).thenReturn(true);

    PropertyDto propertyDto = getPropertyDto(SECURED_KEY, PROJECT_UUID, USER_UUID);
    underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_KEY, PROJECT_NAME, ComponentQualifiers.PROJECT);

    verify(auditPersister).addProperty(any(), newValueCaptor.capture(), eq(false));
    PropertyNewValue newValue = newValueCaptor.getValue();
    assertThat(newValue)
      .extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue,
        PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin,
        PropertyNewValue::getComponentUuid, PropertyNewValue::getComponentKey,
        PropertyNewValue::getComponentName, PropertyNewValue::getQualifier)
      .containsExactly(propertyDto.getKey(), null, propertyDto.getUserUuid(),
        USER_LOGIN, propertyDto.getEntityUuid(), PROJECT_KEY, PROJECT_NAME, "TRK");
    assertThat(newValue.toString()).contains("componentUuid");
  }

  @Test
  void deleteTrackedPropertyByQueryIsPersisted() {
    when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
    PropertyQuery query = getPropertyQuery(KEY);
    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
    underTest.saveProperty(session, propertyDto, USER_LOGIN, PROJECT_KEY, PROJECT_NAME, ComponentQualifiers.PROJECT);

    underTest.deleteByQuery(session, query);

    verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false));
    PropertyNewValue newValue = newValueCaptor.getValue();
    assertThat(newValue)
      .extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue,
        PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin,
        PropertyNewValue::getComponentUuid, PropertyNewValue::getComponentKey,
        PropertyNewValue::getComponentName)
      .containsExactly(query.key(), null, query.userUuid(),
        null, query.entityUuid(), null, null);
    assertThat(newValue.toString()).doesNotContain("userLogin");
  }

  @Test
  void deleteTrackedPropertyByQueryWithoutAffectedRowsIsNotPersisted() {
    PropertyQuery query = getPropertyQuery(KEY);

    underTest.deleteByQuery(session, query);

    verifyNoInteractions(auditPersister);
  }

  @Test
  void deleteTrackedPropertyIsPersisted() {
    when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);
    underTest.saveProperty(session, propertyDto, USER_LOGIN, null, null, null);

    underTest.delete(session, propertyDto, USER_LOGIN, PROJECT_KEY, PROJECT_NAME, ComponentQualifiers.PROJECT);

    verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false));
    PropertyNewValue newValue = newValueCaptor.getValue();
    assertThat(newValue)
      .extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue,
        PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin,
        PropertyNewValue::getComponentUuid, PropertyNewValue::getComponentKey,
        PropertyNewValue::getComponentName)
      .containsExactly(propertyDto.getKey(), propertyDto.getValue(), propertyDto.getUserUuid(),
        USER_LOGIN, propertyDto.getEntityUuid(), PROJECT_KEY, PROJECT_NAME);
    assertThat(newValue.toString()).contains("userLogin");
  }

  @Test
  void deleteTrackedPropertyWithoutAffectedRowsIsNotPersisted() {
    PropertyDto propertyDto = getPropertyDto(KEY, PROJECT_UUID, USER_UUID);

    underTest.delete(session, propertyDto, USER_LOGIN, PROJECT_KEY, PROJECT_NAME, ComponentQualifiers.PROJECT);

    verifyNoInteractions(auditPersister);
  }

  @Test
  void deleteTrackedGlobalPropertyIsPersisted() {
    when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
    PropertyDto propertyDto = getPropertyDto(KEY, null, null);
    underTest.saveProperty(session, propertyDto, null, null, null, null);

    underTest.deleteGlobalProperty(KEY, session);

    verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false));
    PropertyNewValue newValue = newValueCaptor.getValue();
    assertThat(newValue)
      .extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue,
        PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin,
        PropertyNewValue::getComponentUuid, PropertyNewValue::getComponentKey,
        PropertyNewValue::getComponentName)
      .containsExactly(KEY, null, null, null,
        null, null, null);
    assertThat(newValue.toString()).doesNotContain("projectUuid");
  }

  @Test
  void deleteTrackedGlobalPropertyWithoutAffectedRowsIsNotPersisted() {
    underTest.deleteGlobalProperty(KEY, session);

    verifyNoInteractions(auditPersister);
  }

  @Test
  void deleteTrackedPropertyByKeyAndValueIsPersisted() {
    when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);

    PropertyDto propertyDto = getPropertyDto(KEY, null, USER_UUID);
    underTest.saveProperty(session, propertyDto, USER_LOGIN, null, null, null);
    underTest.deleteByKeyAndValue(session, KEY, VALUE);

    verify(auditPersister).deleteProperty(any(), newValueCaptor.capture(), eq(false));
    PropertyNewValue newValue = newValueCaptor.getValue();
    assertThat(newValue)
      .extracting(PropertyNewValue::getPropertyKey, PropertyNewValue::getPropertyValue,
        PropertyNewValue::getUserUuid, PropertyNewValue::getUserLogin,
        PropertyNewValue::getComponentUuid, PropertyNewValue::getComponentKey,
        PropertyNewValue::getComponentName)
      .containsExactly(KEY, VALUE, null,
        null, null, null, null);
    assertThat(newValue.toString()).doesNotContain("projectUuid");
  }

  @Test
  void deleteTrackedPropertyByKeyAndValueWithoutAffectedRowsIsNotPersisted() {
    underTest.deleteByKeyAndValue(session, KEY, VALUE);

    verifyNoInteractions(auditPersister);
  }

  private PropertyDto getPropertyDto(String key, @Nullable String projectUuid, @Nullable String userUuid) {
    return new PropertyDto()
      .setKey(key)
      .setValue(VALUE)
      .setUserUuid(userUuid)
      .setEntityUuid(projectUuid);
  }

  private PropertyQuery getPropertyQuery(String key) {
    return PropertyQuery.builder()
      .setKey(key)
      .setEntityUuid(PROJECT_UUID)
      .setUserUuid(USER_UUID)
      .build();
  }

  private UserDto setUserProperties(@Nullable String value) {
    when(auditPersister.isTrackedProperty(KEY)).thenReturn(true);
    when(auditPersister.isTrackedProperty(ANOTHER_KEY)).thenReturn(false);
    when(auditPersister.isTrackedProperty(SECURED_KEY)).thenReturn(true);

    ComponentDto project = db.components().insertPrivateProject().getMainBranchComponent();
    UserDto user = db.users().insertUser();

    if (value == null) {
      value = user.getLogin();
    }

    PropertyDto dto1 = new PropertyDto().setKey(KEY)
      .setEntityUuid(project.uuid())
      .setUserUuid(user.getUuid())
      .setValue(value);
    PropertyDto dto2 = new PropertyDto().setKey(ANOTHER_KEY)
      .setEntityUuid(project.uuid())
      .setUserUuid(user.getUuid())
      .setValue(value);
    PropertyDto dto3 = new PropertyDto().setKey(SECURED_KEY)
      .setEntityUuid(project.uuid())
      .setUserUuid(user.getUuid())
      .setValue(value);
    db.properties().insertProperty(dto1, project.getKey(), project.name(), project.qualifier(), user.getLogin());
    db.properties().insertProperty(dto2, project.getKey(), project.name(), project.qualifier(), user.getLogin());
    db.properties().insertProperty(dto3, project.getKey(), project.name(), project.qualifier(), user.getLogin());
    return user;
  }
}