Skip to content

Commit

Permalink
update configuration as code test
Browse files Browse the repository at this point in the history
  • Loading branch information
jetersen committed Jan 17, 2022
1 parent 9a03243 commit 2b81645
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package org.jenkinsci.plugins.stashNotifier;

import io.jenkins.plugins.casc.ConfigurationAsCode;
import org.apache.commons.io.IOUtils;
import io.jenkins.plugins.casc.ConfigurationContext;
import io.jenkins.plugins.casc.ConfiguratorRegistry;
import io.jenkins.plugins.casc.misc.ConfiguredWithCode;
import io.jenkins.plugins.casc.misc.JenkinsConfiguredWithCodeRule;
import io.jenkins.plugins.casc.model.CNode;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;

import static io.jenkins.plugins.casc.misc.Util.getUnclassifiedRoot;
import static io.jenkins.plugins.casc.misc.Util.toStringFromYamlFile;
import static io.jenkins.plugins.casc.misc.Util.toYamlString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.core.Is.is;

public class ConfigAsCodeTest {
@Rule
public JenkinsRule rule = new JenkinsRule();
@Rule public JenkinsConfiguredWithCodeRule rule = new JenkinsConfiguredWithCodeRule();

@Test
@ConfiguredWithCode("configuration-as-code.yml")
public void should_support_jcasc_from_yaml() throws Exception {
URL configFileUrl = ConfigAsCodeTest.class.getResource(getClass().getSimpleName() + "/configuration-as-code.yml");
ConfigurationAsCode.get().configure(configFileUrl.toString());
StashNotifier.DescriptorImpl stashNotifierConfig = rule.jenkins.getDescriptorByType(StashNotifier.DescriptorImpl.class);

assertThat(stashNotifierConfig.isConsiderUnstableAsSuccess(), equalTo(true));
Expand All @@ -34,26 +33,16 @@ public void should_support_jcasc_from_yaml() throws Exception {
}

@Test
@ConfiguredWithCode("configuration-as-code.yml")
public void should_support_jcasc_to_yaml() throws Exception {
StashNotifier.DescriptorImpl stashNotifierConfig = rule.jenkins.getDescriptorByType(StashNotifier.DescriptorImpl.class);

stashNotifierConfig.setConsiderUnstableAsSuccess(true);
stashNotifierConfig.setCredentialsId("bitbucket-credentials");
stashNotifierConfig.setDisableInprogressNotification(true);
stashNotifierConfig.setIgnoreUnverifiedSsl(true);
stashNotifierConfig.setIncludeBuildNumberInKey(true);
stashNotifierConfig.setPrependParentProjectKey(true);
stashNotifierConfig.setStashRootUrl("https://my.company.intranet/bitbucket");
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
ConfigurationContext context = new ConfigurationContext(registry);
CNode yourAttribute = getUnclassifiedRoot(context).get("notifyBitbucket");

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ConfigurationAsCode.get().export(outputStream);
String exportedYaml = outputStream.toString("UTF-8");
String exported = toYamlString(yourAttribute);

InputStream yamlStream = getClass().getResourceAsStream(getClass().getSimpleName() + "/configuration-as-code.yml");
String expectedYaml = IOUtils.toString(yamlStream, "UTF-8")
.replaceAll("\r\n?", "\n")
.replace("unclassified:\n", "");
String expected = toStringFromYamlFile(this, "configuration-as-code-expected.yml");

assertThat(exportedYaml, containsString(expectedYaml));
assertThat(exported, is(expected));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
considerUnstableAsSuccess: true
credentialsId: "bitbucket-credentials"
disableInprogressNotification: true
ignoreUnverifiedSsl: true
includeBuildNumberInKey: true
prependParentProjectKey: true
stashRootUrl: "https://my.company.intranet/bitbucket"

0 comments on commit 2b81645

Please sign in to comment.