/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to you 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 org.apache.geode.management;

import static org.apache.geode.test.awaitility.GeodeAwaitility.await;
import static org.junit.Assert.assertEquals;

import java.io.Serializable;

import javax.management.ObjectName;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.TestName;

import org.apache.geode.cache.Cache;
import org.apache.geode.cache.partition.PartitionRegionHelper;
import org.apache.geode.distributed.DistributedMember;
import org.apache.geode.test.dunit.rules.ClusterStartupRule;
import org.apache.geode.test.dunit.rules.MemberVM;
import org.apache.geode.test.junit.categories.GfshTest;
import org.apache.geode.test.junit.rules.GfshCommandRule;
import org.apache.geode.test.junit.rules.serializable.SerializableTestName;

@Category({GfshTest.class})
public class MemberMXBeanDistributedTest implements
    Serializable {

  private static MemberVM locator;
  private static MemberVM server1;
  private static MemberVM server2;
  private static MemberVM server3;
  private static MemberVM server4;

  @ClassRule
  public static ClusterStartupRule lsRule = new ClusterStartupRule();

  @ClassRule
  public static GfshCommandRule gfsh = new GfshCommandRule();

  @Rule
  public TestName testName = new SerializableTestName();

  @BeforeClass
  public static void before() throws Exception {
    locator = lsRule.startLocatorVM(0);
    server1 = lsRule.startServerVM(1, "", locator.getPort());
    server2 = lsRule.startServerVM(2, "", locator.getPort());
    server3 = lsRule.startServerVM(3, "", locator.getPort());
    server4 = lsRule.startServerVM(4, "", locator.getPort());

    gfsh.connectAndVerify(locator);
  }

  @Test
  public void testBucketCount() {
    String regionName = "testCreateRegion";

    gfsh.executeAndAssertThat("create region"
        + " --name=" + regionName
        + " --type=PARTITION_PERSISTENT"
        + " --total-num-buckets=1000").statusIsSuccess();

    server1.invoke(() -> createBuckets(regionName));
    server2.invoke(() -> createBuckets(regionName));
    server3.invoke(() -> createBuckets(regionName));
    server4.invoke(() -> createBuckets(regionName));

    await().untilAsserted(() -> {
      final int sumOfBuckets = server1.invoke(() -> getBucketsInitialized()) +
          server2.invoke(() -> getBucketsInitialized()) +
          server3.invoke(() -> getBucketsInitialized()) +
          server4.invoke(() -> getBucketsInitialized());
      assertEquals("Expected bucket count is 1000, and actual count is " + sumOfBuckets,
          sumOfBuckets, 1000);
    });

    for (int i = 1; i < 4; i++) {
      gfsh.executeAndAssertThat("create region"
          + " --name=" + regionName + i
          + " --type=PARTITION_PERSISTENT"
          + " --total-num-buckets=1000"
          + " --colocated-with=" + regionName).statusIsSuccess();
    }

    await().untilAsserted(() -> {
      final int sumOfBuckets = server1.invoke(() -> getBucketsInitialized()) +
          server2.invoke(() -> getBucketsInitialized()) +
          server3.invoke(() -> getBucketsInitialized()) +
          server4.invoke(() -> getBucketsInitialized());
      assertEquals("Expected bucket count is 4000, and actual count is " + sumOfBuckets,
          sumOfBuckets, 4000);
    });

  }

  private int getBucketsInitialized() {
    Cache cache = ClusterStartupRule.getCache();

    DistributedMember member = cache.getDistributedSystem().getDistributedMember();
    ManagementService mgmtService = ManagementService.getManagementService(cache);
    ObjectName memberMBeanName = mgmtService.getMemberMBeanName(member);
    MemberMXBean memberMXBean = mgmtService.getMBeanInstance(memberMBeanName, MemberMXBean.class);

    return memberMXBean.getTotalBucketCount();
  }

  private void createBuckets(String regionName) {
    Cache cache = ClusterStartupRule.getCache();
    PartitionRegionHelper.assignBucketsToPartitions(cache.getRegion(regionName));
  }

}

@@ -914,33 +914,46 @@
 org.
-junit
+assertj.core.api
 .Assert
+ions
 .assert
-Equals
+That
 ;%0A%0Ai
@@ -3343,158 +3343,138 @@
 sert
-Equals(%22Expected bucket count is 1000, and actual count is %22 + sumOfBuckets,%0A          sumOfBuckets, 1000);%0A    %7D);%0A%0A    for (int i = 1; i %3C 4; i++) %7B
+That(sumOfBuckets).isEqualTo(1000);%0A    %7D);%0A%0A    for (int i = 1; i %3C 4; i++) %7B%0A      final String tempRegioName = regionName + i;%0A
 %0A   
@@ -3543,30 +3543,29 @@
 ame=%22 + 
-r
+tempR
 egio
-n
 Name
- + i
 %0A       
@@ -3705,16 +3705,249 @@
 ccess();
+%0A%0A      server1.invoke(() -%3E createBuckets(tempRegioName));%0A      server2.invoke(() -%3E createBuckets(tempRegioName));%0A      server3.invoke(() -%3E createBuckets(tempRegioName));%0A      server4.invoke(() -%3E createBuckets(tempRegioName));
 %0A    %7D%0A%0A
@@ -4248,109 +4248,37 @@
 sert
-Equals(%22Expected bucket count is 4000, and actual count is %22 + sumOfBuckets,%0A          sumOfBuckets, 
+That(sumOfBuckets).isEqualTo(
 4000

test updated, added bucket initialization after creation of each child region (#5432)