<commit_before>package io.mangoo.core;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;

import io.mangoo.TestExtension;
import io.mangoo.enums.Default;
import io.mangoo.enums.Key;
import io.mangoo.enums.Mode;

@ExtendWith({TestExtension.class})
public class ConfigTest {
    
    @Test
    public void testFlashCookieName() {
        // given
        final Config config = Application.getInstance(Config.class);

        // then
        assertThat(config.getFlashCookieName(), equalTo("test-flash"));
    }
    
    @Test
    public void testGetSessionCookieName() throws JsonGenerationException, JsonMappingException, IOException {
        // given
        String sessionCookieName = UUID.randomUUID().toString();
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());

        // when
        Map<String, String> configValues = ImmutableMap.of("session.cookie.name", sessionCookieName);
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();
        
        // then
        assertThat(config.getSessionCookieName(), equalTo(sessionCookieName));
        assertThat(tempConfig.delete(), equalTo(true));
    }
    
    @Test
    public void testGetSessionCookieNameDefaultValue() throws JsonGenerationException, JsonMappingException, IOException {
        //given
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());
        
        // when
        Map<String, String> configValues = new HashMap<>();
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();

        // then
        assertThat(config.getSessionCookieName(), equalTo(Default.SESSION_COOKIE_NAME.toString()));
        assertThat(tempConfig.delete(), equalTo(true));
    }      

    @Test
    public void testGetApplicationSecret() throws JsonGenerationException, JsonMappingException, IOException {
        // given
        String applicationSecret = UUID.randomUUID().toString();
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());

        // when
        Map<String, String> configValues = ImmutableMap.of("application.secret", applicationSecret);
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();

        // then
        assertThat(config.getApplicationSecret(), equalTo(applicationSecret));
        assertThat(tempConfig.delete(), equalTo(true));
    }
    
    @Test
    public void testGetAuthenticationCookieName() throws JsonGenerationException, JsonMappingException, IOException {
        // given
        String authenticationCookieName = UUID.randomUUID().toString();
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());

        // when
        Map<String, String> configValues = ImmutableMap.of("authentication.cookie.name", authenticationCookieName);
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();
        
        // then
        assertThat(config.getAuthenticationCookieName(), equalTo(authenticationCookieName));
        assertThat(tempConfig.delete(), equalTo(true));
    }
    
    @Test
    public void testGetAuthenticationCookieNameDefaultValue() throws JsonGenerationException, JsonMappingException, IOException {
        //given
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());
        
        // when
        Map<String, String> configValues = new HashMap<>();
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();

        // then
        assertThat(config.getAuthenticationCookieName(), equalTo(Default.AUTHENTICATION_COOKIE_NAME.toString()));
        assertThat(tempConfig.delete(), equalTo(true));
    }    
  
    @Test
    public void testGetAuthenticationCookieExpires() throws JsonGenerationException, JsonMappingException, IOException {
        // given
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());
        String expires = "42";

        // when
        Map<String, String> configValues = ImmutableMap.of("authentication.cookie.expires", expires);
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();
        
        // then
        assertThat(config.isAuthenticationCookieExpires(), equalTo(Boolean.valueOf(expires)));
        assertThat(tempConfig.delete(), equalTo(true));
    }
    
    @Test
    public void testGetAuthenticationCookieExpiresDefaultValue() throws JsonGenerationException, JsonMappingException, IOException {
        // given
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());
        
        // when
        Map<String, String> configValues = new HashMap<>();
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();

        // then
        assertThat(config.isAuthenticationCookieExpires(), equalTo(Default.AUTHENTICATION_COOKIE_EXPIRES.toBoolean()));
        assertThat(tempConfig.delete(), equalTo(true));
    }
    
    @Test
    public void testGetSessionCookieExpires() throws JsonGenerationException, JsonMappingException, IOException {
        // given
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());
        String expires = "24";<commit_msg>Fixed failing unit test<commit_after>package io.mangoo.core;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;

import io.mangoo.TestExtension;
import io.mangoo.enums.Default;
import io.mangoo.enums.Key;
import io.mangoo.enums.Mode;

