// Copyright (C) 2011 Splunk Inc.
//
// Splunk Inc. licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.splunk.shuttl.archiver.importexport.csv;

import static com.splunk.shuttl.testutil.TUtilsFile.*;
import static java.util.Arrays.*;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.splunk.shuttl.archiver.importexport.ShellExecutor;
import com.splunk.shuttl.archiver.importexport.csv.CsvExportFailedException;
import com.splunk.shuttl.archiver.importexport.csv.CsvExporter;
import com.splunk.shuttl.archiver.importexport.csv.GetsBucketsCsvFile;
import com.splunk.shuttl.archiver.importexport.csv.splunk.SplunkExportTool;
import com.splunk.shuttl.archiver.model.Bucket;
import com.splunk.shuttl.testutil.TUtilsBucket;

@Test(groups = { "fast-unit" })
public class CsvExporterTest {

    private CsvExporter csvExporter;
    private SplunkExportTool exportTool;
    private GetsBucketsCsvFile getsBucketsCsvFile;
    private Bucket bucket;
    private ShellExecutor shellExecutor;
    private Map<String, String> emptyMap;

    @BeforeMethod
    public void setUp() {
	exportTool = mock(SplunkExportTool.class);
	getsBucketsCsvFile = mock(GetsBucketsCsvFile.class);
	shellExecutor = mock(ShellExecutor.class);
	csvExporter = new CsvExporter(exportTool, getsBucketsCsvFile,
		shellExecutor);

	bucket = TUtilsBucket.createTestBucket();
	emptyMap = Collections.<String, String> emptyMap();
    }

    public void exportBucketToCsv_givenRuntimeExportToolAndOutputDir_callsRuntimeInTheCorrectWay()
	    throws IOException {
	when(exportTool.getExecutableCommand()).thenReturn("/exporttool/path");
	when(exportTool.getEnvironment()).thenReturn(emptyMap);
	when(getsBucketsCsvFile.getCsvFile(bucket))
		.thenReturn(createTestFile());
	String bucketPath = bucket.getDirectory().getAbsolutePath();

	String[] command = new String[] { "/exporttool/path", bucketPath,
		"/csv/path", "-csv" };
	when(shellExecutor.executeCommand(emptyMap, asList(command)))
		.thenReturn(0);

	csvExporter.exportBucketToCsv(bucket);
    }

    @SuppressWarnings("unchecked")
    @Test(groups = { "fast-unit" }, expectedExceptions = { CsvExportFailedException.class })
    public void exportBucketToCsv_nonZeroExitStatus_throwCsvExportFailedException()
	    throws IOException, InterruptedException {
	when(shellExecutor.executeCommand(anyMap(), anyList())).thenReturn(1);

	when(exportTool.getExecutableCommand()).thenReturn("/exporttool/path");
	when(getsBucketsCsvFile.getCsvFile(bucket)).thenReturn(
		new File("/dummy/file"));
	csvExporter.exportBucketToCsv(bucket);
    }

    @SuppressWarnings("unchecked")
    @Test(groups = { "fast-unit" }, expectedExceptions = { CsvExportFailedException.class })
    public void exportBucketToCsv_csvFileDoesNotExistAfterExport_throwCsvExportFailedException() {
	File nonExistantCsvFile = createTestFile();
	when(getsBucketsCsvFile.getCsvFile(bucket)).thenReturn(
		nonExistantCsvFile);

	when(shellExecutor.executeCommand(anyMap(), anyList())).thenReturn(0);
	when(exportTool.getExecutableCommand()).thenReturn("/exporttool/path");

	FileUtils.deleteQuietly(nonExistantCsvFile);
	csvExporter.exportBucketToCsv(bucket);
    }
}

@@ -1126,220 +1126,8 @@
 or;%0A
-import com.splunk.shuttl.archiver.importexport.csv.CsvExportFailedException;%0Aimport com.splunk.shuttl.archiver.importexport.csv.CsvExporter;%0Aimport com.splunk.shuttl.archiver.importexport.csv.GetsBucketsCsvFile;%0A
 impo
@@ -2013,65 +2013,77 @@
 iven
-RuntimeExportToolAndOutputDir_callsRuntimeInTheCorrectWay
+ExecutableCommandEnvironmentAndCsvExportPath_executesSpecifiedCommand
 ()%0A%09
@@ -2237,16 +2237,50 @@
 tyMap);%0A
+%09File csvFile = createTestFile();%0A
 %09when(ge
@@ -2315,19 +2315,16 @@
 bucket))
-%0A%09%09
 .thenRet
@@ -2328,31 +2328,22 @@
 Return(c
-reateTest
+sv
 File
-()
 );%0A%09Stri
@@ -2400,16 +2400,57 @@
 ath();%0A%0A
+%09csvExporter.exportBucketToCsv(bucket);%0A%0A
 %09String%5B
@@ -2514,19 +2514,33 @@
 ,%0A%09%09
-%22/
 csv
-/path%22
+File.getAbsolutePath()
 , %22-
@@ -2548,20 +2548,22 @@
 sv%22 %7D;%0A%09
-when
+verify
 (shellEx
@@ -2560,32 +2560,33 @@
 fy(shellExecutor
+)
 .executeCommand(
@@ -2615,67 +2615,8 @@
 nd))
-)%0A%09%09.thenReturn(0);%0A%0A%09csvExporter.exportBucketToCsv(bucket)
 ;%0A  

Fix CsvExporterTest so it tests something. #18