package io.vrap.rmf.raml.persistence;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import org.assertj.core.util.Lists;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;

@Ignore
@RunWith(DataProviderRunner.class)
public class RamlTck2Test implements ResourceFixtures {
    private class TckParseException extends Exception {
        public TckParseException(String message, Throwable cause) {
            super(message, cause);
        }
    }
    private final static URL tckURL = RamlTck2Test.class.getResource("/raml-tck-wip-2.0.0/tests");

    @Test
    @UseDataProvider("testFiles")
    public void tckFilesParse(final File f, final Boolean valid, final String description) throws IOException, TckParseException {


        final Resource resource;
        final URI fileURI = URI.createURI(f.toURI().toString());
        try {
            resource = fromUri(fileURI);
        } catch (Exception e) {
            throw new TckParseException(fileURI.toString(), e);
        }
        try {
            if (valid) {
                assertThat(resource.getErrors()).describedAs(description).isEmpty();
            } else {
                assertThat(resource.getErrors()).describedAs(description).isNotEmpty();
            }
        } catch (AssertionError e) {
            throw new TckParseException(fileURI.toString(), e);
        }
    }

    @DataProvider
    public static List<List<Object>> testFiles() throws IOException {
        final List<File> files = Files.walk(Paths.get(tckURL.getPath()))
                .filter(Files::isRegularFile)
                .filter(path -> path.toString().endsWith("test-config.json")).map(Path::toFile).collect(Collectors.toList());
        return files.stream().map(file -> {
            List<List<Object>> map = Lists.newArrayList();
            ObjectMapper mapper = new ObjectMapper();
            try {
                JsonNode json = mapper.readValue(file, JsonNode.class);
                JsonNode tests = json.get("tests");
                if (tests != null) {
                    tests.elements().forEachRemaining(jsonNode -> {
                        List<Object> a = Lists.newArrayList(
                                new File(file.getParentFile().getPath() + "/" + jsonNode.get("input").asText()),
                                jsonNode.get("valid").asBoolean(),
                                jsonNode.get("description").asText()
                        );
                        map.add(a);
                    });
                }
            } catch (Exception e) {

            }
            return map;
        }).flatMap(List::stream).collect(Collectors.toList());
    }
}

@@ -1197,19 +1197,26 @@
 vider(%22t
-est
+ckSemantic
 Files%22)%0A
@@ -1235,124 +1235,744 @@
 oid 
-tckFilesParse(final File f, final Boolean valid, final String description) throws IOException, TckParseException %7B%0A%0A
+semantic(final File f, final Boolean valid, final String description) throws IOException, TckParseException %7B%0A        tckParse(f, valid, description);%0A    %7D%0A%0A    @Test%0A    @UseDataProvider(%22tckSpecExampleFiles%22)%0A    public void specExamples(final File f, final Boolean valid, final String description) throws IOException, TckParseException %7B%0A        tckParse(f, valid, description);%0A    %7D%0A%0A    @Test%0A    @UseDataProvider(%22tckSyntaxFiles%22)%0A    public void syntax(final File f, final Boolean valid, final String description) throws IOException, TckParseException %7B%0A        tckParse(f, valid, description);%0A    %7D%0A%0A    public void tckParse(final File f, final Boolean valid, final String description) throws IOException, TckParseException %7B
 %0A   
@@ -2652,17 +2652,463 @@
 %3E%3E t
-estFiles(
+ckSemanticFiles() throws IOException %7B%0A        return testFiles(%22semantic%22);%0A    %7D%0A%0A    @DataProvider%0A    public static List%3CList%3CObject%3E%3E tckSpecExampleFiles() throws IOException %7B%0A        return testFiles(%22spec-examples%22);%0A    %7D%0A%0A    @DataProvider%0A    public static List%3CList%3CObject%3E%3E tckSyntaxFiles() throws IOException %7B%0A        return testFiles(%22syntax%22);%0A    %7D%0A%0A    @DataProvider%0A    public static List%3CList%3CObject%3E%3E testFiles(final String testSuite
 ) th
@@ -3245,16 +3245,85 @@
 arFile)%0A
+                .filter(path -%3E path.toString().contains(testSuite))%0A
         

split TCK 2.0 test suites