// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.engine;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.io.TempDir;
import org.terasology.engine.core.module.ModuleManager;
import org.terasology.engine.core.paths.PathManager;
import org.terasology.engine.testUtil.ModuleManagerFactory;
import org.terasology.gestalt.module.ModuleEnvironment;
import org.terasology.gestalt.module.dependencyresolution.DependencyResolver;
import org.terasology.gestalt.module.dependencyresolution.ResolutionResult;
import org.terasology.reflection.ModuleTypeRegistry;

import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertTrue;

public abstract class ModuleEnvironmentTest {
    protected ModuleManager moduleManager;
    protected ModuleTypeRegistry typeRegistry;

    @BeforeEach
    public void before(@TempDir Path tempHome) throws Exception {
        PathManager.getInstance().useOverrideHomePath(tempHome);

        moduleManager = ModuleManagerFactory.create();

        DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
        ResolutionResult result = resolver.resolve(moduleManager.getRegistry().getModuleIds());

        assertTrue(result.isSuccess());

        ModuleEnvironment environment = moduleManager.loadEnvironment(result.getModules(), true);
        typeRegistry = new ModuleTypeRegistry(environment);

        setup();
    }

    protected void setup() {

    }
}

@@ -190,16 +190,79 @@
 empDir;%0A
+import org.terasology.engine.core.module.ExternalApiWhitelist;%0A
 import o
@@ -448,194 +448,37 @@
 ogy.
-gestalt.module.ModuleEnvironment;%0Aimport org.terasology.gestalt.module.dependencyresolution.DependencyResolver;%0Aimport org.terasology.gestalt.module.dependencyresolution.ResolutionResult
+reflection.ModuleTypeRegistry
 ;%0Aim
@@ -500,38 +500,32 @@
 logy.reflection.
-Module
 TypeRegistry;%0A%0Ai
@@ -554,66 +554,42 @@
 th;%0A
-%0A
 import 
-static org.junit.jupiter.api.Assertions.assertTrue
+java.util.stream.Collectors
 ;%0A%0Ap
@@ -927,25 +927,24 @@
 eate();%0A
-%0A
         
 Dependen
@@ -939,141 +939,47 @@
     
-DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());%0A        ResolutionResult result = resolver.resolve
+moduleManager.resolveAndLoadEnvironment
 (mod
@@ -1020,25 +1020,24 @@
 Ids());%0A
-%0A
         
 assertTr
@@ -1032,49 +1032,138 @@
     
-assertTrue(result.isSuccess
+TypeRegistry.WHITELISTED_CLASSES = ExternalApiWhitelist.CLASSES.stream().map(Class::getName).collect(Collectors.toSet
 ());%0A
-%0A
         
 Modu
@@ -1162,96 +1162,73 @@
     
-ModuleEnvironment environment = moduleManager.loadEnvironment(result.getModules(), true)
+TypeRegistry.WHITELISTED_PACKAGES = ExternalApiWhitelist.PACKAGES
 ;%0A  
@@ -1271,17 +1271,34 @@
 egistry(
-e
+moduleManager.getE
 nvironme
@@ -1299,16 +1299,18 @@
 ironment
+()
 );%0A%0A    

initialize TypeRegistry list of permitted classes