blob: 4bc26a2838c521367dd89909898007eacdef6c3b [file] [log] [blame]
Shawn Pearcec709c4c2015-08-28 15:30:42 -07001// Copyright (C) 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
Dave Borowitzfde41fd2015-09-16 15:14:38 -040017import static com.google.common.truth.Truth.assertThat;
Shawn Pearcec709c4c2015-08-28 15:30:42 -070018import static com.google.gitiles.TestGitilesUrls.URLS;
19import static java.nio.charset.StandardCharsets.UTF_8;
Shawn Pearcec709c4c2015-08-28 15:30:42 -070020
21import com.google.gson.reflect.TypeToken;
22import com.google.template.soy.data.SoyListData;
23import com.google.template.soy.data.SoyMapData;
24import com.google.template.soy.data.restricted.NullData;
25
26import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
27import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
28import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
29import org.eclipse.jgit.junit.TestRepository;
30import org.junit.Before;
31import org.junit.Test;
Shawn Pearce9be60222015-08-31 11:41:19 -070032import org.junit.runner.RunWith;
33import org.junit.runners.JUnit4;
Shawn Pearcec709c4c2015-08-28 15:30:42 -070034
35import java.util.Map;
36
37/** Tests for {@link HostIndexServlet}. */
Shawn Pearce9be60222015-08-31 11:41:19 -070038@RunWith(JUnit4.class)
Shawn Pearcec709c4c2015-08-28 15:30:42 -070039public class HostIndexServletTest extends ServletTest {
40 private static final String NAME = "foo/bar/repo";
41
42 @Override
43 @Before
44 public void setUp() throws Exception {
45 repo = new TestRepository<DfsRepository>(
46 new InMemoryRepository(new DfsRepositoryDescription(NAME)));
47 servlet = TestGitilesServlet.create(repo);
48 }
49
50 @Test
51 public void rootHtml() throws Exception {
52 Map<String, ?> data = buildData("/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040053 assertThat(data).containsEntry("hostName", URLS.getHostName(null));
54 assertThat(data).containsEntry("breadcrumbs", NullData.INSTANCE);
55 assertThat(data).containsEntry("prefix", "");
Shawn Pearcec709c4c2015-08-28 15:30:42 -070056
57 SoyListData repos = (SoyListData) data.get("repositories");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040058 assertThat(repos).hasSize(1);
Shawn Pearcec709c4c2015-08-28 15:30:42 -070059
60 SoyMapData ent = (SoyMapData) repos.get(0);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040061 assertThat(ent.get("name").toString()).isEqualTo(NAME);
62 assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -070063 }
64
65 @Test
66 public void fooSubdirHtml() throws Exception {
67 Map<String, ?> data = buildData("/foo/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040068 assertThat(data).containsEntry("hostName", URLS.getHostName(null) + "/foo");
69 assertThat(data).containsEntry("prefix", "foo/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -070070
71 SoyListData breadcrumbs = (SoyListData) data.get("breadcrumbs");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040072 assertThat(breadcrumbs.length()).isEqualTo(2);
Shawn Pearcec709c4c2015-08-28 15:30:42 -070073
74 SoyListData repos = (SoyListData) data.get("repositories");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040075 assertThat(repos.length()).isEqualTo(1);
Shawn Pearcec709c4c2015-08-28 15:30:42 -070076
77 SoyMapData ent = (SoyMapData) repos.get(0);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040078 assertThat(ent.get("name").toString()).isEqualTo("bar/repo");
79 assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -070080 }
81
82 @Test
83 public void fooBarSubdirHtml() throws Exception {
84 Map<String, ?> data = buildData("/foo/bar/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040085 assertThat(data).containsEntry("hostName", URLS.getHostName(null) + "/foo/bar");
86 assertThat(data).containsEntry("prefix", "foo/bar/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -070087
88 SoyListData breadcrumbs = (SoyListData) data.get("breadcrumbs");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040089 assertThat(breadcrumbs.length()).isEqualTo(3);
Shawn Pearcec709c4c2015-08-28 15:30:42 -070090
91 SoyListData repos = (SoyListData) data.get("repositories");
Dave Borowitzfde41fd2015-09-16 15:14:38 -040092 assertThat(repos.length()).isEqualTo(1);
Shawn Pearcec709c4c2015-08-28 15:30:42 -070093
94 SoyMapData ent = (SoyMapData) repos.get(0);
Dave Borowitzfde41fd2015-09-16 15:14:38 -040095 assertThat(ent.get("name").toString()).isEqualTo("repo");
96 assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/");
Shawn Pearcec709c4c2015-08-28 15:30:42 -070097 }
98
99 @Test
100 public void rootText() throws Exception {
101 String name = repo.getRepository().getDescription().getRepositoryName();
102 FakeHttpServletResponse res = buildText("/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400103 assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo(name + "\n");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700104 }
105
106 @Test
107 public void fooSubdirText() throws Exception {
108 FakeHttpServletResponse res = buildText("/foo/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400109 assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo("bar/repo\n");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700110 }
111
112 @Test
113 public void fooBarSubdirText() throws Exception {
114 FakeHttpServletResponse res = buildText("/foo/bar/");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400115 assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo("repo\n");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700116 }
117
118 @Test
119 public void rootJson() throws Exception {
120 String name = repo.getRepository().getDescription().getRepositoryName();
121 Map<String, RepositoryDescription> res = buildJson(
122 "/",
123 new TypeToken<Map<String, RepositoryDescription>>() {}.getType());
124
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400125 assertThat(res).hasSize(1);
126 assertThat(res).containsKey(name);
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700127 RepositoryDescription d = res.get(name);
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400128 assertThat(d.name).isEqualTo(name);
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700129 }
130
131 @Test
132 public void fooSubdirJson() throws Exception {
133 Map<String, RepositoryDescription> res = buildJson(
134 "/foo/",
135 new TypeToken<Map<String, RepositoryDescription>>() {}.getType());
136
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400137 assertThat(res).hasSize(1);
138 assertThat(res).containsKey("bar/repo");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700139 RepositoryDescription d = res.get("bar/repo");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400140 assertThat(d.name).isEqualTo(repo.getRepository().getDescription().getRepositoryName());
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700141 }
142
143 @Test
144 public void fooBarSubdirJson() throws Exception {
145 Map<String, RepositoryDescription> res = buildJson(
146 "/foo/bar/",
147 new TypeToken<Map<String, RepositoryDescription>>() {}.getType());
148
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400149 assertThat(res).hasSize(1);
150 assertThat(res).containsKey("repo");
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700151 RepositoryDescription d = res.get("repo");
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400152 assertThat(d.name).isEqualTo(repo.getRepository().getDescription().getRepositoryName());
Shawn Pearcec709c4c2015-08-28 15:30:42 -0700153 }
154
155 @Test
156 public void emptySubdirectoryList() throws Exception {
157 assertNotFound("/no.repos/", null);
158 }
159}