All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xceptance.xlt.nocoding.parser.yaml.YamlParser Maven / Gradle / Ivy

Go to download

A library based on XLT to run Web test cases that are written in either YAML or CSV format.

The newest version!
/*
 * Copyright (c) 2013-2023 Xceptance Software Technologies GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xceptance.xlt.nocoding.parser.yaml;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.SequenceNode;
import org.yaml.snakeyaml.parser.ParserException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.xceptance.xlt.nocoding.command.Command;
import com.xceptance.xlt.nocoding.parser.Parser;
import com.xceptance.xlt.nocoding.parser.yaml.command.action.ActionParser;
import com.xceptance.xlt.nocoding.parser.yaml.command.store.StoreParser;
import com.xceptance.xlt.nocoding.parser.yaml.command.storeDefault.StoreDefaultParser;
import com.xceptance.xlt.nocoding.util.Constants;

/**
 * Reads a Yaml file and generates a list filled with {@link Command}s out of the Yaml file.
 *
 * @author ckeiner
 */
public class YamlParser implements Parser
{

    /**
     * Parses the content of the Reader reader to a list of {@link Command}s
     *
     * @param reader
     *            The Reader that contains {@link Command}s in a parser dependent format
     * @return A list of {@link Command}s
     * @throws IOException
     *             If a {@link Reader} or {@link JsonParser} cannot be created or the file cannot be mapped to a
     *             {@link JsonNode}.
     */
    @Override
    public List parse(final Reader reader) throws IOException
    {
        final List commands = new ArrayList();
        // Parse the Yaml with snakeyml
        final Yaml yaml = new Yaml();
        // final Object loadedYaml = yaml.load(createReader(pathToFile));
        final Node root = yaml.compose(reader);
        if (root instanceof SequenceNode)
        {
            final List commandWrappersList = ((SequenceNode) root).getValue();
            for (final Node singleCommandWrapper : commandWrappersList)
            {
                if (singleCommandWrapper instanceof MappingNode)
                {
                    final List commandMapping = ((MappingNode) singleCommandWrapper).getValue();
                    commandMapping.stream().forEachOrdered(singleMapping -> {
                        final String commandName = YamlParserUtils.transformScalarNodeToString(singleCommandWrapper.getStartMark(),
                                                                                               singleMapping.getKeyNode());
                        // Verify the command is legal
                        if (Constants.isPermittedListItem(commandName))
                        {
                            if (commandName.equals("Action"))
                            {
                                commands.addAll(ActionParser.parse(singleCommandWrapper.getStartMark(), singleMapping.getValueNode()));
                            }
                            else if (commandName.equals("Store"))
                            {
                                commands.addAll(StoreParser.parse(singleCommandWrapper.getStartMark(), singleMapping.getValueNode()));
                            }
                            else
                            {
                                commands.addAll(StoreDefaultParser.parse(singleCommandWrapper.getStartMark(), singleMapping));
                            }
                        }
                        else
                        {
                            throw new ParserException("Node", root.getStartMark(), " contains a not permitted list item",
                                                      singleCommandWrapper.getStartMark());
                        }
                    });
                }
                else
                {
                    throw new ParserException("Node", root.getStartMark(),
                                              " contains a " + singleCommandWrapper.getNodeId() + " but it must contain mapping",
                                              singleCommandWrapper.getStartMark());

                }
            }
        }
        else if (root == null)
        {
            // Do nothing
        }
        else
        {
            throw new ParserException("Node at", root.getStartMark(), " contains a " + root.getNodeId() + " but it must contain an array.",
                                      null);
        }

        return commands;
    }

    @Override
    public List getExtensions()
    {
        return Arrays.asList("yml", "yaml");
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy