aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/googlesource/gerrit/plugins/qtcodereview/QtCommandAdminChangeStatusIT.java
blob: 4a291275decb18ecf718eb6743733970ad9fcb3b (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
// Copyright (C) 2019 The Qt Company

package com.googlesource.gerrit.plugins.qtcodereview;

import static com.google.common.truth.Truth.assertThat;

import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.TestPlugin;
import com.google.gerrit.acceptance.UseSsh;
import com.google.gerrit.reviewdb.client.Change;
import com.google.gerrit.reviewdb.client.ChangeMessage;

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 QtCommandAdminChangeStatusIT extends QtCodeReviewIT {

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

        String changeId = Integer.toString(c.getChange().getId().get());
        qtAdminChangeStatus(c, changeId, "new", "staged", Change.Status.STAGED);
        qtAdminChangeStatus(c, changeId, "staged", "integrating", Change.Status.INTEGRATING);
        qtAdminChangeStatus(c, changeId, "integrating", "merged", Change.Status.MERGED);
        qtAdminChangeStatus(c, changeId, "merged", "abandoned", Change.Status.ABANDONED);
        qtAdminChangeStatus(c, changeId, "abandoned", "deferred", Change.Status.DEFERRED);
        qtAdminChangeStatus(c, changeId, "deferred", "new", Change.Status.NEW);
    }

    @Test
    public void error_ChangeNotFound() throws Exception {
        PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
        String result;
        result = qtAdminChangeStatusExpectFail(c, "9999", "new", "staged", Change.Status.NEW);
        assertThat(result).contains("change not found");
    }

    @Test
    public void error_InvalidChangeId() throws Exception {
        PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
        String result;
        result = qtAdminChangeStatusExpectFail(c, "invalid", "new", "staged", Change.Status.NEW);
        assertThat(result).contains("change-id not numeric");
    }

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

        String result;
        String changeId = Integer.toString(c.getChange().getId().get());
        result = qtAdminChangeStatusExpectFail(c, changeId, "invalid", "staged", Change.Status.NEW);
        assertThat(result).contains("invalid from status");
    }

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

        String result;
        String changeId = Integer.toString(c.getChange().getId().get());
        result = qtAdminChangeStatusExpectFail(c, changeId, "new", "staaaaged", Change.Status.NEW);
        assertThat(result).contains("invalid to status");
    }

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

        String result;
        String changeId = Integer.toString(c.getChange().getId().get());
        result = qtAdminChangeStatusExpectFail(c, changeId, "merged", "staged", Change.Status.NEW);
        assertThat(result).contains("change status was not");
    }
    @Test
    public void error_NoPermission() throws Exception {
        PushOneCommit.Result c = pushCommit("master", "commitmsg1", "file1", "content1");
        String changeId = Integer.toString(c.getChange().getId().get());

        String commandStr;
        commandStr ="gerrit-plugin-qt-workflow change-status";
        commandStr += " --project " + project.get();
        commandStr += " --change-id " + changeId;
        commandStr += " --from new";
        commandStr += " --to merged";
        String resultStr = userSshSession.exec(commandStr);
        assertThat(userSshSession.getError()).contains("not permitted");

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

    private  void qtAdminChangeStatus(PushOneCommit.Result c,
                                       String changeId,
                                       String from,
                                       String to,
                                       Change.Status expectedStatus)
                                       throws Exception {
        String result = qtAdminChangeStatusCommon(c,
                                                  changeId,
                                                  from,
                                                  to,
                                                  expectedStatus,
                                                  false);
        assertThat(result).isNull();
    }

    private String qtAdminChangeStatusExpectFail(PushOneCommit.Result c,
                                                 String changeId,
                                                 String from,
                                                 String to,
                                                 Change.Status expectedStatus)
                                                 throws Exception {
        return qtAdminChangeStatusCommon(c,
                                         changeId,
                                         from,
                                         to,
                                         expectedStatus,
                                         true);
    }


    private String qtAdminChangeStatusCommon(PushOneCommit.Result c,
                                             String changeId,
                                             String from,
                                             String to,
                                             Change.Status expectedStatus,
                                             Boolean expectFail)
                                             throws Exception {
        String commandStr;
        commandStr ="gerrit-plugin-qt-workflow change-status";
        commandStr += " --project " + project.get();
        commandStr += " --change-id " + changeId;
        commandStr += " --from " + from;
        commandStr += " --to " + to;
        String resultStr = adminSshSession.exec(commandStr);
        if (expectFail) assertThat(adminSshSession.getError()).isNotNull();
        else assertThat(adminSshSession.getError()).isNull();

        Change change = c.getChange().change();
        assertThat(change.getStatus()).isEqualTo(expectedStatus);
        return adminSshSession.getError();
    }
}