package jenkins.metrics.api;

import hudson.util.VersionNumber;
import jenkins.model.Jenkins;
import org.apache.commons.lang.ArrayUtils;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.RestartableJenkinsRule;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.io.IOException;
import java.lang.management.ManagementFactory;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class MetricsTest {

    private static final String SYSPROP_FOR_LOGS_PATH = "hudson.triggers.SafeTimerTask.logsTargetDir";
    @Rule
    public final TemporaryFolder folder = new TemporaryFolder();
    @Rule
    public RestartableJenkinsRule story = new RestartableJenkinsRule();

    @BeforeClass
    @AfterClass
    public static void cleanProperty() throws IOException {
        System.clearProperty(SYSPROP_FOR_LOGS_PATH);
    }


    @Issue("JENKINS-50291")
    @Test
    public void logsDirectory() {

        story.then(j -> {
            Assume.assumeTrue(Jenkins.getVersion().isNewerThan(new VersionNumber("2.113"))); // JENKINS-50291 introduced in 2.114

            assertTrue(Metrics.HealthChecker.getLogFile(j.jenkins).getAbsolutePath().startsWith(j.getInstance().getRootDir().getAbsolutePath()));
            System.setProperty("hudson.triggers.SafeTimerTask.logsTargetDir", folder.newFolder().getAbsolutePath());
        });

        story.then(j -> assertFalse(Metrics.HealthChecker.getLogFile(j.jenkins).getAbsolutePath().startsWith(j.getInstance().getRootDir().getAbsolutePath())));
    }

    @Test
    public void jmxDomain() {
        story.then(r -> {
            assertTrue(ArrayUtils.contains(ManagementFactory.getPlatformMBeanServer().getDomains(), "io.jenkins"));
        });
    }

    @Test
    public void jmxMetricsExcluded() {
        story.then(r -> {
            MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
            assertThat(mBeanServer.queryNames(new ObjectName("io.jenkins:name=gc.*"), null), is(empty()));
            assertThat(mBeanServer.queryNames(new ObjectName("io.jenkins:name=vm.*"), null), is(empty()));
            assertThat(mBeanServer.queryNames(new ObjectName("io.jenkins:name=*history"), null), is(empty()));
            assertThat(mBeanServer.queryNames(new ObjectName("io.jenkins:name=*5m"), null), is(empty()));
            assertThat(mBeanServer.queryNames(new ObjectName("io.jenkins:name=*1h"), null), is(empty()));
        });
    }
}

@@ -675,16 +675,59 @@
 ers.is;%0A
+import static org.hamcrest.core.IsNot.not;%0A
 import s
@@ -2809,32 +2809,149 @@
 , is(empty()));%0A
+            assertThat(mBeanServer.queryNames(new ObjectName(%22io.jenkins:name=jenkins.*%22), null), is(not(empty())));%0A
         %7D);%0A    

Add a control case