package com.blackboard.testing.lambda;

import static com.blackboard.testing.lambda.logger.LoggerContainer.LOGGER;

import com.blackboard.testing.common.LambdaBaseTest;
import com.blackboard.testing.runner.ParallelParameterized;
import com.blackboard.testing.testcontext.TestUUID;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.junit.runner.RunWith;
import org.junit.runner.manipulation.Filter;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
import org.reflections.Reflections;

@RunWith(ParallelParameterized.class)
public class LambdaTestSuite {

    private static List<Class> getTestClasses(String folderName) {
        Reflections reflections = new Reflections(folderName);
        Set<Class<? extends LambdaBaseTest>> allClasses = reflections.getSubTypesOf(LambdaBaseTest.class);
        List<Class> classes = new ArrayList<>();
        classes.addAll(allClasses);
        return classes;
    }

    protected static List<TestRequest> getTestRequests(String folderName, Filter filter) {
        List<TestRequest> requests = new ArrayList<>();
        getTestClasses(folderName).forEach(testClass -> {
            try {
                new BlockJUnit4ClassRunner(testClass).getDescription().getChildren()
                        .forEach(description -> {
                            if (filter.shouldRun(description)) {
                                TestRequest request = new TestRequest(description);
                                request.setTestRunUUID(TestUUID.getTestUUID());
                                requests.add(request);
                            }
                        });
            } catch (InitializationError e) {
                LOGGER.log(e);
            }
        });
        return requests;
    }
}

@@ -107,16 +107,61 @@
 .LOGGER;
+%0Aimport static java.util.Optional.ofNullable;
 %0A%0Aimport
@@ -319,16 +319,65 @@
 stUUID;%0A
+import java.io.File;%0Aimport java.io.IOException;%0A
 import j
@@ -436,19 +436,81 @@
 va.util.
-Set
+Map;%0Aimport java.util.Set;%0Aimport org.apache.commons.io.FileUtils
 ;%0Aimport
@@ -1975,11 +1975,724 @@
 s;%0A    %7D
+%0A%0A    protected void writeAttachments(Map%3CString, byte%5B%5D%3E attachments) %7B%0A        File outputDirectory = new File(System.getProperty(%22user.dir%22) + %22/build/screenshots/%22);%0A        outputDirectory.mkdirs();%0A%0A        attachments.forEach((fileName, bytes) -%3E %7B%0A            try %7B%0A                FileUtils.writeByteArrayToFile(new File(outputDirectory, fileName), bytes);%0A            %7D catch (IOException e) %7B%0A                LOGGER.log(e);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    protected void logTestResult(TestRequest request, TestResult result) %7B%0A        LOGGER.log(%22Test %25s:%25s completed.%22, request.getTestClass(), request.getFrameworkMethod());%0A        ofNullable(result.getThrowable()).ifPresent(LOGGER::log);%0A    %7D
 %0A%7D%0A

Write screenshots from lambda execution