/*
 *  The MIT License
 *
 *   Copyright (c) 2015, Mahmoud Ben Hassine (mahmoud@benhassine.fr)
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

package org.easybatch.core.reader;

import org.easybatch.core.api.Record;
import org.easybatch.core.api.Report;
import org.easybatch.core.processor.RecordCollector;
import org.easybatch.core.record.MultiRecord;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.easybatch.core.impl.EngineBuilder.aNewEngine;
import static org.easybatch.core.util.Utils.FILE_SEPARATOR;
import static org.easybatch.core.util.Utils.JAVA_IO_TMPDIR;

/**
 * Test class for {@link FileMultiRecordReader}.
 * 
 * @author Mahmoud Ben Hassine (mahmoud@benhassine.fr)
 */
public class FileMultiRecordReaderTest {

    private static final int CHUNK_SIZE = 2;

    private File dataSource;

    private File test1, test2, test3, test4;

    private FileMultiRecordReader fileMultiRecordReader;

    private Comparator<Record> recordComparator;

    @Before
    public void setUp() throws Exception {
        // TODO use https://github.com/google/jimfs when moving to Java 7
        dataSource = new File(JAVA_IO_TMPDIR + FILE_SEPARATOR + "testFileMultiRecordReader");
        dataSource.mkdir();
        createTestFiles(dataSource);
        fileMultiRecordReader = new FileMultiRecordReader(CHUNK_SIZE, dataSource);

        // Needed to sort FileRecords since the order of File.listFiles() is OS dependant.
        recordComparator = new Comparator<Record>() {
            @Override
            public int compare(Record record1, Record record2) {
                return record1.getPayload().toString().compareTo(record2.getPayload().toString());
            }
        };
    }

    @Test
    public void testMultiFileReading() throws Exception {
        Report report = aNewEngine()
                .reader(fileMultiRecordReader)
                .processor(new RecordCollector<MultiRecord>())
                .build().call();

        List<MultiRecord> multiRecords = (List<MultiRecord>) report.getBatchResult();

        assertThat(multiRecords).isNotNull().hasSize(2);

        MultiRecord chunk1 = multiRecords.get(0);
        List<Record> records = chunk1.getPayload();
        Collections.sort(records, recordComparator);
        assertThat(records.size()).isEqualTo(2);
        assertThat(records.get(0).getPayload().toString()).endsWith("test1.txt");
        assertThat(records.get(1).getPayload().toString()).endsWith("test2.txt");
        
        MultiRecord chunk2 = multiRecords.get(1);
        Collections.sort(records, recordComparator);
        records = chunk2.getPayload();
        assertThat(records.size()).isEqualTo(2);
        assertThat(records.get(0).getPayload().toString()).endsWith("test3.txt");
        assertThat(records.get(1).getPayload().toString()).endsWith("test4.txt");
    }

    @After
    public void tearDown() throws Exception {
        cleanUpTestFiles();
    }

    private void createTestFiles(File dataSource) throws IOException {
        String basePath = dataSource.getAbsolutePath() + FILE_SEPARATOR;
        test1 = new File(basePath + "test1.txt");
        test2 = new File(basePath + "test2.txt");
        test3 = new File(basePath + "test3.txt");
        test4 = new File(basePath + "test4.txt");
        test1.createNewFile();
        test2.createNewFile();
        test3.createNewFile();
        test4.createNewFile();
    }

    private void cleanUpTestFiles() {
        test1.delete();
        test2.delete();
        test3.delete();
        test4.delete();
        dataSource.delete();
    }

}
@@ -1535,67 +1535,8 @@
 on;%0A
-import java.util.Collections;%0Aimport java.util.Comparator;%0A
 impo
@@ -2140,58 +2140,8 @@
 r;%0A%0A
-    private Comparator%3CRecord%3E recordComparator;%0A%0A
     
@@ -2510,365 +2510,8 @@
 ce);
-%0A%0A        // Needed to sort FileRecords since the order of File.listFiles() is OS dependant.%0A        recordComparator = new Comparator%3CRecord%3E() %7B%0A            @Override%0A            public int compare(Record record1, Record record2) %7B%0A                return record1.getPayload().toString().compareTo(record2.getPayload().toString());%0A            %7D%0A        %7D;
 %0A   
@@ -2958,16 +2958,67 @@
 get(0);%0A
+        MultiRecord chunk2 = multiRecords.get(1);%0A%0A
         
@@ -3073,627 +3073,135 @@
     
-Collections.sort(records, recordComparator);%0A        assertThat(records.size()).isEqualTo(2);%0A        assertThat(records.get(0).getPayload().toString()).endsWith(%22test1.txt%22);%0A        assertThat(records.get(1).getPayload().toString()).endsWith(%22test2.txt%22);%0A        %0A        MultiRecord chunk2 = multiRecords.get(1);%0A        Collections.sort(records, recordComparator);%0A        records = chunk2.getPayload();%0A        assertThat(records.size()).isEqualTo(2);%0A        assertThat(records.get(0).getPayload().toString()).endsWith(%22test3.txt%22);%0A        assertThat(records.get(1).getPayload().toString()).endsWith(%22test4.txt%22
+assertThat(records.size()).isEqualTo(2);%0A%0A        records = chunk2.getPayload();%0A        assertThat(records.size()).isEqualTo(2
 );%0A 

remove assertions on file names since their order is random