TestProjectInvalidation: Add generic method to make project.config invalid
For tests that check behaviour for projects with an invalid project.config file (Gerrit throws ConfigInvalidException on load) it is not relevant how the project.config file is made invalid. Hence these tests should not need to specify a specific project.config update that invalidates the project.config file. Add a new method to TestProjectInvalidation that allows them to invalidate the project.config file without specifying an update step. Signed-off-by: Edwin Kempin <ekempin@google.com> Change-Id: I0bd144101addf0c4b009516c40517c5a1f83a8f7
This commit is contained in:
		@@ -280,6 +280,29 @@ public class ProjectOperationsImpl implements ProjectOperations {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    private void invalidateProject(TestProjectInvalidation testProjectInvalidation)
 | 
					    private void invalidateProject(TestProjectInvalidation testProjectInvalidation)
 | 
				
			||||||
        throws Exception {
 | 
					        throws Exception {
 | 
				
			||||||
 | 
					      if (testProjectInvalidation.makeProjectConfigInvalid()) {
 | 
				
			||||||
 | 
					        Config projectConfig = new Config();
 | 
				
			||||||
 | 
					        projectConfig.fromText(getConfig().toText());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Make the project config invalid by adding a permission entry with an invalid permission
 | 
				
			||||||
 | 
					        // name.
 | 
				
			||||||
 | 
					        projectConfig.setString(
 | 
				
			||||||
 | 
					            "access", "refs/*", "Invalid Permission Name", "group Administrators");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        setConfig(projectConfig);
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					          projectCache.evict(nameKey);
 | 
				
			||||||
 | 
					        } catch (Exception e) {
 | 
				
			||||||
 | 
					          // Evicting the project from the cache, also triggers a reindex of the project.
 | 
				
			||||||
 | 
					          // The reindex step fails if the project config is invalid. That's fine, since it was our
 | 
				
			||||||
 | 
					          // intention to make the project config invalid. Hence we ignore exceptions that are cause
 | 
				
			||||||
 | 
					          // by an invalid project config here.
 | 
				
			||||||
 | 
					          if (!Throwables.getCausalChain(e).stream()
 | 
				
			||||||
 | 
					              .anyMatch(ConfigInvalidException.class::isInstance)) {
 | 
				
			||||||
 | 
					            throw e;
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      if (!testProjectInvalidation.projectConfigUpdater().isEmpty()) {
 | 
					      if (!testProjectInvalidation.projectConfigUpdater().isEmpty()) {
 | 
				
			||||||
        Config projectConfig = new Config();
 | 
					        Config projectConfig = new Config();
 | 
				
			||||||
        projectConfig.fromText(getConfig().toText());
 | 
					        projectConfig.fromText(getConfig().toText());
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,16 +28,31 @@ import org.eclipse.jgit.lib.Config;
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
@AutoValue
 | 
					@AutoValue
 | 
				
			||||||
public abstract class TestProjectInvalidation {
 | 
					public abstract class TestProjectInvalidation {
 | 
				
			||||||
 | 
					  public abstract boolean makeProjectConfigInvalid();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public abstract ImmutableList<Consumer<Config>> projectConfigUpdater();
 | 
					  public abstract ImmutableList<Consumer<Config>> projectConfigUpdater();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  abstract ThrowingConsumer<TestProjectInvalidation> projectInvalidator();
 | 
					  abstract ThrowingConsumer<TestProjectInvalidation> projectInvalidator();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public static Builder builder(ThrowingConsumer<TestProjectInvalidation> projectInvalidator) {
 | 
					  public static Builder builder(ThrowingConsumer<TestProjectInvalidation> projectInvalidator) {
 | 
				
			||||||
    return new AutoValue_TestProjectInvalidation.Builder().projectInvalidator(projectInvalidator);
 | 
					    return new AutoValue_TestProjectInvalidation.Builder()
 | 
				
			||||||
 | 
					        .projectInvalidator(projectInvalidator)
 | 
				
			||||||
 | 
					        .makeProjectConfigInvalid(false);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @AutoValue.Builder
 | 
					  @AutoValue.Builder
 | 
				
			||||||
  public abstract static class Builder {
 | 
					  public abstract static class Builder {
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Updates the project.config file so that it becomes invalid and loading it within Gerrit fails
 | 
				
			||||||
 | 
					     * with {@link org.eclipse.jgit.errors.ConfigInvalidException}.
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public Builder makeProjectConfigInvalid() {
 | 
				
			||||||
 | 
					      makeProjectConfigInvalid(true);
 | 
				
			||||||
 | 
					      return this;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    protected abstract Builder makeProjectConfigInvalid(boolean makeProjectConfigInvalid);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					    /**
 | 
				
			||||||
     * Adds a consumer that can update the project config.
 | 
					     * Adds a consumer that can update the project config.
 | 
				
			||||||
     *
 | 
					     *
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -894,14 +894,10 @@ public class ProjectIT extends AbstractDaemonTest {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  @Test
 | 
					  @Test
 | 
				
			||||||
  public void getProjectThatHasInvalidProjectConfig() throws Exception {
 | 
					  public void getProjectThatHasInvalidProjectConfig() throws Exception {
 | 
				
			||||||
    // Make the project config invalid by adding permission entry with an invalid permission name.
 | 
					 | 
				
			||||||
    projectOperations
 | 
					    projectOperations
 | 
				
			||||||
        .project(allProjects)
 | 
					        .project(allProjects)
 | 
				
			||||||
        .forInvalidation()
 | 
					        .forInvalidation()
 | 
				
			||||||
        .addProjectConfigUpdater(
 | 
					        .makeProjectConfigInvalid()
 | 
				
			||||||
            cfg ->
 | 
					 | 
				
			||||||
                cfg.setString(
 | 
					 | 
				
			||||||
                    "access", "refs/*", "Invalid Permission Name", "group Administrators"))
 | 
					 | 
				
			||||||
        .invalidate();
 | 
					        .invalidate();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // We must test this via the REST API since ExceptionHook is not invoked from the Java API.
 | 
					    // We must test this via the REST API since ExceptionHook is not invoked from the Java API.
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user