summaryrefslogtreecommitdiffstats
path: root/javatests/com/google/gerrit/acceptance/rest/change/ChangeIdIT.java
blob: a2ad7fc2f8c528b12b7191c41808ab621f3abb87 (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
// Copyright (C) 2017 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 com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.extensions.api.changes.ChangeApi;
import org.junit.Test;

public class ChangeIdIT extends AbstractDaemonTest {

  @Test
  public void projectChangeNumberReturnsChange() throws Exception {
    PushOneCommit.Result c = createChange();
    RestResponse res = adminRestSession.get(changeDetail(getProjectChangeNumber(c.getChangeId())));
    res.assertOK();
  }

  @Test
  public void wrongProjectChangeNumberReturnsNotFound() throws Exception {
    PushOneCommit.Result c = createChange();
    RestResponse res1 =
        adminRestSession.get(
            changeDetail("unknown/project~" + getNumericChangeId(c.getChangeId())));
    res1.assertNotFound();

    RestResponse res2 = adminRestSession.get(project.get() + "~" + Integer.MAX_VALUE);
    res2.assertNotFound();

    // Try a non-numeric change number
    RestResponse res3 = adminRestSession.get(project.get() + "~some-id");
    res3.assertNotFound();
  }

  @Test
  public void changeNumberReturnsChange() throws Exception {
    PushOneCommit.Result c = createChange();
    RestResponse res = adminRestSession.get(changeDetail(getNumericChangeId(c.getChangeId())));
    res.assertOK();
  }

  @Test
  public void wrongChangeNumberReturnsNotFound() throws Exception {
    RestResponse res = adminRestSession.get(changeDetail(String.valueOf(Integer.MAX_VALUE)));
    res.assertNotFound();
  }

  @Test
  public void tripletChangeIdReturnsChange() throws Exception {
    PushOneCommit.Result c = createChange();
    RestResponse res = adminRestSession.get(changeDetail(getTriplet(c.getChangeId())));
    res.assertOK();
  }

  @Test
  public void wrongTripletChangeIdReturnsNotFound() throws Exception {
    PushOneCommit.Result c = createChange();
    RestResponse res1 = adminRestSession.get(changeDetail("unknown~master~" + c.getChangeId()));
    res1.assertNotFound();

    RestResponse res2 =
        adminRestSession.get(changeDetail(project.get() + "~unknown~" + c.getChangeId()));
    res2.assertNotFound();

    RestResponse res3 = adminRestSession.get(changeDetail(project.get() + "~master~I1234567890"));
    res3.assertNotFound();
  }

  @Test
  public void changeIdReturnsChange() throws Exception {
    PushOneCommit.Result c = createChange();
    RestResponse res = adminRestSession.get(changeDetail(c.getChangeId()));
    res.assertOK();
  }

  @Test
  public void wrongChangeIdReturnsNotFound() throws Exception {
    RestResponse res = adminRestSession.get(changeDetail("I1234567890"));
    res.assertNotFound();
  }

  private static String changeDetail(String changeId) {
    return "/changes/" + changeId + "/detail";
  }

  /** Convert a changeId (I0...01) to project~changeNumber (project~00001) */
  private String getProjectChangeNumber(String changeId) throws Exception {
    ChangeApi cApi = gApi.changes().id(changeId);
    return cApi.get().project + "~" + cApi.get()._number;
  }

  /** Convert a changeId (I0...01) to a triplet (project~branch~I0...01) */
  private String getTriplet(String changeId) throws Exception {
    ChangeApi cApi = gApi.changes().id(changeId);
    return cApi.get().project + "~" + cApi.get().branch + "~" + changeId;
  }

  /** Convert a changeId (I0...01) to a numeric changeId (00001) */
  private String getNumericChangeId(String changeId) throws Exception {
    return Integer.toString(gApi.changes().id(changeId).get()._number);
  }
}