blob: 5f26f7137bc8beaf3ab524ee6fd91d72736edc2f [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
17import static com.google.gitiles.TestGitilesUrls.URLS;
18import static java.nio.charset.StandardCharsets.UTF_8;
19import static org.junit.Assert.assertEquals;
Shawn Pearce9be60222015-08-31 11:41:19 -070020import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.assertSame;
Shawn Pearcec709c4c2015-08-28 15:30:42 -070022
23import com.google.gson.reflect.TypeToken;
24import com.google.template.soy.data.SoyListData;
25import com.google.template.soy.data.SoyMapData;
26import com.google.template.soy.data.restricted.NullData;
27
28import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
29import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
30import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
31import org.eclipse.jgit.junit.TestRepository;
32import org.junit.Before;
33import org.junit.Test;
Shawn Pearce9be60222015-08-31 11:41:19 -070034import org.junit.runner.RunWith;
35import org.junit.runners.JUnit4;
Shawn Pearcec709c4c2015-08-28 15:30:42 -070036
37import java.util.Map;
38
39/** Tests for {@link HostIndexServlet}. */
Shawn Pearce9be60222015-08-31 11:41:19 -070040@RunWith(JUnit4.class)
Shawn Pearcec709c4c2015-08-28 15:30:42 -070041public class HostIndexServletTest extends ServletTest {
42 private static final String NAME = "foo/bar/repo";
43
44 @Override
45 @Before
46 public void setUp() throws Exception {
47 repo = new TestRepository<DfsRepository>(
48 new InMemoryRepository(new DfsRepositoryDescription(NAME)));
49 servlet = TestGitilesServlet.create(repo);
50 }
51
52 @Test
53 public void rootHtml() throws Exception {
54 Map<String, ?> data = buildData("/");
55 assertEquals(URLS.getHostName(null), data.get("hostName"));
56 assertSame(NullData.INSTANCE, data.get("breadcrumbs"));
57 assertEquals("", data.get("prefix"));
58
59 SoyListData repos = (SoyListData) data.get("repositories");
60 assertEquals(1, repos.length());
61
62 SoyMapData ent = (SoyMapData) repos.get(0);
63 assertEquals(NAME, ent.get("name").toString());
64 assertEquals("/b/" + NAME + "/", ent.get("url").toString());
65 }
66
67 @Test
68 public void fooSubdirHtml() throws Exception {
69 Map<String, ?> data = buildData("/foo/");
70 assertEquals(URLS.getHostName(null) + "/foo", data.get("hostName"));
71 assertEquals("foo/", data.get("prefix"));
72
73 SoyListData breadcrumbs = (SoyListData) data.get("breadcrumbs");
74 assertEquals(2, breadcrumbs.length());
75
76 SoyListData repos = (SoyListData) data.get("repositories");
77 assertEquals(1, repos.length());
78
79 SoyMapData ent = (SoyMapData) repos.get(0);
80 assertEquals("bar/repo", ent.get("name").toString());
81 assertEquals("/b/" + NAME + "/", ent.get("url").toString());
82 }
83
84 @Test
85 public void fooBarSubdirHtml() throws Exception {
86 Map<String, ?> data = buildData("/foo/bar/");
87 assertEquals(URLS.getHostName(null) + "/foo/bar", data.get("hostName"));
88 assertEquals("foo/bar/", data.get("prefix"));
89
90 SoyListData breadcrumbs = (SoyListData) data.get("breadcrumbs");
91 assertEquals(3, breadcrumbs.length());
92
93 SoyListData repos = (SoyListData) data.get("repositories");
94 assertEquals(1, repos.length());
95
96 SoyMapData ent = (SoyMapData) repos.get(0);
97 assertEquals("repo", ent.get("name").toString());
98 assertEquals("/b/" + NAME + "/", ent.get("url").toString());
99 }
100
101 @Test
102 public void rootText() throws Exception {
103 String name = repo.getRepository().getDescription().getRepositoryName();
104 FakeHttpServletResponse res = buildText("/");
105 assertEquals(name + "\n", new String(res.getActualBody(), UTF_8));
106 }
107
108 @Test
109 public void fooSubdirText() throws Exception {
110 FakeHttpServletResponse res = buildText("/foo/");
111 assertEquals("bar/repo\n", new String(res.getActualBody(), UTF_8));
112 }
113
114 @Test
115 public void fooBarSubdirText() throws Exception {
116 FakeHttpServletResponse res = buildText("/foo/bar/");
117 assertEquals("repo\n", new String(res.getActualBody(), UTF_8));
118 }
119
120 @Test
121 public void rootJson() throws Exception {
122 String name = repo.getRepository().getDescription().getRepositoryName();
123 Map<String, RepositoryDescription> res = buildJson(
124 "/",
125 new TypeToken<Map<String, RepositoryDescription>>() {}.getType());
126
127 assertEquals(1, res.size());
128 RepositoryDescription d = res.get(name);
129 assertNotNull(name + " exists", d);
130 assertEquals(name, d.name);
131 }
132
133 @Test
134 public void fooSubdirJson() throws Exception {
135 Map<String, RepositoryDescription> res = buildJson(
136 "/foo/",
137 new TypeToken<Map<String, RepositoryDescription>>() {}.getType());
138
139 assertEquals(1, res.size());
140 RepositoryDescription d = res.get("bar/repo");
141 assertNotNull("bar/repo exists", d);
142 assertEquals(repo.getRepository().getDescription().getRepositoryName(), d.name);
143 }
144
145 @Test
146 public void fooBarSubdirJson() throws Exception {
147 Map<String, RepositoryDescription> res = buildJson(
148 "/foo/bar/",
149 new TypeToken<Map<String, RepositoryDescription>>() {}.getType());
150
151 assertEquals(1, res.size());
152 RepositoryDescription d = res.get("repo");
153 assertNotNull("repo exists", d);
154 assertEquals(repo.getRepository().getDescription().getRepositoryName(), d.name);
155 }
156
157 @Test
158 public void emptySubdirectoryList() throws Exception {
159 assertNotFound("/no.repos/", null);
160 }
161}