/*
 * Copyright 2017-present Open Networking Foundation
 *
 * 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 io.atomix.core;

import io.atomix.cluster.Member;
import io.atomix.core.profile.Profile;
import io.atomix.primitive.protocol.PrimitiveProtocol;
import org.junit.AfterClass;
import org.junit.BeforeClass;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * Base Atomix test.
 */
public abstract class AbstractPrimitiveTest extends AbstractAtomixTest {
  private static List<Atomix> instances;
  private static int id = 10;

  /**
   * Returns the primitive protocol with which to test.
   *
   * @return the protocol with which to test
   */
  protected abstract PrimitiveProtocol protocol();

  /**
   * Returns a new Atomix instance.
   *
   * @return a new Atomix instance.
   */
  protected Atomix atomix() throws Exception {
    Atomix instance = createAtomix(Member.Type.EPHEMERAL, id++, Arrays.asList(1, 2, 3), Arrays.asList()).start().get(10, TimeUnit.SECONDS);
    instances.add(instance);
    return instance;
  }

  @BeforeClass
  public static void setupAtomix() throws Exception {
    AbstractAtomixTest.setupAtomix();
    instances = new ArrayList<>();
    instances.add(createAtomix(Member.Type.PERSISTENT, 1, Arrays.asList(1, 2, 3), Arrays.asList(), Profile.CONSENSUS, Profile.DATA_GRID));
    instances.add(createAtomix(Member.Type.PERSISTENT, 2, Arrays.asList(1, 2, 3), Arrays.asList(), Profile.CONSENSUS, Profile.DATA_GRID));
    instances.add(createAtomix(Member.Type.PERSISTENT, 3, Arrays.asList(1, 2, 3), Arrays.asList(), Profile.CONSENSUS, Profile.DATA_GRID));
    List<CompletableFuture<Atomix>> futures = instances.stream().map(Atomix::start).collect(Collectors.toList());
    CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).get(30, TimeUnit.SECONDS);
  }

  @AfterClass
  public static void teardownAtomix() throws Exception {
    List<CompletableFuture<Void>> futures = instances.stream().map(Atomix::stop).collect(Collectors.toList());
    try {
      CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).join();
    } catch (Exception e) {
      // Do nothing
    }
    AbstractAtomixTest.teardownAtomix();
  }
}

@@ -687,27 +687,23 @@
 ix.c
-ore.profile.Profile
+luster.MemberId
 ;%0Aim
@@ -755,16 +755,151 @@
 otocol;%0A
+import io.atomix.protocols.backup.partition.PrimaryBackupPartitionGroup;%0Aimport io.atomix.protocols.raft.partition.RaftPartitionGroup;%0A
 import o
@@ -1111,16 +1111,52 @@
 meUnit;%0A
+import java.util.function.Function;%0A
 import j
@@ -1924,24 +1924,584 @@
 Exception %7B%0A
+    Function%3CAtomix.Builder, Atomix%3E build = builder -%3E%0A        builder.withManagementGroup(RaftPartitionGroup.builder(%22system%22)%0A            .withNumPartitions(1)%0A            .withMembers(%221%22, %222%22, %223%22)%0A            .build())%0A            .addPartitionGroup(RaftPartitionGroup.builder(%22raft%22)%0A                .withNumPartitions(3)%0A                .withMembers(%221%22, %222%22, %223%22)%0A                .build())%0A            .addPartitionGroup(PrimaryBackupPartitionGroup.builder(%22data%22)%0A                .withNumPartitions(7)%0A                .build())%0A            .build();%0A
     Abstract
@@ -2664,44 +2664,13 @@
 (), 
-Profile.CONSENSUS, Profile.DATA_GRID
+build
 ));%0A
@@ -2772,44 +2772,13 @@
 (), 
-Profile.CONSENSUS, Profile.DATA_GRID
+build
 ));%0A
@@ -2880,44 +2880,13 @@
 (), 
-Profile.CONSENSUS, Profile.DATA_GRID
+build
 ));%0A

Use fewer partitions for primitive tests.