Search in sources :

Example 11 with Configurator

use of io.jenkins.plugins.casc.Configurator in project configuration-as-code-plugin by jenkinsci.

the class UpdateCenterConfiguratorTest method shouldSetUpdateCenterSites.

@Test
@ConfiguredWithCode("UpdateCenter.yml")
public void shouldSetUpdateCenterSites() throws Exception {
    UpdateCenter updateCenter = j.jenkins.getUpdateCenter();
    List<UpdateSite> sites = updateCenter.getSites();
    assertEquals(2, sites.size());
    UpdateSite siteOne = sites.get(0);
    assertEquals("default", siteOne.getId());
    assertEquals("https://updates.jenkins.io/update-center.json", siteOne.getUrl());
    UpdateSite siteTwo = sites.get(1);
    assertEquals("experimental", siteTwo.getId());
    assertEquals("https://updates.jenkins.io/experimental/update-center.json", siteTwo.getUrl());
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final Configurator c = context.lookupOrFail(UpdateCenter.class);
    final CNode node = c.describe(updateCenter, context);
    assertNotNull(node);
    Mapping site1 = node.asMapping().get("sites").asSequence().get(1).asMapping();
    assertEquals("experimental", site1.getScalarValue("id"));
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) UpdateCenter(hudson.model.UpdateCenter) Configurator(io.jenkins.plugins.casc.Configurator) Mapping(io.jenkins.plugins.casc.model.Mapping) UpdateSite(hudson.model.UpdateSite) Test(org.junit.Test) ConfiguredWithCode(io.jenkins.plugins.casc.misc.ConfiguredWithCode)

Example 12 with Configurator

use of io.jenkins.plugins.casc.Configurator in project configuration-as-code-plugin by jenkinsci.

the class DataBoundConfiguratorTest method exportWithSets.

@Test
@SuppressWarnings("unchecked")
public void exportWithSets() throws Exception {
    HashSet<String> set = new HashSet<>();
    set.add("foo");
    Bar bar = new Bar(set);
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    final Configurator c = registry.lookupOrFail(Bar.class);
    final ConfigurationContext context = new ConfigurationContext(registry);
    CNode node = c.describe(bar, context);
    assertNotNull(node);
    assertTrue(node instanceof Mapping);
    Mapping map = (Mapping) node;
    assertEquals(map.get("strings").toString(), "[foo]");
    assertEquals(Util.toYamlString(map.get("strings")).trim(), "- \"foo\"");
    assertFalse(map.containsKey("other"));
    // now with two elements
    set.add("bar");
    node = c.describe(bar, context);
    assertNotNull(node);
    assertTrue(node instanceof Mapping);
    map = (Mapping) node;
    assertEquals(map.get("strings").toString(), "[bar, foo]");
    assertEquals(Util.toYamlString(map.get("strings")).trim(), "- \"bar\"\n- \"foo\"");
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) Configurator(io.jenkins.plugins.casc.Configurator) Mapping(io.jenkins.plugins.casc.model.Mapping) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 13 with Configurator

use of io.jenkins.plugins.casc.Configurator in project configuration-as-code-plugin by jenkinsci.

the class DataBoundConfiguratorTest method exportYaml.

@Test
public void exportYaml() throws Exception {
    Foo foo = new Foo("foo", true, 42);
    foo.setZot("zot");
    foo.setDbl(12.34);
    // whole numbers are exported as "<number>.0"
    foo.setFlt(1f);
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    final Configurator c = registry.lookupOrFail(Foo.class);
    final ConfigurationContext context = new ConfigurationContext(registry);
    final CNode node = c.describe(foo, context);
    assertNotNull(node);
    assertTrue(node instanceof Mapping);
    Mapping map = (Mapping) node;
    assertEquals(map.get("foo").toString(), "foo");
    assertEquals(map.get("bar").toString(), "true");
    assertEquals(map.get("qix").toString(), "42");
    assertEquals(map.get("zot").toString(), "zot");
    assertEquals(map.get("dbl").toString(), "12.34");
    assertEquals(map.get("flt").toString(), "1.0");
    assertEquals(Util.toYamlString(map.get("foo")).trim(), "\"foo\"");
    assertEquals(Util.toYamlString(map.get("bar")).trim(), "true");
    assertEquals(Util.toYamlString(map.get("qix")).trim(), "42");
    assertEquals(Util.toYamlString(map.get("zot")).trim(), "\"zot\"");
    assertEquals(Util.toYamlString(map.get("dbl")).trim(), "\"12.34\"");
    assertEquals(Util.toYamlString(map.get("flt")).trim(), "\"1.0\"");
    assertFalse(map.containsKey("other"));
}
Also used : CNode(io.jenkins.plugins.casc.model.CNode) ConfiguratorRegistry(io.jenkins.plugins.casc.ConfiguratorRegistry) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) Configurator(io.jenkins.plugins.casc.Configurator) Mapping(io.jenkins.plugins.casc.model.Mapping) Test(org.junit.Test)

