package org.allurefw.report;

import org.allurefw.report.entity.TestCase;
import org.allurefw.report.entity.TestCaseResult;
import org.allurefw.report.entity.TestRun;

import javax.inject.Inject;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author charlie (Dmitry Baev).
 */
public class ReportFactory {

    private final Set<TestCaseResultsReader> testCaseReaders;

    private final TestRunReader testRunReader;

    private final Set<Plugin> plugins;

    private final HashMap<String, TestCase> testCases;

    @Inject
    public ReportFactory(Set<Plugin> plugins, Set<TestCaseResultsReader> testCaseReaders,
                         TestRunReader testRunReader) {
        this.plugins = plugins;
        this.testCaseReaders = testCaseReaders;
        this.testRunReader = testRunReader;
        this.testCases = new HashMap<>();
    }

    public ReportInfo create(Path... sources) {
        List<TestCaseResult> results = new ArrayList<>();
        for (Path source : sources) {
            TestRun testRun = testRunReader.readTestRun(source);
            List<TestCaseResult> testCaseResults = readTestCases(source)
                    .collect(Collectors.toList());
            for (TestCaseResult result : testCaseResults) {
                TestCase testCase = getTestCase(result);
                testCase.updateLinks(result.getLinks());
                testCase.updateParametersNames(result.getParameters());
            }
            results.addAll(testCaseResults);
        }
        return new ReportInfo(plugins, testCases, results);
    }

    private TestCase getTestCase(TestCaseResult result) {
        return testCases.computeIfAbsent(result.getId(), id -> new TestCase()
                .withId(id)
                .withName(result.getName())
                .withDescription(result.getDescription())
                .withDescriptionHtml(result.getDescriptionHtml())
        );
    }

    private Stream<TestCaseResult> readTestCases(Path source) {
        return testCaseReaders.stream()
                .flatMap(reader -> reader.readResults(source).stream());
    }
}

@@ -120,51 +120,8 @@
 ult;
-%0Aimport org.allurefw.report.entity.TestRun;
 %0A%0Aim
@@ -480,56 +480,8 @@
 s;%0A%0A
-    private final TestRunReader testRunReader;%0A%0A
     
@@ -676,62 +676,8 @@
 ders
-,%0A                         TestRunReader testRunReader
 ) %7B%0A
@@ -760,52 +760,8 @@
 rs;%0A
-        this.testRunReader = testRunReader;%0A
     
@@ -953,73 +953,8 @@
 ) %7B%0A
-            TestRun testRun = testRunReader.readTestRun(source);%0A
     
@@ -1243,24 +1243,24 @@
 etLinks());%0A
-
             
@@ -1315,24 +1315,84 @@
 ameters());%0A
+                testCase.getResults().add(result.toInfo());%0A
             

fix report factory