// 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.testing;

import com.google.gerrit.lifecycle.LifecycleManager;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountManager;
import com.google.gerrit.server.account.AuthRequest;
import com.google.gerrit.server.schema.SchemaCreator;
import com.google.gerrit.server.util.RequestContext;
import com.google.gerrit.server.util.ThreadLocalRequestContext;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
import com.google.inject.util.Providers;
import org.junit.rules.MethodRule;
import org.junit.runners.model.FrameworkMethod;
import org.junit.runners.model.Statement;

public final class SchemaUpgradeTestEnvironment implements MethodRule {
  @Inject private AccountManager accountManager;
  @Inject private IdentifiedUser.GenericFactory userFactory;
  @Inject private SchemaFactory<ReviewDb> schemaFactory;
  @Inject private SchemaCreator schemaCreator;
  @Inject private ThreadLocalRequestContext requestContext;
  // Only for use in setting up/tearing down injector.
  @Inject private InMemoryDatabase inMemoryDatabase;

  private ReviewDb db;
  private LifecycleManager lifecycle;

  @Override
  public Statement apply(Statement base, FrameworkMethod method, Object target) {
    return new Statement() {
      @Override
      public void evaluate() throws Throwable {
        try {
          setUp(target);
          base.evaluate();
        } finally {
          tearDown();
        }
      }
    };
  }

  public void setApiUser(Account.Id id) {
    IdentifiedUser user = userFactory.create(id);
    requestContext.setContext(
        new RequestContext() {
          @Override
          public CurrentUser getUser() {
            return user;
          }

          @Override
          public Provider<ReviewDb> getReviewDbProvider() {
            return Providers.of(db);
          }
        });
  }

  private void setUp(Object target) throws Exception {
    Injector injector = Guice.createInjector(new InMemoryModule());
    injector.injectMembers(this);
    lifecycle = new LifecycleManager();
    lifecycle.add(injector);
    lifecycle.start();

    try (ReviewDb underlyingDb = inMemoryDatabase.getDatabase().open()) {
      schemaCreator.create(underlyingDb);
    }
    db = schemaFactory.open();
    setApiUser(accountManager.authenticate(AuthRequest.forUser("user")).getAccountId());

    // Inject target members after setting API user, so it can @Inject a ReviewDb if it wants.
    injector.injectMembers(target);
  }

  private void tearDown() {
    if (lifecycle != null) {
      lifecycle.stop();
    }
    if (requestContext != null) {
      requestContext.setContext(null);
    }
    if (db != null) {
      db.close();
    }
    InMemoryDatabase.drop(inMemoryDatabase);
  }
}

@@ -1388,16 +1388,52 @@
 viders;%0A
+import org.eclipse.jgit.lib.Config;%0A
 import o
@@ -1459,16 +1459,16 @@
 odRule;%0A
-
 import o
@@ -1622,16 +1622,66 @@
 dRule %7B%0A
+  private final Provider%3CConfig%3E configProvider;%0A%0A
   @Injec
@@ -2117,16 +2117,578 @@
 cycle;%0A%0A
+  /** Create a test environment using an empty base config. */%0A  public SchemaUpgradeTestEnvironment() %7B%0A    this(Config::new);%0A  %7D%0A%0A  /**%0A   * Create a test environment using the specified base config.%0A   *%0A   * %3Cp%3EThe config is passed as a provider so it can be lazily initialized after this rule is%0A   * instantiated, for example using %7B@link ConfigSuite%7D.%0A   *%0A   * @param configProvider possibly-lazy provider for the base config.%0A   */%0A  public SchemaUpgradeTestEnvironment(Provider%3CConfig%3E configProvider) %7B%0A    this.configProvider = configProvider;%0A  %7D%0A%0A
   @Overr
@@ -3458,16 +3458,93 @@
 ption %7B%0A
+    Config cfg = configProvider.get();%0A    InMemoryModule.setDefaults(cfg);%0A%0A
     Inje
@@ -3558,16 +3558,24 @@
 jector =
+%0A       
  Guice.c
@@ -3607,16 +3607,58 @@
 yModule(
+cfg, NoteDbMode.newNotesMigrationFromEnv()
 ));%0A    

Support passing a non-empty config