package com.poiji.deserialize;

import com.poiji.deserialize.model.Employee;
import com.poiji.bind.Poiji;
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.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.poiji.option.PoijiOptions.PoijiOptionsBuilder;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

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

    private String path;
    private List<Employee> expectedEmployess;

    public DerializersEmptyCellTest(String path, List<Employee> expectedEmployess) {
        this.path = path;
        this.expectedEmployess = expectedEmployess;
    }

    @Parameterized.Parameters(name = "{index}: ({0})={1}")
    public static Iterable<Object[]> queries() throws Exception {
        return Arrays.asList(new Object[][]{
                {"src/test/resources/employeeswithemptycells.xlsx", unmarshalling()},
        });
    }

    @Test
    public void shouldMapEmptyCellsAndSkipTwoRows() {

            PoijiOptions options = PoijiOptionsBuilder.settings(2).build();
            List<Employee> actualEmployees = Poiji.fromExcel(new File(path), Employee.class, options);

            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 expectedEmployee1 = expectedEmployess.get(0);
            Employee expectedEmployee2 = expectedEmployess.get(1);

            assertThat(actualEmployee1.toString(), is(expectedEmployee1.toString()));
            assertThat(actualEmployee2.toString(), is(expectedEmployee2.toString()));

    }

    private static List<Employee> unmarshalling() {
        List<Employee> employees = new ArrayList<>(2);

        Employee employee1 = new Employee();
        employee1.setEmployeeId(123123L);
        employee1.setSurname("Derue");
        employee1.setSingle(false);
        employee1.setAge(0);
        employee1.setBirthday("5/3/1997");
        employees.add(employee1);

        Employee employee2 = new Employee();
        employee2.setEmployeeId(135923L);
        employee2.setName("Paul");
        employee2.setSurname("Raul");
        employee2.setSingle(false);
        employee2.setAge(31);
        employee2.setBirthday("4/9/1986");
        employees.add(employee2);

        return employees;
    }
}

@@ -25,16 +25,45 @@
 alize;%0A%0A
+import com.poiji.bind.Poiji;%0A
 import c
@@ -112,34 +112,48 @@
 t com.poiji.
-bind.Poiji
+exception.PoijiExcelType
 ;%0Aimport com
@@ -298,16 +298,104 @@
 o.File;%0A
+import java.io.FileInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0A
 import j
@@ -713,16 +713,53 @@
 ertThat;
+%0Aimport static org.junit.Assert.fail;
 %0A%0A/**%0A *
@@ -936,16 +936,48 @@
 ployess;
+%0A    private boolean fromStream;
 %0A%0A    pu
@@ -1051,16 +1051,36 @@
 mployess
+, boolean fromStream
 ) %7B%0A    
@@ -1149,24 +1149,62 @@
 dEmployess;%0A
+        this.fromStream = fromStream;%0A
     %7D%0A%0A    @
@@ -1307,25 +1307,8 @@
 es()
- throws Exception
  %7B%0A 
@@ -1434,16 +1434,115 @@
 alling()
+, false%7D,%0A                %7B%22src/test/resources/employeeswithemptycells.xlsx%22, unmarshalling(), true
 %7D,%0A     
@@ -1628,20 +1628,16 @@
         
- 
-   
 PoijiOpt
@@ -1696,28 +1696,24 @@
 ();%0A        
-    
 List%3CEmploye
@@ -1714,16 +1714,385 @@
 mployee%3E
+ actualEmployees;%0A%0A        if (fromStream) %7B%0A            try (InputStream stream = new FileInputStream(new File(path))) %7B%0A                actualEmployees = Poiji.fromExcel(stream, PoijiExcelType.XLSX, Employee.class, options);%0A            %7D catch (IOException e) %7B%0A                fail(e.getMessage());%0A                return;%0A            %7D%0A        %7D else %7B%0A           
  actualE
@@ -2160,29 +2160,35 @@
 tions);%0A
-%0A
+   
      
+%7D%0A%0A
        
+ 
 assertTh
@@ -2224,36 +2224,32 @@
 lue());%0A        
-    
 assertThat(actua
@@ -2272,28 +2272,24 @@
 ), not(0));%0A
-    
         asse
@@ -2351,36 +2351,32 @@
 e()));%0A%0A        
-    
 Employee actualE
@@ -2402,36 +2402,32 @@
 ployees.get(0);%0A
-    
         Employee
@@ -2470,36 +2470,32 @@
 et(1);%0A%0A        
-    
 Employee expecte
@@ -2529,28 +2529,24 @@
 ess.get(0);%0A
-    
         Empl
@@ -2597,36 +2597,32 @@
 et(1);%0A%0A        
-    
 assertThat(actua
@@ -2675,28 +2675,24 @@
 String()));%0A
-    
         asse

update test case