Fix tests to avoid implicit merges.

Fixed test created changes which implicitly merge content of some
other branch to the target branch of the change.

In addition to implicit merge, the RefAdvertisementIT tests created a
different commit graph than specified in comments in the file - it is
also fixed by this change.

Bug: Google b/271135486
Release-Notes: skip
Change-Id: I6833c1d179b2757781e41042814511ca55fbe973
diff --git a/javatests/com/google/gerrit/acceptance/api/change/SubmitTypeRuleIT.java b/javatests/com/google/gerrit/acceptance/api/change/SubmitTypeRuleIT.java
index 308e4e0..8e11f70 100644
--- a/javatests/com/google/gerrit/acceptance/api/change/SubmitTypeRuleIT.java
+++ b/javatests/com/google/gerrit/acceptance/api/change/SubmitTypeRuleIT.java
@@ -28,6 +28,7 @@
 import com.google.gerrit.acceptance.AbstractDaemonTest;
 import com.google.gerrit.acceptance.NoHttpd;
 import com.google.gerrit.acceptance.PushOneCommit;
+import com.google.gerrit.acceptance.PushOneCommit.Result;
 import com.google.gerrit.entities.Change;
 import com.google.gerrit.entities.RefNames;
 import com.google.gerrit.extensions.api.changes.ReviewInput;
@@ -89,13 +90,19 @@
   }
 
   private AtomicInteger fileCounter;
