package net.sf.jguard.jee.authentication;

import com.google.inject.AbstractModule;
import javax.inject.Inject;
import com.google.inject.Module;
import com.mycila.testing.plugin.guice.Bind;
import com.mycila.testing.plugin.guice.ModuleProvider;
import net.sf.jguard.core.authentication.AuthenticationScope;
import net.sf.jguard.core.authentication.configuration.JGuardAuthenticationMarkups;
import net.sf.jguard.core.authentication.configuration.JGuardConfiguration;
import net.sf.jguard.core.authentication.loginmodules.UserNamePasswordLoginModule;
import net.sf.jguard.core.authentication.manager.AuthenticationManagerModule;
import net.sf.jguard.core.lifecycle.*;
import net.sf.jguard.core.technology.MockScopes;
import net.sf.jguard.core.technology.Scopes;
import net.sf.jguard.core.test.JGuardTest;
import net.sf.jguard.ext.SecurityConstants;
import net.sf.jguard.ext.authentication.loginmodules.XmlLoginModule;
import net.sf.jguard.ext.authentication.manager.XmlAuthenticationManager;
import net.sf.jguard.ext.authorization.manager.XmlAuthorizationManager;
import org.junit.Assert;
import org.junit.Test;

import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.login.Configuration;
import java.net.URL;
import java.util.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class JGuardConfigurationTest extends JGuardTest {


    @Inject
    public JGuardConfiguration jGuardConfiguration;

    @Bind
    Request request = new MockRequestAdapter(new MockRequest());
    @Bind
    Response response = new MockResponseAdapter(new MockResponse());

    @ModuleProvider
    public Iterable<Module> providesAuthenticationModule() {
        URL applicationPath = Thread.currentThread().getContextClassLoader().getResource(".");
        Collection modules = new ArrayList();
        modules.addAll(super.providesModules(
                AuthenticationScope.LOCAL,
                true,
                applicationPath,
                XmlAuthorizationManager.class));
        modules.add(new AbstractModule() {

            @Override
            protected void configure() {
                bind(Scopes.class).to(MockScopes.class);
            }
        });
        return modules;
    }


    @Test
    public void testGetApplicationEntry() {
        AppConfigurationEntry[] appConfigurationEntries = jGuardConfiguration.getAppConfigurationEntry(APPLICATION_NAME);
        List<AppConfigurationEntry> entries = Arrays.asList(appConfigurationEntries);
        AppConfigurationEntry firstEntry = entries.get(0);
        assertEquals("first AppConfigurationEntry should be configured with a class= " + XmlLoginModule.class.getName(), XmlLoginModule.class.getName(), firstEntry.getLoginModuleName());
        assertEquals("first AppConfigurationEntry should be configured with a flag Required ", AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, firstEntry.getControlFlag());
        assertTrue(firstEntry.getOptions().containsKey(JGuardAuthenticationMarkups.DEBUG.getLabel()));
        assertTrue(firstEntry.getOptions().get(JGuardAuthenticationMarkups.DEBUG.getLabel()).equals(Boolean.FALSE.toString()));
        assertTrue(firstEntry.getOptions().get(SecurityConstants.AUTHORIZATION_DATABASE_IMPORT_XML_DATA).equals(Boolean.TRUE.toString()));
    }


    @Test(expected = IllegalArgumentException.class)
    public void testNullApplicationName() {
        String applicationName = null;
        Map<String, Object> authenticationSettings = new HashMap();
        List<AppConfigurationEntry> appConfigurationEntries = new ArrayList();
        JGuardConfiguration jGuardConfiguration = new JGuardConfiguration(applicationName, authenticationSettings, appConfigurationEntries);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testEmptyApplciationName() {
        String applicationName = "";
        Map<String, Object> authenticationSettings = new HashMap();
        List<AppConfigurationEntry> appConfigurationEntries = new ArrayList();
        JGuardConfiguration jGuardConfiguration = new JGuardConfiguration(applicationName, authenticationSettings, appConfigurationEntries);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testNullAuthenticationSettings() {
        String applicationName = "dummy";
        Map<String, Object> authenticationSettings = null;
        List<AppConfigurationEntry> appConfigurationEntries = new ArrayList();
        JGuardConfiguration jGuardConfiguration = new JGuardConfiguration(applicationName, authenticationSettings, appConfigurationEntries);
    }


    

    @Test(expected = IllegalArgumentException.class)
    public void testNullAppConigurationEntries() {
        String applicationName = "dummy";
        Map<String, Object> authenticationSettings = new HashMap();
        List<AppConfigurationEntry> appConfigurationEntries = null;
        JGuardConfiguration jGuardConfiguration = new JGuardConfiguration(applicationName, authenticationSettings, appConfigurationEntries);
    }


    @Override
    protected AuthenticationManagerModule buildAuthenticationManagerModule() {
        return new AuthenticationManagerModule(APPLICATION_NAME, authenticationXmlFileLocation, XmlAuthenticationManager.class);
    }

    @Test
    public void testEquals() {
        List<AppConfigurationEntry> appConfigurationEntries = new ArrayList<AppConfigurationEntry>();
        Map<String, ?> entryOptions = new HashMap<String, Object>();
        appConfigurationEntries.add(new AppConfigurationEntry(UserNamePasswordLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, entryOptions));
        JGuardConfiguration jGuardConfiguration = new JGuardConfiguration("toto", new HashMap(), appConfigurationEntries);
        Configuration configuration = new Configuration() {

            @Override
            public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
                return new AppConfigurationEntry[0];
            }
        };

        Assert.assertNotSame(jGuardConfiguration, configuration);
    }
}

@@ -81,36 +81,8 @@
 le;%0A
-import javax.inject.Inject;%0A
 impo
@@ -102,32 +102,32 @@
 .inject.Module;%0A
+
 import com.mycil
@@ -1077,16 +1077,44 @@
 .Test;%0A%0A
+import javax.inject.Inject;%0A
 import j
@@ -1230,16 +1230,61 @@
 et.URL;%0A
+import java.security.AccessControlException;%0A
 import j
@@ -3662,50 +3662,8 @@
     
- JGuardConfiguration jGuardConfiguration =
  new
@@ -3839,18 +3839,18 @@
 mptyAppl
-c
 i
+c
 ationNam
@@ -4050,50 +4050,8 @@
     
- JGuardConfiguration jGuardConfiguration =
  new
@@ -4440,50 +4440,8 @@
     
- JGuardConfiguration jGuardConfiguration =
  new
@@ -4540,14 +4540,8 @@
 %7D%0A%0A%0A
-    %0A%0A
     
@@ -4829,50 +4829,8 @@
     
- JGuardConfiguration jGuardConfiguration =
  new
@@ -4920,24 +4920,177 @@
 es);%0A    %7D%0A%0A
+    @Test(expected = AccessControlException.class)%0A    public void testRefresh_without_auth_permission() %7B%0A        jGuardConfiguration.refresh();%0A    %7D%0A%0A
 %0A    @Overri

add one more test