// Copyright (C) 2015 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.git;

import static com.google.common.truth.Truth.assertThat;

import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.reviewdb.client.Project;

import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.RefSpec;

public abstract class AbstractSubmoduleSubscription extends AbstractDaemonTest {
  protected TestRepository<?> createProjectWithPush(String name)
      throws Exception {
    Project.NameKey project = createProject(name);
    grant(Permission.PUSH, project, "refs/heads/*");
    grant(Permission.SUBMIT, project, "refs/for/refs/heads/*");
    return cloneProject(project);
  }

  protected void createSubscription(
      TestRepository<?> repo, String branch, String subscribeToRepo,
      String subscribeToBranch) throws Exception {
    subscribeToRepo = name(subscribeToRepo);

    // The submodule subscription module checks for gerrit.canonicalWebUrl to
    // detect if it's configured for automatic updates. It doesn't matter if
    // it serves from that URL.
    String url = cfg.getString("gerrit", null, "canonicalWebUrl") + "/"
        + subscribeToRepo;

    Config cfg = new Config();
    cfg.setString("submodule", subscribeToRepo, "path", subscribeToRepo);
    cfg.setString("submodule", subscribeToRepo, "url", url);
    cfg.setString("submodule", subscribeToRepo, "branch", subscribeToBranch);

    repo.branch("HEAD").commit().insertChangeId()
      .message("subject: adding new subscription")
      .add(".gitmodules", cfg.toText().toString())
      .create();

    repo.git().push().setRemote("origin").setRefSpecs(
        new RefSpec("HEAD:refs/heads/" + branch)).call();
  }

  protected void expectToHaveSubmoduleState(TestRepository<?> repo,
      String branch, String submodule, ObjectId expectedId) throws Exception {

    submodule = name(submodule);
    ObjectId commitId = repo.git().fetch().setRemote("origin").call()
        .getAdvertisedRef("refs/heads/" + branch).getObjectId();

    RevWalk rw = repo.getRevWalk();
    RevCommit c = rw.parseCommit(commitId);
    rw.parseBody(c.getTree());

    RevTree tree = c.getTree();
    RevObject actualId = repo.get(tree, submodule);

    assertThat(actualId).isEqualTo(expectedId);
  }
}
@@ -1604,23 +1604,16 @@
 ription(
-%0A      
 TestRepo
@@ -1634,32 +1634,38 @@
 , String branch,
+%0A     
  String subscrib
@@ -1668,30 +1668,24 @@
 cribeToRepo,
-%0A     
  String subs
@@ -1711,24 +1711,314 @@
 ws Exception
+ %7B%0A    Config config = new Config();%0A    prepareSubscriptionConfigEntry(config, subscribeToRepo, subscribeToBranch);%0A    pushSubscriptionConfig(repo, branch, config);%0A  %7D%0A%0A  protected void prepareSubscriptionConfigEntry(Config config,%0A      String subscribeToRepo, String subscribeToBranch)
  %7B%0A    subsc
@@ -2053,17 +2053,16 @@
 oRepo);%0A
-%0A
     // T
@@ -2343,46 +2343,17 @@
 po;%0A
-%0A
     
-Config cfg = new Config();%0A    cf
+confi
 g.se
@@ -2413,33 +2413,36 @@
 beToRepo);%0A    c
-f
+onfi
 g.setString(%22sub
@@ -2485,17 +2485,20 @@
 );%0A    c
-f
+onfi
 g.setStr
@@ -2560,16 +2560,140 @@
 Branch);
+%0A  %7D%0A%0A  protected void pushSubscriptionConfig(TestRepository%3C?%3E repo,%0A      String branch, Config config) throws Exception %7B
 %0A%0A    re
@@ -2814,17 +2814,20 @@
 ules%22, c
-f
+onfi
 g.toText

Split up createSubscription