e2e-tests: Add FlushProjectsCacheUsingHAGerrit2 scenario(s)

Add a FlushProjectsCacheUsingHAGerrit2 scenario based on core
framework's. Make it reuse the added GetProjectsCacheEntries and
CheckProjectsCacheFlushEntriesUsingHAGerrit1 scenarios, which can also
be run independently as usual. Make all these added scenarios extend the
CacheFlushSimulation core framework class to consistently reuse it.

In order for CheckProjectsCacheFlushEntriesUsingHAGerrit1 to run
independently, as opposed to otherwise being composed, introduce the
PROJECTS_ENTRIES optional JAVA_OPTS property ([1]). Based on initial
projects cache cardinality showed by locally testing this case, that
value starts with being 1 (core default). Local testing so far assumes
HA plugin's dockerized setup, used to develop the plugin e2e tests up
until now.

[1] https://gerrit-review.googlesource.com/Documentation/dev-e2e-tests.html#_environment_properties

Change-Id: Ia93492a0b9f2da83d8260e2753f7fadf8bd6a3f5
diff --git a/src/test/resources/com/ericsson/gerrit/plugins/highavailability/scenarios/CheckProjectsCacheFlushEntriesUsingHAGerrit1.json b/src/test/resources/com/ericsson/gerrit/plugins/highavailability/scenarios/CheckProjectsCacheFlushEntriesUsingHAGerrit1.json
new file mode 100644
index 0000000..281515d
--- /dev/null
+++ b/src/test/resources/com/ericsson/gerrit/plugins/highavailability/scenarios/CheckProjectsCacheFlushEntriesUsingHAGerrit1.json
@@ -0,0 +1,6 @@
+[
+  {
+    "url": "http://HOSTNAME:HTTP_PORT1/a/config/server/caches/projects",
+    "entries": "PROJECTS_ENTRIES"
+  }
+]
diff --git a/src/test/resources/com/ericsson/gerrit/plugins/highavailability/scenarios/FlushProjectsCacheUsingHAGerrit2.json b/src/test/resources/com/ericsson/gerrit/plugins/highavailability/scenarios/FlushProjectsCacheUsingHAGerrit2.json
new file mode 100644
index 0000000..c938973
--- /dev/null
+++ b/src/test/resources/com/ericsson/gerrit/plugins/highavailability/scenarios/FlushProjectsCacheUsingHAGerrit2.json
@@ -0,0 +1,5 @@
+[
+  {
+    "url": "http://HOSTNAME:HTTP_PORT2/a/config/server/caches/projects/flush"
+  }
+]
diff --git a/src/test/resources/com/ericsson/gerrit/plugins/highavailability/scenarios/GetProjectsCacheEntries.json b/src/test/resources/com/ericsson/gerrit/plugins/highavailability/scenarios/GetProjectsCacheEntries.json
new file mode 100644
index 0000000..f7450a4
--- /dev/null
+++ b/src/test/resources/com/ericsson/gerrit/plugins/highavailability/scenarios/GetProjectsCacheEntries.json
@@ -0,0 +1,5 @@
+[
+  {
+    "url": "http://HOSTNAME/a/config/server/caches/projects"
+  }
+]
diff --git a/src/test/scala/com/ericsson/gerrit/plugins/highavailability/scenarios/CheckProjectsCacheFlushEntriesUsingHAGerrit1.scala b/src/test/scala/com/ericsson/gerrit/plugins/highavailability/scenarios/CheckProjectsCacheFlushEntriesUsingHAGerrit1.scala
new file mode 100644
index 0000000..9a9f333
--- /dev/null
+++ b/src/test/scala/com/ericsson/gerrit/plugins/highavailability/scenarios/CheckProjectsCacheFlushEntriesUsingHAGerrit1.scala
@@ -0,0 +1,53 @@
+// Copyright (C) 2020 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.ericsson.gerrit.plugins.highavailability.scenarios
+
+import com.google.gerrit.scenarios.CacheFlushSimulation
+import io.gatling.core.Predef.{atOnceUsers, _}
+import io.gatling.core.feeder.FeederBuilder
+import io.gatling.core.structure.ScenarioBuilder
+import io.gatling.http.Predef._
+
+class CheckProjectsCacheFlushEntriesUsingHAGerrit1 extends CacheFlushSimulation {
+  private val data: FeederBuilder = jsonFile(resource).convert(keys).queue
+
+  override def replaceOverride(in: String): String = {
+    replaceProperty("http_port1", 8081, in)
+  }
+
+  def this(producer: CacheFlushSimulation) {
+    this()
+    this.producer = Some(producer)
+  }
+
+  val test: ScenarioBuilder = scenario(unique)
+    .feed(data)
+    .exec(session => {
+      if (producer.nonEmpty) {
+        session.set(entriesKey, producer.get.expectedEntriesAfterFlush())
+      } else {
+        session
+      }
+    })
+    .exec(http(unique).get("${url}")
+      .check(regex("\"" + memKey + "\": (\\d+)")
+        .is(session => session(entriesKey).as[String])))
+
+  setUp(
+    test.inject(
+      atOnceUsers(1)
+    ),
+  ).protocols(httpProtocol)
+}
diff --git a/src/test/scala/com/ericsson/gerrit/plugins/highavailability/scenarios/FlushProjectsCacheUsingHAGerrit2.scala b/src/test/scala/com/ericsson/gerrit/plugins/highavailability/scenarios/FlushProjectsCacheUsingHAGerrit2.scala
new file mode 100644
index 0000000..9c317d4
--- /dev/null
+++ b/src/test/scala/com/ericsson/gerrit/plugins/highavailability/scenarios/FlushProjectsCacheUsingHAGerrit2.scala
@@ -0,0 +1,65 @@
+// Copyright (C) 2020 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.ericsson.gerrit.plugins.highavailability.scenarios
+
+import com.google.gerrit.scenarios.CacheFlushSimulation
+import io.gatling.core.Predef._
+import io.gatling.core.feeder.FeederBuilder
+import io.gatling.core.structure.ScenarioBuilder
+
+import scala.concurrent.duration._
+
+class FlushProjectsCacheUsingHAGerrit2 extends CacheFlushSimulation {
+  private val data: FeederBuilder = jsonFile(resource).convert(keys).queue
+  private val default: String = name
+
+  override def relativeRuntimeWeight = 2
+
+  override def replaceOverride(in: String): String = {
+    replaceProperty("http_port2", 8082, in)
+  }
+
+  private val flushCache: ScenarioBuilder = scenario(unique)
+    .feed(data)
+    .exec(httpRequest)
+
+  private val createProject = new CreateProjectUsingHAGerrit1(default)
+  private val getCacheEntriesAfterProject = new GetProjectsCacheEntries(this)
+  private val checkCacheEntriesAfterFlush = new CheckProjectsCacheFlushEntriesUsingHAGerrit1(this)
+  private val deleteProject = new DeleteProjectUsingHAGerrit(default)
+
+  setUp(
+    createProject.test.inject(
+      nothingFor(stepWaitTime(createProject) seconds),
+      atOnceUsers(1)
+    ),
+    getCacheEntriesAfterProject.test.inject(
+      nothingFor(stepWaitTime(getCacheEntriesAfterProject) seconds),
+      atOnceUsers(1)
+    ),
+    flushCache.inject(
+      nothingFor(stepWaitTime(this) seconds),
+      atOnceUsers(1)
+    ),
+    checkCacheEntriesAfterFlush.test.inject(
+      nothingFor(stepWaitTime(checkCacheEntriesAfterFlush) seconds),
+      atOnceUsers(1)
+    ),
+    deleteProject.test.inject(
+      nothingFor(stepWaitTime(deleteProject) seconds),
+      atOnceUsers(1)
+    ),
+  ).protocols(httpProtocol)
+}
diff --git a/src/test/scala/com/ericsson/gerrit/plugins/highavailability/scenarios/GetProjectsCacheEntries.scala b/src/test/scala/com/ericsson/gerrit/plugins/highavailability/scenarios/GetProjectsCacheEntries.scala
new file mode 100644
index 0000000..9565365
--- /dev/null
+++ b/src/test/scala/com/ericsson/gerrit/plugins/highavailability/scenarios/GetProjectsCacheEntries.scala
@@ -0,0 +1,46 @@
+// Copyright (C) 2020 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.ericsson.gerrit.plugins.highavailability.scenarios
+
+import com.google.gerrit.scenarios.CacheFlushSimulation
+import io.gatling.core.Predef._
+import io.gatling.core.feeder.FeederBuilder
+import io.gatling.core.structure.ScenarioBuilder
+import io.gatling.http.Predef.{http, _}
+
+class GetProjectsCacheEntries extends CacheFlushSimulation {
+  private val data: FeederBuilder = jsonFile(resource).convert(keys).queue
+
+  def this(consumer: CacheFlushSimulation) {
+    this()
+    this.consumer = Some(consumer)
+  }
+
+  val test: ScenarioBuilder = scenario(unique)
+    .feed(data)
+    .exec(http(unique).get("${url}")
+      .check(regex("\"" + memKey + "\": (\\d+)").saveAs(entriesKey)))
+    .exec(session => {
+      if (consumer.nonEmpty) {
+        consumer.get.entriesBeforeFlush(session(entriesKey).as[Int])
+      }
+      session
+    })
+
+  setUp(
+    test.inject(
+      atOnceUsers(1)
+    )).protocols(httpProtocol)
+}