aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/googlesource/gerrit/plugins/qtcodereview/QtUnStageIT.java
blob: 633515bc99f635c8d2daefdbb7ca571a7341d7ee (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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (C) 2019 The Qt Company

package com.googlesource.gerrit.plugins.qtcodereview;

import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableList;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.acceptance.TestPlugin;
import com.google.gerrit.acceptance.UseSsh;

import com.google.gerrit.common.data.Permission;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.ChangeMessage;

import org.apache.http.HttpStatus;

import org.eclipse.jgit.revwalk.RevCommit;

import java.util.ArrayList;

import org.junit.Before;
import org.junit.Test;

@TestPlugin(
    name = "gerrit-plugin-qt-workflow",
    sysModule = "com.googlesource.gerrit.plugins.qtcodereview.QtModule",
    sshModule = "com.googlesource.gerrit.plugins.qtcodereview.QtSshModule"
)

@UseSsh
public class QtUnStageIT extends QtCodeReviewIT {

    private final String UNSTAGED_MSG = "Unstaged";

    @Before
    public void SetDefaultPermissions() throws Exception {
        createBranch(new Branch.NameKey(project, "feature"));
        grant(project, "refs/heads/master", Permission.QT_STAGE, false, REGISTERED_USERS);
    }

    @Test
    public void singleChange_UnStage() throws Exception {
        RevCommit initialHead = getRemoteHead();
        PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
        approve(c.getChangeId());
        QtStage(c);

        RevCommit stagingHead = qtUnStageExpectCommit(c, initialHead);
        assertApproval(c.getChangeId(), admin);
    }

    @Test
    public void multiChange_UnStage_First() throws Exception {
        // Push 3 independent commits
        RevCommit initialHead = getRemoteHead();
        PushOneCommit.Result c1 = pushCommit("master", "commitmsg1", "file1", "content1");
        testRepo.reset(initialHead);
        PushOneCommit.Result c2 = pushCommit("master", "commitmsg2", "file2", "content2");
        testRepo.reset(initialHead);
        PushOneCommit.Result c3 = pushCommit("master", "commitmsg3", "file3", "content3");

        approve(c1.getChangeId());
        QtStage(c1);
        approve(c2.getChangeId());
        QtStage(c2);
        approve(c3.getChangeId());
        QtStage(c3);

        RevCommit stagingHead = qtUnStageExpectCherryPick(c1, c3);
        Change change = c2.getChange().change();
        assertThat(change.getStatus()).isEqualTo(Change.Status.STAGED);
        change = c3.getChange().change();
        assertThat(change.getStatus()).isEqualTo(Change.Status.STAGED);
    }

    @Test
    public void multiChange_UnStage_Middle() throws Exception {
        // Push 3 independent commits
        RevCommit initialHead = getRemoteHead();
        PushOneCommit.Result c1 = pushCommit("master", "commitmsg1", "file1", "content1");
        testRepo.reset(initialHead);
        PushOneCommit.Result c2 = pushCommit("master", "commitmsg2", "file2", "content2");
        testRepo.reset(initialHead);
        PushOneCommit.Result c3 = pushCommit("master", "commitmsg3", "file3", "content3");

        approve(c1.getChangeId());
        QtStage(c1);
        approve(c2.getChangeId());
        QtStage(c2);
        approve(c3.getChangeId());
        QtStage(c3);

        RevCommit stagingHead = qtUnStageExpectCherryPick(c2, c3);
        Change change = c1.getChange().change();
        assertThat(change.getStatus()).isEqualTo(Change.Status.STAGED);
        change = c3.getChange().change();
        assertThat(change.getStatus()).isEqualTo(Change.Status.STAGED);
    }

    @Test
    public void multiChange_UnStage_Last() throws Exception {
        // Push 3 independent commits
        RevCommit initialHead = getRemoteHead();
        PushOneCommit.Result c1 = pushCommit("master", "commitmsg1", "file1", "content1");
        testRepo.reset(initialHead);
        PushOneCommit.Result c2 = pushCommit("master", "commitmsg2", "file2", "content2");
        testRepo.reset(initialHead);
        PushOneCommit.Result c3 = pushCommit("master", "commitmsg3", "file3", "content3");

        approve(c1.getChangeId());
        QtStage(c1);
        approve(c2.getChangeId());
        QtStage(c2);
        RevCommit expectedFastForward = getRemoteHead(project, R_STAGING + "master");
        approve(c3.getChangeId());
        QtStage(c3);

        RevCommit stagingHead = qtUnStageExpectCommit(c3, expectedFastForward);
        Change change = c1.getChange().change();
        assertThat(change.getStatus()).isEqualTo(Change.Status.STAGED);
        change = c2.getChange().change();
        assertThat(change.getStatus()).isEqualTo(Change.Status.STAGED);
    }

    @Test
    public void multiChange_UnStage_Merge_When_First() throws Exception {
        RevCommit initialHead = getRemoteHead();

        // make changes on feature branch
        PushOneCommit.Result f1 = pushCommit("feature", "commitmsg1", "file1", "content1");
        PushOneCommit.Result f2 = pushCommit("feature", "commitmsg2", "file2", "content2");
        approve(f1.getChangeId());
        gApi.changes().id(f1.getChangeId()).current().submit();
        approve(f2.getChangeId());
        gApi.changes().id(f2.getChangeId()).current().submit();

        // make a change on master branch
        testRepo.reset(initialHead);
        PushOneCommit.Result c1 = pushCommit("master", "commitmsg3", "file3", "content3");
        approve(c1.getChangeId());
        gApi.changes().id(c1.getChangeId()).current().submit();

        // merge feature branch into master
        PushOneCommit mm = pushFactory.create(db, admin.getIdent(), testRepo);
        mm.setParents(ImmutableList.of(c1.getCommit(), f2.getCommit()));
        PushOneCommit.Result m = mm.to("refs/for/master");
        m.assertOkStatus();
        approve(m.getChangeId());
        QtStage(m);

        // Stage another change
        testRepo.reset(initialHead);
        PushOneCommit.Result c2 = pushCommit("master", "commitmsg3", "file3", "content3");
        approve(c2.getChangeId());
        QtStage(c2);

        RevCommit stagingHead = qtUnStageExpectCherryPick(m, c2);
        String gitLog = getRemoteLog("refs/staging/master").toString();
        assertThat(gitLog).contains(initialHead.getId().name());
        assertThat(gitLog).contains(c1.getCommit().getId().name());
        assertThat(gitLog).contains(stagingHead.getId().name());
        assertThat(gitLog).doesNotContain(m.getCommit().getId().name());
        assertThat(gitLog).doesNotContain(f1.getCommit().getId().name());
        assertThat(gitLog).doesNotContain(f2.getCommit().getId().name());
    }

    @Test
    public void multiChange_UnStage_Before_MergeCommit() throws Exception {
        RevCommit initialHead = getRemoteHead();

        // make changes on feature branch
        PushOneCommit.Result f1 = pushCommit("feature", "commitmsg1", "file1", "content1");
        PushOneCommit.Result f2 = pushCommit("feature", "commitmsg2", "file2", "content2");
        approve(f1.getChangeId());
        gApi.changes().id(f1.getChangeId()).current().submit();
        approve(f2.getChangeId());
        gApi.changes().id(f2.getChangeId()).current().submit();

        // make a change on master branch
        testRepo.reset(initialHead);
        PushOneCommit.Result c1 = pushCommit("master", "commitmsg3", "file3", "content3");
        approve(c1.getChangeId());
        gApi.changes().id(c1.getChangeId()).current().submit();

        // Stage a change
        testRepo.reset(initialHead);
        PushOneCommit.Result c2 = pushCommit("master", "commitmsg4", "file4", "content4");
        approve(c2.getChangeId());
        QtStage(c2);

        // merge feature branch and stage it on top
        PushOneCommit mm = pushFactory.create(db, admin.getIdent(), testRepo);
        mm.setParents(ImmutableList.of(c1.getCommit(), f2.getCommit()));
        PushOneCommit.Result m = mm.to("refs/for/master");
        m.assertOkStatus();
        approve(m.getChangeId());
        QtStage(m);

        RevCommit stagingHead = qtUnStageExpectMerge(c2, m);
        String gitLog = getRemoteLog("refs/staging/master").toString();
        assertThat(gitLog).contains(initialHead.getId().name());
        assertThat(gitLog).contains(c1.getCommit().getId().name());
        assertThat(gitLog).contains(stagingHead.getId().name());
        assertThat(gitLog).contains(f1.getCommit().getId().name());
        assertThat(gitLog).contains(f2.getCommit().getId().name());
        assertThat(gitLog).contains(m.getCommit().getId().name());
        assertThat(gitLog).doesNotContain(c2.getCommit().getId().name());
    }

    @Test
    public void errorUnStage_No_Permission() throws Exception {
        PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
        approve(c.getChangeId());
        QtStage(c);

         deny(project, "refs/heads/master", Permission.QT_STAGE, REGISTERED_USERS);

        RestResponse response = qtUnStageExpectFail(c, HttpStatus.SC_FORBIDDEN);
        assertThat(response.getEntityContent()).contains("not permitted");

        grant(project, "refs/heads/master", Permission.QT_STAGE, false, REGISTERED_USERS);
    }

    @Test
    public void errorUnStage_Wrong_Status() throws Exception {
        PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
        approve(c.getChangeId());

        RestResponse response = qtUnStageExpectFail(c, HttpStatus.SC_CONFLICT);
        assertThat(response.getEntityContent()).isEqualTo("change is NEW");
    }

    @Test
    public void errorUnStage_Invalid_ChangeId() throws Exception {
        PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
        approve(c.getChangeId());
        QtStage(c);

        RestResponse response = call_REST_API_UnStage("thischangeidnotfound", c.getCommit().getName());
        response.assertStatus(HttpStatus.SC_NOT_FOUND);
        assertThat(response.getEntityContent()).contains("Not found: thischangeidnotfound");
    }

    @Test
    public void errorUnStage_Invalid_RevisionId() throws Exception {
        PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
        approve(c.getChangeId());
        QtStage(c);

        RestResponse response = call_REST_API_UnStage(c.getChangeId(), "thisrevisionidnotfound");
        response.assertStatus(HttpStatus.SC_NOT_FOUND);
        assertThat(response.getEntityContent()).contains("Not found: thisrevisionidnotfound");
    }

    @Test
    public void errorUnStage_Revision_Not_Current() throws Exception {

        PushOneCommit.Result c1 = pushCommit("master", "commitmsg1", "file1", "content1");
        PushOneCommit.Result c2 = amendCommit(c1.getChangeId());
        approve(c2.getChangeId());
        QtStage(c2);

        RestResponse response = call_REST_API_UnStage(c1.getChangeId(), c1.getCommit().getName());
        response.assertStatus(HttpStatus.SC_CONFLICT);
        assertThat(response.getEntityContent()).contains("is not current revision");
    }

    private RevCommit qtUnStageExpectCherryPick(PushOneCommit.Result c,
                                                PushOneCommit.Result expectedContent)
                                                throws Exception {
        return qtUnStage(c, null, expectedContent, false);
    }

    private RevCommit qtUnStageExpectCommit(PushOneCommit.Result c,
                                           RevCommit expectedStagingHead)
                                           throws Exception {
        return qtUnStage(c, expectedStagingHead, null, false);
    }

    private RevCommit qtUnStageExpectMerge(PushOneCommit.Result c,
                                           PushOneCommit.Result expectedContent)
                                           throws Exception {
        return qtUnStage(c, null, expectedContent, true);
    }

    private RevCommit qtUnStage(PushOneCommit.Result c,
                                RevCommit expectedStagingHead,
                                PushOneCommit.Result expectedContent,
                                boolean merge)
                                throws Exception {
        String branch = getBranchNameFromRef(c.getChange().change().getDest().get());
        String stagingRef = R_STAGING + branch;
        String branchRef = R_HEADS + branch;
        RevCommit originalCommit = c.getCommit();
        String changeId = c.getChangeId();
        RevCommit initialHead = getRemoteHead(project, branchRef);
        RevCommit oldStagingHead = getRemoteHead(project, stagingRef);

        RestResponse response = call_REST_API_UnStage(changeId, getCurrentPatchId(c));
        response.assertOK();

        RevCommit masterHead = getRemoteHead(project, branchRef);
        assertThat(masterHead.getId()).isEqualTo(initialHead.getId()); // master is not updated

        RevCommit stagingHead = getRemoteHead(project, stagingRef);
        assertThat(stagingHead).isNotEqualTo(oldStagingHead);

        if (merge) {
            assertThat(stagingHead.getParentCount()).isEqualTo(2);
            assertThat(stagingHead.getParent(1)).isEqualTo(expectedContent.getCommit());
            expectedStagingHead = stagingHead;
        } else if (expectedStagingHead == null && expectedContent != null) {
            assertCherryPick(stagingHead, expectedContent.getCommit(), getCurrentPatchSHA(expectedContent));
            expectedStagingHead = stagingHead;
        } else {
            assertThat(stagingHead).isEqualTo(expectedStagingHead); // staging is updated
        }

        if (expectedStagingHead.equals(oldStagingHead)) {
            assertRefUpdatedEvents(stagingRef); // no events
        } else {
            assertRefUpdatedEvents(stagingRef, oldStagingHead, expectedStagingHead);
            resetEvents();
        }

        Change change = c.getChange().change();
        assertThat(change.getStatus()).isEqualTo(Change.Status.NEW);

        ArrayList<ChangeMessage> messages = new ArrayList(c.getChange().messages());
        assertThat(messages.get(messages.size() - 1).getMessage()).isEqualTo(UNSTAGED_MSG);

        return stagingHead;
    }

    private RestResponse qtUnStageExpectFail(PushOneCommit.Result c,
                                             int expectedStatus)
                                             throws Exception {
        String branch = getBranchNameFromRef(c.getChange().change().getDest().get());
        String stagingRef = R_STAGING + branch;
        String branchRef = R_HEADS + branch;

        RevCommit initialHead = getRemoteHead(project, branchRef);
        RevCommit oldStagingHead = getRemoteHead(project, stagingRef);

        RestResponse response = call_REST_API_UnStage(c.getChangeId(), getCurrentPatchId(c));
        response.assertStatus(expectedStatus);

        RevCommit masterHead = getRemoteHead(project, branchRef);
        assertThat(masterHead.getId()).isEqualTo(initialHead.getId()); // master is not updated

        RevCommit stagingHead = getRemoteHead(project, stagingRef);
        if (stagingHead != null) assertThat(stagingHead.getId()).isEqualTo(oldStagingHead.getId()); // staging is not updated

        assertRefUpdatedEvents(branchRef); // no events
        assertRefUpdatedEvents(stagingRef); // no events

        return response;
    }

}