blob: fdf2eec1373e1d94e535ebe8c1e8e31cb3b9cd60 [file] [log] [blame]
Nodir Turakulov4bc26002015-08-18 18:24:37 -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
Dave Borowitzfde41fd2015-09-16 15:14:38 -040017import static com.google.common.truth.Truth.assertThat;
18import static com.google.common.truth.Truth.assertWithMessage;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070019import static javax.servlet.http.HttpServletResponse.SC_NOT_FOUND;
20import static javax.servlet.http.HttpServletResponse.SC_OK;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070021
Terry Parker53c93f82016-03-22 16:21:49 -070022import com.google.common.base.Optional;
Dave Borowitza774f592015-10-26 11:41:27 -040023import com.google.common.base.Strings;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070024import com.google.common.net.HttpHeaders;
Dave Borowitz77cf5f22015-10-26 11:05:07 -040025import com.google.gson.FieldNamingPolicy;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070026import com.google.gson.Gson;
Dave Borowitz77cf5f22015-10-26 11:05:07 -040027import com.google.gson.GsonBuilder;
Dave Borowitz8e290352015-10-26 11:33:30 -040028import com.google.gson.reflect.TypeToken;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070029
30import org.eclipse.jgit.internal.storage.dfs.DfsRepository;
31import org.eclipse.jgit.internal.storage.dfs.DfsRepositoryDescription;
32import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
Terry Parker2e3afb22016-03-24 15:23:04 -070033import org.eclipse.jgit.junit.MockSystemReader;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070034import org.eclipse.jgit.junit.TestRepository;
Terry Parker53c93f82016-03-22 16:21:49 -070035import org.eclipse.jgit.lib.PersonIdent;
Terry Parker2e3afb22016-03-24 15:23:04 -070036import org.eclipse.jgit.revwalk.RevWalk;
37import org.eclipse.jgit.util.SystemReader;
38import org.junit.After;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070039import org.junit.Before;
40
Nodir Turakulov4bc26002015-08-18 18:24:37 -070041import java.util.Map;
Terry Parker53c93f82016-03-22 16:21:49 -070042import java.util.TimeZone;
Nodir Turakulov4bc26002015-08-18 18:24:37 -070043
44/** Base class for servlet tests. */
45public class ServletTest {
46 protected TestRepository<DfsRepository> repo;
47 protected GitilesServlet servlet;
48
49 @Before
50 public void setUp() throws Exception {
Terry Parker2e3afb22016-03-24 15:23:04 -070051 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 Turakulov4bc26002015-08-18 18:24:37 -070055 servlet = TestGitilesServlet.create(repo);
56 }
57
Terry Parker2e3afb22016-03-24 15:23:04 -070058 @After
59 public void tearDown() {
Dave Borowitzcf38c032016-05-02 11:06:23 -040060 SystemReader.setInstance(null);
Terry Parker2e3afb22016-03-24 15:23:04 -070061 }
62
Nodir Turakulov4bc26002015-08-18 18:24:37 -070063 protected FakeHttpServletResponse buildResponse(
Dave Borowitzcf38c032016-05-02 11:06:23 -040064 String path, String queryString, int expectedStatus) throws Exception {
Nodir Turakulov4bc26002015-08-18 18:24:37 -070065 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 Borowitzfde41fd2015-09-16 15:14:38 -040072 assertThat(res.getStatus()).isEqualTo(expectedStatus);
Nodir Turakulov4bc26002015-08-18 18:24:37 -070073 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 Borowitzfde41fd2015-09-16 15:14:38 -040082 assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/html");
Nodir Turakulov4bc26002015-08-18 18:24:37 -070083 if (assertHasETag) {
Dave Borowitzfde41fd2015-09-16 15:14:38 -040084 assertWithMessage("missing ETag").that(res.getHeader(HttpHeaders.ETAG)).isNotNull();
Nodir Turakulov4bc26002015-08-18 18:24:37 -070085 }
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 Borowitzfde41fd2015-09-16 15:14:38 -0400105 assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("text/plain");
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700106 return res;
107 }
108
Dave Borowitza774f592015-10-26 11:41:27 -0400109 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 Borowitzfde41fd2015-09-16 15:14:38 -0400113 assertThat(res.getHeader(HttpHeaders.CONTENT_TYPE)).isEqualTo("application/json");
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700114 String body = res.getActualBodyString();
115 String magic = ")]}'\n";
Dave Borowitzfde41fd2015-09-16 15:14:38 -0400116 assertThat(body).startsWith(magic);
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700117 return body.substring(magic.length());
118 }
119
Dave Borowitzcf38c032016-05-02 11:06:23 -0400120 protected <T> T buildJson(Class<T> classOfT, String path, String queryString) throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400121 return newGson().fromJson(buildJsonRaw(path, queryString), classOfT);
Paweł Hajdan, Jrf7cd3372015-10-15 12:30:46 +0200122 }
123
Dave Borowitza774f592015-10-26 11:41:27 -0400124 protected <T> T buildJson(Class<T> classOfT, String path) throws Exception {
125 return buildJson(classOfT, path, null);
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400126 }
127
Dave Borowitza774f592015-10-26 11:41:27 -0400128 protected <T> T buildJson(TypeToken<T> typeOfT, String path, String queryString)
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400129 throws Exception {
Dave Borowitza774f592015-10-26 11:41:27 -0400130 return newGson().fromJson(buildJsonRaw(path, queryString), typeOfT.getType());
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700131 }
132
Dave Borowitza774f592015-10-26 11:41:27 -0400133 protected <T> T buildJson(TypeToken<T> typeOfT, String path) throws Exception {
134 return buildJson(typeOfT, path, null);
Dave Borowitz77cf5f22015-10-26 11:05:07 -0400135 }
136
137 private static Gson newGson() {
138 return new GsonBuilder()
139 .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
140 .create();
Nodir Turakulov4bc26002015-08-18 18:24:37 -0700141 }
142
Terry Parker53c93f82016-03-22 16:21:49 -0700143 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 Turakulov4bc26002015-08-18 18:24:37 -0700148 protected void assertNotFound(String path, String queryString) throws Exception {
149 buildResponse(path, queryString, SC_NOT_FOUND);
150 }
151}