Example 14 with Configurator

use of io.jenkins.plugins.casc.Configurator in project configuration-as-code-plugin by jenkinsci.

the class MavenConfigurator method describe.

@NonNull
@Override
public Set<Attribute<GlobalMavenConfig, ?>> describe() {
    final Set<Attribute<GlobalMavenConfig, ?>> attributes = super.describe();
    final Descriptor descriptor = Jenkins.get().getDescriptorOrDie(Maven.class);
    final Configurator<Descriptor> task = new DescriptorConfigurator(descriptor);
    for (Attribute attribute : task.describe()) {
        attributes.add(new Attribute<GlobalMavenConfig, Object>(attribute.getName(), attribute.getType()).multiple(attribute.isMultiple()).getter(g -> attribute.getValue(descriptor)).setter((g, v) -> attribute.setValue(descriptor, v)));
    }
    return attributes;
}
Also used : Restricted(org.kohsuke.accmod.Restricted) Maven(hudson.tasks.Maven) Descriptor(hudson.model.Descriptor) ConfigurationContext(io.jenkins.plugins.casc.ConfigurationContext) Jenkins(jenkins.model.Jenkins) Configurator(io.jenkins.plugins.casc.Configurator) Set(java.util.Set) GlobalMavenConfig(jenkins.mvn.GlobalMavenConfig) NoExternalUse(org.kohsuke.accmod.restrictions.NoExternalUse) CNode(io.jenkins.plugins.casc.model.CNode) Mapping(io.jenkins.plugins.casc.model.Mapping) BaseConfigurator(io.jenkins.plugins.casc.BaseConfigurator) DescriptorConfigurator(io.jenkins.plugins.casc.impl.configurators.DescriptorConfigurator) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Attribute(io.jenkins.plugins.casc.Attribute) Extension(hudson.Extension) CheckForNull(edu.umd.cs.findbugs.annotations.CheckForNull) GlobalConfiguration(jenkins.model.GlobalConfiguration) Attribute(io.jenkins.plugins.casc.Attribute) DescriptorConfigurator(io.jenkins.plugins.casc.impl.configurators.DescriptorConfigurator) Descriptor(hudson.model.Descriptor) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 15 with Configurator

use of io.jenkins.plugins.casc.Configurator in project configuration-as-code-plugin by jenkinsci.

the class PrimitiveConfiguratorTest method _Integer.

@Test
public void _Integer() throws Exception {
    Configurator c = registry.lookupOrFail(Integer.class);
    final Object value = c.configure(new Scalar("123"), context);
    assertEquals(123, (int) value);
}
Also used : Configurator(io.jenkins.plugins.casc.Configurator) Scalar(io.jenkins.plugins.casc.model.Scalar) Test(org.junit.Test)

Aggregations

Configurator (io.jenkins.plugins.casc.Configurator)19 Test (org.junit.Test)16 CNode (io.jenkins.plugins.casc.model.CNode)10 ConfigurationContext (io.jenkins.plugins.casc.ConfigurationContext)9 Mapping (io.jenkins.plugins.casc.model.Mapping)9 ConfiguratorRegistry (io.jenkins.plugins.casc.ConfiguratorRegistry)8 Scalar (io.jenkins.plugins.casc.model.Scalar)8 ConfiguredWithCode (io.jenkins.plugins.casc.misc.ConfiguredWithCode)5 Jenkins (jenkins.model.Jenkins)4 ProxyConfiguration (hudson.ProxyConfiguration)2 BaseConfigurator (io.jenkins.plugins.casc.BaseConfigurator)2 DescriptorConfigurator (io.jenkins.plugins.casc.impl.configurators.DescriptorConfigurator)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 AdminWhitelistRule (jenkins.security.s2m.AdminWhitelistRule)2 MasterKillSwitchConfiguration (jenkins.security.s2m.MasterKillSwitchConfiguration)2 Issue (org.jvnet.hudson.test.Issue)2 CheckForNull (edu.umd.cs.findbugs.annotations.CheckForNull)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 Extension (hudson.Extension)1