package org.dbflute.intro.app.web.log;

import org.apache.commons.io.FileUtils;
import org.dbflute.intro.app.logic.simple.DbFluteIntroLogic;
import org.dbflute.intro.unit.IntroBaseTestCase;
import org.lastaflute.web.response.JsonResponse;

import java.io.File;
import java.util.List;

/**
 * @author deco
 */
public class LogActionTest extends IntroBaseTestCase {

    // ===================================================================================
    //                                                                            Settings
    //                                                                            ========
    @Override
    public void setUp() throws Exception {
        super.setUp();
        createTestClient();
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
        deleteTestClient();
    }

    // ===================================================================================
    //                                                                                Test
    //                                                                                ====
    public void test_index_success_no_files() throws Exception {
        // ## Arrange ##
        File logDir = new File(DbFluteIntroLogic.BASE_DIR_PATH, "dbflute_" + TEST_CLIENT_PROJECT + "/log/");
        File[] logFiles = logDir.listFiles((dir, name) -> name.endsWith(".log"));
        if (logFiles != null) {
            for (File file : logFiles) {
                FileUtils.deleteQuietly(file);
            }
        }

        LogAction action = new LogAction();
        inject(action);

        // ## Act ##
        JsonResponse<List<LogBean>> response = action.index(TEST_CLIENT_PROJECT);

        // ## Assert ##
        assertTrue(response.isReturnAsEmptyBody());
    }
}

@@ -183,16 +183,99 @@
 stCase;%0A
+import org.dbflute.utflute.lastaflute.mock.TestingJsonData;%0Aimport org.junit.Test;%0A
 import o
@@ -337,16 +337,44 @@
 o.File;%0A
+import java.io.IOException;%0A
 import j
@@ -469,16 +469,382 @@
 Case %7B%0A%0A
+    // ===================================================================================%0A    //                                                                          Definition%0A    //                                                                          ==========%0A    private static final String LOG_FILE_PATH = %22dbflute_%22 + TEST_CLIENT_PROJECT + %22/log/%22;%0A%0A
     // =
@@ -1719,107 +1719,1457 @@
     
-File logDir = new File(DbFluteIntroLogic.BASE_DIR_PATH, %22dbflute_%22 + TEST_CLIENT_PROJECT + %22/
+deleteLogFiles();%0A%0A        LogAction action = new LogAction();%0A        inject(action);%0A%0A        // ## Act ##%0A        JsonResponse%3CList%3CLogBean%3E%3E response = action.index(TEST_CLIENT_PROJECT);%0A%0A        // ## Assert ##%0A        assertTrue(response.isReturnAsEmptyBody());%0A    %7D%0A%0A    @Test%0A    public void test_index_success_have_files() throws IOException %7B%0A        // ## Arrange ##%0A        deleteLogFiles();%0A%0A        String content = %22content%22;%0A        createLogFile(%22test.log%22, content);%0A        createLogFile(%22test.txt%22, content);%0A%0A        LogAction action = new LogAction();%0A        inject(action);%0A%0A        // ## Act ##%0A        JsonResponse%3CList%3CLogBean%3E%3E response = action.index(TEST_CLIENT_PROJECT);%0A%0A        // ## Assert ##%0A        TestingJsonData%3CList%3CLogBean%3E%3E jsonData = validateJsonData(response);%0A        List%3CLogBean%3E jsonBean = jsonData.getJsonBean();%0A        assertHasAnyElement(jsonBean);%0A        jsonBean.forEach(bean -%3E %7B%0A            assertTrue(bean.fileName.endsWith(%22.
 log
-/
 %22)
+);%0A            assertEquals(content, bean.content);%0A        %7D);%0A    %7D%0A%0A    // ===================================================================================%0A    //                                                                         Test Helper%0A    //                                                                         ===========%0A    private void deleteLogFiles() %7B%0A        File logDir =  new File(DbFluteIntroLogic.BASE_DIR_PATH, LOG_FILE_PATH)
 ;%0A  
@@ -3396,52 +3396,99 @@
   %7D%0A
-%0A
     
+%7D%0A%0A
     
-LogAction action = new LogAc
+private void createLogFile(String fileName, String content) throws IOExcep
 tion
-();
+ %7B
 %0A   
@@ -3496,33 +3496,100 @@
     
-inject(action
+File logFile = new File(DbFluteIntroLogic.BASE_DIR_PATH, LOG_FILE_PATH + fileName
 );%0A
-%0A
         
 // #
@@ -3588,112 +3588,103 @@
     
-// ## Act ##%0A        JsonResponse%3CList%3CLogBean%3E%3E response = action.index(TEST_CLIENT_PROJECT
+if (!logFile.getParentFile().exists()) %7B%0A            logFile.getParentFile().mkdirs(
 );%0A
-%0A
         
 // #
@@ -3683,73 +3683,83 @@
     
-// ## Assert ##%0A        assertTrue(response.isReturnAsEmptyBody()
+%7D%0A        logFile.createNewFile();%0A        FileUtils.write(logFile, content
 );%0A 

improve log action test