diff --git a/pom.xml b/pom.xml index 516a1fdfe..5b4370876 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.jenkins-ci.plugins plugin - 4.52 + 4.73 @@ -26,7 +26,7 @@ 0.10 4.7.0 - 4.1.6.1 + 4.2.18-442.v02e107157925 2.21 3.24.2 @@ -60,7 +60,7 @@ - scm:git:git://github.com/jenkinsci/prometheus-plugin.git + scm:git:https://github.com/jenkinsci/prometheus-plugin.git scm:git:git@github.com:jenkinsci/prometheus-plugin.git https://github.com/jenkinsci/prometheus-plugin/ prometheus-2.3.2 @@ -160,18 +160,26 @@ org.mockito mockito-core + ${mockito.version} test - - org.mockito - mockito-inline - test - + + + + + + com.github.stefanbirkner system-lambda ${system-lambda.version} test + + com.google.inject.extensions + guice-testlib + 7.0.0 + test + diff --git a/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/RegexDisabledMetric.java b/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/RegexDisabledMetric.java index a72528d75..c57c88798 100644 --- a/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/RegexDisabledMetric.java +++ b/src/main/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/RegexDisabledMetric.java @@ -22,16 +22,6 @@ public String getRegex() { return regex; } - public FormValidation doCheckRegex(@QueryParameter String value) { - - try { - Pattern.compile(value); - return FormValidation.ok(); - } catch (PatternSyntaxException exception) { - return FormValidation.error("Not a valid regex"); - } - } - @Override public Descriptor getDescriptor() { return new DescriptorImpl(); diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumerationTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumerationTest.java index 8e5fa46a2..62bffde59 100644 --- a/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumerationTest.java +++ b/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/FilteredMetricEnumerationTest.java @@ -5,7 +5,6 @@ import org.junit.jupiter.api.Test; import org.mockito.MockedStatic; -import java.util.Enumeration; import java.util.Iterator; import java.util.List; @@ -42,4 +41,34 @@ void testFilterMatches() { } } + + @Test + void testIterator() { + try (MockedStatic statusCheckerMockedStatic = mockStatic(MetricStatusChecker.class)) { + + statusCheckerMockedStatic.when(() -> MetricStatusChecker.isEnabled("metric_1")).thenReturn(false); + statusCheckerMockedStatic.when(() -> MetricStatusChecker.isEnabled("metric_2")).thenReturn(true); + + List list = List.of( + new Collector.MetricFamilySamples("metric_1", Collector.Type.GAUGE, "help1", List.of()), + new Collector.MetricFamilySamples("metric_2", Collector.Type.GAUGE, "help2", List.of()) + ); + + Iterator iterator = list.iterator(); + + + FilteredMetricEnumeration filteredMetricEnumeration = new FilteredMetricEnumeration(iterator); + + int foundElements = 0; + String foundKey = ""; + while (filteredMetricEnumeration.asIterator().hasNext()) { + Collector.MetricFamilySamples familySamples = filteredMetricEnumeration.nextElement(); + foundKey = familySamples.name; + foundElements++; + } + Assertions.assertEquals(1, foundElements); + Assertions.assertEquals("metric_2", foundKey); + + } + } } \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/NamedDisabledMetricTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/NamedDisabledMetricTest.java new file mode 100644 index 000000000..affe080ab --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/NamedDisabledMetricTest.java @@ -0,0 +1,17 @@ +package org.jenkinsci.plugins.prometheus.config.disabledmetrics; + +import hudson.model.Descriptor; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.*; + +public class NamedDisabledMetricTest { + + @Test + void testDescriptorName() { + NamedDisabledMetric sut = new NamedDisabledMetric("some_metric"); + Descriptor descriptor = sut.getDescriptor(); + Assertions.assertEquals("Fully qualified Name Entry", descriptor.getDisplayName()); + } +} \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/RegexDisabledMetricTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/RegexDisabledMetricTest.java new file mode 100644 index 000000000..0ea1175ac --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/config/disabledmetrics/RegexDisabledMetricTest.java @@ -0,0 +1,17 @@ +package org.jenkinsci.plugins.prometheus.config.disabledmetrics; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.Assert.*; + +public class RegexDisabledMetricTest { + + @Test + void testDescriptorName() { + RegexDisabledMetric sut = new RegexDisabledMetric("some_regex"); + Assertions.assertEquals("Regex Entry", sut.getDescriptor().getDisplayName()); + } + + +} \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/context/ContextTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/context/ContextTest.java new file mode 100644 index 000000000..ea8d9358b --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/context/ContextTest.java @@ -0,0 +1,50 @@ +package org.jenkinsci.plugins.prometheus.context; + +import com.codahale.metrics.MetricRegistry; +import com.google.inject.Guice; +import com.google.inject.Injector; +import jenkins.metrics.api.Metrics; +import jenkins.model.Jenkins; +import org.jenkinsci.plugins.prometheus.config.PrometheusConfiguration; +import org.jenkinsci.plugins.prometheus.service.DefaultPrometheusMetrics; +import org.jenkinsci.plugins.prometheus.service.PrometheusMetrics; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; + +import static org.mockito.Mockito.*; + +public class ContextTest { + + @Test + public void testCanInjectContext() { + try (MockedStatic jenkinsStatic = mockStatic(Jenkins.class); + MockedStatic prometheusConfigurationStatic = mockStatic(PrometheusConfiguration.class); + MockedStatic metricsStatic = mockStatic(Metrics.class)) { + Jenkins mockedJenkins = mock(Jenkins.class); + jenkinsStatic.when(Jenkins::get).thenReturn(mockedJenkins); + + MetricRegistry mockedMetrics = mock(MetricRegistry.class); + metricsStatic.when(Metrics::metricRegistry).thenReturn(mockedMetrics); + + PrometheusConfiguration mockedPrometheusConfiguration = mock(PrometheusConfiguration.class); + when(mockedPrometheusConfiguration.getLabeledBuildParameterNamesAsArray()).thenReturn(new String[]{}); + when(mockedPrometheusConfiguration.getDefaultNamespace()).thenReturn("default"); + prometheusConfigurationStatic.when(PrometheusConfiguration::get).thenReturn(mockedPrometheusConfiguration); + + Injector injector = Guice.createInjector(new Context()); + PrometheusMetrics prometheusMetrics = injector.getInstance(PrometheusMetrics.class); + + Assertions.assertNotNull(prometheusMetrics); + Assertions.assertEquals(DefaultPrometheusMetrics.class, prometheusMetrics.getClass()); + + PrometheusMetrics prometheusMetrics2 = injector.getInstance(PrometheusMetrics.class); + + Assertions.assertEquals(prometheusMetrics, prometheusMetrics2, "Should be the same as it's a singleton!"); + + + } + + } + +} \ No newline at end of file diff --git a/src/test/java/org/jenkinsci/plugins/prometheus/util/JobsTest.java b/src/test/java/org/jenkinsci/plugins/prometheus/util/JobsTest.java new file mode 100644 index 000000000..925cc748c --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/prometheus/util/JobsTest.java @@ -0,0 +1,44 @@ +package org.jenkinsci.plugins.prometheus.util; + +import hudson.model.Job; +import jenkins.model.Jenkins; +import org.jenkinsci.plugins.prometheus.config.PrometheusConfiguration; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.mockStatic; + +public class JobsTest { + + @Test + void testEachJob() { + try (MockedStatic jenkinsStatic = mockStatic(Jenkins.class)) { + Jenkins jenkins = mock(Jenkins.class); + List jobs = List.of(mockJob("name1"), mockJob("name2")); + when(jenkins.getAllItems(Job.class)).thenReturn(jobs); + jenkinsStatic.when(Jenkins::get).thenReturn(jenkins); + + + List names = new ArrayList<>(); + Jobs.forEachJob(job -> { + names.add(job.getName()); + }); + + Assertions.assertEquals(2, names.size()); + Assertions.assertTrue(names.contains("name1")); + Assertions.assertTrue(names.contains("name2")); + } + } + + private static Job mockJob(String name) { + Job mock = mock(Job.class); + when(mock.getName()).thenReturn(name); + return mock; + } +} \ No newline at end of file