@ExtendWith({TestExtension.class})
public class ConfigTest {
    
    @Test
    public void testFlashCookieName() {
        // given
        final Config config = Application.getInstance(Config.class);

        // then
        assertThat(config.getFlashCookieName(), equalTo("test-flash"));
    }
    
    @Test
    public void testGetSessionCookieName() throws JsonGenerationException, JsonMappingException, IOException {
        // given
        String sessionCookieName = UUID.randomUUID().toString();
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());

        // when
        Map<String, String> configValues = ImmutableMap.of("session.cookie.name", sessionCookieName);
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();
        
        // then
        assertThat(config.getSessionCookieName(), equalTo(sessionCookieName));
        assertThat(tempConfig.delete(), equalTo(true));
    }
    
    @Test
    public void testGetSessionCookieNameDefaultValue() throws JsonGenerationException, JsonMappingException, IOException {
        //given
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());
        
        // when
        Map<String, String> configValues = new HashMap<>();
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();

        // then
        assertThat(config.getSessionCookieName(), equalTo(Default.SESSION_COOKIE_NAME.toString()));
        assertThat(tempConfig.delete(), equalTo(true));
    }      

    @Test
    public void testGetApplicationSecret() throws JsonGenerationException, JsonMappingException, IOException {
        // given
        String applicationSecret = UUID.randomUUID().toString();
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());

        // when
        Map<String, String> configValues = ImmutableMap.of("application.secret", applicationSecret);
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();

        // then
        assertThat(config.getApplicationSecret(), equalTo(applicationSecret));
        assertThat(tempConfig.delete(), equalTo(true));
    }
    
    @Test
    public void testGetAuthenticationCookieName() throws JsonGenerationException, JsonMappingException, IOException {
        // given
        String authenticationCookieName = UUID.randomUUID().toString();
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());

        // when
        Map<String, String> configValues = ImmutableMap.of("authentication.cookie.name", authenticationCookieName);
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();
        
        // then
        assertThat(config.getAuthenticationCookieName(), equalTo(authenticationCookieName));
        assertThat(tempConfig.delete(), equalTo(true));
    }
    
    @Test
    public void testGetAuthenticationCookieNameDefaultValue() throws JsonGenerationException, JsonMappingException, IOException {
        //given
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());
        
        // when
        Map<String, String> configValues = new HashMap<>();
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();

        // then
        assertThat(config.getAuthenticationCookieName(), equalTo(Default.AUTHENTICATION_COOKIE_NAME.toString()));
        assertThat(tempConfig.delete(), equalTo(true));
    }    
  
    @Test
    public void testGetAuthenticationCookieExpires() throws JsonGenerationException, JsonMappingException, IOException {
        // given
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());
        String expires = "42";

        // when
        Map<String, String> configValues = ImmutableMap.of("authentication.cookie.expires", expires);
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();
        
        // then
        assertThat(config.isAuthenticationCookieExpires(), equalTo(Boolean.valueOf(expires)));
        assertThat(tempConfig.delete(), equalTo(true));
    }
    
    @Test
    public void testGetAuthenticationCookieExpiresDefaultValue() throws JsonGenerationException, JsonMappingException, IOException {
        // given
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());
        
        // when
        Map<String, String> configValues = new HashMap<>();
        File tempConfig = createTempConfig(configValues);
        Config config = new Config();

        // then
        assertThat(config.isAuthenticationCookieExpires(), equalTo(Default.AUTHENTICATION_COOKIE_EXPIRES.toBoolean()));
        assertThat(tempConfig.delete(), equalTo(true));
    }
    
    @Test
    public void testGetSessionCookieExpires() throws JsonGenerationException, JsonMappingException, IOException {
        // given
        System.setProperty(Key.APPLICATION_MODE.toString(), Mode.TEST.toString());
        String expires = "60";
{'fraction_non_alphanumeric': 0.10654539548481404, 'fraction_numerical': 0.0006496670456391099, 'mean_word_length': 3.398214285714286, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3317114', 'n_tokens_mistral': 3174, 'n_tokens_neox': 3145, 'n_words': 614}