package uk.ac.ebi.quickgo.index.ontology;

import uk.ac.ebi.quickgo.index.common.DocumentReaderException;
import uk.ac.ebi.quickgo.model.ontology.eco.ECOTerm;
import uk.ac.ebi.quickgo.model.ontology.eco.EvidenceCodeOntology;
import uk.ac.ebi.quickgo.model.ontology.generic.GenericTerm;
import uk.ac.ebi.quickgo.model.ontology.go.GOTerm;
import uk.ac.ebi.quickgo.model.ontology.go.GeneOntology;

import de.bechte.junit.runners.context.HierarchicalContextRunner;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.item.ExecutionContext;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.Is.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Created 11/01/16
 * @author Edd
 */
@RunWith(HierarchicalContextRunner.class)
public class OntologyReaderTest {

    private OntologyReader ontologyReader;
    private int docCount;

    public class DocReaderAccessingValidOntologies {

        @Before
        public void setUp() {
            GeneOntology go = mock(GeneOntology.class);
            EvidenceCodeOntology eco = mock(EvidenceCodeOntology.class);

            when(go.getTerms()).thenReturn(createGOTerms());
            when(eco.getTerms()).thenReturn(createECOTerms());

            // set document count
            docCount += go.getTerms().size() + eco.getTerms().size();

            Optional<GeneOntology> goOptional = Optional.of(go);
            Optional<EvidenceCodeOntology> ecoOptional = Optional.of(eco);
            ontologyReader = new OntologyReader(goOptional, ecoOptional);

            ontologyReader.open(new ExecutionContext());
        }

        @Test
        public void readsAllDocsWithoutError() throws Exception {
            int count = 0;
            while (ontologyReader.read() != null) {
                count++;
            }
            assertThat(count, is(docCount));
        }
    }

    public class DocReaderAccessingInvalidOntologies {
        private Optional<GeneOntology> validGOOptional;
        private Optional<EvidenceCodeOntology> validECOOptional;

        @Before
        public void setUp() {
            GeneOntology go = mock(GeneOntology.class);
            when(go.getTerms()).thenReturn(createGOTerms());

            EvidenceCodeOntology eco = mock(EvidenceCodeOntology.class);
            when(eco.getTerms()).thenReturn(createECOTerms());

            validGOOptional = Optional.of(go);
            validECOOptional = Optional.of(eco);
        }

        @Test(expected = DocumentReaderException.class)
        public void openingEmptyGOWillCauseDocumentReaderException() {
            ontologyReader = new OntologyReader(validGOOptional, Optional.empty());
            ontologyReader.open(new ExecutionContext());
        }

        @Test(expected = DocumentReaderException.class)
        public void openingEmptyECOWillCauseDocumentReaderException() {
            ontologyReader = new OntologyReader(Optional.empty(), validECOOptional);
            ontologyReader.open(new ExecutionContext());
        }

        @Test
        public void opensSuccessfully() {
            ontologyReader = new OntologyReader(validGOOptional, validECOOptional);
            ontologyReader.open(new ExecutionContext());
            assertThat(ontologyReader, is(not(nullValue())));
        }
    }

    private List<GenericTerm> createGOTerms() {
        GOTerm term = new GOTerm();
        term.setId("GO:0000001");
        return Collections.singletonList(term);
    }

    private List<GenericTerm> createECOTerms() {
        ECOTerm term1 = new ECOTerm();
        term1.setId("ECO:0000001");

        ECOTerm term2 = new ECOTerm();
        term2.setId("ECO:0000002");

        return Arrays.asList(term1, term2);
    }

    @After
    public void after() {
        ontologyReader.close();
    }

}

@@ -40,71 +40,8 @@
 y;%0A%0A
-import uk.ac.ebi.quickgo.index.common.DocumentReaderException;%0A
 impo
@@ -473,35 +473,8 @@
 st;%0A
-import java.util.Optional;%0A
 impo
@@ -1544,149 +1544,8 @@
 e();
-%0A%0A            Optional%3CGeneOntology%3E goOptional = Optional.of(go);%0A            Optional%3CEvidenceCodeOntology%3E ecoOptional = Optional.of(eco);
 %0A   
@@ -1595,33 +1595,16 @@
 r(go
-Optional, ecoOptional
+, eco
 );%0A
-%0A
     
@@ -1998,25 +1998,16 @@
 private 
-Optional%3C
 GeneOnto
@@ -2014,17 +2014,16 @@
 logy
-%3E
  validGO
 Opti
@@ -2018,24 +2018,16 @@
  validGO
-Optional
 ;%0A      
@@ -2040,17 +2040,8 @@
 ate 
-Optional%3C
 Evid
@@ -2056,17 +2056,16 @@
 Ontology
-%3E
  validEC
@@ -2065,24 +2065,16 @@
 validECO
-Optional
 ;%0A%0A     
@@ -2122,39 +2122,31 @@
             
-GeneOntology go
+validGO
  = mock(Gene
@@ -2171,34 +2171,39 @@
            when(
-go
+validGO
 .getTerms()).the
@@ -2245,32 +2245,16 @@
     
-EvidenceCodeOntology eco
+validECO
  = m
@@ -2295,35 +2295,40 @@
            when(
-eco
+validECO
 .getTerms()).the
@@ -2357,105 +2357,8 @@
 ());
-%0A%0A            validGOOptional = Optional.of(go);%0A            validECOOptional = Optional.of(eco);
 %0A   
@@ -2386,38 +2386,39 @@
 (expected = 
-DocumentReader
+IllegalArgument
 Exception.cl
@@ -2552,91 +2552,14 @@
 idGO
-Optional, Optional.empty());%0A            ontologyReader.open(new ExecutionContext()
+, null
 );%0A 
@@ -2593,30 +2593,31 @@
 ected = 
-DocumentReader
+IllegalArgument
 Exceptio
@@ -2749,99 +2749,22 @@
 der(
-Optional.empty(), validECOOptional);%0A            ontologyReader.open(new ExecutionContext()
+null, validECO
 );%0A 
@@ -2884,24 +2884,16 @@
 (validGO
-Optional
 , validE
@@ -2898,16 +2898,8 @@
 dECO
-Optional
 );%0A 
@@ -3498,24 +3498,51 @@
 () %7B%0A       
+ if(ontologyReader != null)
  ontologyRea

Remove DocumentReaderException, remove use of optional on ontology handles.