From 9efa3082f11658416ad7151b1492d8e0b309325e Mon Sep 17 00:00:00 2001 From: Nikita Tkachenko Date: Wed, 18 Sep 2024 17:18:14 +0200 Subject: [PATCH] Additional tests --- .../datadog/DatadogGlobalConfiguration.java | 42 ++++++++++++++++--- .../DatadogGlobalConfigurationSaveTest.java | 5 +++ .../DatadogGlobalConfigurationTest.java | 18 ++++---- 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration.java b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration.java index a7a765cc..e375024d 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration.java @@ -25,21 +25,18 @@ of this software and associated documentation files (the "Software"), to deal package org.datadog.jenkins.plugins.datadog; -import static org.datadog.jenkins.plugins.datadog.configuration.DatadogAgentConfiguration.DatadogAgentConfigurationDescriptor.getDefaultAgentHost; -import static org.datadog.jenkins.plugins.datadog.configuration.DatadogAgentConfiguration.DatadogAgentConfigurationDescriptor.getDefaultAgentLogCollectionPort; -import static org.datadog.jenkins.plugins.datadog.configuration.DatadogAgentConfiguration.DatadogAgentConfigurationDescriptor.getDefaultAgentPort; -import static org.datadog.jenkins.plugins.datadog.configuration.DatadogAgentConfiguration.DatadogAgentConfigurationDescriptor.getDefaultAgentTraceCollectionPort; -import static org.datadog.jenkins.plugins.datadog.configuration.api.key.DatadogTextApiKey.DatadogTextApiKeyDescriptor.getDefaultKey; - +import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.annotations.XStreamConverter; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; +import hudson.XmlFile; import hudson.init.InitMilestone; import hudson.init.Initializer; import hudson.model.AbstractProject; import hudson.util.FormValidation; import hudson.util.FormValidation.Kind; import hudson.util.Secret; +<<<<<<< Updated upstream import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -48,7 +45,11 @@ of this software and associated documentation files (the "Software"), to deal import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; +======= +import hudson.util.XStream2; +>>>>>>> Stashed changes import jenkins.model.GlobalConfiguration; +import jenkins.model.Jenkins; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.datadog.jenkins.plugins.datadog.clients.ClientHolder; @@ -67,12 +68,32 @@ of this software and associated documentation files (the "Software"), to deal import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.interceptor.RequirePOST; +import javax.annotation.Nonnull; +import java.io.File; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; +import java.util.stream.Collectors; + +import static org.datadog.jenkins.plugins.datadog.configuration.DatadogAgentConfiguration.DatadogAgentConfigurationDescriptor.*; +import static org.datadog.jenkins.plugins.datadog.configuration.api.key.DatadogTextApiKey.DatadogTextApiKeyDescriptor.getDefaultKey; + @Extension public class DatadogGlobalConfiguration extends GlobalConfiguration { private static final Logger logger = Logger.getLogger(DatadogGlobalConfiguration.class.getName()); private static final String DISPLAY_NAME = "Datadog Plugin"; + private static final XStream XSTREAM; + + static { + XSTREAM = new XStream2(XStream2.getDefaultDriver()); + XSTREAM.autodetectAnnotations(true); + } + // Event String constants public static final String SYSTEM_EVENTS = "ItemLocationChanged," + "ComputerOnline,ComputerOffline,ComputerTemporarilyOnline,ComputerTemporarilyOffline," @@ -147,7 +168,16 @@ public DatadogGlobalConfiguration() { loadEnvVariables(); // Load environment variables } +<<<<<<< Updated upstream @Initializer(after = InitMilestone.PLUGINS_STARTED) +======= + @Override + protected XmlFile getConfigFile() { + return new XmlFile(XSTREAM, new File(Jenkins.get().getRootDir(), getId() + ".xml")); + } + + @Initializer(after = InitMilestone.SYSTEM_CONFIG_LOADED) +>>>>>>> Stashed changes public void onStartup() { try { DatadogClient client = this.datadogClientConfiguration.createClient(); diff --git a/src/test/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfigurationSaveTest.java b/src/test/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfigurationSaveTest.java index 29aa6fe5..53953bfe 100644 --- a/src/test/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfigurationSaveTest.java +++ b/src/test/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfigurationSaveTest.java @@ -2,6 +2,7 @@ import com.thoughtworks.xstream.XStream; import hudson.util.XStream2; +import org.datadog.jenkins.plugins.datadog.configuration.DatadogApiConfiguration; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; @@ -78,6 +79,10 @@ public void canSaveAndLoadGlobalConfiguration() { private static final XStream XSTREAM = new XStream2(XStream2.getDefaultDriver()); + static { + XSTREAM.processAnnotations(new Class[] { DatadogGlobalConfiguration.class, DatadogApiConfiguration.class }); + } + private static DatadogGlobalConfiguration parseConfigurationFromResource(String resourceName) { URL resource = DatadogGlobalConfigurationSaveTest.class.getResource(resourceName); return (DatadogGlobalConfiguration) XSTREAM.fromXML(resource); diff --git a/src/test/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfigurationTest.java b/src/test/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfigurationTest.java index b09574bb..55804538 100644 --- a/src/test/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfigurationTest.java +++ b/src/test/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfigurationTest.java @@ -18,13 +18,6 @@ public class DatadogGlobalConfigurationTest { - private static final XStream XSTREAM = new XStream2(XStream2.getDefaultDriver()); - - private static DatadogGlobalConfiguration parseConfigurationFromResource(String resourceName) { - URL resource = DatadogGlobalConfigurationTest.class.getResource(resourceName); - return (DatadogGlobalConfiguration) XSTREAM.fromXML(resource); - } - @Test public void canLoadGlobalConfiguration() { DatadogGlobalConfiguration configuration = parseConfigurationFromResource("globalConfiguration.xml"); @@ -254,4 +247,15 @@ public void canLoadGlobalConfigurationFromLegacyFormatReportingToAgent() { assertTrue(configuration.isCacheBuildRuns()); assertFalse(configuration.isUseAwsInstanceHostname()); } + + private static final XStream XSTREAM = new XStream2(XStream2.getDefaultDriver()); + + static { + XSTREAM.processAnnotations(new Class[] { DatadogGlobalConfiguration.class, DatadogApiConfiguration.class }); + } + + private static DatadogGlobalConfiguration parseConfigurationFromResource(String resourceName) { + URL resource = DatadogGlobalConfigurationTest.class.getResource(resourceName); + return (DatadogGlobalConfiguration) XSTREAM.fromXML(resource); + } }