Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests and build improvements #560

Merged
merged 3 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>4.52</version>
<version>4.73</version>
<relativePath />
</parent>

Expand All @@ -26,7 +26,7 @@

<cloudbees-disk-usage-simple.version>0.10</cloudbees-disk-usage-simple.version>
<code-coverage-api.version>4.7.0</code-coverage-api.version>
<metrics.version>4.1.6.1</metrics.version>
<metrics.version>4.2.18-442.v02e107157925</metrics.version>
<pipeline-rest-api.version>2.21</pipeline-rest-api.version>

<assertj-core.version>3.24.2</assertj-core.version>
Expand Down Expand Up @@ -60,7 +60,7 @@
</developers>

<scm>
<connection>scm:git:git://github.com/jenkinsci/prometheus-plugin.git</connection>
<connection>scm:git:https://github.com/jenkinsci/prometheus-plugin.git</connection>
<developerConnection>scm:git:[email protected]:jenkinsci/prometheus-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/prometheus-plugin/</url>
<tag>prometheus-2.3.2</tag>
Expand Down Expand Up @@ -160,18 +160,26 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.mockito</groupId>-->
<!-- <artifactId>mockito-inline</artifactId>-->
<!-- <version>${mockito.version}</version>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-lambda</artifactId>
<version>${system-lambda.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-testlib</artifactId>
<version>7.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entry> getDescriptor() {
return new DescriptorImpl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -42,4 +41,34 @@ void testFilterMatches() {

}
}

@Test
void testIterator() {
try (MockedStatic<MetricStatusChecker> statusCheckerMockedStatic = mockStatic(MetricStatusChecker.class)) {

statusCheckerMockedStatic.when(() -> MetricStatusChecker.isEnabled("metric_1")).thenReturn(false);
statusCheckerMockedStatic.when(() -> MetricStatusChecker.isEnabled("metric_2")).thenReturn(true);

List<Collector.MetricFamilySamples> 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<Collector.MetricFamilySamples> 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);

}
}
}
Original file line number Diff line number Diff line change
@@ -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<Entry> descriptor = sut.getDescriptor();
Assertions.assertEquals("Fully qualified Name Entry", descriptor.getDisplayName());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}


}
Original file line number Diff line number Diff line change
@@ -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<Jenkins> jenkinsStatic = mockStatic(Jenkins.class);
MockedStatic<PrometheusConfiguration> prometheusConfigurationStatic = mockStatic(PrometheusConfiguration.class);
MockedStatic<Metrics> 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!");


}

}

}
44 changes: 44 additions & 0 deletions src/test/java/org/jenkinsci/plugins/prometheus/util/JobsTest.java
Original file line number Diff line number Diff line change
@@ -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<Jenkins> jenkinsStatic = mockStatic(Jenkins.class)) {
Jenkins jenkins = mock(Jenkins.class);
List<Job> jobs = List.of(mockJob("name1"), mockJob("name2"));
when(jenkins.getAllItems(Job.class)).thenReturn(jobs);
jenkinsStatic.when(Jenkins::get).thenReturn(jenkins);


List<String> 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;
}
}
Loading