io.jenkins.plugins.casc.model.Sequence Java Examples

The following examples show how to use io.jenkins.plugins.casc.model.Sequence. 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: DataBoundConfiguratorTest.java    From configuration-as-code-plugin with MIT License 6 votes vote down vote up
@Test
@Issue("PR #838, Issue #222")
public void export_mapping_should_not_be_null() throws Exception {
    j.createFreeStyleProject("testJob1");
    ConfigurationAsCode casc = ConfigurationAsCode.get();
    casc.configure(this.getClass().getResource("DataBoundDescriptorNonNull.yml")
            .toString());

    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    final Mapping configNode = getJenkinsRoot(context);
    final CNode viewsNode = configNode.get("views");
    Mapping listView = viewsNode.asSequence().get(1).asMapping().get("list").asMapping();
    Mapping otherListView = viewsNode.asSequence().get(2).asMapping().get("list").asMapping();
    Sequence listViewColumns = listView.get("columns").asSequence();
    Sequence otherListViewColumns = otherListView.get("columns").asSequence();
    assertNotNull(listViewColumns);
    assertEquals(6, listViewColumns.size());
    assertNotNull(otherListViewColumns);
    assertEquals(7, otherListViewColumns.size());
    assertEquals("loggedInUsersCanDoAnything", configNode.getScalarValue("authorizationStrategy"));
    assertEquals("plainText", configNode.getScalarValue("markupFormatter"));
}
 
Example #2
Source File: GitToolConfigurator.java    From git-client-plugin with MIT License 6 votes vote down vote up
@NonNull
private List<ToolProperty<?>> instantiateProperties(@CheckForNull CNode props, @NonNull ConfigurationContext context) throws ConfiguratorException {
    List<ToolProperty<?>> toolProperties = new ArrayList<>();
    if (props == null) {
        return toolProperties;
    }
    final Configurator<ToolProperty> configurator = context.lookupOrFail(ToolProperty.class);
    if (props instanceof Sequence) {
        Sequence s = (Sequence) props;
        for (CNode cNode : s) {
            toolProperties.add(configurator.configure(cNode, context));
        }
    } else {
        toolProperties.add(configurator.configure(props, context));
    }
    return toolProperties;
}
 
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: StepGenerator.java    From simple-pull-request-job-plugin with Apache License 2.0 5 votes vote down vote up
private Sequence doMappingForSequence(List<Object> objects) throws ConversionException {
    Sequence sequence = new Sequence();

    for (Object object : objects) {
        if (object instanceof Map) {
            sequence.add(doMappingForMap((Map<String, Object>) object));
        } else if (object instanceof Sequence) {
            sequence.add(doMappingForSequence((List) object));
        } else {
            sequence.add(doMappingForScalar(object));
        }
    }

    return sequence;
}
 
Example #5
Source File: Attribute.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
public CNode describe(Owner instance, ConfigurationContext context) throws ConfiguratorException {
    final Configurator c = context.lookup(type);
    if (c == null) {
        return new Scalar("FAILED TO EXPORT\n" + instance.getClass().getName()+"#"+name +
                ": No configurator found for type " + type);
    }
    try {
        Object o = getValue(instance);
        if (o == null) {
            return null;
        }

        // In Export we sensitive only those values which do not get rendered as secrets
        boolean shouldBeMasked = isSecret(instance);
        if (multiple) {
            Sequence seq = new Sequence();
            if (o.getClass().isArray()) o = Arrays.asList((Object[]) o);
            if (o instanceof Iterable) {
                for (Object value : (Iterable) o) {
                    seq.add(_describe(c, context, value, shouldBeMasked));
                }
            } else {
                LOGGER.log(Level.FINE, o.getClass().toString() + " is not iterable");
            }
            return seq;
        }
        return _describe(c, context, o, shouldBeMasked);
    } catch (Exception | /* Jenkins.getDescriptorOrDie */AssertionError e) {
        // Don't fail the whole export, prefer logging this error
        LOGGER.log(Level.WARNING, "Failed to export", e);
        return new Scalar("FAILED TO EXPORT\n" + instance.getClass().getName() + "#" + name + ": "
            + printThrowable(e));
    }
}
 
Example #6
Source File: Attribute.java    From configuration-as-code-plugin with MIT License 5 votes vote down vote up
/**
 * This function is for the JSONSchemaGeneration
 * @param instance Owner Instance
 * @param context Context to be passed
 * @return CNode object describing the structure of the node
 */
