package org.sil.bloom.reader.models;

import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;

/**
 * Created by Thomson on 10/20/2017.
 * These tests run on a real device (or emulator) which makes them very slow.
 * If you don't need some real-device behavior, put your test in the test folder BookCollectionTest,
 * rather than here in androidTest.
 */
public class BookCollectionTest {
    // This test requires a real device because infuriatingly JSON object parsing is not supported
    // in the regular unit test environment, and setShelvesAndTitleOfBook uses it.
    @Test
    public void setBookFilters_bookOnTwoShelves_addsThem() throws Exception {
        // suggested for a file in test assets using a subclass of InstrumentationTestCase. But that is deprecated.
        //InputStream testInput = getInstrumentation().getTargetContext().getResources().getAssets().open("testmeta.bloompub");
        // Looks for a file in a parallel directory.
        // Since this file is app\src\androidTest\java\org\sil\bloom\reader\models\BookCollectionTest.java,
        // the file it looks for is app\src\androidTest\resources\org\sil\bloom\reader\models\testmeta.bloompub
        // That is a version-controlled test data file (described further below).
        InputStream testInput = getClass().getResourceAsStream("testmeta.bloompub");
        String [] suffixesToTest = {"bloompub", "bloomd"};
        for (String suffix : suffixesToTest){
            // Botheration! We're not allowed to know the path to one of our assets.
            // Copy it to a temp file.
            File file = File.createTempFile("some book", suffix);
            file.deleteOnExit();
            OutputStream output = new FileOutputStream(file);
            byte[] buffer = new byte[4 * 1024]; // or other buffer size
            int read;

            while ((read = testInput.read(buffer)) != -1) {
                output.write(buffer, 0, read);
            }
            output.flush();
            testInput.close();
            // so now file is a temp zip file containing meta.json containing
            // "tags":["Animals", "bloomshelf:Level 2", "bloomshelf:rise/PNG"]
            BookOrShelf book = new BookOrShelf(file.getPath());
            BookCollection.setShelvesAndTitleOfBook(book);
            assertThat(book.isBookInShelf("Level 2"), is(true));
            assertThat(book.isBookInShelf("rise/PNG"), is(true));
            assertThat(book.isBookInShelf("Animals"), is(false));
        }
    }
}
@@ -805,16 +805,127 @@
 ption %7B%0A
+        String %5B%5D suffixesToTest = %7B%22.bloompub%22, %22.bloomd%22%7D;%0A        for (String suffix : suffixesToTest)%7B%0A    
         
@@ -1040,16 +1040,20 @@
         
+  
+  
 //InputS
@@ -1172,16 +1172,20 @@
         
+    
 // Looks
@@ -1229,16 +1229,20 @@
         
+ 
+   
 // Since
@@ -1333,16 +1333,20 @@
 t.java,%0A
+    
         
@@ -1453,24 +1453,28 @@
 pub%0A        
+ 
+   
 // That is a
@@ -1535,16 +1535,20 @@
 below).%0A
+    
         
@@ -1628,112 +1628,8 @@
 %22);%0A
-        String %5B%5D suffixesToTest = %7B%22bloompub%22, %22bloomd%22%7D;%0A        for (String suffix : suffixesToTest)%7B
 %0A   

Fix test (BL-10036)