package umm3601.digitalDisplayGarden;


import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;

import static com.mongodb.client.model.Filters.eq;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

/**
 * Created by benek020 on 3/6/17.
 */
public class TestExcelParser {

    public MongoClient mongoClient = new MongoClient();
    public MongoDatabase testDB;
    public ExcelParser parser;
    public InputStream fromFile;

    @Before
    public void clearAndPopulateDatabase(){
        mongoClient.dropDatabase("test");
        testDB = mongoClient.getDatabase("test");
        fromFile = this.getClass().getResourceAsStream("/IDPH_STD_Illinois_By_County_By_Sex.xlsx");
        parser = new ExcelParser(fromFile);
    }



    @Test
    public void testSpeadsheetToDoubleArray(){
        String[][] plantArray = parser.extractFromXLSX(fromFile);
        //printDoubleArray(plantArray);

        assertEquals(1668, plantArray.length);
        assertEquals(plantArray[40].length, plantArray[1234].length);
        assertEquals("ALEXANDER", plantArray[5][2]);

    }

    @Test
    public void testCollapse(){
        String[][] plantArray = parser.extractFromXLSX(fromFile);
        //System.out.println(plantArray.length);
        //printDoubleArray(plantArray);

        plantArray = parser.collapseHorizontally(plantArray);
        plantArray = parser.collapseVertically(plantArray);

        //printDoubleArray(plantArray);

        assertEquals(1668, plantArray.length);
        assertEquals(10, plantArray[30].length);
        assertEquals(10, plantArray[0].length);
        assertEquals(10, plantArray[3].length);
    }

    @Test
    public void testReplaceNulls(){
        String[][] plantArray = parser.extractFromXLSX(fromFile);
        plantArray = parser.collapseHorizontally(plantArray);
        plantArray = parser.collapseVertically(plantArray);
        parser.replaceNulls(plantArray);

        for (String[] row : plantArray){
            for (String cell : row){
                assertNotNull(cell);
            }
        }
    }

    @Test
    public void testPopulateDatabase(){
        String[][] plantArray = parser.extractFromXLSX(fromFile);
        plantArray = parser.collapseHorizontally(plantArray);
        plantArray = parser.collapseVertically(plantArray);
        parser.replaceNulls(plantArray);

        parser.populateDatabase(plantArray, "an arbitrary ID");
        MongoCollection plants = testDB.getCollection("plants");


        assertEquals(1664, plants.count());
        assertEquals(16, plants.count(eq("Sort", "104")));
    }



    private static void printDoubleArray(String[][] input){
        for(int i = 0; i < input.length; i++){
            if (!(input[i] == (null))) {
                for (int j = 0; j < input[i].length; j++) {
                    //System.out.print(" | " + "i: " + i + " j: " + j + " value: " + input[i][j] );
                    System.out.print(" | " + input[i][j]);
                }
                System.out.println();
                System.out.println("-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
            }
        }
    }
}

@@ -807,42 +807,25 @@
 m(%22/
-IDPH_STD_Illinois_By_County_By_Sex
+AccessionList2016
 .xls
@@ -1060,35 +1060,35 @@
   assertEquals(1
-668
+000
 , plantArray.len
@@ -1152,12 +1152,11 @@
 ray%5B
-1234
+963
 %5D.le
@@ -1188,17 +1188,94 @@
 ls(%22
-ALEXANDER
+2016 Accession List: Steve's Design%22, plantArray%5B0%5D%5B1%5D);%0A        assertEquals(%22Begonia
 %22, p
@@ -1288,12 +1288,12 @@
 ray%5B
-5%5D%5B2
+6%5D%5B1
 %5D);%0A
@@ -1687,12 +1687,11 @@
 als(
-1668
+362
 , pl
@@ -1721,34 +1721,33 @@
    assertEquals(
-10
+8
 , plantArray%5B30%5D
@@ -1769,34 +1769,33 @@
    assertEquals(
-10
+8
 , plantArray%5B0%5D.
@@ -1824,18 +1824,17 @@
 tEquals(
-10
+8
 , plantA
@@ -2715,12 +2715,11 @@
 als(
-1664
+286
 , pl
@@ -2755,17 +2755,17 @@
 Equals(1
-6
+1
 , plants
@@ -2779,19 +2779,30 @@
 eq(%22
-Sort%22, %22104
+commonName%22, %22Geranium
 %22)))

Fix tests in TestExcelParser