| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package com.google.gitiles; |
| 16 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 17 | import static com.google.common.truth.Truth.assertThat; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 18 | import static com.google.gitiles.TestGitilesUrls.URLS; |
| 19 | import static java.nio.charset.StandardCharsets.UTF_8; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 20 | |
| 21 | import com.google.gson.reflect.TypeToken; |
| 22 | import com.google.template.soy.data.SoyListData; |
| 23 | import com.google.template.soy.data.SoyMapData; |
| 24 | import com.google.template.soy.data.restricted.NullData; |
| 25 | |
| 26 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 27 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 28 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| 29 | import org.eclipse.jgit.junit.TestRepository; |
| 30 | import org.junit.Before; |
| 31 | import org.junit.Test; |
| Shawn Pearce | 9be6022 | 2015-08-31 11:41:19 -0700 | [diff] [blame] | 32 | import org.junit.runner.RunWith; |
| 33 | import org.junit.runners.JUnit4; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 34 | |
| 35 | import java.util.Map; |
| 36 | |
| 37 | /** Tests for {@link HostIndexServlet}. */ |
| Shawn Pearce | 9be6022 | 2015-08-31 11:41:19 -0700 | [diff] [blame] | 38 | @RunWith(JUnit4.class) |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 39 | public class HostIndexServletTest extends ServletTest { |
| 40 | private static final String NAME = "foo/bar/repo"; |
| Dave Borowitz | 8e29035 | 2015-10-26 11:33:30 -0400 | [diff] [blame^] | 41 | private static final TypeToken<Map<String, RepositoryDescription>> REPOS = |
| 42 | new TypeToken<Map<String, RepositoryDescription>>() {}; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 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("/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 55 | assertThat(data).containsEntry("hostName", URLS.getHostName(null)); |
| 56 | assertThat(data).containsEntry("breadcrumbs", NullData.INSTANCE); |
| 57 | assertThat(data).containsEntry("prefix", ""); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 58 | |
| 59 | SoyListData repos = (SoyListData) data.get("repositories"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 60 | assertThat(repos).hasSize(1); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 61 | |
| 62 | SoyMapData ent = (SoyMapData) repos.get(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 63 | assertThat(ent.get("name").toString()).isEqualTo(NAME); |
| 64 | assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void fooSubdirHtml() throws Exception { |
| 69 | Map<String, ?> data = buildData("/foo/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 70 | assertThat(data).containsEntry("hostName", URLS.getHostName(null) + "/foo"); |
| 71 | assertThat(data).containsEntry("prefix", "foo/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 72 | |
| 73 | SoyListData breadcrumbs = (SoyListData) data.get("breadcrumbs"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 74 | assertThat(breadcrumbs.length()).isEqualTo(2); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 75 | |
| 76 | SoyListData repos = (SoyListData) data.get("repositories"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 77 | assertThat(repos.length()).isEqualTo(1); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 78 | |
| 79 | SoyMapData ent = (SoyMapData) repos.get(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 80 | assertThat(ent.get("name").toString()).isEqualTo("bar/repo"); |
| 81 | assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void fooBarSubdirHtml() throws Exception { |
| 86 | Map<String, ?> data = buildData("/foo/bar/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 87 | assertThat(data).containsEntry("hostName", URLS.getHostName(null) + "/foo/bar"); |
| 88 | assertThat(data).containsEntry("prefix", "foo/bar/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 89 | |
| 90 | SoyListData breadcrumbs = (SoyListData) data.get("breadcrumbs"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 91 | assertThat(breadcrumbs.length()).isEqualTo(3); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 92 | |
| 93 | SoyListData repos = (SoyListData) data.get("repositories"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 94 | assertThat(repos.length()).isEqualTo(1); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 95 | |
| 96 | SoyMapData ent = (SoyMapData) repos.get(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 97 | assertThat(ent.get("name").toString()).isEqualTo("repo"); |
| 98 | assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void rootText() throws Exception { |
| 103 | String name = repo.getRepository().getDescription().getRepositoryName(); |
| 104 | FakeHttpServletResponse res = buildText("/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 105 | assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo(name + "\n"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void fooSubdirText() throws Exception { |
| 110 | FakeHttpServletResponse res = buildText("/foo/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 111 | assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo("bar/repo\n"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void fooBarSubdirText() throws Exception { |
| 116 | FakeHttpServletResponse res = buildText("/foo/bar/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 117 | assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo("repo\n"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void rootJson() throws Exception { |
| 122 | String name = repo.getRepository().getDescription().getRepositoryName(); |
| Dave Borowitz | 8e29035 | 2015-10-26 11:33:30 -0400 | [diff] [blame^] | 123 | Map<String, RepositoryDescription> res = buildJson("/", REPOS); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 124 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 125 | assertThat(res).hasSize(1); |
| 126 | assertThat(res).containsKey(name); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 127 | RepositoryDescription d = res.get(name); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 128 | assertThat(d.name).isEqualTo(name); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void fooSubdirJson() throws Exception { |
| Dave Borowitz | 8e29035 | 2015-10-26 11:33:30 -0400 | [diff] [blame^] | 133 | Map<String, RepositoryDescription> res = buildJson("/foo/", REPOS); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 134 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 135 | assertThat(res).hasSize(1); |
| 136 | assertThat(res).containsKey("bar/repo"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 137 | RepositoryDescription d = res.get("bar/repo"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 138 | assertThat(d.name).isEqualTo(repo.getRepository().getDescription().getRepositoryName()); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Test |
| 142 | public void fooBarSubdirJson() throws Exception { |
| Dave Borowitz | 8e29035 | 2015-10-26 11:33:30 -0400 | [diff] [blame^] | 143 | Map<String, RepositoryDescription> res = buildJson("/foo/bar/", REPOS); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 144 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 145 | assertThat(res).hasSize(1); |
| 146 | assertThat(res).containsKey("repo"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 147 | RepositoryDescription d = res.get("repo"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 148 | assertThat(d.name).isEqualTo(repo.getRepository().getDescription().getRepositoryName()); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void emptySubdirectoryList() throws Exception { |
| 153 | assertNotFound("/no.repos/", null); |
| 154 | } |
| 155 | } |