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.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.ws.rs.core.Response;

import org.apache.helix.TestHelper;
import org.apache.helix.rest.common.HelixRestNamespace;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestDefaultMonitoringMbeans extends AbstractTestClass {
  private static final String DEFAULT_METRIC_DOMAIN = "org.glassfish.jersey";

  // 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());
  }

  @Test
  public void testMBeanApplicationName()
      throws MalformedObjectNameException {
    Set<String> namespaces =
        new HashSet<>(Arrays.asList(HelixRestNamespace.DEFAULT_NAMESPACE_NAME, TEST_NAMESPACE));
    MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> objectNames =
        mBeanServer.queryNames(new ObjectName(DEFAULT_METRIC_DOMAIN + ":*"), null);

    Set<String> appNames = new HashSet<>();
    for (ObjectName mBeanName : objectNames) {
      appNames.add(mBeanName.getKeyProperty("type"));
    }

    Assert.assertEquals(appNames.size(), namespaces.size(), String
        .format("appNames: %s does't have the same size as namespaces: %s.", appNames, namespaces));

    for (String appName : appNames) {
      Assert.assertTrue(namespaces.contains(appName), String
          .format("Application name: %s is not one of the namespaces: %s.", appName, namespaces));
      namespaces.remove(appName);
    }
  }
}

@@ -1172,62 +1172,8 @@
 er;%0A
-import javax.management.MalformedObjectNameException;%0A
 impo
@@ -3424,30 +3424,24 @@
 onName()
-%0A     
  throws 
 Malforme
@@ -3436,27 +3436,8 @@
 ows 
-MalformedObjectName
 Exce
@@ -3640,24 +3640,57 @@
 anServer();%0A
+%0A    TestHelper.verify(() -%3E %7B%0A  
     Set%3CObje
@@ -3719,16 +3719,18 @@
         
+ 
+ 
 mBeanSer
@@ -3794,24 +3794,26 @@
 %22), null);%0A%0A
+  
     Set%3CStri
@@ -3844,24 +3844,26 @@
 et%3C%3E();%0A    
+  
 for (ObjectN
@@ -3899,16 +3899,18 @@
 %7B%0A      
+ 
+ 
 appNames
@@ -3957,206 +3957,43 @@
     
-%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 : 
+  %7D%0A      return namespaces.equals(
 appN
@@ -4001,210 +4001,43 @@
 mes)
- %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
+%7D, TestHelper.WAIT_DURATION);
 %0A  %7D

Fix flaky test testMBeanApplicationName (#1298)