blob: fffd0039bfce3f64e132b70f865c7827d3deee3d [file] [log] [blame]
// Copyright (C) 2026 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.opensearch;
import static com.google.common.truth.Truth.assertWithMessage;
import com.google.gerrit.index.IndexDefinition;
import com.google.gerrit.server.LibModuleType;
import com.google.gerrit.testing.GerritTestName;
import com.google.gerrit.testing.InMemoryModule;
import com.google.gerrit.testing.IndexConfig;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import java.util.Collection;
import java.util.UUID;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.eclipse.jgit.lib.Config;
public final class OpenSearchTestUtils {
private static final String OPENSEARCH_USERNAME = "admin";
public static void configure(Config config, Container container, String prefix) {
config.setString("index", null, "type", "opensearch");
config.setString("opensearch", null, "server", container.getHttpHost().toURI());
config.setString("opensearch", null, "prefix", prefix);
config.setInt("index", null, "maxLimit", 10000);
if (container.isSecurityEnabled()) {
config.setString("opensearch", null, "username", OPENSEARCH_USERNAME);
config.setString("opensearch", null, "password", container.getPassword());
}
}
public static void createAllIndexes(Injector injector) {
Collection<IndexDefinition<?, ?, ?>> indexDefs =
injector.getInstance(Key.get(new TypeLiteral<Collection<IndexDefinition<?, ?, ?>>>() {}));
for (IndexDefinition<?, ?, ?> indexDef : indexDefs) {
indexDef.getIndexCollection().getSearchIndex().deleteAll();
}
}
public static Config getConfig(OpenSearchVersion version) {
Container container = Container.createAndStart(version);
String indicesPrefix = UUID.randomUUID().toString();
Config cfg = new Config();
configure(cfg, container, indicesPrefix);
return cfg;
}
public static Config createConfig() {
Config cfg = IndexConfig.create();
return cfg;
}
public static void configureOpenSearchModule(Config openSearchConfig) {
openSearchConfig.setString(
"index",
null,
"install" + LibModuleType.INDEX_MODULE_TYPE.getConfigKey(),
"com.google.gerrit.opensearch.OpenSearchIndexModule");
}
public static class ContainerTestModule extends AbstractModule {
private final Container container;
ContainerTestModule(Container container) {
this.container = container;
}
@Override
protected void configure() {
bind(RestClientProvider.class).to(ContainerRestClientProvider.class);
bind(Container.class).toInstance(container);
}
}
public static Injector createInjector(
Config config, GerritTestName testName, Container container) {
Config opensearchConfig = new Config(config);
OpenSearchTestUtils.configureOpenSearchModule(opensearchConfig);
InMemoryModule.setDefaults(opensearchConfig);
String indicesPrefix = testName.getSanitizedMethodName();
OpenSearchTestUtils.configure(opensearchConfig, container, indicesPrefix);
return Guice.createInjector(
new ContainerTestModule(container), new InMemoryModule(opensearchConfig));
}
public static CloseableHttpClient createHttpClient(Container container) {
return HttpClients.custom().build();
}
public static void closeIndex(
CloseableHttpClient client, Container container, GerritTestName testName) throws Exception {
ClassicHttpResponse response =
client.execute(
new HttpPost(
String.format(
"%s/%s*/_close",
container.getHttpHost().toURI(), testName.getSanitizedMethodName())));
int statusCode = response.getCode();
assertWithMessage(
"response status code should be %s, but was %s. Full response was %s",
HttpStatus.SC_OK, statusCode, EntityUtils.toString(response.getEntity()))
.that(statusCode)
.isEqualTo(HttpStatus.SC_OK);
}
private OpenSearchTestUtils() {
// hide default constructor
}
}