summaryrefslogtreecommitdiffstats
path: root/javatests/com/google/gerrit/acceptance/api/change/ChangeSubmitRequirementIT.java
blob: fcc33c9e828bbe04a1bb5432e6adcb4d3ecab47d (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
// Copyright (C) 2018 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.api.change;

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

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.common.data.SubmitRecord;
import com.google.gerrit.common.data.SubmitRequirement;
import com.google.gerrit.extensions.annotations.Exports;
import com.google.gerrit.extensions.api.changes.ChangeApi;
import com.google.gerrit.extensions.api.changes.ReviewInput;
import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.SubmitRequirementInfo;
import com.google.gerrit.extensions.config.FactoryModule;
import com.google.gerrit.server.query.change.ChangeData;
import com.google.gerrit.server.rules.SubmitRule;
import com.google.inject.Inject;
import com.google.inject.Module;
import com.google.inject.Singleton;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.junit.Test;

public class ChangeSubmitRequirementIT extends AbstractDaemonTest {
  private static final SubmitRequirement req =
      SubmitRequirement.builder()
          .setType("custom_rule")
          .setFallbackText("Fallback text")
          .addCustomValue("key", "value")
          .build();
  private static final SubmitRequirementInfo reqInfo =
      new SubmitRequirementInfo(
          "NOT_READY", "Fallback text", "custom_rule", ImmutableMap.of("key", "value"));

  @Override
  public Module createModule() {
    return new FactoryModule() {
      @Override
      public void configure() {
        bind(SubmitRule.class)
            .annotatedWith(Exports.named("CustomSubmitRule"))
            .to(CustomSubmitRule.class);
      }
    };
  }

  @Inject CustomSubmitRule rule;

  @Test
  public void submitRequirementIsPropagated() throws Exception {
    rule.block(false);
    PushOneCommit.Result r = createChange();

    ChangeInfo result = gApi.changes().id(r.getChangeId()).get();
    assertThat(result.requirements).isEmpty();

    rule.block(true);
    result = gApi.changes().id(r.getChangeId()).get();
    assertThat(result.requirements).containsExactly(reqInfo);
  }

  @Test
  public void submitRequirementIsPropagatedInQuery() throws Exception {
    rule.block(false);
    PushOneCommit.Result r = createChange();

    String query = "status:open project:" + project.get();
    List<ChangeInfo> result = gApi.changes().query(query).get();
    assertThat(result).hasSize(1);
    assertThat(result.get(0).requirements).isEmpty();

    // Submit rule behavior is changed, but the query still returns
    // the previous result from the index
    rule.block(true);
    result = gApi.changes().query(query).get();
    assertThat(result).hasSize(1);
    assertThat(result.get(0).requirements).isEmpty();

    // The submit rule result is updated after the change is reindexed
    gApi.changes().id(r.getChangeId()).index();
    result = gApi.changes().query(query).get();
    assertThat(result).hasSize(1);
    assertThat(result.get(0).requirements).containsExactly(reqInfo);
  }

  @Test
  public void submittableQueryRuleNotReady() throws Exception {
    ChangeApi change = newChangeApi();

    // Satisfy the default rule.
    approveChange(change);

    // The custom rule is NOT_READY.
    rule.block(true);
    change.index();

    assertThat(queryIsSubmittable()).isEmpty();
  }

  @Test
  public void submittableQueryRuleError() throws Exception {
    ChangeApi change = newChangeApi();

    // Satisfy the default rule.
    approveChange(change);

    rule.status(Optional.of(SubmitRecord.Status.RULE_ERROR));
    change.index();

    assertThat(queryIsSubmittable()).isEmpty();
  }

  @Test
  public void submittableQueryDefaultRejected() throws Exception {
    ChangeApi change = newChangeApi();

    // CodeReview:-2 the change, causing the default rule to fail.
    rejectChange(change);

    rule.status(Optional.of(SubmitRecord.Status.OK));
    change.index();

    assertThat(queryIsSubmittable()).isEmpty();
  }

  @Test
  public void submittableQueryRuleOk() throws Exception {
    ChangeApi change = newChangeApi();

    // Satisfy the default rule.
    approveChange(change);

    rule.status(Optional.of(SubmitRecord.Status.OK));
    change.index();

    List<ChangeInfo> result = queryIsSubmittable();
    assertThat(result).hasSize(1);
    assertThat(result.get(0).changeId).isEqualTo(change.info().changeId);
  }

  @Test
  public void submittableQueryRuleNoRecord() throws Exception {
    ChangeApi change = newChangeApi();

    // Satisfy the default rule.
    approveChange(change);

    // Our custom rule isn't providing any submit records.
    rule.status(Optional.empty());
    change.index();

    // is:submittable should return the change, since it was approved and the custom rule is not
    // blocking it.
    List<ChangeInfo> result = queryIsSubmittable();
    assertThat(result).hasSize(1);
    assertThat(result.get(0).changeId).isEqualTo(change.info().changeId);
  }

  private List<ChangeInfo> queryIsSubmittable() throws Exception {
    return gApi.changes().query("is:submittable project:" + project.get()).get();
  }

  private ChangeApi newChangeApi() throws Exception {
    return gApi.changes().id(createChange().getChangeId());
  }

  private void approveChange(ChangeApi changeApi) throws Exception {
    changeApi.current().review(ReviewInput.approve());
  }

  private void rejectChange(ChangeApi changeApi) throws Exception {
    changeApi.current().review(ReviewInput.reject());
  }

  @Singleton
  private static class CustomSubmitRule implements SubmitRule {
    private Optional<SubmitRecord.Status> recordStatus = Optional.empty();

    public void block(boolean block) {
      this.status(block ? Optional.of(SubmitRecord.Status.NOT_READY) : Optional.empty());
    }

    public void status(Optional<SubmitRecord.Status> status) {
      this.recordStatus = status;
    }

    @Override
    public Optional<SubmitRecord> evaluate(ChangeData changeData) {
      if (this.recordStatus.isPresent()) {
        SubmitRecord record = new SubmitRecord();
        record.labels = new ArrayList<>();
        record.status = this.recordStatus.get();
        record.requirements = ImmutableList.of(req);
        return Optional.of(record);
      }
      return Optional.empty();
    }
  }
}