package ru.ydn.wicket.wicketorientdb.filter;

import com.orientechnologies.orient.core.record.impl.ODocument;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import ru.ydn.wicket.wicketorientdb.model.OQueryModel;
import ru.ydn.wicket.wicketorientdb.utils.query.filter.number.NumberFilterCriteriaCreator;

import java.util.Arrays;

import static org.junit.Assert.assertTrue;
import static ru.ydn.wicket.wicketorientdb.filter.ITesterFilterConstants.*;

public class TestFilters {

    @ClassRule
    public static WicketOrientDbFilterTesterScope wicket = new WicketOrientDbFilterTesterScope();

    private OQueryModel<ODocument> queryModel;

    @Before
    public void setUpOQueryModel() {
        queryModel = new OQueryModel<>("SELECT FROM " + TEST_CLASS_NAME);
    }

    @After
    public void destroyOQueryModel() {
        queryModel.detach();
        queryModel = null;
    }

    @Test
    public void testNumberEqualsFilter() {
        NumberFilterCriteriaCreator creator = new NumberFilterCriteriaCreator();
        queryModel.setFilterCriteria(creator.createEqualsFilterCriteria(NUMBER_FIELD, NUM_VALUE_1, true));
        assertTrue(queryModel.getObject().size() == 1);
        assertTrue(NUM_VALUE_1 == (Integer) queryModel.getObject().get(0).field(NUMBER_FIELD));
        queryModel.detach();

        queryModel.setFilterCriteria(creator.createEqualsFilterCriteria(NUMBER_FIELD, NUM_VALUE_2, true));
        assertTrue(queryModel.getObject().size() == 1);
        assertTrue("expected: " + NUM_VALUE_2 + " get: " + queryModel.getObject().get(0).field(NUMBER_FIELD),
                NUM_VALUE_2 == (Integer) queryModel.getObject().get(0).field(NUMBER_FIELD));
        queryModel.detach();

        queryModel.setFilterCriteria(creator.createEqualsFilterCriteria(NUMBER_FIELD, NUM_VALUE_3, false));
        assertTrue(queryModel.getObject().size() == DOCUMENTS_NUM - 1);
        for (ODocument document : queryModel.getObject()) {
            assertTrue((Integer) document.field(NUMBER_FIELD) != NUM_VALUE_3);
        }
    }

    @Test
    public void testNumberRangeFilter() {
        NumberFilterCriteriaCreator creator = new NumberFilterCriteriaCreator();
        queryModel.setFilterCriteria(creator.createRangeFilterCriteria(NUMBER_FIELD, NUM_VALUE_1, NUM_VALUE_3, true));
        assertTrue(queryModel.getObject().size() == DOCUMENTS_NUM - 1);
        assertTrue((Integer) queryModel.getObject().get(0).field(NUMBER_FIELD) == NUM_VALUE_1);
        assertTrue((Integer) queryModel.getObject().get(1).field(NUMBER_FIELD) == NUM_VALUE_2);
        assertTrue((Integer) queryModel.getObject().get(2).field(NUMBER_FIELD) == NUM_VALUE_3);
        queryModel.detach();

        queryModel.setFilterCriteria(creator.createRangeFilterCriteria(NUMBER_FIELD, NUM_VALUE_1, NUM_VALUE_3, false));
        assertTrue(queryModel.getObject().size() == 1);
        assertTrue((Integer) queryModel.getObject().get(0).field(NUMBER_FIELD) == NUM_VALUE_4);
    }

    @Test
    public void testNumberValuesFilter() {
        NumberFilterCriteriaCreator creator = new NumberFilterCriteriaCreator();
        queryModel.setFilterCriteria(creator.createValuesFilterCriteria(NUMBER_FIELD,
                Arrays.asList(NUM_VALUE_1, NUM_VALUE_4), true));
        assertTrue(queryModel.getObject().size() == 2);
        assertTrue((Integer) queryModel.getObject().get(0).field(NUMBER_FIELD) == NUM_VALUE_1);
        assertTrue((Integer) queryModel.getObject().get(1).field(NUMBER_FIELD) == NUM_VALUE_4);
        queryModel.detach();

        queryModel.setFilterCriteria(creator.createValuesFilterCriteria(NUMBER_FIELD,
                Arrays.asList(NUM_VALUE_1, NUM_VALUE_4), false));
        assertTrue(queryModel.getObject().size() == 2);
        assertTrue((Integer) queryModel.getObject().get(0).field(NUMBER_FIELD) == NUM_VALUE_2);
        assertTrue((Integer) queryModel.getObject().get(1).field(NUMBER_FIELD) == NUM_VALUE_3);
    }
}

@@ -2420,154 +2420,97 @@
     
-assertTrue((Integer) queryModel.getObject().get(0).field(NUMBER_FIELD) == NUM_VALUE_1);%0A        assertTrue((Integer) queryModel.getObject().get(1)
+for (ODocument document : queryModel.getObject()) %7B%0A            Integer number = document
 .fie
@@ -2525,34 +2525,22 @@
 R_FIELD)
- == NUM_VALUE_2);%0A
+;%0A    
         
@@ -2554,67 +2554,92 @@
 rue(
-(Integer) queryModel.getObject().get(2).field(NUMBER_FIELD)
+%22Error in range filtering%22, number == NUM_VALUE_1 %7C%7C number == NUM_VALUE_2 %7C%7C number
  == 
@@ -2644,32 +2644,42 @@
 = NUM_VALUE_3);%0A
+        %7D%0A
         queryMod
@@ -3324,58 +3324,97 @@
     
-assertTrue((Integer) queryModel.getObject().get(0)
+for (ODocument document : queryModel.getObject()) %7B%0A            Integer number = document
 .fie
@@ -3433,26 +3433,14 @@
 ELD)
- == NUM_VALUE_1);%0A
+;%0A    
     
@@ -3458,67 +3458,93 @@
 rue(
-(Integer) queryModel.getObject().get(1).field(NUMBER_FIELD)
+%22Error in values join filtering%22,%0A                    number == NUM_VALUE_1 %7C%7C number
  == 
@@ -3553,24 +3553,34 @@
 M_VALUE_4);%0A
+        %7D%0A
         quer
@@ -3797,32 +3797,290 @@
 ).size() == 2);%0A
+        for (ODocument document : queryModel.getObject()) %7B%0A            Integer number = document.field(NUMBER_FIELD);%0A            assertTrue(%22Error in values no join filtering%22,%0A                    number == NUM_VALUE_2 %7C%7C number == NUM_VALUE_3);%0A        %7D%0A
         assertTr

Fix test