blob: 893e259970fbf26c6b77c11e0348195844950078 [file] [log] [blame]
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +02001// Copyright 2015 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.gitiles;
16
17import static com.google.common.truth.Truth.assertThat;
18
Dave Borowitz77cf5f22015-10-26 11:05:07 -040019import com.google.gitiles.CommitJsonData.Commit;
20import com.google.gitiles.CommitJsonData.Log;
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020021import com.google.gitiles.DateFormatter.Format;
Dave Borowitz77cf5f22015-10-26 11:05:07 -040022import com.google.gson.reflect.TypeToken;
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020023
24import org.eclipse.jgit.revwalk.RevCommit;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27import org.junit.runners.JUnit4;
28
29import java.util.ArrayList;
30
31/** Tests for {@link LogServlet}. */
32@RunWith(JUnit4.class)
33public class LogServletTest extends ServletTest {
Dave Borowitz77cf5f22015-10-26 11:05:07 -040034 private static final TypeToken<Log> LOG = new TypeToken<Log>() {};
35
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020036 @Test
37 public void basicLog() throws Exception {
38 RevCommit commit = repo.branch("HEAD").commit().create();
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020039
Dave Borowitza774f592015-10-26 11:41:27 -040040 Log response = buildJson(LOG, "/repo/+log");
Dave Borowitz77cf5f22015-10-26 11:05:07 -040041 assertThat(response.log).hasSize(1);
42 verifyJsonCommit(response.log.get(0), commit);
43 assertThat(response.log.get(0).treeDiff).isNull();
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020044 }
45
46 @Test
47 public void treeDiffLog() throws Exception {
48 String contents1 = "foo\n";
49 String contents2 = "foo\ncontents\n";
50 RevCommit c1 = repo.update("master", repo.commit().add("foo", contents1));
51 RevCommit c2 = repo.update("master", repo.commit().parent(c1).add("foo", contents2));
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020052
Dave Borowitza774f592015-10-26 11:41:27 -040053 Log response = buildJson(LOG, "/repo/+log/master", "name-status=1");
Dave Borowitz77cf5f22015-10-26 11:05:07 -040054 assertThat(response.log).hasSize(2);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020055
Dave Borowitz77cf5f22015-10-26 11:05:07 -040056 Commit jc2 = response.log.get(0);
57 verifyJsonCommit(jc2, c2);
58 assertThat(jc2.treeDiff).hasSize(1);
59 assertThat(jc2.treeDiff.get(0).type).isEqualTo("modify");
60 assertThat(jc2.treeDiff.get(0).oldPath).isEqualTo("foo");
61 assertThat(jc2.treeDiff.get(0).newPath).isEqualTo("foo");
62
63 Commit jc1 = response.log.get(1);
64 verifyJsonCommit(jc1, c1);
65 assertThat(jc1.treeDiff).hasSize(1);
66 assertThat(jc1.treeDiff.get(0).type).isEqualTo("add");
67 assertThat(jc1.treeDiff.get(0).oldPath).isEqualTo("/dev/null");
68 assertThat(jc1.treeDiff.get(0).newPath).isEqualTo("foo");
69 }
70
71 private void verifyJsonCommit(Commit jsonCommit, RevCommit commit) throws Exception {
72 repo.getRevWalk().parseBody(commit);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020073 GitilesAccess access = new TestGitilesAccess(repo.getRepository()).forRequest(null);
74 DateFormatter df = new DateFormatter(access, Format.DEFAULT);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +020075 assertThat(jsonCommit.commit).isEqualTo(commit.name());
76 assertThat(jsonCommit.tree).isEqualTo(commit.getTree().name());
77
78 ArrayList<String> expectedParents = new ArrayList<>();
79 for (int i = 0; i < commit.getParentCount(); i++) {
80 expectedParents.add(commit.getParent(i).name());
81 }
82 assertThat(jsonCommit.parents).containsExactlyElementsIn(expectedParents);
83
84 assertThat(jsonCommit.author.name).isEqualTo(commit.getAuthorIdent().getName());
85 assertThat(jsonCommit.author.email).isEqualTo(commit.getAuthorIdent().getEmailAddress());
86 assertThat(jsonCommit.author.time).isEqualTo(df.format(commit.getAuthorIdent()));
87 assertThat(jsonCommit.committer.name).isEqualTo(commit.getCommitterIdent().getName());
88 assertThat(jsonCommit.committer.email).isEqualTo(commit.getCommitterIdent().getEmailAddress());
89 assertThat(jsonCommit.committer.time).isEqualTo(df.format(commit.getCommitterIdent()));
90 assertThat(jsonCommit.message).isEqualTo(commit.getFullMessage());
91 }
92}