/**
 * Copyright (c) 2013-2016, The SeedStack authors <http://seedstack.org>
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package org.seedstack.seed.core.internal.configuration;

import com.google.common.collect.Lists;
import io.nuun.kernel.api.plugin.context.InitContext;
import org.junit.Before;
import org.junit.Test;
import org.mockito.internal.util.reflection.Whitebox;
import org.seedstack.coffig.Coffig;
import org.seedstack.seed.ApplicationConfig;
import org.seedstack.seed.core.SeedRuntime;
import org.seedstack.seed.core.internal.diagnostic.DiagnosticManagerImpl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ConfigurationPluginTest {
    private ConfigurationPlugin configurationPlugin;

    @Before
    public void before() {
        configurationPlugin = new ConfigurationPlugin();
        Coffig coffig = mock(Coffig.class);
        when(coffig.get(ApplicationConfig.class)).thenReturn(new ApplicationConfig());
        Whitebox.setInternalState(configurationPlugin, "configuration", coffig);
        SeedRuntime seedRuntime = mock(SeedRuntime.class);
        Whitebox.setInternalState(configurationPlugin, "seedRuntime", seedRuntime);
        Whitebox.setInternalState(configurationPlugin, "diagnosticManager", new DiagnosticManagerImpl());
    }

    @Test
    public void nameTest() {
        assertThat(configurationPlugin.name()).isNotEmpty();
    }

    @Test
    public void verify_nativeUnitModule_instance() {
        Object object = configurationPlugin.nativeUnitModule();
        assertThat(object).isInstanceOf(ConfigurationModule.class);
    }

    @Test
    public void initPluginTest() {
        InitContext initContext = mockInitContextForCore(
                Lists.newArrayList("META-INF/configuration/file.yaml", "bad.yaml"),
                Lists.newArrayList("META-INF/configuration/file.yml", "bad.yml"),
                Lists.newArrayList("META-INF/configuration/file.json", "bad.json")
        );
        configurationPlugin.init(initContext);
    }

    private InitContext mockInitContextForCore(ArrayList<String> yamlFiles, ArrayList<String> ymlFiles, ArrayList<String> jsonFiles) {
        InitContext initContext = mock(InitContext.class);

        Map<String, Collection<String>> mapResourcesByRegex = new HashMap<>();
        mapResourcesByRegex.put(".*\\.yaml", yamlFiles);
        mapResourcesByRegex.put(".*\\.yml", ymlFiles);
        mapResourcesByRegex.put(".*\\.json", jsonFiles);

        when(initContext.mapResourcesByRegex()).thenReturn(mapResourcesByRegex);

        return initContext;
    }

    @Test
    public void package_root_should_valid() {
        String pluginPackageRoot = configurationPlugin.pluginPackageRoot();
        assertThat(pluginPackageRoot).contains("META-INF.configuration");
    }
}

@@ -2294,16 +2294,112 @@
 d.json%22)
+,%0A                Lists.newArrayList(%22META-INF/configuration/file.properties%22, %22bad.properties%22)
 %0A       
@@ -2587,16 +2587,51 @@
 sonFiles
+, ArrayList%3CString%3E propertiesFiles
 ) %7B%0A    
@@ -2933,16 +2933,85 @@
 nFiles);
+%0A        mapResourcesByRegex.put(%22.*%5C%5C.properties%22, propertiesFiles);
 %0A%0A      

Fix config plugin test