Line data Source code
1 : // Copyright (C) 2020 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.testsuite.project; 16 : 17 : import com.google.auto.value.AutoValue; 18 : import com.google.common.collect.ImmutableList; 19 : import com.google.gerrit.acceptance.testsuite.ThrowingConsumer; 20 : import java.util.function.Consumer; 21 : import org.eclipse.jgit.lib.Config; 22 : 23 : /** 24 : * API to invalidate projects in tests. 25 : * 26 : * <p>This allows to test Gerrit behavior when there is invalid project data in NoteDb (e.g. an 27 : * invalid {@code project.config} file). 28 : */ 29 : @AutoValue 30 3 : public abstract class TestProjectInvalidation { 31 : public abstract boolean makeProjectConfigInvalid(); 32 : 33 : public abstract ImmutableList<Consumer<Config>> projectConfigUpdater(); 34 : 35 : abstract ThrowingConsumer<TestProjectInvalidation> projectInvalidator(); 36 : 37 : public static Builder builder(ThrowingConsumer<TestProjectInvalidation> projectInvalidator) { 38 3 : return new AutoValue_TestProjectInvalidation.Builder() 39 3 : .projectInvalidator(projectInvalidator) 40 3 : .makeProjectConfigInvalid(false); 41 : } 42 : 43 : @AutoValue.Builder 44 3 : public abstract static class Builder { 45 : /** 46 : * Updates the project.config file so that it becomes invalid and loading it within Gerrit fails 47 : * with {@link org.eclipse.jgit.errors.ConfigInvalidException}. 48 : */ 49 : public Builder makeProjectConfigInvalid() { 50 2 : makeProjectConfigInvalid(true); 51 2 : return this; 52 : } 53 : 54 : protected abstract Builder makeProjectConfigInvalid(boolean makeProjectConfigInvalid); 55 : 56 : /** 57 : * Adds a consumer that can update the project config. 58 : * 59 : * <p>This allows tests to set arbitrary values in the project config. 60 : */ 61 : public Builder addProjectConfigUpdater(Consumer<Config> projectConfigUpdater) { 62 2 : projectConfigUpdaterBuilder().add(projectConfigUpdater); 63 2 : return this; 64 : } 65 : 66 : protected abstract ImmutableList.Builder<Consumer<Config>> projectConfigUpdaterBuilder(); 67 : 68 : abstract Builder projectInvalidator( 69 : ThrowingConsumer<TestProjectInvalidation> projectInvalidator); 70 : 71 : abstract TestProjectInvalidation autoBuild(); 72 : 73 : /** Executes the project invalidation as specified. */ 74 : public void invalidate() { 75 3 : TestProjectInvalidation projectInvalidation = autoBuild(); 76 3 : projectInvalidation.projectInvalidator().acceptAndThrowSilently(projectInvalidation); 77 3 : } 78 : } 79 : }