package org.apache.helix.rest.server;

/*
 * 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.
 */

import java.lang.management.ManagementFactory;
import java.util.Random;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.ws.rs.core.Response;
import org.apache.helix.TestHelper;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestDefaultMonitoringMbeans extends AbstractTestClass {

  // For entire testing environment, we could have 2 - 4 rest server during the testing. So we dont
  // know which REST server got the request and report number. So we have to loop all of them to
  // report data.

  // This is unstable test because the getcluster MBean is even not there after our call
  // and this is not critical for all existing logic. So disable it now.
  // TODO: Make MBean can be stable queried.
  @Test (enabled = false)
  public void testDefaultMonitoringMbeans()
      throws MBeanException, ReflectionException, InstanceNotFoundException, InterruptedException {
    System.out.println("Start test :" + TestHelper.getTestMethodName());
    int listClusters = new Random().nextInt(10);
    for (int i = 0; i < listClusters; i++) {
      get("clusters", null, Response.Status.OK.getStatusCode(), true);
    }

    MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
    boolean correctReports = false;

    // It may take couple milisecond to propagate the data to MBeanServer
    while (!correctReports) {
      for (ObjectName objectName : beanServer.queryNames(null, null)) {
        if (objectName.toString().contains("getClusters")) {
          // The object name is complicated, so we get the matched one and try to find out whether
          // they have the expected attributes and value matched our expectation.
          try {
            if (beanServer.getAttribute(objectName, "RequestCount_total")
                .equals(Long.valueOf(listClusters))) {
              correctReports = true;
            }
          } catch (AttributeNotFoundException e) {
          }
        }
      }
      Thread.sleep(50);
    }

    Assert.assertTrue(correctReports);
    System.out.println("End test :" + TestHelper.getTestMethodName());
  }
}

@@ -911,15 +911,89 @@
 til.
-Random;
+Arrays;%0Aimport java.util.HashSet;%0Aimport java.util.Random;%0Aimport java.util.Set;%0A
 %0Aimp
@@ -1165,24 +1165,78 @@
 BeanServer;%0A
+import javax.management.MalformedObjectNameException;%0A
 import javax
@@ -1338,16 +1338,17 @@
 sponse;%0A
+%0A
 import o
@@ -1371,24 +1371,80 @@
 TestHelper;%0A
+import org.apache.helix.rest.common.HelixRestNamespace;%0A
 import org.t
@@ -1494,16 +1494,16 @@
 .Test;%0A%0A
-
 public c
@@ -1562,16 +1562,94 @@
 tClass %7B
+%0A  private static final String DEFAULT_METRIC_DOMAIN = %22org.glassfish.jersey%22;
 %0A%0A  // F
@@ -3429,11 +3429,987 @@
 ());%0A  %7D
+%0A%0A  @Test%0A  public void testMBeanApplicationName()%0A      throws MalformedObjectNameException %7B%0A    Set%3CString%3E namespaces =%0A        new HashSet%3C%3E(Arrays.asList(HelixRestNamespace.DEFAULT_NAMESPACE_NAME, TEST_NAMESPACE));%0A    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();%0A    Set%3CObjectName%3E objectNames =%0A        mBeanServer.queryNames(new ObjectName(DEFAULT_METRIC_DOMAIN + %22:*%22), null);%0A%0A    Set%3CString%3E appNames = new HashSet%3C%3E();%0A    for (ObjectName mBeanName : objectNames) %7B%0A      appNames.add(mBeanName.getKeyProperty(%22type%22));%0A    %7D%0A%0A    Assert.assertEquals(appNames.size(), namespaces.size(), String%0A        .format(%22appNames: %25s does't have the same size as namespaces: %25s.%22, appNames, namespaces));%0A%0A    for (String appName : appNames) %7B%0A      Assert.assertTrue(namespaces.contains(appName), String%0A          .format(%22Application name: %25s is not one of the namespaces: %25s.%22, appName, namespaces));%0A      namespaces.remove(appName);%0A    %7D%0A  %7D
 %0A%7D%0A

Add unit test for setting application name.