package com.splicemachine.derby.impl.sql.execute.operations.export;

import com.google.common.collect.Lists;
import com.splicemachine.constants.SpliceConstants;
import com.splicemachine.job.JobResults;
import com.splicemachine.job.JobStats;

import com.splicemachine.db.iapi.error.StandardException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mockito;

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

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

public class ExportFailedTaskCleanupTest {

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();


    @Test
    public void cleanup() throws StandardException, IOException {

        File expectedFile = new File(temporaryFolder.getRoot(), "export_82010203042A060708.csv");
        byte[] failedTaskId = new byte[]{-126, 1, 2, 3, 4, 42, 6, 7, 8};
        List<byte[]> failedTaskIdList = Lists.newArrayList(failedTaskId);

        //
        // Given: export params and a export file that already exists.
        //
        String exportPath = temporaryFolder.getRoot().getAbsolutePath();
        ExportParams exportParams = new ExportParams(exportPath, false, 1, null, null, null);
        Configuration conf = SpliceConstants.config;
 		// Mapr4.0 specific fix
 		// See DB-2859        
 		System.setProperty("zookeeper.sasl.client", "false");   
 		System.setProperty("zookeeper.sasl.serverconfig", "fake");
 		conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
 		conf.set(FileSystem.FS_DEFAULT_NAME_KEY,"file:///");
        ExportFile exportFile = new ExportFile(exportParams, failedTaskId, conf);
        OutputStream outputStream = exportFile.getOutputStream();
        outputStream.write(1);
        outputStream.close();
        assertTrue(expectedFile.exists());

        //
        // Given: JobResults that know of the failed task
        //
        JobStats jobStats = Mockito.mock(JobStats.class);
        JobResults jobResults = Mockito.mock(JobResults.class);
        when(jobResults.getJobStats()).thenReturn(jobStats);
        when(jobStats.getFailedTasks()).thenReturn(failedTaskIdList);


        //
        // When: we call clean
        //
        new ExportFailedTaskCleanup().cleanup(jobResults, exportParams);


        //
        // Then assert the export file is deleted.
        //
        assertFalse(expectedFile.exists());

    }


}
@@ -922,16 +922,162 @@
 eption %7B
+%0A        Configuration conf = SpliceConstants.config;%0A        // Mapr4.0 specific fix%0A        conf.set(FileSystem.FS_DEFAULT_NAME_KEY,%22file:///%22);
 %0A%0A      
@@ -1580,369 +1580,8 @@
 l);%0A
-        Configuration conf = SpliceConstants.config;%0A %09%09// Mapr4.0 specific fix%0A %09%09// See DB-2859        %0A %09%09System.setProperty(%22zookeeper.sasl.client%22, %22false%22);   %0A %09%09System.setProperty(%22zookeeper.sasl.serverconfig%22, %22fake%22);%0A %09%09conf.set(%22fs.hdfs.impl%22, %22org.apache.hadoop.hdfs.DistributedFileSystem%22);%0A %09%09conf.set(FileSystem.FS_DEFAULT_NAME_KEY,%22file:///%22);%0A
     

delete unnecessary system properties in UT