| 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 | |
| 17 | import static com.google.gitiles.TestGitilesUrls.URLS; |
| 18 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 19 | import static org.junit.Assert.assertEquals; |
| Shawn Pearce | 9be6022 | 2015-08-31 11:41:19 -0700 | [diff] [blame] | 20 | import static org.junit.Assert.assertNotNull; |
| 21 | import static org.junit.Assert.assertSame; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 22 | |
| 23 | import com.google.gson.reflect.TypeToken; |
| 24 | import com.google.template.soy.data.SoyListData; |
| 25 | import com.google.template.soy.data.SoyMapData; |
| 26 | import com.google.template.soy.data.restricted.NullData; |
| 27 | |
| 28 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 29 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 30 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| 31 | import org.eclipse.jgit.junit.TestRepository; |
| 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| Shawn Pearce | 9be6022 | 2015-08-31 11:41:19 -0700 | [diff] [blame] | 34 | import org.junit.runner.RunWith; |
| 35 | import org.junit.runners.JUnit4; |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 36 | |
| 37 | import java.util.Map; |
| 38 | |
| 39 | /** Tests for {@link HostIndexServlet}. */ |
| Shawn Pearce | 9be6022 | 2015-08-31 11:41:19 -0700 | [diff] [blame] | 40 | @RunWith(JUnit4.class) |
| Shawn Pearce | c709c4c | 2015-08-28 15:30:42 -0700 | [diff] [blame] | 41 | public 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 | } |