-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ComponentDbTester.java
303 lines (256 loc) · 11.8 KB
/
ComponentDbTester.java
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
/*
* SonarQube
* Copyright (C) 2009-2017 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.component;
import java.util.Arrays;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static java.util.Arrays.asList;
import static org.sonar.db.component.BranchType.LONG;
import static org.sonar.db.component.ComponentTesting.newApplication;
import static org.sonar.db.component.ComponentTesting.newBranchDto;
import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
import static org.sonar.db.component.ComponentTesting.newProjectBranch;
import static org.sonar.db.component.ComponentTesting.newPublicProjectDto;
import static org.sonar.db.component.ComponentTesting.newSubView;
import static org.sonar.db.component.ComponentTesting.newView;
import static org.sonar.db.component.SnapshotTesting.newAnalysis;
public class ComponentDbTester {
private final DbTester db;
private final DbClient dbClient;
private final DbSession dbSession;
public ComponentDbTester(DbTester db) {
this.db = db;
this.dbClient = db.getDbClient();
this.dbSession = db.getSession();
}
public SnapshotDto insertProjectAndSnapshot(ComponentDto component) {
return insertComponentAndSnapshot(component);
}
public SnapshotDto insertViewAndSnapshot(ComponentDto component) {
return insertComponentAndSnapshot(component);
}
private SnapshotDto insertComponentAndSnapshot(ComponentDto component) {
dbClient.componentDao().insert(dbSession, component);
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(component));
db.commit();
return snapshot;
}
public ComponentDto insertComponent(ComponentDto component) {
return insertComponentImpl(component, null, noExtraConfiguration());
}
public ComponentDto insertPrivateProject() {
return insertComponentImpl(newPrivateProjectDto(db.getDefaultOrganization()), true, noExtraConfiguration());
}
public ComponentDto insertPublicProject() {
return insertComponentImpl(ComponentTesting.newPublicProjectDto(db.getDefaultOrganization()), false, noExtraConfiguration());
}
@SafeVarargs
public final ComponentDto insertPrivateProject(Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(newPrivateProjectDto(db.getDefaultOrganization()), true, dtoPopulators);
}
@SafeVarargs
public final ComponentDto insertPublicProject(Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(newPublicProjectDto(db.getDefaultOrganization()), false, dtoPopulators);
}
@SafeVarargs
public final ComponentDto insertPrivateProject(OrganizationDto organizationDto, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(newPrivateProjectDto(organizationDto), true, dtoPopulators);
}
@SafeVarargs
public final ComponentDto insertPublicProject(OrganizationDto organizationDto, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(newPublicProjectDto(organizationDto), false, dtoPopulators);
}
public ComponentDto insertPrivateProject(OrganizationDto organizationDto) {
return insertComponentImpl(newPrivateProjectDto(organizationDto), true, noExtraConfiguration());
}
public ComponentDto insertPublicProject(OrganizationDto organizationDto) {
return insertComponentImpl(newPublicProjectDto(organizationDto), false, noExtraConfiguration());
}
public ComponentDto insertPrivateProject(OrganizationDto organizationDto, String uuid) {
return insertComponentImpl(newPrivateProjectDto(organizationDto, uuid), true, noExtraConfiguration());
}
public ComponentDto insertPublicProject(OrganizationDto organizationDto, String uuid) {
return insertComponentImpl(newPublicProjectDto(organizationDto, uuid), false, noExtraConfiguration());
}
@SafeVarargs
public final ComponentDto insertPrivateProject(OrganizationDto organizationDto, String uuid, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(newPrivateProjectDto(organizationDto, uuid), true, dtoPopulators);
}
/**
* @deprecated since 6.6
* @see #insertPublicPortfolio(OrganizationDto, Consumer[])
*/
@Deprecated
public ComponentDto insertView() {
return insertComponentImpl(newView(db.getDefaultOrganization()), false, noExtraConfiguration());
}
/**
* @deprecated since 6.6
* @see #insertPublicPortfolio(OrganizationDto, Consumer[])
*/
public ComponentDto insertView(Consumer<ComponentDto> dtoPopulator) {
return insertComponentImpl(newView(db.getDefaultOrganization()), false, dtoPopulator);
}
/**
* @deprecated since 6.6
* @see #insertPublicPortfolio(OrganizationDto, Consumer[])
*/
public ComponentDto insertView(OrganizationDto organizationDto) {
return insertComponentImpl(newView(organizationDto), false, noExtraConfiguration());
}
/**
* @deprecated since 6.6
* @see #insertPublicPortfolio(OrganizationDto, Consumer[])
*/
public ComponentDto insertView(OrganizationDto organizationDto, Consumer<ComponentDto> dtoPopulator) {
return insertComponentImpl(newView(organizationDto), false, dtoPopulator);
}
/**
* @deprecated since 6.6
* @see #insertPublicPortfolio(OrganizationDto, Consumer[])
*/
public ComponentDto insertView(String uuid) {
return insertComponentImpl(newView(db.getDefaultOrganization(), uuid), false, noExtraConfiguration());
}
/**
* @deprecated since 6.6
* @see #insertPublicPortfolio(OrganizationDto, Consumer[])
*/
public ComponentDto insertView(OrganizationDto organizationDto, String uuid) {
return insertComponentImpl(newView(organizationDto, uuid), false, noExtraConfiguration());
}
@SafeVarargs
public final ComponentDto insertPublicPortfolio(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(newView(organization).setPrivate(false), false, dtoPopulators);
}
@SafeVarargs
public final ComponentDto insertPrivatePortfolio(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(newView(organization).setPrivate(true), true, dtoPopulators);
}
@SafeVarargs
public final ComponentDto insertPublicApplication(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(newApplication(organization).setPrivate(false), false, dtoPopulators);
}
@SafeVarargs
public final ComponentDto insertPrivateApplication(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(newApplication(organization).setPrivate(true), true, dtoPopulators);
}
/**
* @deprecated since 6.6
* @see #insertPublicApplication(OrganizationDto, Consumer[])
*/
@SafeVarargs
public final ComponentDto insertApplication(OrganizationDto organizationDto, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(newApplication(organizationDto), false, dtoPopulators);
}
@SafeVarargs
public final ComponentDto insertSubView(ComponentDto view, Consumer<ComponentDto>... dtoPopulators) {
return insertComponentImpl(newSubView(view), view.isPrivate(), dtoPopulators);
}
private static <T> Consumer<T> noExtraConfiguration() {
return (t) -> {
};
}
@SafeVarargs
private final ComponentDto insertComponentImpl(ComponentDto component, @Nullable Boolean isPrivate, Consumer<ComponentDto>... dtoPopulators) {
Arrays.stream(dtoPopulators)
.forEach(dtoPopulator -> dtoPopulator.accept(component));
checkState(isPrivate == null || component.isPrivate() == isPrivate, "Illegal modification of private flag");
dbClient.componentDao().insert(dbSession, component);
db.commit();
return component;
}
public void insertComponents(ComponentDto... components) {
dbClient.componentDao().insert(dbSession, asList(components));
db.commit();
}
public SnapshotDto insertSnapshot(SnapshotDto snapshotDto) {
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, snapshotDto);
db.commit();
return snapshot;
}
public SnapshotDto insertSnapshot(ComponentDto componentDto) {
return insertSnapshot(componentDto, noExtraConfiguration());
}
public SnapshotDto insertSnapshot(ComponentDto componentDto, Consumer<SnapshotDto> consumer) {
SnapshotDto snapshotDto = SnapshotTesting.newAnalysis(componentDto);
consumer.accept(snapshotDto);
SnapshotDto snapshot = dbClient.snapshotDao().insert(dbSession, snapshotDto);
db.commit();
return snapshot;
}
public void insertSnapshots(SnapshotDto... snapshotDtos) {
dbClient.snapshotDao().insert(dbSession, asList(snapshotDtos));
db.commit();
}
@SafeVarargs
public final ComponentDto insertMainBranch(Consumer<ComponentDto>... dtoPopulators) {
return insertMainBranch(db.getDefaultOrganization(), dtoPopulators);
}
@SafeVarargs
public final ComponentDto insertMainBranch(OrganizationDto organization, Consumer<ComponentDto>... dtoPopulators) {
ComponentDto project = newPrivateProjectDto(organization);
BranchDto branchDto = newBranchDto(project, LONG);
Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(project));
insertComponent(project);
dbClient.branchDao().insert(dbSession, branchDto);
db.commit();
return project;
}
@SafeVarargs
public final ComponentDto insertMainBranch(OrganizationDto organization, String mainBranchName, Consumer<ComponentDto>... dtoPopulators) {
ComponentDto project = newPrivateProjectDto(organization);
BranchDto branchDto = newBranchDto(project, LONG).setKey(mainBranchName);
Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(project));
insertComponent(project);
dbClient.branchDao().insert(dbSession, branchDto);
db.commit();
return project;
}
@SafeVarargs
public final ComponentDto insertProjectBranch(ComponentDto project, Consumer<BranchDto>... dtoPopulators) {
// MainBranchProjectUuid will be null if it's a main branch
BranchDto branchDto = newBranchDto(firstNonNull(project.getMainBranchProjectUuid(), project.projectUuid()), LONG);
Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(branchDto));
ComponentDto branch = newProjectBranch(project, branchDto);
insertComponent(branch);
dbClient.branchDao().insert(dbSession, branchDto);
db.commit();
return branch;
}
public final ComponentDto insertProjectBranch(ComponentDto project, BranchDto branchDto) {
// MainBranchProjectUuid will be null if it's a main branch
checkArgument(branchDto.getProjectUuid().equals(firstNonNull(project.getMainBranchProjectUuid(), project.projectUuid())));
ComponentDto branch = newProjectBranch(project, branchDto);
insertComponent(branch);
dbClient.branchDao().insert(dbSession, branchDto);
db.commit();
return branch;
}
private static <T> T firstNonNull(@Nullable T first, T second) {
return (first != null) ? first : second;
}
}