summaryrefslogtreecommitdiffstats
path: root/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/rest/change/DraftChangeIT.java
blob: da7d7b556f7d985844d9e14b5ab42565dded418b (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
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.acceptance.rest.change;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume;
import static java.util.stream.Collectors.toList;

import com.google.common.collect.Iterables;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.acceptance.RestSession;
import com.google.gerrit.acceptance.TestProjectInput;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.client.ChangeStatus;
import com.google.gerrit.extensions.client.ListChangesOption;
import com.google.gerrit.extensions.client.ReviewerState;
import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.LabelInfo;
import com.google.gerrit.extensions.common.RevisionInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gerrit.server.notedb.PatchSetState;
import com.google.gerrit.server.update.BatchUpdate;
import com.google.gerrit.server.update.BatchUpdateOp;
import com.google.gerrit.server.update.ChangeContext;
import com.google.gerrit.testutil.ConfigSuite;
import com.google.inject.Inject;
import java.util.Collection;
import java.util.EnumSet;
import java.util.List;
import org.eclipse.jgit.lib.Config;
import org.junit.Test;

public class DraftChangeIT extends AbstractDaemonTest {
  @ConfigSuite.Config
  public static Config allowDraftsDisabled() {
    return allowDraftsDisabledConfig();
  }

  @Inject private BatchUpdate.Factory updateFactory;

  @Test
  public void deleteDraftChange() throws Exception {
    assume().that(isAllowDrafts()).isTrue();
    PushOneCommit.Result result = createDraftChange();
    result.assertOkStatus();
    String changeId = result.getChangeId();
    String triplet = project.get() + "~master~" + changeId;
    ChangeInfo c = get(triplet);
    assertThat(c.id).isEqualTo(triplet);
    assertThat(c.status).isEqualTo(ChangeStatus.DRAFT);
    deleteChange(changeId, adminRestSession).assertNoContent();

    exception.expect(ResourceNotFoundException.class);
    get(triplet);
  }

  @Test
  public void deleteDraftChangeOfAnotherUser() throws Exception {
    assume().that(isAllowDrafts()).isTrue();
    PushOneCommit.Result changeResult = createDraftChange();
    changeResult.assertOkStatus();
    String changeId = changeResult.getChangeId();
    Change.Id id = changeResult.getChange().getId();

    // The user needs to be able to see the draft change (which reviewers can).
    gApi.changes().id(changeId).addReviewer(user.fullName);

    setApiUser(user);
    exception.expect(AuthException.class);
    exception.expectMessage(String.format("Deleting change %s is not permitted", id));
    gApi.changes().id(changeId).delete();
  }

  @Test
  @TestProjectInput(cloneAs = "user")
  public void deleteDraftChangeWhenDraftsNotAllowedAsNormalUser() throws Exception {
    assume().that(isAllowDrafts()).isFalse();

    setApiUser(user);
    // We can't create a draft change while the draft workflow is disabled.
    // For this reason, we create a normal change and modify the database.
    PushOneCommit.Result changeResult =
        pushFactory.create(db, user.getIdent(), testRepo).to("refs/for/master");
    Change.Id id = changeResult.getChange().getId();
    markChangeAsDraft(id);
    setDraftStatusOfPatchSetsOfChange(id, true);

    String changeId = changeResult.getChangeId();
    exception.expect(MethodNotAllowedException.class);
    exception.expectMessage("Draft workflow is disabled");
    gApi.changes().id(changeId).delete();
  }

  @Test
  @TestProjectInput(cloneAs = "user")
  public void deleteDraftChangeWhenDraftsNotAllowedAsAdmin() throws Exception {
    assume().that(isAllowDrafts()).isFalse();

    setApiUser(user);
    // We can't create a draft change while the draft workflow is disabled.
    // For this reason, we create a normal change and modify the database.
    PushOneCommit.Result changeResult =
        pushFactory.create(db, user.getIdent(), testRepo).to("refs/for/master");
    Change.Id id = changeResult.getChange().getId();
    markChangeAsDraft(id);
    setDraftStatusOfPatchSetsOfChange(id, true);

    String changeId = changeResult.getChangeId();

    // Grant those permissions to admins.
    grant(Permission.VIEW_DRAFTS, project, "refs/*");
    grant(Permission.DELETE_DRAFTS, project, "refs/*");

    try {
      setApiUser(admin);
      gApi.changes().id(changeId).delete();
    } finally {
      removePermission(Permission.DELETE_DRAFTS, project, "refs/*");
      removePermission(Permission.VIEW_DRAFTS, project, "refs/*");
    }

    setApiUser(user);
    assertThat(query(changeId)).isEmpty();
  }

  @Test
  public void deleteDraftChangeWithNonDraftPatchSet() throws Exception {
    assume().that(isAllowDrafts()).isTrue();

    PushOneCommit.Result changeResult = createDraftChange();
    Change.Id id = changeResult.getChange().getId();
    setDraftStatusOfPatchSetsOfChange(id, false);

    String changeId = changeResult.getChangeId();
    exception.expect(ResourceConflictException.class);
    exception.expectMessage(
        String.format("Cannot delete draft change %s: patch set 1 is not a draft", id));
    gApi.changes().id(changeId).delete();
  }

  @Test
  public void publishDraftChange() throws Exception {
    assume().that(isAllowDrafts()).isTrue();
    PushOneCommit.Result result = createDraftChange();
    result.assertOkStatus();
    String changeId = result.getChangeId();
    String triplet = project.get() + "~master~" + changeId;
    ChangeInfo c = get(triplet);
    assertThat(c.id).isEqualTo(triplet);
    assertThat(c.status).isEqualTo(ChangeStatus.DRAFT);
    assertThat(c.revisions.get(c.currentRevision).draft).isTrue();
    publishChange(changeId).assertNoContent();
    c = get(triplet);
    assertThat(c.status).isEqualTo(ChangeStatus.NEW);
    assertThat(c.revisions.get(c.currentRevision).draft).isNull();
  }

  @Test
  public void publishDraftPatchSet() throws Exception {
    assume().that(isAllowDrafts()).isTrue();
    PushOneCommit.Result result = createDraftChange();
    result.assertOkStatus();
    String changeId = result.getChangeId();
    String triplet = project.get() + "~master~" + changeId;
    ChangeInfo c = get(triplet);
    assertThat(c.id).isEqualTo(triplet);
    assertThat(c.status).isEqualTo(ChangeStatus.DRAFT);
    publishPatchSet(changeId).assertNoContent();
    assertThat(get(triplet).status).isEqualTo(ChangeStatus.NEW);
  }

  @Test
  public void createDraftChangeWhenDraftsNotAllowed() throws Exception {
    assume().that(isAllowDrafts()).isFalse();
    PushOneCommit.Result r = createDraftChange();
    r.assertErrorStatus("draft workflow is disabled");
  }

  @Test
  public void listApprovalsOnDraftChange() throws Exception {
    assume().that(isAllowDrafts()).isTrue();
    PushOneCommit.Result result = createDraftChange();
    result.assertOkStatus();
    String changeId = result.getChangeId();
    String triplet = project.get() + "~master~" + changeId;

    gApi.changes().id(triplet).addReviewer(user.fullName);

    ChangeInfo info = get(triplet);
    LabelInfo label = info.labels.get("Code-Review");
    assertThat(label.all).hasSize(1);
    assertThat(label.all.get(0)._accountId).isEqualTo(user.id.get());
    assertThat(label.all.get(0).value).isEqualTo(0);

    Collection<AccountInfo> ccs = info.reviewers.get(ReviewerState.REVIEWER);
    assertThat(ccs).hasSize(1);
    assertThat(ccs.iterator().next()._accountId).isEqualTo(user.id.get());

    setApiUser(user);
    gApi.changes().id(triplet).current().review(ReviewInput.recommend());
    setApiUser(admin);

    label = get(triplet).labels.get("Code-Review");
    assertThat(label.all).hasSize(1);
    assertThat(label.all.get(0)._accountId).isEqualTo(user.id.get());
    assertThat(label.all.get(0).value).isEqualTo(1);
  }

  private static RestResponse deleteChange(String changeId, RestSession s) throws Exception {
    return s.delete("/changes/" + changeId);
  }

  private RestResponse publishChange(String changeId) throws Exception {
    return adminRestSession.post("/changes/" + changeId + "/publish");
  }

  private RestResponse publishPatchSet(String changeId) throws Exception {
    PatchSet patchSet =
        Iterables.getOnlyElement(queryProvider.get().byKeyPrefix(changeId)).currentPatchSet();
    return adminRestSession.post(
        "/changes/" + changeId + "/revisions/" + patchSet.getRevision().get() + "/publish");
  }

  private void markChangeAsDraft(Change.Id id) throws Exception {
    try (BatchUpdate batchUpdate =
        updateFactory.create(db, project, atrScope.get().getUser(), TimeUtil.nowTs())) {
      batchUpdate.addOp(id, new MarkChangeAsDraftUpdateOp());
      batchUpdate.execute();
    }

    ChangeStatus changeStatus = gApi.changes().id(id.get()).get().status;
    assertThat(changeStatus).isEqualTo(ChangeStatus.DRAFT);
  }

  private void setDraftStatusOfPatchSetsOfChange(Change.Id id, boolean draftStatus)
      throws Exception {
    try (BatchUpdate batchUpdate =
        updateFactory.create(db, project, atrScope.get().getUser(), TimeUtil.nowTs())) {
      batchUpdate.addOp(id, new DraftStatusOfPatchSetsUpdateOp(draftStatus));
      batchUpdate.execute();
    }

    Boolean expectedDraftStatus = draftStatus ? Boolean.TRUE : null;
    List<Boolean> patchSetDraftStatuses = getPatchSetDraftStatuses(id);
    patchSetDraftStatuses.forEach(status -> assertThat(status).isEqualTo(expectedDraftStatus));
  }

  private List<Boolean> getPatchSetDraftStatuses(Change.Id id) throws Exception {
    Collection<RevisionInfo> revisionInfos =
        gApi.changes()
            .id(id.get())
            .get(EnumSet.of(ListChangesOption.ALL_REVISIONS))
            .revisions
            .values();
    return revisionInfos.stream().map(revisionInfo -> revisionInfo.draft).collect(toList());
  }

  private static class MarkChangeAsDraftUpdateOp implements BatchUpdateOp {
    @Override
    public boolean updateChange(ChangeContext ctx) throws Exception {
      Change change = ctx.getChange();

      // Change status in database.
      change.setStatus(Change.Status.DRAFT);

      // Change status in NoteDb.
      PatchSet.Id currentPatchSetId = change.currentPatchSetId();
      ctx.getUpdate(currentPatchSetId).setStatus(Change.Status.DRAFT);

      return true;
    }
  }

  private class DraftStatusOfPatchSetsUpdateOp implements BatchUpdateOp {
    private final boolean draftStatus;

    DraftStatusOfPatchSetsUpdateOp(boolean draftStatus) {
      this.draftStatus = draftStatus;
    }

    @Override
    public boolean updateChange(ChangeContext ctx) throws Exception {
      Collection<PatchSet> patchSets = psUtil.byChange(db, ctx.getNotes());

      // Change status in database.
      patchSets.forEach(patchSet -> patchSet.setDraft(draftStatus));
      db.patchSets().update(patchSets);

      // Change status in NoteDb.
      PatchSetState patchSetState = draftStatus ? PatchSetState.DRAFT : PatchSetState.PUBLISHED;
      patchSets
          .stream()
          .map(PatchSet::getId)
          .map(ctx::getUpdate)
          .forEach(changeUpdate -> changeUpdate.setPatchSetState(patchSetState));

      return true;
    }
  }
}