| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -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; |
| 18 | import static com.google.common.truth.Truth.assertWithMessage; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 19 | import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND; |
| 20 | import static javax.servlet.http.HttpServletResponse.SC_OK; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 21 | |
| Terry Parker | 53c93f8 | 2016-03-22 16:21:49 -0700 | [diff] [blame] | 22 | import com.google.common.base.Optional; |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 23 | import com.google.common.base.Strings; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 24 | import com.google.common.net.HttpHeaders; |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 25 | import com.google.gson.FieldNamingPolicy; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 26 | import com.google.gson.Gson; |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 27 | import com.google.gson.GsonBuilder; |
| Dave Borowitz | 8e29035 | 2015-10-26 11:33:30 -0400 | [diff] [blame] | 28 | import com.google.gson.reflect.TypeToken; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 29 | |
| 30 | import org.eclipse.jgit.internal.storage.dfs.DfsRepository; |
| 31 | import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription; |
| 32 | import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; |
| Terry Parker | 2e3afb2 | 2016-03-24 15:23:04 -0700 | [diff] [blame] | 33 | import org.eclipse.jgit.junit.MockSystemReader; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 34 | import org.eclipse.jgit.junit.TestRepository; |
| Terry Parker | 53c93f8 | 2016-03-22 16:21:49 -0700 | [diff] [blame] | 35 | import org.eclipse.jgit.lib.PersonIdent; |
| Terry Parker | 2e3afb2 | 2016-03-24 15:23:04 -0700 | [diff] [blame] | 36 | import org.eclipse.jgit.revwalk.RevWalk; |
| 37 | import org.eclipse.jgit.util.SystemReader; |
| 38 | import org.junit.After; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 39 | import org.junit.Before; |
| 40 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 41 | import java.util.Map; |
| Terry Parker | 53c93f8 | 2016-03-22 16:21:49 -0700 | [diff] [blame] | 42 | import java.util.TimeZone; |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 43 | |
| 44 | /** Base class for servlet tests. */ |
| 45 | public class ServletTest { |
| 46 | protected TestRepository<DfsRepository> repo; |
| 47 | protected GitilesServlet servlet; |
| 48 | |
| 49 | @Before |
| 50 | public void setUp() throws Exception { |
| Terry Parker | 2e3afb2 | 2016-03-24 15:23:04 -0700 | [diff] [blame] | 51 | MockSystemReader mockSystemReader = new MockSystemReader(); |
| 52 | SystemReader.setInstance(mockSystemReader); |
| 53 | DfsRepository r = new InMemoryRepository(new DfsRepositoryDescription("repo")); |
| 54 | repo = new TestRepository<>(r, new RevWalk(r), mockSystemReader); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 55 | servlet = TestGitilesServlet.create(repo); |
| 56 | } |
| 57 | |
| Terry Parker | 2e3afb2 | 2016-03-24 15:23:04 -0700 | [diff] [blame] | 58 | @After |
| 59 | public void tearDown() { |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 60 | SystemReader.setInstance(null); |
| Terry Parker | 2e3afb2 | 2016-03-24 15:23:04 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 63 | protected FakeHttpServletResponse buildResponse( |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 64 | String path, String queryString, int expectedStatus) throws Exception { |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 65 | FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); |
| 66 | req.setPathInfo(path); |
| 67 | if (queryString != null) { |
| 68 | req.setQueryString(queryString); |
| 69 | } |
| 70 | FakeHttpServletResponse res = new FakeHttpServletResponse(); |
| 71 | servlet.service(req, res); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 72 | assertThat(res.getStatus()).isEqualTo(expectedStatus); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 73 | return res; |
| 74 | } |
| 75 | |
| 76 | protected FakeHttpServletResponse build(String path) throws Exception { |
| 77 | return buildResponse(path, null, SC_OK); |
| 78 | } |
| 79 | |
| 80 | protected String buildHtml(String path, boolean assertHasETag) throws Exception { |
| 81 | FakeHttpServletResponse res = build(path); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 82 | assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/html"); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 83 | if (assertHasETag) { |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 84 | assertWithMessage("missing ETag").that(res.getHeader(HttpHeaders.ETAG)).isNotNull(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 85 | } |
| 86 | return res.getActualBodyString(); |
| 87 | } |
| 88 | |
| 89 | protected String buildHtml(String path) throws Exception { |
| 90 | return buildHtml(path, true); |
| 91 | } |
| 92 | |
| 93 | protected Map<String, ?> buildData(String path) throws Exception { |
| 94 | // Render the page through Soy to ensure templates are valid, then return |
| 95 | // the Soy data for introspection. |
| 96 | FakeHttpServletRequest req = FakeHttpServletRequest.newRequest(); |
| 97 | req.setPathInfo(path); |
| 98 | FakeHttpServletResponse res = new FakeHttpServletResponse(); |
| 99 | servlet.service(req, res); |
| 100 | return BaseServlet.getData(req); |
| 101 | } |
| 102 | |
| 103 | protected FakeHttpServletResponse buildText(String path) throws Exception { |
| 104 | FakeHttpServletResponse res = buildResponse(path, "format=text", SC_OK); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 105 | assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/plain"); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 106 | return res; |
| 107 | } |
| 108 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 109 | private String buildJsonRaw(String path, String queryString) throws Exception { |
| 110 | String fmt = "format=JSON"; |
| 111 | queryString = Strings.isNullOrEmpty(queryString) ? fmt : fmt + "&" + queryString; |
| 112 | FakeHttpServletResponse res = buildResponse(path, queryString, SC_OK); |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 113 | assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("application/json"); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 114 | String body = res.getActualBodyString(); |
| 115 | String magic = ")]}'\n"; |
| Dave Borowitz | fde41fd | 2015-09-16 15:14:38 -0400 | [diff] [blame] | 116 | assertThat(body).startsWith(magic); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 117 | return body.substring(magic.length()); |
| 118 | } |
| 119 | |
| Dave Borowitz | cf38c03 | 2016-05-02 11:06:23 -0400 | [diff] [blame] | 120 | protected <T> T buildJson(Class<T> classOfT, String path, String queryString) throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 121 | return newGson().fromJson(buildJsonRaw(path, queryString), classOfT); |
| Paweł Hajdan, Jr | f7cd337 | 2015-10-15 12:30:46 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 124 | protected <T> T buildJson(Class<T> classOfT, String path) throws Exception { |
| 125 | return buildJson(classOfT, path, null); |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 128 | protected <T> T buildJson(TypeToken<T> typeOfT, String path, String queryString) |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 129 | throws Exception { |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 130 | return newGson().fromJson(buildJsonRaw(path, queryString), typeOfT.getType()); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| Dave Borowitz | a774f59 | 2015-10-26 11:41:27 -0400 | [diff] [blame] | 133 | protected <T> T buildJson(TypeToken<T> typeOfT, String path) throws Exception { |
| 134 | return buildJson(typeOfT, path, null); |
| Dave Borowitz | 77cf5f2 | 2015-10-26 11:05:07 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | private static Gson newGson() { |
| 138 | return new GsonBuilder() |
| 139 | .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) |
| 140 | .create(); |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| Terry Parker | 53c93f8 | 2016-03-22 16:21:49 -0700 | [diff] [blame] | 143 | protected String currentTimeFormatted() { |
| 144 | PersonIdent p = new PersonIdent(repo.getRepository()); |
| 145 | return new DateFormatter(Optional.<TimeZone>absent(), DateFormatter.Format.ISO).format(p); |
| 146 | } |
| 147 | |
| Nodir Turakulov | 4bc2600 | 2015-08-18 18:24:37 -0700 | [diff] [blame] | 148 | protected void assertNotFound(String path, String queryString) throws Exception { |
| 149 | buildResponse(path, queryString, SC_NOT_FOUND); |
| 150 | } |
| 151 | } |