Java Code Examples for io.jenkins.plugins.casc.model.Mapping#put()

The following examples show how to use io.jenkins.plugins.casc.model.Mapping#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Configurator.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
/**
 * Describe Structure of the attributes, as required by the schema.
 * @param instance
 * @param context
 * @since 1.35
 * @return CNode describing the attributes.
 */
@CheckForNull
default CNode describeStructure(T instance, ConfigurationContext context)
    throws Exception {
    Mapping mapping = new Mapping();
    for (Attribute attribute : getAttributes()) {
        if (context.getMode().equals("JSONSchema")) {
            attribute.setJsonSchema(true);
        }
        CNode value = attribute.describeForSchema(instance, context);
        if (value != null) {
            mapping.put(attribute.getName(), attribute.getType().getSimpleName());
        }
    }
    return mapping;
}
 
Example 2
Source File: DataBoundConfiguratorTest.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
@Test
public void shouldThrowConfiguratorException() {
    Mapping config = new Mapping();
    config.put("foo", "foo");
    config.put("bar", "abcd");
    config.put("qix", "99");
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    try {
        registry.lookupOrFail(Foo.class).configure(config, new ConfigurationContext(registry));
        fail("above action is excepted to throw ConfiguratorException!");
    } catch (ConfiguratorException e) {
        assertThat(e.getMessage(), is("foo: Failed to construct instance of class io.jenkins.plugins.casc.impl.configurators.DataBoundConfiguratorTest$Foo.\n" +
                " Constructor: public io.jenkins.plugins.casc.impl.configurators.DataBoundConfiguratorTest$Foo(java.lang.String,boolean,int).\n" +
                " Arguments: [java.lang.String, java.lang.Boolean, java.lang.Integer].\n" +
                " Expected Parameters: foo java.lang.String, bar boolean, qix int"));
    }
}
 
Example 3
Source File: GitToolConfigurator.java    From git-client-plugin with MIT License 6 votes vote down vote up
@CheckForNull
@Override
public CNode describe(GitTool instance, ConfigurationContext context) throws Exception {
    Mapping mapping = new Mapping();
    if (instance instanceof JGitTool) {
        mapping.put("name", JGitTool.MAGIC_EXENAME);
    } else if (instance instanceof JGitApacheTool) {
        mapping.put("name", JGitApacheTool.MAGIC_EXENAME);
    } else if (instance != null) {
        mapping.put("name", instance.getName());
        mapping.put("home", instance.getHome());
    }
    if (context != null && instance != null && instance.getProperties() != null && !instance.getProperties().isEmpty()) {
        final Configurator<ToolProperty> configurator = context.lookupOrFail(ToolProperty.class);
        Sequence s = new Sequence(instance.getProperties().size());
        for (ToolProperty<?> property : instance.getProperties()) {
            s.add(configurator.describe(property, context));
        }
        mapping.put("properties", s);
    }
    return mapping;
}
 
Example 4
Source File: DataBoundConfiguratorTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
public void shouldNotLogSecrets() throws Exception {
    Mapping config = new Mapping();
    config.put("secret", "mySecretValue");
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    registry.lookupOrFail(SecretHolder.class).configure(config, new ConfigurationContext(registry));
    assertLogContains(logging, "secret");
    assertNotInLog(logging, "mySecretValue");
}
 
Example 5
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testInstanceGitTool() throws Exception {
    Mapping mapping = new Mapping();
    String gitHome = "testGitHome";
    String gitName = "testGitName";
    mapping.put("home", gitHome);
    mapping.put("name", gitName);
    ConfigurationContext context = new ConfigurationContext(null);
    GitTool gitTool = gitToolConfigurator.instance(mapping, context);
    assertThat(gitTool, is(instanceOf(GitTool.class)));
    assertThat(gitTool, is(not(instanceOf(JGitTool.class))));
    assertThat(gitTool, is(not(instanceOf(JGitApacheTool.class))));
    assertThat(gitTool.getHome(), is(gitHome));
    assertThat(gitTool.getName(), is(gitName));
}
 
