summaryrefslogtreecommitdiffstats
path: root/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/rest/project/GetCommitIT.java
blob: 76d17f146d27c6f2e91cbfeb9f01349c193c8a9e (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
// Copyright (C) 2014 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.project;

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

import com.google.common.collect.Iterables;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.GitUtil;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.extensions.common.CommitInfo;
import com.google.gerrit.server.git.ProjectConfig;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class GetCommitIT extends AbstractDaemonTest {
  private TestRepository<Repository> repo;

  @Before
  public void setUp() throws Exception {
    repo = GitUtil.newTestRepository(repoManager.openRepository(project));
    blockRead("refs/*");
  }

  @After
  public void tearDown() throws Exception {
    if (repo != null) {
      repo.getRepository().close();
    }
  }

  @Test
  public void getNonExistingCommit_NotFound() throws Exception {
    assertNotFound(ObjectId.fromString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef"));
  }

  @Test
  public void getMergedCommit_Found() throws Exception {
    unblockRead();
    RevCommit commit =
        repo.parseBody(repo.branch("master").commit().message("Create\n\nNew commit\n").create());

    CommitInfo info = getCommit(commit);
    assertThat(info.commit).isEqualTo(commit.name());
    assertThat(info.subject).isEqualTo("Create");
    assertThat(info.message).isEqualTo("Create\n\nNew commit\n");
    assertThat(info.author.name).isEqualTo("J. Author");
    assertThat(info.author.email).isEqualTo("jauthor@example.com");
    assertThat(info.committer.name).isEqualTo("J. Committer");
    assertThat(info.committer.email).isEqualTo("jcommitter@example.com");

    CommitInfo parent = Iterables.getOnlyElement(info.parents);
    assertThat(parent.commit).isEqualTo(commit.getParent(0).name());
    assertThat(parent.subject).isEqualTo("Initial empty repository");
    assertThat(parent.message).isNull();
    assertThat(parent.author).isNull();
    assertThat(parent.committer).isNull();
  }

  @Test
  public void getMergedCommit_NotFound() throws Exception {
    RevCommit commit =
        repo.parseBody(repo.branch("master").commit().message("Create\n\nNew commit\n").create());
    assertNotFound(commit);
  }

  @Test
  public void getOpenChange_Found() throws Exception {
    unblockRead();
    PushOneCommit.Result r =
        pushFactory.create(db, admin.getIdent(), testRepo).to("refs/for/master");
    r.assertOkStatus();

    CommitInfo info = getCommit(r.getCommit());
    assertThat(info.commit).isEqualTo(r.getCommit().name());
    assertThat(info.subject).isEqualTo("test commit");
    assertThat(info.message).isEqualTo("test commit\n\nChange-Id: " + r.getChangeId() + "\n");
    assertThat(info.author.name).isEqualTo("Administrator");
    assertThat(info.author.email).isEqualTo("admin@example.com");
    assertThat(info.committer.name).isEqualTo("Administrator");
    assertThat(info.committer.email).isEqualTo("admin@example.com");

    CommitInfo parent = Iterables.getOnlyElement(info.parents);
    assertThat(parent.commit).isEqualTo(r.getCommit().getParent(0).name());
    assertThat(parent.subject).isEqualTo("Initial empty repository");
    assertThat(parent.message).isNull();
    assertThat(parent.author).isNull();
    assertThat(parent.committer).isNull();
  }

  @Test
  public void getOpenChange_NotFound() throws Exception {
    PushOneCommit.Result r =
        pushFactory.create(db, admin.getIdent(), testRepo).to("refs/for/master");
    r.assertOkStatus();
    assertNotFound(r.getCommit());
  }

  private void unblockRead() throws Exception {
    ProjectConfig pc = projectCache.checkedGet(project).getConfig();
    pc.getAccessSection("refs/*").remove(new Permission(Permission.READ));
    saveProjectConfig(project, pc);
  }

  private void assertNotFound(ObjectId id) throws Exception {
    userRestSession.get("/projects/" + project.get() + "/commits/" + id.name()).assertNotFound();
  }

  private CommitInfo getCommit(ObjectId id) throws Exception {
    RestResponse r = userRestSession.get("/projects/" + project.get() + "/commits/" + id.name());
    r.assertOK();
    CommitInfo result = newGson().fromJson(r.getReader(), CommitInfo.class);
    r.consume();
    return result;
  }
}