/*
 *  [2012] - [2016] Codenvy, S.A.
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Codenvy S.A. and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Codenvy S.A.
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Codenvy S.A..
 */
package com.codenvy.im.cli.command;

import com.codenvy.im.cli.preferences.PreferenceNotFoundException;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

import static java.nio.file.Files.createDirectory;
import static java.nio.file.Files.createFile;
import static java.nio.file.Files.write;
import static org.apache.commons.io.FileUtils.deleteQuietly;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;
import static org.testng.Assert.assertEquals;

/** @author Igor Vinokur */
public class TestAuditCommand extends AbstractTestCommand {
    private final static Path REPORT_DIRECTORY = Paths.get("target/audit");

    private AuditCommand spyCommand;

    @BeforeMethod
    public void initMocks() throws IOException {
        spyCommand = spy(new AuditCommand());
        performBaseMocks(spyCommand, true);
    }

    @AfterMethod
    public void tearDown() {
        deleteQuietly(REPORT_DIRECTORY.toFile());
    }

    @Test
    public void shouldPrintContentOfAuditReport() throws Exception {
        createDirectory(REPORT_DIRECTORY);
        doAnswer(invocation -> {
            Path report = createFile(REPORT_DIRECTORY.resolve("report.txt"));
            write(report, "Report_Content".getBytes());
            return null;
        }).when(mockFacade).requestAuditReport(anyString(), anyString());
        CommandInvoker commandInvoker = new CommandInvoker(spyCommand, mockCommandSession);

        String reportContent = commandInvoker.invoke().getOutputStream();

        assertEquals(reportContent, "Report_Content\n");
    }

    @Test
    public void shouldPrintContentOfNewestAuditReport() throws Exception {
        createDirectory(REPORT_DIRECTORY);
        Path report1 = createFile(REPORT_DIRECTORY.resolve("report1.txt"));
        write(report1, "First_Report".getBytes());
        Path report2 = createFile(REPORT_DIRECTORY.resolve("report2.txt"));
        write(report2, "Second_Report".getBytes());
        doAnswer(invocation -> {
            Path report3 = createFile(REPORT_DIRECTORY.resolve("report3.txt"));
            write(report3, "Third_Report".getBytes());
            return null;
        }).when(mockFacade).requestAuditReport(anyString(), anyString());
        CommandInvoker commandInvoker = new CommandInvoker(spyCommand, mockCommandSession);

        String reportContent = commandInvoker.invoke().getOutputStream();

        assertEquals(reportContent, "Third_Report\n");
    }

    @Test
    public void shouldPrintErrorWhenExecutingAuditNotFromAdmin() throws Exception {
        doThrow(PreferenceNotFoundException.class).when(mockFacade).requestAuditReport(anyString(), anyString());

        String errorMessage = commandInvoker.invoke().disableAnsi().getOutputStream();

        assertEquals(errorMessage, "Please, login into Codenvy\n");
    }

    @Test
    public void shouldPrintErrorWhenAuditDirectoryIsEmpty() throws Exception {
        doNothing().when(mockFacade).requestAuditReport(anyString(), anyString());

        String errorMessage = commandInvoker.invoke().disableAnsi().getOutputStream();

        assertEquals(errorMessage, "Audit directory is empty\n");
    }
}

@@ -2716,125 +2716,117 @@
     
-Path report2 = createFile(REPORT_DIRECTORY.resolve(%22report2.txt%22));%0A        write(report2, %22Second_Report%22.getBytes()
+//Sleep is needed here because file.lastModified() returns time rounded to seconds.%0A        Thread.sleep(1000
 );%0A 
@@ -2880,17 +2880,17 @@
 h report
-3
+2
  = creat
@@ -2965,16 +2965,17 @@
 port
-3, %22Thir
+2, %22Secon
 d_Re
@@ -3301,12 +3301,13 @@
 t, %22
-Thir
+Secon
 d_Re

Fix TestAuditCommand test (#983)