package com.poiji.deserialize;

import com.poiji.bind.Poiji;
import com.poiji.deserialize.model.byid.Employee;
import com.poiji.exception.PoijiException;
import com.poiji.option.PoijiOptions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

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

import static com.poiji.util.Data.unmarshallingDeserialize;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

/**
 * Created by hakan on 17/01/2017.
 */
@RunWith(Parameterized.class)
public class DeserializersPasswordTest {

    private String path;
    private List<Employee> expectedEmployess;
    private Class<?> expectedException;
    private int indexSheet;

    public DeserializersPasswordTest(String path,
                                     List<Employee> expectedEmployess,
                                     Class<?> expectedException,
                                     int indexSheet) {
        this.path = path;
        this.expectedEmployess = expectedEmployess;
        this.expectedException = expectedException;
        this.indexSheet = indexSheet;
    }

    @Parameterized.Parameters(name = "{index}: ({0})={1}")
    public static Iterable<Object[]> queries() {
        return Arrays.asList(new Object[][]{
                {"src/test/resources/employees.xlsx", unmarshallingDeserialize(), null, -1},
                {"src/test/resources/employees_sheet2.xlsx", unmarshallingDeserialize(), null, 1},
                {"src/test/resources/cloud.xls", unmarshallingDeserialize(), PoijiException.class, -1},
                {"src/test/resources/cloud", unmarshallingDeserialize(), PoijiException.class, -1},
        });
    }

    @Test
    public void shouldMapExcelToJava() {

        try {

            List<Employee> actualEmployees;
            if (indexSheet == 1) {
                PoijiOptions poijiOptions = PoijiOptions.PoijiOptionsBuilder.settings().sheetIndex(indexSheet).build();
                actualEmployees = Poiji.fromExcel(new File(path), Employee.class, poijiOptions);
            } else {
                actualEmployees = Poiji.fromExcel(new File(path), Employee.class);
            }

            assertThat(actualEmployees, notNullValue());
            assertThat(actualEmployees.size(), not(0));
            assertThat(actualEmployees.size(), is(expectedEmployess.size()));

            Employee actualEmployee1 = actualEmployees.get(0);
            Employee actualEmployee2 = actualEmployees.get(1);
            Employee actualEmployee3 = actualEmployees.get(2);

            Employee expectedEmployee1 = expectedEmployess.get(0);
            Employee expectedEmployee2 = expectedEmployess.get(1);
            Employee expectedEmployee3 = expectedEmployess.get(2);

            assertThat(actualEmployee1.toString(), is(expectedEmployee1.toString()));
            assertThat(actualEmployee2.toString(), is(expectedEmployee2.toString()));
            assertThat(actualEmployee3.toString(), is(expectedEmployee3.toString()));
        } catch (Exception e) {
            if (expectedException == null) {
                fail(e.getMessage());
            } else {
                assertThat(e, instanceOf(expectedException));
            }
        }
    }

}

@@ -108,51 +108,8 @@
 ee;%0A
-import com.poiji.exception.PoijiException;%0A
 impo
@@ -676,18 +676,18 @@
  on 
-17/01
+22/06
 /201
-7
+8
 .%0A *
@@ -883,30 +883,31 @@
 private 
-int indexSheet
+String password
 ;%0A%0A    p
@@ -1128,22 +1128,23 @@
     
-int indexSheet
+String password
 ) %7B%0A
@@ -1290,31 +1290,27 @@
 his.
-indexSheet = indexSheet
+password = password
 ;%0A  
@@ -1513,16 +1513,25 @@
 mployees
+-password
 .xlsx%22, 
@@ -1564,18 +1564,22 @@
 , null, 
--1
+%221234%22
 %7D,%0A     
@@ -1623,262 +1623,64 @@
 yees
-_sheet2.xlsx%22, unmarshallingDeserialize(), null, 1%7D,%0A                %7B%22src/test/resources/cloud.xls%22, unmarshallingDeserialize(), PoijiException.class, -1%7D,%0A                %7B%22src/test/resources/cloud%22, unmarshallingDeserialize(), PoijiException.class, -1
+-password.xls%22, unmarshallingDeserialize(), null, %229876%22
 %7D,%0A 
@@ -1768,91 +1768,8 @@
  %7B%0A%0A
-            List%3CEmployee%3E actualEmployees;%0A            if (indexSheet == 1) %7B%0A    
     
@@ -1852,29 +1852,25 @@
 s().
-sheetIndex(indexSheet
+password(password
 ).bu
@@ -1880,35 +1880,46 @@
 ();%0A            
-   
+List%3CEmployee%3E
  actualEmployees
@@ -1987,126 +1987,8 @@
 ns);
-%0A            %7D else %7B%0A                actualEmployees = Poiji.fromExcel(new File(path), Employee.class);%0A            %7D
 %0A%0A  

update test class