/*******************************************************************************
* (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License v2.0 which accompany this distribution.
*
* The Apache License is available at
* http://www.apache.org/licenses/LICENSE-2.0
*
*******************************************************************************/

package io.cloudslang.lang.tools.build.tester.parse;

/*
 * Created by orius123 on 05/11/14.
 */

import ch.lambdaj.function.convert.Converter;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.cloudslang.lang.compiler.SlangSource;
import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yaml.snakeyaml.Yaml;

import java.io.IOException;
import java.util.Map;

import static ch.lambdaj.Lambda.convertMap;

@Component
public class TestCasesYamlParser {

    @Autowired
    private Yaml yaml;

    private ObjectMapper objectMapper = new ObjectMapper();

    public Map<String, SlangTestCase> parse(SlangSource source) {

        Validate.notEmpty(source.getSource(), "Source " + source.getName() + " cannot be empty");

        try {
            @SuppressWarnings("unchecked") Map<String, Map> parsedTestCases = yaml.loadAs(source.getSource(), Map.class);
            return convertMap(parsedTestCases, new Converter<Map, SlangTestCase>() {
                @Override
                public SlangTestCase convert(Map from) {
                    return parseTestCase(from);
                }
            });
        } catch (Throwable e) {
            throw new RuntimeException("There was a problem parsing the YAML source: " + source.getName() + ".\n" + e.getMessage(), e);
        }
    }

    private SlangTestCase parseTestCase(Map map) {
        try {
            String content = objectMapper.writeValueAsString(map);
            return objectMapper.readValue(content, SlangTestCase.class);
        } catch (IOException e) {
            throw new RuntimeException("Error parsing slang test case", e);
        }
    }
}

@@ -745,22 +745,148 @@
 ons.
-lang3.Validate
+collections4.MapUtils;%0Aimport org.apache.commons.lang3.StringUtils;%0Aimport org.apache.commons.lang3.Validate;%0Aimport org.apache.log4j.Logger
 ;%0Aim
@@ -1056,16 +1056,42 @@
 eption;%0A
+import java.util.HashMap;%0A
 import j
@@ -1306,69 +1306,327 @@
    p
-ublic Map%3CString, SlangTestCase%3E parse(SlangSource source) %7B%0A
+rivate final static Logger log = Logger.getLogger(TestCasesYamlParser.class);%0A%0A    public Map%3CString, SlangTestCase%3E parse(SlangSource source) %7B%0A%0A        if(StringUtils.isEmpty(source.getSource()))%7B%0A            log.info(%22No tests cases were found in: %22 + source.getName());%0A            return new HashMap%3C%3E();%0A        %7D
 %0A   
@@ -1849,32 +1849,217 @@
 (), Map.class);%0A
+            if (MapUtils.isEmpty(parsedTestCases))%7B%0A                log.info(%22No tests cases were found in: %22 + source.getName());%0A                return new HashMap%3C%3E();%0A            %7D%0A
             retu

Allow dealing with an empty file of test cases