Example 6
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test(expected = ConfiguratorException.class)
public void testInstanceGitToolWithoutHome() throws Exception {
    Mapping mapping = new Mapping();
    mapping.put("name", "testGitName"); // No home mapping defined
    ConfigurationContext context = new ConfigurationContext(null);
    gitToolConfigurator.instance(mapping, context);
}
 
Example 7
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testInstanceJGitApacheToolWithHome() throws Exception {
    Mapping mapping = new Mapping();
    mapping.put("name", JGitApacheTool.MAGIC_EXENAME);
    mapping.put("home", "unused-value-for-home"); // Will log a message
    ConfigurationContext context = new ConfigurationContext(null);
    GitTool gitTool = gitToolConfigurator.instance(mapping, context);
    assertThat(gitTool, is(instanceOf(JGitApacheTool.class)));
    assertThat(gitTool, is(not(instanceOf(JGitTool.class))));
}
 
Example 8
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testInstanceJGitApacheTool() throws Exception {
    Mapping mapping = new Mapping();
    mapping.put("name", JGitApacheTool.MAGIC_EXENAME);
    ConfigurationContext context = new ConfigurationContext(null);
    GitTool gitTool = gitToolConfigurator.instance(mapping, context);
    assertThat(gitTool, is(instanceOf(JGitApacheTool.class)));
    assertThat(gitTool, is(not(instanceOf(JGitTool.class))));
}
 
Example 9
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testInstanceJGitToolWithHome() throws Exception {
    Mapping mapping = new Mapping();
    mapping.put("name", JGitTool.MAGIC_EXENAME);
    mapping.put("home", "unused-value-for-home"); // Will log a message
    ConfigurationContext context = new ConfigurationContext(null);
    GitTool gitTool = gitToolConfigurator.instance(mapping, context);
    assertThat(gitTool, is(instanceOf(JGitTool.class)));
    assertThat(gitTool, is(not(instanceOf(JGitApacheTool.class))));
}
 
Example 10
Source File: GitToolConfiguratorTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testInstanceJGitTool() throws Exception {
    Mapping mapping = new Mapping();
    mapping.put("name", JGitTool.MAGIC_EXENAME);
    ConfigurationContext context = new ConfigurationContext(null);
    GitTool gitTool = gitToolConfigurator.instance(mapping, context);
    assertThat(gitTool, is(instanceOf(JGitTool.class)));
    assertThat(gitTool, is(not(instanceOf(JGitApacheTool.class))));
}
 
Example 11
Source File: DataBoundConfiguratorTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
@Issue("SECURITY-1497")
public void shouldNotLogSecretsForUndefinedConstructors() throws Exception {
    Mapping config = new Mapping();
    config.put("secret", "mySecretValue");
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    registry.lookupOrFail(SecretHolderWithString.class).configure(config, new ConfigurationContext(registry));
    assertLogContains(logging, "secret");
    assertNotInLog(logging, "mySecretValue");
}
 
Example 12
Source File: StepGenerator.java    From simple-pull-request-job-plugin with Apache License 2.0 5 votes vote down vote up
private Mapping doMappingForMap(Map<String, Object> map) throws ConversionException {
    Mapping mapping = new Mapping();

    for (Map.Entry<String, Object> entry : map.entrySet()) {
        if (entry.getValue() instanceof Map) {
            mapping.put(entry.getKey(), doMappingForMap((Map<String, Object>) entry.getValue()));
        } else if (entry.getValue() instanceof List) {
            mapping.put(entry.getKey(), doMappingForSequence((List) entry.getValue()));
        } else {
            mapping.put(entry.getKey(), doMappingForScalar(entry.getValue()));
        }
    }

    return mapping;
}
 
Example 13
Source File: DataBoundConfiguratorTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
public void configureWithSets() throws Exception {
    Mapping config = new Mapping();
    Sequence sequence = new Sequence();
    sequence.add(new Scalar("bar"));
    sequence.add(new Scalar("foo"));
    config.put("strings", sequence);
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    final Bar configured = (Bar) registry.lookupOrFail(Bar.class).configure(config, new ConfigurationContext(registry));
    Set<String> strings = configured.getStrings();
    assertTrue(strings.contains("foo"));
    assertTrue(strings.contains("bar"));
    assertFalse(strings.contains("baz"));
}
 
Example 14
Source File: DataBoundConfiguratorTest.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
@Test
public void configure_databound() throws Exception {
    Mapping config = new Mapping();
    config.put("foo", "foo");
    config.put("bar", "true");
    config.put("qix", "123");
    config.put("zot", "DataBoundSetter");
    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    final Foo configured = (Foo) registry.lookupOrFail(Foo.class).configure(config, new ConfigurationContext(registry));
    assertEquals("foo", configured.foo);
    assertTrue(configured.bar);
    assertEquals(123, configured.qix);
    assertEquals("DataBoundSetter", configured.zot);
    assertThat(configured.initialized, is(true));
}
 
Example 15
Source File: Configurator.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
/**
 * Describe a component as a Configuration Nodes {@link CNode} to be exported as yaml.
 * Only export attributes which are <b>not</b> set to default value.
 */
@CheckForNull
default CNode describe(T instance, ConfigurationContext context) throws Exception {
    Mapping mapping = new Mapping();
    for (Attribute attribute : getAttributes()) {
        CNode value = attribute.describe(instance, context);
        if (value != null) {
            mapping.put(attribute.getName(), value);
        }
    }
    return mapping;
}
 
Example 16
Source File: GlobalConfigurationCategoryConfigurator.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
private void describe(Descriptor d, Mapping mapping, ConfigurationContext context) {
    final DescriptorConfigurator c = new DescriptorConfigurator(d);
    try {
        final CNode node = c.describe(d, context);
        if (node != null) mapping.put(c.getName(), node);
    } catch (Exception e) {
        final Scalar scalar = new Scalar(
            "FAILED TO EXPORT\n" + d.getClass().getName() + " : " + printThrowable(e));
        mapping.put(c.getName(), scalar);
    }
}
 
Example 17
Source File: BaseConfigurator.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
protected @NonNull Mapping compare(T instance, T reference, ConfigurationContext context) throws Exception {

        Mapping mapping = new Mapping();
        for (Attribute attribute : getAttributes()) {
            if (attribute.equals(instance, reference)) continue;
            mapping.put(attribute.getName(), attribute.describe(instance, context));
        }
        return mapping;
    }
 
Example 18
Source File: DataBoundConfigurator.java    From configuration-as-code-plugin with MIT License 4 votes vote down vote up
@CheckForNull
@Override
public CNode describe(T instance, ConfigurationContext context) throws Exception {

    // Here we assume a correctly designed DataBound Object will have required attributes set by DataBoundConstructor
    // and all others using DataBoundSetters. So constructor parameters for sure are part of the description, others
    // need to be compared with default values.

    // Build same object with only constructor parameters
    final Constructor constructor = getDataBoundConstructor();

    final Parameter[] parameters = constructor.getParameters();
    final String[] names = ClassDescriptor.loadParameterNames(constructor);
    final Attribute[] attributes = new Attribute[parameters.length];
    final Object[] args = new Object[parameters.length];
    for (int i = 0; i < parameters.length; i++) {
        final Parameter p = parameters[i];
        final Attribute a = createAttribute(names[i], TypePair.of(p));
        if (a != null) {
            Object value = a.getValue(instance);
            if (value != null) {
                Object converted = Stapler.CONVERT_UTILS.convert(value, a.getType());
                if (converted instanceof Collection ||
                    p.getType().isArray() || !a.isMultiple()) {
                    args[i] = converted;
                } else if (Set.class.isAssignableFrom(p.getType())) {
                    args[i] = Collections.singleton(converted);
                } else {
                    args[i] = Collections.singletonList(converted);
                }
            }
            if (args[i] == null && p.getType().isPrimitive()) {
                args[i] = defaultValue(p.getType());
            }
            attributes[i] = a;
        }
    }

    T ref = (T) constructor.newInstance(args);

    // compare instance with this "default" object
    Mapping mapping = compare(instance, ref, context);

    // add constructor parameters
    for (int i = 0; i < parameters.length; i++) {
        if (args[i] == null) continue;
        mapping.put(names[i], attributes[i].describe(instance, context));
    }

    return mapping;
}