package uk.ac.ebi.quickgo.rest.controller.search;

import uk.ac.ebi.quickgo.repo.solr.document.ontology.OntologyDocMocker;
import uk.ac.ebi.quickgo.repo.solr.document.ontology.OntologyDocument;
import uk.ac.ebi.quickgo.repo.solr.io.ontology.OntologyRepository;

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class OntologySearchIT extends SearchControllerSetup {
    @Autowired
    private OntologyRepository repository;

    private static final String ONTOLOGY_RESOURCE_URL = SEARCH_RESOURCE_URL + "/ontology";

    @Before
    public void setUp() throws Exception {
        repository.deleteAll();
        resourceUrl = ONTOLOGY_RESOURCE_URL;
    }

    @Test
    public void requestWithInValidFacetFieldReturns400Response() throws Exception {
        OntologyDocument doc1 = OntologyDocMocker.createGODoc("GO:0000001", "go1");
        OntologyDocument doc2 = OntologyDocMocker.createGODoc("GO:0000002", "go2");
        OntologyDocument doc3 = OntologyDocMocker.createGODoc("GO:0000003", "go3");

        saveToRepository(doc1, doc2, doc3);

        checkInvalidFacetResponse("go", "incorrect_field");
    }

    @Test
    public void requestWithValidFacetFieldReturnsResponseWithFacetInResult() throws Exception {
        OntologyDocument doc1 = OntologyDocMocker.createGODoc("GO:0000001", "go1");
        OntologyDocument doc2 = OntologyDocMocker.createGODoc("GO:0000002", "go2");
        OntologyDocument doc3 = OntologyDocMocker.createGODoc("GO:0000003", "go3");

        saveToRepository(doc1, doc2, doc3);

        checkValidFacetResponse("go", "aspect");
    }

    @Test
    public void requestWithMultipleValidFacetFieldsReturnsResponseWithMultipleFacetsInResult() throws Exception {
        OntologyDocument doc1 = OntologyDocMocker.createGODoc("GO:0000001", "go1");
        OntologyDocument doc2 = OntologyDocMocker.createGODoc("GO:0000002", "go2");
        OntologyDocument doc3 = OntologyDocMocker.createGODoc("GO:0000003", "go3");

        saveToRepository(doc1, doc2, doc3);

        checkValidFacetResponse("go", "aspect", "ontologyType");
    }

    private void saveToRepository(OntologyDocument... documents) {
        for(OntologyDocument doc : documents) {
            repository.save(doc);
        }
    }
}
@@ -1620,24 +1620,54 @@
 e(%22go%22, 
-%22aspect%22
+OntologyFieldSpec.Search.aspect.name()
 );%0A    %7D
@@ -2132,19 +2132,89 @@
 o%22, 
-%22aspect%22, %22
+OntologyFieldSpec.Search.aspect.name(),%0A                OntologyFieldSpec.Search.
 onto
@@ -2221,17 +2221,23 @@
 logyType
-%22
+.name()
 );%0A    %7D
@@ -2297,32 +2297,32 @@
 .. documents) %7B%0A
-
         for(Onto
@@ -2316,16 +2316,17 @@
      for
+ 
 (Ontolog

Use OntologyFieldSpec.Search enum for the search fields, instead of strings.