Add initial framework of Plugin API

Add initial implementation of Plugin API providing the list of plugins,
with integration test stub.

Since we're not loading plugins during acceptance tests, the API will
always return an empty list, and the test only really serves to ensure
that there are no missing dependency injections etc.

After finding a way to make the list return actual data, either by
installing dummy plugins or mocking the PluginLoader, the tests can
be expanded.

Change-Id: I02e3b34a64fa1134dee4b1375fdb9a635651c0ca
This commit is contained in:
David Pursehouse
2017-07-25 11:41:34 +01:00
parent ebbc55a809
commit e253101f5a
11 changed files with 191 additions and 2 deletions

View File

@@ -0,0 +1,7 @@
load("//gerrit-acceptance-tests:tests.bzl", "acceptance_tests")
acceptance_tests(
srcs = glob(["*IT.java"]),
group = "api_plugin",
labels = ["api"],
)

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2017 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.api.plugin;
import static com.google.common.truth.Truth.assertThat;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import org.junit.Test;
@NoHttpd
public class PluginIT extends AbstractDaemonTest {
@Test
public void noPlugins() throws Exception {
assertThat(gApi.plugins().list().get()).isEmpty();
}
}

View File

@@ -18,6 +18,7 @@ import com.google.gerrit.extensions.api.accounts.Accounts;
import com.google.gerrit.extensions.api.changes.Changes;
import com.google.gerrit.extensions.api.config.Config;
import com.google.gerrit.extensions.api.groups.Groups;
import com.google.gerrit.extensions.api.plugins.Plugins;
import com.google.gerrit.extensions.api.projects.Projects;
import com.google.gerrit.extensions.restapi.NotImplementedException;
@@ -32,6 +33,8 @@ public interface GerritApi {
Projects projects();
Plugins plugins();
/**
* A default implementation which allows source compatibility when adding new methods to the
* interface.
@@ -61,5 +64,10 @@ public interface GerritApi {
public Projects projects() {
throw new NotImplementedException();
}
@Override
public Plugins plugins() {
throw new NotImplementedException();
}
}
}

View File

@@ -0,0 +1,51 @@
// Copyright (C) 2017 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.extensions.api.plugins;
import com.google.gerrit.extensions.common.PluginInfo;
import com.google.gerrit.extensions.restapi.RestApiException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
public interface Plugins {
ListRequest list();
abstract class ListRequest {
private boolean all;
public List<PluginInfo> get() throws RestApiException {
Map<String, PluginInfo> map = getAsMap();
List<PluginInfo> result = new ArrayList<>(map.size());
for (Map.Entry<String, PluginInfo> e : map.entrySet()) {
result.add(e.getValue());
}
return result;
}
public abstract SortedMap<String, PluginInfo> getAsMap() throws RestApiException;
public ListRequest all(boolean all) {
this.all = all;
return this;
}
public boolean getAll() {
return all;
}
}
}

View File

@@ -19,6 +19,7 @@ import com.google.gerrit.extensions.api.accounts.Accounts;
import com.google.gerrit.extensions.api.changes.Changes;
import com.google.gerrit.extensions.api.config.Config;
import com.google.gerrit.extensions.api.groups.Groups;
import com.google.gerrit.extensions.api.plugins.Plugins;
import com.google.gerrit.extensions.api.projects.Projects;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@@ -30,15 +31,22 @@ class GerritApiImpl implements GerritApi {
private final Config config;
private final Groups groups;
private final Projects projects;
private final Plugins plugins;
@Inject
GerritApiImpl(
Accounts accounts, Changes changes, Config config, Groups groups, Projects projects) {
Accounts accounts,
Changes changes,
Config config,
Groups groups,
Projects projects,
Plugins plugins) {
this.accounts = accounts;
this.changes = changes;
this.config = config;
this.groups = groups;
this.projects = projects;
this.plugins = plugins;
}
@Override
@@ -65,4 +73,9 @@ class GerritApiImpl implements GerritApi {
public Projects projects() {
return projects;
}
@Override
public Plugins plugins() {
return plugins;
}
}

View File

@@ -26,6 +26,7 @@ public class Module extends AbstractModule {
install(new com.google.gerrit.server.api.changes.Module());
install(new com.google.gerrit.server.api.config.Module());
install(new com.google.gerrit.server.api.groups.Module());
install(new com.google.gerrit.server.api.plugins.Module());
install(new com.google.gerrit.server.api.projects.Module());
}
}

View File

@@ -0,0 +1,25 @@
// Copyright (C) 2017 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.server.api.plugins;
import com.google.gerrit.extensions.api.plugins.Plugins;
import com.google.inject.AbstractModule;
public class Module extends AbstractModule {
@Override
protected void configure() {
bind(Plugins.class).to(PluginsImpl.class);
}
}

View File

@@ -0,0 +1,46 @@
// Copyright (C) 2017 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.server.api.plugins;
import com.google.gerrit.extensions.api.plugins.Plugins;
import com.google.gerrit.extensions.common.PluginInfo;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.server.plugins.ListPlugins;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.util.SortedMap;
@Singleton
public class PluginsImpl implements Plugins {
private final Provider<ListPlugins> listProvider;
@Inject
PluginsImpl(Provider<ListPlugins> listProvider) {
this.listProvider = listProvider;
}
@Override
public ListRequest list() {
return new ListRequest() {
@Override
public SortedMap<String, PluginInfo> getAsMap() throws RestApiException {
ListPlugins list = listProvider.get();
list.setAll(this.getAll());
return list.apply();
}
};
}
}

View File

@@ -75,6 +75,11 @@ public class ListPlugins implements RestReadView<TopLevelResource> {
return display(null);
}
public SortedMap<String, PluginInfo> apply() {
format = OutputFormat.JSON;
return display(null);
}
public SortedMap<String, PluginInfo> display(@Nullable PrintWriter stdout) {
SortedMap<String, PluginInfo> output = new TreeMap<>();
List<Plugin> plugins = Lists.newArrayList(pluginLoader.getPlugins(all));

View File

@@ -18,7 +18,7 @@ import com.google.gerrit.extensions.systemstatus.ServerInformation;
import com.google.inject.Singleton;
@Singleton
class ServerInformationImpl implements ServerInformation {
public class ServerInformationImpl implements ServerInformation {
volatile State state = State.STARTUP;
@Override

View File

@@ -21,6 +21,7 @@ import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.gerrit.extensions.client.AuthType;
import com.google.gerrit.extensions.config.FactoryModule;
import com.google.gerrit.extensions.systemstatus.ServerInformation;
import com.google.gerrit.gpg.GpgModule;
import com.google.gerrit.metrics.DisabledMetricMaker;
import com.google.gerrit.metrics.MetricMaker;
@@ -62,6 +63,7 @@ import com.google.gerrit.server.notedb.GwtormChangeBundleReader;
import com.google.gerrit.server.notedb.MutableNotesMigration;
import com.google.gerrit.server.notedb.NotesMigration;
import com.google.gerrit.server.patch.DiffExecutor;
import com.google.gerrit.server.plugins.ServerInformationImpl;
import com.google.gerrit.server.project.DefaultPermissionBackendModule;
import com.google.gerrit.server.schema.DataSourceType;
import com.google.gerrit.server.schema.H2AccountPatchReviewStore;
@@ -240,6 +242,8 @@ public class InMemoryModule extends FactoryModule {
throw new ProvisionException("index type unsupported in tests: " + indexType);
}
}
bind(ServerInformationImpl.class);
bind(ServerInformation.class).to(ServerInformationImpl.class);
}
@Provides