| 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"; |
| 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 Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 53 | assertThat(data).containsEntry("hostName", URLS.getHostName(null)); |
| 54 | assertThat(data).containsEntry("breadcrumbs", NullData.INSTANCE); |
| 55 | assertThat(data).containsEntry("prefix", ""); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 56 | |
| 57 | SoyListData repos = (SoyListData) data.get("repositories"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 58 | assertThat(repos).hasSize(1); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 59 | |
| 60 | SoyMapData ent = (SoyMapData) repos.get(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 61 | assertThat(ent.get("name").toString()).isEqualTo(NAME); |
| 62 | assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void fooSubdirHtml() throws Exception { |
| 67 | Map<String, ?> data = buildData("/foo/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 68 | assertThat(data).containsEntry("hostName", URLS.getHostName(null) + "/foo"); |
| 69 | assertThat(data).containsEntry("prefix", "foo/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 70 | |
| 71 | SoyListData breadcrumbs = (SoyListData) data.get("breadcrumbs"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 72 | assertThat(breadcrumbs.length()).isEqualTo(2); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 73 | |
| 74 | SoyListData repos = (SoyListData) data.get("repositories"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 75 | assertThat(repos.length()).isEqualTo(1); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 76 | |
| 77 | SoyMapData ent = (SoyMapData) repos.get(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 78 | assertThat(ent.get("name").toString()).isEqualTo("bar/repo"); |
| 79 | assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void fooBarSubdirHtml() throws Exception { |
| 84 | Map<String, ?> data = buildData("/foo/bar/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 85 | assertThat(data).containsEntry("hostName", URLS.getHostName(null) + "/foo/bar"); |
| 86 | assertThat(data).containsEntry("prefix", "foo/bar/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 87 | |
| 88 | SoyListData breadcrumbs = (SoyListData) data.get("breadcrumbs"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 89 | assertThat(breadcrumbs.length()).isEqualTo(3); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 90 | |
| 91 | SoyListData repos = (SoyListData) data.get("repositories"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 92 | assertThat(repos.length()).isEqualTo(1); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 93 | |
| 94 | SoyMapData ent = (SoyMapData) repos.get(0); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 95 | assertThat(ent.get("name").toString()).isEqualTo("repo"); |
| 96 | assertThat(ent.get("url").toString()).isEqualTo("/b/" + NAME + "/"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void rootText() throws Exception { |
| 101 | String name = repo.getRepository().getDescription().getRepositoryName(); |
| 102 | FakeHttpServletResponse res = buildText("/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 103 | assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo(name + "\n"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void fooSubdirText() throws Exception { |
| 108 | FakeHttpServletResponse res = buildText("/foo/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 109 | assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo("bar/repo\n"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void fooBarSubdirText() throws Exception { |
| 114 | FakeHttpServletResponse res = buildText("/foo/bar/"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 115 | assertThat(new String(res.getActualBody(), UTF_8)).isEqualTo("repo\n"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 116 | } |
| 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 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 { |
| 133 | Map<String, RepositoryDescription> res = buildJson( |
| 134 | "/foo/", |
| 135 | new TypeToken<Map<String, RepositoryDescription>>() {}.getType()); |
| 136 | |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 137 | assertThat(res).hasSize(1); |
| 138 | assertThat(res).containsKey("bar/repo"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 139 | RepositoryDescription d = res.get("bar/repo"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 140 | assertThat(d.name).isEqualTo(repo.getRepository().getDescription().getRepositoryName()); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 141 | } |
| 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 Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 149 | assertThat(res).hasSize(1); |
| 150 | assertThat(res).containsKey("repo"); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 151 | RepositoryDescription d = res.get("repo"); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame^] | 152 | assertThat(d.name).isEqualTo(repo.getRepository().getDescription().getRepositoryName()); |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Test |
| 156 | public void emptySubdirectoryList() throws Exception { |
| 157 | assertNotFound("/no.repos/", null); |
| 158 | } |
| 159 | } |