Skip to content

Commit

Permalink
Additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog committed Sep 18, 2024
1 parent 5d76891 commit 9efa308
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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,"
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 9efa308

Please sign in to comment.