Line data Source code
1 : // Copyright (C) 2021 The Android Open Source Project 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.gerrit.acceptance; 16 : 17 : import static com.google.common.base.Preconditions.checkArgument; 18 : 19 : import com.google.gerrit.common.Nullable; 20 : import com.google.gerrit.entities.Change; 21 : import com.google.gerrit.extensions.annotations.Exports; 22 : import com.google.gerrit.extensions.common.PluginDefinedInfo; 23 : import com.google.gerrit.index.query.Predicate; 24 : import com.google.gerrit.index.query.QueryParseException; 25 : import com.google.gerrit.json.OutputFormat; 26 : import com.google.gerrit.server.DynamicOptions; 27 : import com.google.gerrit.server.change.ChangePluginDefinedInfoFactory; 28 : import com.google.gerrit.server.query.change.ChangeData; 29 : import com.google.gerrit.server.query.change.ChangeQueryBuilder; 30 : import com.google.gerrit.server.restapi.change.QueryChanges; 31 : import com.google.gerrit.sshd.commands.Query; 32 : import com.google.gson.Gson; 33 : import com.google.gson.reflect.TypeToken; 34 : import com.google.inject.AbstractModule; 35 : import com.google.inject.Inject; 36 : import com.google.inject.Provider; 37 : import java.util.Collection; 38 : import java.util.Collections; 39 : import java.util.HashMap; 40 : import java.util.List; 41 : import java.util.Map; 42 : import org.kohsuke.args4j.Option; 43 : 44 2 : public abstract class AbstractPredicateTest extends AbstractDaemonTest { 45 : public static final String PLUGIN_NAME = "my-plugin"; 46 2 : public static final Gson GSON = OutputFormat.JSON.newGson(); 47 : 48 2 : protected static class PluginModule extends AbstractModule { 49 : @Override 50 : public void configure() { 51 2 : bind(DynamicOptions.DynamicBean.class) 52 2 : .annotatedWith(Exports.named(Query.class)) 53 2 : .to(MyQueryOptions.class); 54 2 : bind(DynamicOptions.DynamicBean.class) 55 2 : .annotatedWith(Exports.named(QueryChanges.class)) 56 2 : .to(MyQueryOptions.class); 57 2 : bind(ChangePluginDefinedInfoFactory.class) 58 2 : .annotatedWith(Exports.named("sample")) 59 2 : .to(AttributeFactory.class); 60 2 : } 61 : } 62 : 63 2 : public static class MyQueryOptions implements DynamicOptions.DynamicBean { 64 : @Option(name = "--sample") 65 : public boolean sample; 66 : } 67 : 68 : protected static class AttributeFactory implements ChangePluginDefinedInfoFactory { 69 : private final Provider<ChangeQueryBuilder> queryBuilderProvider; 70 : 71 : @Inject 72 2 : AttributeFactory(Provider<ChangeQueryBuilder> queryBuilderProvider) { 73 2 : this.queryBuilderProvider = queryBuilderProvider; 74 2 : } 75 : 76 : @Override 77 : public Map<Change.Id, PluginDefinedInfo> createPluginDefinedInfos( 78 : Collection<ChangeData> cds, DynamicOptions.BeanProvider beanProvider, String plugin) { 79 2 : MyQueryOptions options = (MyQueryOptions) beanProvider.getDynamicBean(plugin); 80 2 : Map<Change.Id, PluginDefinedInfo> res = new HashMap<>(); 81 2 : if (options.sample) { 82 : try { 83 2 : Predicate<ChangeData> predicate = queryBuilderProvider.get().parse("label:Code-Review+2"); 84 2 : for (ChangeData cd : cds) { 85 2 : PluginDefinedInfo myInfo = new PluginDefinedInfo(); 86 2 : if (predicate.isMatchable() && predicate.asMatchable().match(cd)) { 87 2 : myInfo.message = "matched"; 88 : } else { 89 0 : myInfo.message = "not matched"; 90 : } 91 2 : res.put(cd.getId(), myInfo); 92 2 : } 93 0 : } catch (QueryParseException e) { 94 : // ignored 95 2 : } 96 : } 97 2 : return res; 98 : } 99 : } 100 : 101 : protected static List<PluginDefinedInfo> decodeRawPluginsList(@Nullable Object plugins) { 102 2 : if (plugins == null) { 103 0 : return Collections.emptyList(); 104 : } 105 2 : checkArgument(plugins instanceof List, "not a list: %s", plugins); 106 2 : return GSON.fromJson( 107 2 : GSON.toJson(plugins), new TypeToken<List<PluginDefinedInfo>>() {}.getType()); 108 : } 109 : }