use of io.jenkins.plugins.casc.model.Mapping in project configuration-as-code-plugin by jenkinsci.
the class DataBoundConfiguratorTest method packageParametersAreNonnullByDefault.
@Test
public void packageParametersAreNonnullByDefault() {
Mapping config = new Mapping();
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
String expectedMessage = "string is required to configure class io.jenkins.plugins.casc.impl.configurators.nonnull.nonnullparampackage.PackageParametersAreNonnullByDefault";
ConfiguratorException exception = assertThrows(ConfiguratorException.class, () -> registry.lookupOrFail(PackageParametersAreNonnullByDefault.class).configure(config, new ConfigurationContext(registry)));
assertThat(exception.getMessage(), is(expectedMessage));
}
use of io.jenkins.plugins.casc.model.Mapping 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"));
}
use of io.jenkins.plugins.casc.model.Mapping in project configuration-as-code-plugin by jenkinsci.
the class CredentialsTest method testExportSSHCredentials.
@ConfiguredWithCode("GlobalCredentials.yml")
@Test
public void testExportSSHCredentials() throws Exception {
ConfiguratorRegistry registry = ConfiguratorRegistry.get();
ConfigurationContext context = new ConfigurationContext(registry);
CredentialsRootConfigurator root = ExtensionList.lookupSingleton(CredentialsRootConfigurator.class);
CNode node = root.describe(root.getTargetComponent(context), context);
assertNotNull(node);
final Mapping mapping = node.asMapping();
Mapping sshCredential = mapping.get("system").asMapping().get("domainCredentials").asSequence().get(0).asMapping().get("credentials").asSequence().get(1).asMapping().get("basicSSHUserPrivateKey").asMapping();
assertThat(sshCredential.getScalarValue("scope"), is("SYSTEM"));
assertThat(sshCredential.getScalarValue("id"), is("agent-private-key"));
assertThat(sshCredential.getScalarValue("username"), is("agentuser"));
String passphrase = sshCredential.getScalarValue("passphrase");
assertThat(passphrase, not("password"));
assertThat(requireNonNull(Secret.decrypt(passphrase), "Failed to decrypt the password from " + passphrase).getPlainText(), is("password"));
String sshKeyExported = sshCredential.get("privateKeySource").asMapping().get("directEntry").asMapping().get("privateKey").asScalar().getValue();
assertThat(sshKeyExported, not("sp0ds9d+skkfjf"));
assertThat(requireNonNull(Secret.decrypt(sshKeyExported)).getPlainText(), is("sp0ds9d+skkfjf"));
}
use of io.jenkins.plugins.casc.model.Mapping in project configuration-as-code-plugin by jenkinsci.
the class GlobalConfigurationCategoryConfigurator method describe.
@CheckForNull
@Override
public CNode describe(GlobalConfigurationCategory instance, ConfigurationContext context) {
final Mapping mapping = new Mapping();
Jenkins.get().getExtensionList(Descriptor.class).stream().filter(this::filterDescriptors).forEach(d -> describe(d, mapping, context));
return mapping;
}
use of io.jenkins.plugins.casc.model.Mapping in project configuration-as-code-plugin by jenkinsci.
the class YamlUtils method loadFrom.
/**
* Load configuration-as-code model from a snakeyaml Node
*/
private static Mapping loadFrom(Node node) {
final ModelConstructor constructor = new ModelConstructor();
constructor.setComposer(new Composer(null, null) {
@Override
public Node getSingleNode() {
return node;
}
});
return (Mapping) constructor.getSingleData(Mapping.class);
}
Aggregations