package dk.lessismore.nojpa.solr;

import dk.lessismore.nojpa.db.methodquery.NList;
import dk.lessismore.nojpa.db.methodquery.NQL;
import dk.lessismore.nojpa.db.testmodel.Car;
import dk.lessismore.nojpa.db.testmodel.DatabaseCreatorTest;
import dk.lessismore.nojpa.db.testmodel.Person;
import dk.lessismore.nojpa.reflection.db.DatabaseCreator;
import dk.lessismore.nojpa.reflection.db.model.ModelObjectSearchService;
import dk.lessismore.nojpa.reflection.db.model.ModelObjectService;
import dk.lessismore.nojpa.reflection.db.model.SolrServiceImpl;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.junit.Test;

/**
 * Created by niakoi on 7/23/14.
 */
public class NQLTest {
    @Test
    public void test01() {
        DatabaseCreator.createDatabase("dk.lessismore.nojpa.db.testmodel");
        SolrServiceImpl solrService = new SolrServiceImpl();
        solrService.setCoreName("nojpa");
        solrService.setCleanOnStartup(true);

        ModelObjectSearchService.addSolrServer(Person.class, solrService.getServer());
        Person mPerson = NQL.mock(Person.class);

        for (int i = 0; i < 10; i++) {
            Person person = ModelObjectService.create(Person.class);
            person.setName("person " + i);
            if (i < 7) {
                Car car = ModelObjectService.create(Car.class);
                person.setCar(car);
            }
            ModelObjectService.save(person);
            ModelObjectSearchService.put(person);
        }
        solrService.commit();

        NList<Person> personsWithoutCar = NQL.search(mPerson).search(NQL.all(NQL.has(mPerson.getName(), NQL.Comp.EQUAL, "Person"), NQL.hasNull(mPerson.getCar()))).getList();
        System.out.println("personsWithoutCar.getNumberFound() = " + personsWithoutCar.getNumberFound());
        for (Person person : personsWithoutCar) {
            System.out.println("WITHOUT person = " + person.getName());
        }

        NList<Person> personsWithCar = NQL.search(mPerson).search(NQL.hasNotNull(mPerson.getCar())).getList();
        System.out.println("personsWithCar.getNumberFound() = " + personsWithCar.getNumberFound());
        for (Person person : personsWithCar) {
            System.out.println("WITH person = " + person.getName());
        }

        NList<Person> persons = NQL.search(mPerson).getList();
        System.out.println("persons.getNumberFound() = " + persons.getNumberFound());
        for (Person person : persons) {
            System.out.println("ALL person = " + person.getName());
        }

        QueryResponse response = solrService.query(new SolrQuery("( (_Person_name__TXT:( Person )) AND -(_Person_car__ID:[\"\" TO *]) )"));
        System.out.println("response = " + response);


    }
}

@@ -540,16 +540,47 @@
 ceImpl;%0A
+import junit.framework.Assert;%0A
 import o
@@ -1773,36 +1773,36 @@
         
-System.out.println(%22
+Assert.assertEquals(
 personsW
@@ -1831,789 +1831,488 @@
 nd()
- = %22 + personsWithoutCar.getNumberFound());%0A        for (Person person : personsWithoutCar) %7B%0A            System.out.println(%22WITHOUT person = %22 + person.getName());%0A        %7D%0A%0A        NList%3CPerson%3E personsWithCar = NQL.search(mPerson).search(NQL.hasNotNull(mPerson.getCar())).getList();%0A        System.out.println(%22personsWithCar.getNumberFound() = %22 + personsWithCar.getNumberFound());%0A        for (Person person : personsWithCar) %7B%0A            System.out.println(%22WITH person = %22 + person.getName());%0A        %7D%0A%0A        NList%3CPerson%3E persons = NQL.search(mPerson).getList();%0A        System.out.println(%22persons.getNumberFound() = %22 + persons.getNumberFound());%0A        for (Person person : persons) %7B%0A            System.out.println(%22ALL person = %22 + person.getName());%0A        %7D
+, 3);%0A%0A        NList%3CPerson%3E personsWithoutCar2 = NQL.search(mPerson).searchIsNull(mPerson.getCar()).getList();%0A        Assert.assertEquals(personsWithoutCar2.getNumberFound(), 3);%0A%0A        NList%3CPerson%3E personsWithCar = NQL.search(mPerson).search(NQL.hasNotNull(mPerson.getCar())).getList();%0A        Assert.assertEquals(personsWithCar.getNumberFound(), 7);%0A%0A        NList%3CPerson%3E persons = NQL.search(mPerson).getList();%0A        Assert.assertEquals(persons.getNumberFound(), 10);
 %0A%0A  
@@ -2461,56 +2461,72 @@
     
-System.out.println(%22response = %22 + response
+Assert.assertEquals(response.getResults().getNumFound(), 3
 );%0A%0A%0A
+%0A
     

on has null