+
+  // The change is used only to verify that the rule is valid. It is never submitted in the test.
   private Change.Id testChangeId;
 
   @Before
   public void setUp() throws Exception {
     fileCounter = new AtomicInteger();
     gApi.projects().name(project.get()).branch("test").create(new BranchInput());
-    testChangeId = createChange("test", "test change").getChange().getId();
+    Result testChange = createChange("test", "test change");
+    testChangeId = testChange.getChange().getId();
+    // Reset repo back to the original state - otherwise all changes in tests have testChange as a
+    // parent.
+    testRepo.reset(testChange.getCommit().getParent(0));
   }
 
   private void setRulesPl(String rule) throws Exception {
diff --git a/javatests/com/google/gerrit/acceptance/api/project/CommitIT.java b/javatests/com/google/gerrit/acceptance/api/project/CommitIT.java
index 84a4a40..c33175d 100644
--- a/javatests/com/google/gerrit/acceptance/api/project/CommitIT.java
+++ b/javatests/com/google/gerrit/acceptance/api/project/CommitIT.java
@@ -121,7 +121,10 @@
 
     createBranch(BranchNameKey.create(project, "test-branch-1"));
     createBranch(BranchNameKey.create(project, "test-branch-2"));
-    createAndSubmitChange("refs/for/test-branch-1");
+    RevCommit changeCommit = createAndSubmitChange("refs/for/test-branch-1").getCommit();
+    // Reset repo back to the original state - otherwise all changes in tests have testChange as a
+    // parent.
+    testRepo.reset(changeCommit.getParent(0));
     createAndSubmitChange("refs/for/test-branch-2");
 
     assertThat(getIncludedIn(baseChange.getCommit().getId()).branches)
diff --git a/javatests/com/google/gerrit/acceptance/git/RefAdvertisementIT.java b/javatests/com/google/gerrit/acceptance/git/RefAdvertisementIT.java
index 3bec694..0dd026f 100644
--- a/javatests/com/google/gerrit/acceptance/git/RefAdvertisementIT.java
+++ b/javatests/com/google/gerrit/acceptance/git/RefAdvertisementIT.java
@@ -163,8 +163,6 @@
   //    (c3_open)                            (c4_open)
   //
   private void setUpChanges() throws Exception {
-    gApi.projects().name(project.get()).branch("branch").create(new BranchInput());
-
     // First 2 changes are merged, which means the tags pointing to them are
     // visible.
     projectOperations
@@ -183,6 +181,9 @@
     metaRef1 = RefNames.changeMetaRef(cd1.getId());
 
     //   rcMaster (c1 master) <-- rcBranch (c2 branch)
+    BranchInput branchInput = new BranchInput();
+    branchInput.revision = mr.getCommit().getName();
+    gApi.projects().name(project.get()).branch("branch").create(branchInput);
     PushOneCommit.Result br =
         pushFactory.create(admin.newIdent(), testRepo).to("refs/for/branch%submit");
     br.assertOkStatus();
@@ -196,7 +197,7 @@
     //      \
     //    (c3_open)
     //
-    mr = pushFactory.create(admin.newIdent(), testRepo).to("refs/for/master");
+    mr = pushFactory.create(admin.newIdent(), testRepo).setParent(rcMaster).to("refs/for/master");
     mr.assertOkStatus();
     cd3 = mr.getChange();
     psRef3 = cd3.currentPatchSet().id().toRefName();
@@ -205,7 +206,7 @@
     //   rcMaster (c1 master) <-- rcBranch (c2 branch)
     //      \                        \
     //     (c3_open)                (c4_open)
-    br = pushFactory.create(admin.newIdent(), testRepo).to("refs/for/branch");
+    br = pushFactory.create(admin.newIdent(), testRepo).setParent(rcBranch).to("refs/for/branch");
     br.assertOkStatus();
     cd4 = br.getChange();
     psRef4 = cd4.currentPatchSet().id().toRefName();
diff --git a/javatests/com/google/gerrit/acceptance/git/SubmoduleSubscriptionsWholeTopicMergeIT.java b/javatests/com/google/gerrit/acceptance/git/SubmoduleSubscriptionsWholeTopicMergeIT.java
index 0d751f1..dd079de 100644
--- a/javatests/com/google/gerrit/acceptance/git/SubmoduleSubscriptionsWholeTopicMergeIT.java
+++ b/javatests/com/google/gerrit/acceptance/git/SubmoduleSubscriptionsWholeTopicMergeIT.java
@@ -645,12 +645,19 @@
     allowMatchingSubmoduleSubscription(subKey, "refs/heads/master", superKey, "refs/heads/master");
     allowMatchingSubmoduleSubscription(superKey, "refs/heads/dev", subKey, "refs/heads/dev");
 
+    // Create 'dev' branches in both repos by pushing changes.
     pushChangeTo(subRepo, "dev");
     pushChangeTo(superRepo, "dev");
 
     createSubmoduleSubscription(superRepo, "master", subKey, "master");
     createSubmoduleSubscription(subRepo, "dev", superKey, "dev");
 
+    // Reset the state of local repositories to avoid implicit merge changes.
+    subRepo.git().fetch();
+    subRepo.reset(subRepo.git().getRepository().findRef("origin/master").getObjectId().getName());
+    superRepo.git().fetch();
+    superRepo.reset(superRepo.git().getRepository().findRef("origin/dev").getObjectId().getName());
+
     ObjectId subMasterHead =
         pushChangeTo(
             subRepo, "refs/for/master", "b.txt", "content b", "some message", "same-topic");
diff --git a/javatests/com/google/gerrit/acceptance/rest/change/SubmitByMergeIfNecessaryIT.java b/javatests/com/google/gerrit/acceptance/rest/change/SubmitByMergeIfNecessaryIT.java
index ac3622f..f8ec7bf 100644
--- a/javatests/com/google/gerrit/acceptance/rest/change/SubmitByMergeIfNecessaryIT.java
+++ b/javatests/com/google/gerrit/acceptance/rest/change/SubmitByMergeIfNecessaryIT.java
@@ -42,6 +42,7 @@
 import com.google.inject.Inject;
 import java.util.List;
 import org.eclipse.jgit.junit.TestRepository;
+import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.transport.RefSpec;
 import org.junit.Test;
@@ -549,19 +550,27 @@
     PushOneCommit.Result changeResult = change.to("refs/for/master");
     approve(changeResult.getChangeId());
 
-    // Create a successor change.
+    // Create a destination branch that later will be made non-visible to user.
+    BranchNameKey secretBranch = BranchNameKey.create(project, "secretBranch");
+    String secretBranchTip =
+        gApi.projects()
+            .name(secretBranch.project().get())
+            .branch(secretBranch.branch())
+            .create(new BranchInput())
+            .get()
+            .revision;
+
+    // Create a successor change which merges visible and non-visible branch. This change
+    // is created as an explicit merge - otherwise Gerrit rejects it on submit as implicit merge.
     PushOneCommit change2 =
         pushFactory.create(admin.newIdent(), testRepo, "feature", "b.txt", "bar");
+    change2.setParents(
+        List.of(
+            changeResult.getCommit(), repo().parseCommit(ObjectId.fromString(secretBranchTip))));
     PushOneCommit.Result change2Result = change2.to("refs/for/master");
-
-    // Move the first change to a destination branch that is non-visible to user so that user cannot
-    // this change anymore.
-    BranchNameKey secretBranch = BranchNameKey.create(project, "secretBranch");
-    gApi.projects()
-        .name(secretBranch.project().get())
-        .branch(secretBranch.branch())
-        .create(new BranchInput());
     gApi.changes().id(changeResult.getChangeId()).move(secretBranch.branch());
+
+    // Hide branch from the user so that user cannot this change anymore.
     projectOperations
         .project(project)
         .forUpdate()