Convert RestSession into the interface in tests.
This allows to provide a different implementations in google internal
tests.
Google-Bug-Id: b/289356590
Release-Notes: skip
Change-Id: I5c30c25f0b5a9a6ca08409861b4079e7c8e97478
diff --git a/java/com/google/gerrit/acceptance/AbstractDaemonTest.java b/java/com/google/gerrit/acceptance/AbstractDaemonTest.java
index faaafe0..eb48d85 100644
--- a/java/com/google/gerrit/acceptance/AbstractDaemonTest.java
+++ b/java/com/google/gerrit/acceptance/AbstractDaemonTest.java
@@ -603,9 +603,9 @@
reindexAccount(admin.id());
reindexAccount(user.id());
- adminRestSession = new RestSession(server, admin);
- userRestSession = new RestSession(server, user);
- anonymousRestSession = new RestSession(server, null);
+ adminRestSession = createRestSession(admin);
+ userRestSession = createRestSession(user);
+ anonymousRestSession = createRestSession(null);
initSsh();
@@ -624,6 +624,10 @@
}
}
+ RestSession createRestSession(@Nullable TestAccount account) {
+ return new GerritServerRestSession(server, account);
+ }
+
protected void initServer(GerritServer.Description classDesc, GerritServer.Description methodDesc)
throws Exception {
Module module = createModule();
diff --git a/java/com/google/gerrit/acceptance/GerritServerRestSession.java b/java/com/google/gerrit/acceptance/GerritServerRestSession.java
new file mode 100644
index 0000000..c2c77fe
--- /dev/null
+++ b/java/com/google/gerrit/acceptance/GerritServerRestSession.java
@@ -0,0 +1,148 @@
+// Copyright (C) 2024 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.google.gerrit.acceptance;
+
+import static com.google.common.net.HttpHeaders.ACCEPT;
+import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
+import static com.google.gerrit.json.OutputFormat.JSON_COMPACT;
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Objects.requireNonNull;
+
+import com.google.gerrit.common.Nullable;
+import com.google.gerrit.extensions.restapi.RawInput;
+import java.io.IOException;
+import org.apache.http.Header;
+import org.apache.http.client.fluent.Request;
+import org.apache.http.entity.BufferedHttpEntity;
+import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.message.BasicHeader;
+
+/** Sends requests to {@link GerritServer} as a specified user. */
+public class GerritServerRestSession extends HttpSession implements RestSession {
+
+ public GerritServerRestSession(GerritServer server, @Nullable TestAccount account) {
+ super(server, account);
+ }
+
+ @Override
+ public RestResponse get(String endPoint) throws IOException {
+ return getWithHeaders(endPoint);
+ }
+
+ @Override
+ public RestResponse getJsonAccept(String endPoint) throws IOException {
+ return getWithHeaders(endPoint, new BasicHeader(ACCEPT, "application/json"));
+ }
+
+ @Override
+ public RestResponse getWithHeaders(String endPoint, Header... headers) throws IOException {
+ Request get = Request.Get(getUrl(endPoint));
+ if (headers != null) {
+ get.setHeaders(headers);
+ }
+ return execute(get);
+ }
+
+ @Override
+ public RestResponse head(String endPoint) throws IOException {
+ return execute(Request.Head(getUrl(endPoint)));
+ }
+
+ @Override
+ public RestResponse put(String endPoint) throws IOException {
+ return put(endPoint, /* content = */ null);
+ }
+
+ @Override
+ public RestResponse put(String endPoint, Object content) throws IOException {
+ return putWithHeaders(endPoint, content);
+ }
+
+ @Override
+ public RestResponse putWithHeaders(String endPoint, Header... headers) throws IOException {
+ return putWithHeaders(endPoint, /* content= */ null, headers);
+ }
+
+ @Override
+ public RestResponse putWithHeaders(String endPoint, Object content, Header... headers)
+ throws IOException {
+ Request put = Request.Put(getUrl(endPoint));
+ if (headers != null) {
+ put.setHeaders(headers);
+ }
+ if (content != null) {
+ addContentToRequest(put, content);
+ }
+ return execute(put);
+ }
+
+ @Override
+ public RestResponse putRaw(String endPoint, RawInput stream) throws IOException {
+ requireNonNull(stream);
+ Request put = Request.Put(getUrl(endPoint));
+ put.addHeader(new BasicHeader(CONTENT_TYPE, stream.getContentType()));
+ put.body(
+ new BufferedHttpEntity(
+ new InputStreamEntity(stream.getInputStream(), stream.getContentLength())));
+ return execute(put);
+ }
+
+ @Override
+ public RestResponse post(String endPoint) throws IOException {
+ return post(endPoint, /* content = */ null);
+ }
+
+ @Override
+ public RestResponse post(String endPoint, Object content) throws IOException {
+ return postWithHeaders(endPoint, content);
+ }
+
+ @Override
+ public RestResponse postWithHeaders(String endPoint, Object content, Header... headers)
+ throws IOException {
+ Request post = Request.Post(getUrl(endPoint));
+ if (headers != null) {
+ post.setHeaders(headers);
+ }
+ if (content != null) {
+ addContentToRequest(post, content);
+ }
+ return execute(post);
+ }
+
+ private static void addContentToRequest(Request request, Object content) {
+ request.addHeader(new BasicHeader(CONTENT_TYPE, "application/json"));
+ request.body(new StringEntity(JSON_COMPACT.newGson().toJson(content), UTF_8));
+ }
+
+ @Override
+ public RestResponse delete(String endPoint) throws IOException {
+ return execute(Request.Delete(getUrl(endPoint)));
+ }
+
+ @Override
+ public RestResponse deleteWithHeaders(String endPoint, Header... headers) throws IOException {
+ Request delete = Request.Delete(getUrl(endPoint));
+ if (headers != null) {
+ delete.setHeaders(headers);
+ }
+ return execute(delete);
+ }
+
+ private String getUrl(String endPoint) {
+ return url + (account != null ? "/a" : "") + endPoint;
+ }
+}
diff --git a/java/com/google/gerrit/acceptance/HttpSession.java b/java/com/google/gerrit/acceptance/HttpSession.java
index 833c53b..ef14a3d 100644
--- a/java/com/google/gerrit/acceptance/HttpSession.java
+++ b/java/com/google/gerrit/acceptance/HttpSession.java
@@ -25,7 +25,7 @@
import org.apache.http.impl.client.HttpClientBuilder;
public class HttpSession {
- protected TestAccount account;
+ protected @Nullable TestAccount account;
protected final String url;
private final Executor executor;
diff --git a/java/com/google/gerrit/acceptance/RestSession.java b/java/com/google/gerrit/acceptance/RestSession.java
index 342cbd0..fd93dbc 100644
--- a/java/com/google/gerrit/acceptance/RestSession.java
+++ b/java/com/google/gerrit/acceptance/RestSession.java
@@ -11,123 +11,48 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
-
package com.google.gerrit.acceptance;
-import static com.google.common.net.HttpHeaders.ACCEPT;
-import static com.google.common.net.HttpHeaders.CONTENT_TYPE;
-import static com.google.gerrit.json.OutputFormat.JSON_COMPACT;
-import static java.nio.charset.StandardCharsets.UTF_8;
-import static java.util.Objects.requireNonNull;
-
-import com.google.gerrit.common.Nullable;
+import com.google.gerrit.common.UsedAt;
import com.google.gerrit.extensions.restapi.RawInput;
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.client.fluent.Request;
-import org.apache.http.entity.BufferedHttpEntity;
-import org.apache.http.entity.InputStreamEntity;
-import org.apache.http.entity.StringEntity;
-import org.apache.http.message.BasicHeader;
-public class RestSession extends HttpSession {
+/** Makes rest requests to gerrit backend. */
+@UsedAt(UsedAt.Project.GOOGLE) // Google has own implementation of this interface in tests.
+public interface RestSession {
+ String url();
- public RestSession(GerritServer server, @Nullable TestAccount account) {
- super(server, account);
- }
+ RestResponse execute(Request request) throws IOException;
- public RestResponse get(String endPoint) throws IOException {
- return getWithHeaders(endPoint);
- }
+ RestResponse get(String endPoint) throws IOException;
- public RestResponse getJsonAccept(String endPoint) throws IOException {
- return getWithHeaders(endPoint, new BasicHeader(ACCEPT, "application/json"));
- }
+ RestResponse getJsonAccept(String endPoint) throws IOException;
- public RestResponse getWithHeaders(String endPoint, Header... headers) throws IOException {
- Request get = Request.Get(getUrl(endPoint));
- if (headers != null) {
- get.setHeaders(headers);
- }
- return execute(get);
- }
+ RestResponse getWithHeaders(String endPoint, Header... headers) throws IOException;
- public RestResponse head(String endPoint) throws IOException {
- return execute(Request.Head(getUrl(endPoint)));
- }
+ RestResponse head(String endPoint) throws IOException;
- public RestResponse put(String endPoint) throws IOException {
- return put(endPoint, /* content = */ null);
- }
+ RestResponse put(String endPoint) throws IOException;
- public RestResponse put(String endPoint, Object content) throws IOException {
- return putWithHeaders(endPoint, content);
- }
+ RestResponse put(String endPoint, Object content) throws IOException;
- public RestResponse putWithHeaders(String endPoint, Header... headers) throws IOException {
- return putWithHeaders(endPoint, /* content= */ null, headers);
- }
+ RestResponse putWithHeaders(String endPoint, Header... headers) throws IOException;
- public RestResponse putWithHeaders(String endPoint, Object content, Header... headers)
- throws IOException {
- Request put = Request.Put(getUrl(endPoint));
- if (headers != null) {
- put.setHeaders(headers);
- }
- if (content != null) {
- addContentToRequest(put, content);
- }
- return execute(put);
- }
+ RestResponse putWithHeaders(String endPoint, Object content, Header... headers)
+ throws IOException;
- public RestResponse putRaw(String endPoint, RawInput stream) throws IOException {
- requireNonNull(stream);
- Request put = Request.Put(getUrl(endPoint));
- put.addHeader(new BasicHeader(CONTENT_TYPE, stream.getContentType()));
- put.body(
- new BufferedHttpEntity(
- new InputStreamEntity(stream.getInputStream(), stream.getContentLength())));
- return execute(put);
- }
+ RestResponse putRaw(String endPoint, RawInput stream) throws IOException;
- public RestResponse post(String endPoint) throws IOException {
- return post(endPoint, /* content = */ null);
- }
+ RestResponse post(String endPoint) throws IOException;
- public RestResponse post(String endPoint, Object content) throws IOException {
- return postWithHeaders(endPoint, content);
- }
+ RestResponse post(String endPoint, Object content) throws IOException;
- public RestResponse postWithHeaders(String endPoint, Object content, Header... headers)
- throws IOException {
- Request post = Request.Post(getUrl(endPoint));
- if (headers != null) {
- post.setHeaders(headers);
- }
- if (content != null) {
- addContentToRequest(post, content);
- }
- return execute(post);
- }
+ RestResponse postWithHeaders(String endPoint, Object content, Header... headers)
+ throws IOException;
- private static void addContentToRequest(Request request, Object content) {
- request.addHeader(new BasicHeader(CONTENT_TYPE, "application/json"));
- request.body(new StringEntity(JSON_COMPACT.newGson().toJson(content), UTF_8));
- }
+ RestResponse delete(String endPoint) throws IOException;
- public RestResponse delete(String endPoint) throws IOException {
- return execute(Request.Delete(getUrl(endPoint)));
- }
-
- public RestResponse deleteWithHeaders(String endPoint, Header... headers) throws IOException {
- Request delete = Request.Delete(getUrl(endPoint));
- if (headers != null) {
- delete.setHeaders(headers);
- }
- return execute(delete);
- }
-
- private String getUrl(String endPoint) {
- return url + (account != null ? "/a" : "") + endPoint;
- }
+ RestResponse deleteWithHeaders(String endPoint, Header... headers) throws IOException;
}
diff --git a/javatests/com/google/gerrit/acceptance/rest/account/ImpersonationIT.java b/javatests/com/google/gerrit/acceptance/rest/account/ImpersonationIT.java
index 4477140..2e706b8 100644
--- a/javatests/com/google/gerrit/acceptance/rest/account/ImpersonationIT.java
+++ b/javatests/com/google/gerrit/acceptance/rest/account/ImpersonationIT.java
@@ -34,7 +34,6 @@
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.RestResponse;
-import com.google.gerrit.acceptance.RestSession;
import com.google.gerrit.acceptance.TestAccount;
import com.google.gerrit.acceptance.UseLocalDisk;
import com.google.gerrit.acceptance.config.GerritConfig;
@@ -98,13 +97,11 @@
@Inject private ProjectOperations projectOperations;
@Inject private RequestScopeOperations requestScopeOperations;
- private RestSession anonRestSession;
private TestAccount admin2;
private GroupInfo newGroup;
@Before
public void setUp() throws Exception {
- anonRestSession = new RestSession(server, null);
admin2 = accountCreator.admin2();
GroupInput gi = new GroupInput();
gi.name = name("New-Group");
@@ -759,7 +756,7 @@
@Test
public void runAsNeverPermittedForAnonymousUsers() throws Exception {
allowRunAs();
- RestResponse res = anonRestSession.getWithHeaders("/changes/", runAsHeader(user.id()));
+ RestResponse res = anonymousRestSession.getWithHeaders("/changes/", runAsHeader(user.id()));
res.assertForbidden();
assertThat(res.getEntityContent()).isEqualTo("not permitted to use X-Gerrit-RunAs");
}
diff --git a/javatests/com/google/gerrit/acceptance/rest/auth/AuthenticationCheckIT.java b/javatests/com/google/gerrit/acceptance/rest/auth/AuthenticationCheckIT.java
index 9f0089e..1951bdd 100644
--- a/javatests/com/google/gerrit/acceptance/rest/auth/AuthenticationCheckIT.java
+++ b/javatests/com/google/gerrit/acceptance/rest/auth/AuthenticationCheckIT.java
@@ -18,7 +18,6 @@
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.RestResponse;
-import com.google.gerrit.acceptance.RestSession;
import java.io.BufferedReader;
import java.util.stream.Collectors;
import org.junit.Test;
@@ -33,8 +32,7 @@
@Test
public void authCheck_anonymousUser_returnsForbidden() throws Exception {
- RestSession anonymous = new RestSession(server, null);
- RestResponse r = anonymous.get("/auth-check");
+ RestResponse r = anonymousRestSession.get("/auth-check");
r.assertForbidden();
}