public CNode describeForSchema (Owner instance, ConfigurationContext context) {
    final Configurator c = context.lookup(type);
    if (c == null) {
        return new Scalar("FAILED TO EXPORT\n" + instance.getClass().getName()+"#"+name +
            ": No configurator found for type " + type);
    }
    try {
        Object o = getType();
        if (o == null) {
            return null;
        }

        // In Export we sensitive only those values which do not get rendered as secrets
        boolean shouldBeMasked = isSecret(instance);
        if (multiple) {
            Sequence seq = new Sequence();
            if (o.getClass().isArray()) o = Arrays.asList(o);
            if (o instanceof Iterable) {
                for (Object value : (Iterable) o) {
                    seq.add(_describe(c, context, value, shouldBeMasked));
                }
            }
            return seq;
        }
        return _describe(c, context, o, shouldBeMasked);
    } catch (Exception e) {
        // Don't fail the whole export, prefer logging this error
        LOGGER.log(Level.WARNING, "Failed to export", e);
        return new Scalar("FAILED TO EXPORT\n" + instance.getClass().getName() + "#" + name + ": "
            + printThrowable(e));
    }
}
 
Example #7
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 #8
Source File: GitHubAppCredentialsJCasCCompatibilityTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
@Test
public void should_support_configuration_export() throws Exception {
    Sequence credentials = getCredentials();
    CNode githubApp = credentials.get(0).asMapping().get("gitHubApp");

    String exported = toYamlString(githubApp)
            // replace secret with a constant value
            .replaceAll("privateKey: .*", "privateKey: \"some-secret-value\"");

    String expected = toStringFromYamlFile(this, "github-app-jcasc-minimal-expected-export.yaml");

    assertThat(exported, is(expected));
}
 
Example #9
Source File: GitHubAppCredentialsJCasCCompatibilityTest.java    From github-branch-source-plugin with MIT License 5 votes vote down vote up
private Sequence getCredentials() throws Exception {
    CredentialsRootConfigurator root = Jenkins.get()
            .getExtensionList(CredentialsRootConfigurator.class).get(0);

    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);
    Mapping configNode = Objects
            .requireNonNull(root.describe(root.getTargetComponent(context), context)).asMapping();
    Mapping domainCredentials = configNode
            .get("system").asMapping().get("domainCredentials")
            .asSequence()
            .get(0).asMapping();
    return domainCredentials.get("credentials").asSequence();
}
 
Example #10
Source File: GitToolConfiguratorJenkinsRuleTest.java    From git-client-plugin with MIT License 5 votes vote down vote up
@Test
public void testDescribeGitTool() throws Exception {
    String gitName = "git-2.19.1-name";
    String gitHome = "/opt/git-2.19.1/bin/git";

    ConfiguratorRegistry registry = ConfiguratorRegistry.get();
    ConfigurationContext context = new ConfigurationContext(registry);

    List<ToolProperty<ToolInstallation>> gitToolProperties = new ArrayList<>();
    List<ToolInstaller> toolInstallers = new ArrayList<>();
    ToolInstaller toolInstaller = new ZipExtractionInstaller("tool-label", "tool-url", "tool-subdir");
    toolInstallers.add(toolInstaller);
    InstallSourceProperty installSourceProperty = new InstallSourceProperty(toolInstallers);
    gitToolProperties.add(installSourceProperty);

    GitTool gitTool = new GitTool(gitName, gitHome, gitToolProperties);

    CNode cNode = gitToolConfigurator.describe(gitTool, context);

    assertThat(cNode, is(notNullValue()));
    assertThat(cNode.getType(), is(CNode.Type.MAPPING));
    Mapping cNodeMapping = cNode.asMapping();
    assertThat(cNodeMapping.getScalarValue("name"), is(gitName));
    assertThat(cNodeMapping.getScalarValue("home"), is(gitHome));

    //         properties:
    //          - installSource:
    //              installers:
    //                - zip:
    //                    label: "tool-label"
    //                    subdir: "tool-subdir"
    //                    url: "tool-url"
    Sequence propertiesSequence = cNodeMapping.get("properties").asSequence();                             // properties:
    Mapping installSourceMapping = propertiesSequence.get(0).asMapping().get("installSource").asMapping(); //  - installSource:
    Sequence installersSequence = installSourceMapping.get("installers").asSequence();                     //      installers:
    Mapping zipMapping = installersSequence.get(0).asMapping().get("zip").asMapping();                     //        - zip:
    assertThat(zipMapping.getScalarValue("label"), is("tool-label"));
    assertThat(zipMapping.getScalarValue("subdir"), is("tool-subdir"));
    assertThat(zipMapping.getScalarValue("url"), is("tool-url"));
}
 
Example #11
Source File: ModelConstructor.java    From configuration-as-code-plugin with MIT License 4 votes vote down vote up
@Override
protected List createDefaultList(int initSize) {
    // respect order from YAML document
    return new Sequence(initSize);
}
 
Example #12
Source File: ModelConstructor.java    From configuration-as-code-plugin with MIT License 4 votes vote down vote up
@Override
protected void constructSequenceStep2(SequenceNode node, Collection collection) {
    ((Sequence) collection).setSource(getSource(node));
    super.constructSequenceStep2(node, collection);
}