/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.xpack.security;

import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;

public class SecurityTests extends ESTestCase {

    private Environment env;

    @Before
    public void setupEnv() {
        Settings settings = Settings.builder()
            .put("path.home", createTempDir()).build();
        env = new Environment(settings);
    }

    public void testCustomRealmExtension() throws Exception {
        Security security = new Security(Settings.EMPTY, env);

        //security.createComponents(null, null, null, )
    }

    public void testCustomRealmExtensionConflict() throws Exception {

    }
}

@@ -278,16 +278,151 @@
 urity;%0A%0A
+import java.io.IOException;%0Aimport java.util.Arrays;%0Aimport java.util.Collection;%0Aimport java.util.Collections;%0Aimport java.util.Map;%0A%0A
 import o
@@ -563,140 +563,1017 @@
 org.
-junit.Before;%0A%0Apublic class SecurityTests extends ESTestCase %7B%0A%0A    private Environment env;%0A%0A    @Before%0A    public void setupEnv()
+elasticsearch.threadpool.ThreadPool;%0Aimport org.elasticsearch.xpack.extensions.XPackExtension;%0Aimport org.elasticsearch.xpack.security.authc.Realm;%0Aimport org.elasticsearch.xpack.security.authc.Realms;%0Aimport org.elasticsearch.xpack.security.authc.file.FileRealm;%0A%0Aimport static org.mockito.Mockito.mock;%0A%0Apublic class SecurityTests extends ESTestCase %7B%0A%0A    public static class DummyExtension extends XPackExtension %7B%0A        private String realmType;%0A        DummyExtension(String realmType) %7B%0A            this.realmType = realmType;%0A        %7D%0A        @Override%0A        public String name() %7B%0A            return %22dummy%22;%0A        %7D%0A        @Override%0A        public String description() %7B%0A            return %22dummy%22;%0A        %7D%0A        @Override%0A        public Map%3CString, Realm.Factory%3E getRealms() %7B%0A            return Collections.singletonMap(realmType, config -%3E null);%0A        %7D%0A    %7D%0A%0A    private Collection%3CObject%3E createComponents(Settings testSettings, XPackExtension... extensions) throws IOException
  %7B%0A 
@@ -1617,16 +1617,34 @@
 uilder()
+.put(testSettings)
 %0A       
@@ -1700,16 +1700,28 @@
         
+Environment 
 env = ne
@@ -1753,145 +1753,719 @@
     
-%7D%0A%0A    public void testCustomRealmExtension() throws Exception %7B%0A        Security security = new Security(Settings.EMPTY, env
+    Security security = new Security(settings, env);%0A        ThreadPool threadPool = mock(ThreadPool.class);%0A        return security.createComponents(null, threadPool, null, Arrays.asList(extensions));%0A    %7D%0A%0A    private %3CT%3E T findComponent(Class%3CT%3E type, Collection%3CObject%3E components) %7B%0A        for (Object obj : components) %7B%0A            if (type.isInstance(obj)) %7B%0A                return type.cast(obj);%0A            %7D%0A        %7D%0A        throw new AssertionError(%22Could not find component of type %22 + type + %22 in components%22);%0A    %7D%0A%0A    public void testCustomRealmExtension() throws Exception %7B%0A        Collection%3CObject%3E components = createComponents(Settings.EMPTY, new DummyExtension(%22myrealm%22)
 );%0A
-%0A
         
 //se
@@ -2464,133 +2464,466 @@
     
-//security.createComponents(null, null, null, )%0A    %7D%0A%0A    public void testCustomRealmExtensionConflict() throws Exception %7B%0A
+Realms realms = findComponent(Realms.class, components);%0A        assertNotNull(realms.realmFactory(%22myrealm%22));%0A    %7D%0A%0A    public void testCustomRealmExtensionConflict() throws Exception %7B%0A        IllegalArgumentException e = expectThrows(IllegalArgumentException.class,%0A            () -%3E createComponents(Settings.EMPTY, new DummyExtension(FileRealm.TYPE)));%0A        assertEquals(%22Realm type %5B%22 + FileRealm.TYPE + %22%5D is already registered%22, e.getMessage());
 %0A   

Add tests for realm construction