aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/it/java/org/sonar/db/property/InternalComponentPropertiesDaoIT.java
blob: bf3561f9d99f549461a3a760de4edb2d345b1d8f (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
/*
 * 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 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.UuidFactory;
import org.sonar.core.util.UuidFactoryFast;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class InternalComponentPropertiesDaoIT {

  private static final String SOME_KEY = "key1";
  private static final String SOME_COMPONENT = "component1";
  private static final String SOME_VALUE = "value";

  private final System2 system2 = mock(System2.class);

  @RegisterExtension
  private final DbTester dbTester = DbTester.create(system2);
  private final DbSession dbSession = dbTester.getSession();
  private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
  private final InternalComponentPropertiesDao underTest = new InternalComponentPropertiesDao(system2, uuidFactory);

  @Test
  void insertOrUpdate_insert_property_if_it_doesnt_already_exist() {
    long createdAt = 10L;
    when(system2.now()).thenReturn(createdAt);

    underTest.insertOrUpdate(dbSession, SOME_COMPONENT, SOME_KEY, SOME_VALUE);

    InternalComponentPropertyDto dto = underTest.selectByComponentUuidAndKey(dbSession, SOME_COMPONENT, SOME_KEY).get();

    assertThat(dto.getComponentUuid()).isEqualTo(SOME_COMPONENT);
    assertThat(dto.getKey()).isEqualTo(SOME_KEY);
    assertThat(dto.getValue()).isEqualTo(SOME_VALUE);
    assertThat(dto.getUpdatedAt()).isEqualTo(createdAt);
    assertThat(dto.getCreatedAt()).isEqualTo(createdAt);
  }

  @Test
  void insertOrUpdate_update_property_if_it_already_exists() {
    long createdAt = 10L;
    when(system2.now()).thenReturn(createdAt);

    InternalComponentPropertyDto dto = saveDto();

    long updatedAt = 20L;
    when(system2.now()).thenReturn(updatedAt);

    String newValue = "newValue";
    underTest.insertOrUpdate(dbSession, dto.getComponentUuid(), dto.getKey(), newValue);

    InternalComponentPropertyDto updatedDto = underTest.selectByComponentUuidAndKey(dbSession, dto.getComponentUuid(), dto.getKey()).get();

    assertThat(updatedDto.getComponentUuid()).isEqualTo(SOME_COMPONENT);
    assertThat(updatedDto.getKey()).isEqualTo(SOME_KEY);
    assertThat(updatedDto.getValue()).isEqualTo(newValue);
    assertThat(updatedDto.getUpdatedAt()).isEqualTo(updatedAt);
    assertThat(updatedDto.getCreatedAt()).isEqualTo(createdAt);
  }

  @Test
  void replaceValue_sets_to_newValue_if_oldValue_matches_expected() {
    long createdAt = 10L;
    when(system2.now()).thenReturn(createdAt);
    InternalComponentPropertyDto dto = saveDto();

    long updatedAt = 20L;
    when(system2.now()).thenReturn(updatedAt);

    String newValue = "other value";
    underTest.replaceValue(dbSession, SOME_COMPONENT, SOME_KEY, SOME_VALUE, newValue);

    InternalComponentPropertyDto updatedDto = underTest.selectByComponentUuidAndKey(dbSession, dto.getComponentUuid(), dto.getKey()).get();

    assertThat(updatedDto.getValue()).isEqualTo(newValue);
    assertThat(updatedDto.getUpdatedAt()).isEqualTo(updatedAt);
    assertThat(updatedDto.getCreatedAt()).isEqualTo(createdAt);
  }

  @Test
  void replaceValue_does_not_replace_if_oldValue_does_not_match_expected() {
    long createdAt = 10L;
    when(system2.now()).thenReturn(createdAt);
    InternalComponentPropertyDto dto = saveDto();

    long updatedAt = 20L;
    when(system2.now()).thenReturn(updatedAt);

    underTest.replaceValue(dbSession, SOME_COMPONENT, SOME_KEY, SOME_VALUE + "foo", "other value");

    InternalComponentPropertyDto updatedDto = underTest.selectByComponentUuidAndKey(dbSession, dto.getComponentUuid(), dto.getKey()).get();

    assertThat(updatedDto.getValue()).isEqualTo(SOME_VALUE);
    assertThat(updatedDto.getUpdatedAt()).isEqualTo(createdAt);
    assertThat(updatedDto.getCreatedAt()).isEqualTo(createdAt);
  }

  @Test
  void replaceValue_does_not_insert_if_record_does_not_exist() {
    underTest.replaceValue(dbSession, SOME_COMPONENT, SOME_KEY, SOME_VALUE, "other value");
    assertThat(underTest.selectByComponentUuidAndKey(dbSession, SOME_COMPONENT, SOME_KEY)).isEmpty();
  }

  @Test
  void select_by_component_uuid_and_key_returns_property() {
    saveDto();

    Optional<InternalComponentPropertyDto> result = underTest.selectByComponentUuidAndKey(dbSession, SOME_COMPONENT, SOME_KEY);
    assertThat(result.get())
      .extracting("componentUuid", "key", "value")
      .contains(SOME_COMPONENT, SOME_KEY, SOME_VALUE);
  }

  @Test
  void select_by_component_uuid_and_key_returns_empty_when_it_doesnt_exist() {
    saveDto();

    assertThat(underTest.selectByComponentUuidAndKey(dbSession, "other_component", SOME_KEY)).isEmpty();
    assertThat(underTest.selectByComponentUuidAndKey(dbSession, SOME_COMPONENT, "other_key")).isEmpty();
  }

  @Test
  void delete_by_component_uuid_deletes_all_properties_with_given_componentUuid() {
    underTest.insertOrUpdate(dbSession, SOME_COMPONENT, SOME_KEY, SOME_VALUE);
    underTest.insertOrUpdate(dbSession, SOME_COMPONENT, "other_key", "foo");
    underTest.insertOrUpdate(dbSession, "other_component", SOME_KEY, SOME_VALUE);

    assertThat(underTest.deleteByComponentUuid(dbSession, SOME_COMPONENT)).isEqualTo(2);
    assertThat(underTest.selectByComponentUuidAndKey(dbSession, SOME_COMPONENT, SOME_KEY)).isEmpty();
    assertThat(underTest.selectByComponentUuidAndKey(dbSession, "other_component", SOME_KEY)).isNotEmpty();
  }

  @Test
  void delete_by_component_uuid_and_key_does_nothing_if_property_doesnt_exist() {
    saveDto();

    assertThat(underTest.deleteByComponentUuid(dbSession, "other_component")).isZero();
    assertThat(underTest.selectByComponentUuidAndKey(dbSession, SOME_COMPONENT, SOME_KEY)).isNotEmpty();
  }

  @Test
  void loadDbKey_loads_dbKeys_for_all_components_with_given_property_and_value() {
    ComponentDto portfolio1 = dbTester.components().insertPublicPortfolio();
    ComponentDto portfolio2 = dbTester.components().insertPublicPortfolio();
    ComponentDto portfolio3 = dbTester.components().insertPublicPortfolio();
    ComponentDto portfolio4 = dbTester.components().insertPublicPortfolio();

    underTest.insertOrUpdate(dbSession, portfolio1.uuid(), SOME_KEY, SOME_VALUE);
    underTest.insertOrUpdate(dbSession, portfolio2.uuid(), SOME_KEY, "bar");
    underTest.insertOrUpdate(dbSession, portfolio3.uuid(), "foo", SOME_VALUE);

    assertThat(underTest.selectDbKeys(dbSession, SOME_KEY, SOME_VALUE)).containsOnly(portfolio1.getKey());
  }

  private InternalComponentPropertyDto saveDto() {
    underTest.insertOrUpdate(dbSession, SOME_COMPONENT, SOME_KEY, SOME_VALUE);
    return underTest.selectByComponentUuidAndKey(dbSession, SOME_COMPONENT, SOME_KEY).get();
  }

}