package org.folio.rest.persist.criteria;

import static org.junit.Assert.assertEquals;

import org.folio.rest.persist.Criteria.Criteria;
import org.junit.Test;

/**
 * @author shale
 *
 */
public class CriteriaTest {

  @Test
  public void testCriteria() {
    try {
      Criteria noOp = new Criteria();
      noOp.addField("'personal'");
      assertEquals("", noOp.toString());

      Criteria noField = new Criteria();
      noField.setOperation("=");
      assertEquals("", noField.toString());

      Criteria schema = new Criteria();
      schema.addField("'personal'").addField("'lastName'").setOperation("=").setValue("123");
      assertEquals("(jsonb->'personal'->>'lastName') =  '123'", schema.toString());

      schema = new Criteria();
      schema.addField("'personal'").addField("'lastName'").setOperation("=").setValue("123");
      schema.setAlias("foo");
      assertEquals("(foo.jsonb->'personal'->>'lastName') =  '123'", schema.toString());

      schema = new Criteria();
      schema.addField("'personal'").addField("'lastName'").setOperation("=").setValue("123");
      assertEquals("(jsonb->'personal'->>'lastName') =  '123'", schema.toString());
      schema.setNotQuery(true);
      assertEquals("( NOT (jsonb->'personal'->>'lastName') =  '123')", schema.toString());

      schema = new Criteria();
      schema.addField("'personal'").setOperation("@>").setValue("{\"a\":\"b\"}");
      assertEquals("(jsonb->'personal') @>  '{\"a\":\"b\"}'" , schema.toString());

      schema = new Criteria();
      schema.setAlias("'personal'").addField("'foo'").setValue("123");
      assertEquals("('personal'.jsonb->>'foo')", schema.toString());

      //guess op type is string since not null, numeric or boolean
      Criteria c = new Criteria();
      c.addField("'price'").addField("'po_currency'").addField("'value'");
      c.setOperation("like");
      c.setValue("USD");
      assertEquals("(jsonb->'price'->'po_currency'->>'value') like  'USD'", c.toString().trim());

      //guess op type is boolean by checking operation
      Criteria d = new Criteria();
      d.addField("'rush'");
      d.setOperation("IS FALSE");
      d.setValue(null);
      assertEquals("(jsonb->>'rush') IS FALSE null", d.toString().trim());

      //guess op type is boolean by checking value
      Criteria aa = new Criteria();
      aa.addField("'rush'");
      aa.setOperation( "!=" );
      aa.setValue( "true" );
      assertEquals("(jsonb->>'rush') !=  'true'", aa.toString().trim());


      Criteria nb = new Criteria();
      nb.addField("'transaction'");
      nb.addField("'status'");
      nb.setOperation("=");
      nb.setValue("rollbackComplete");
      nb.setArray(true);
      assertEquals("(transaction->'status') =  'rollbackComplete'", nb.toString());
      assertEquals("transaction" , nb.getSelect().getSnippet());
      assertEquals("jsonb_array_elements(jsonb->'transaction')", nb.getFrom().getSnippet());

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

@@ -57,106 +57,465 @@
 org.
-junit.Assert.assertEquals;%0A%0Aimport org.folio.rest.persist.Criteria.Criteria;%0Aimport org.junit.Test
+hamcrest.CoreMatchers.is;%0Aimport static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertThat;%0Aimport static org.junit.jupiter.params.provider.Arguments.arguments;%0A%0Aimport java.util.stream.Stream;%0A%0Aimport org.folio.rest.persist.Criteria.Criteria;%0Aimport org.junit.jupiter.api.Test;%0Aimport org.junit.jupiter.params.ParameterizedTest;%0Aimport org.junit.jupiter.params.provider.Arguments;%0Aimport org.junit.jupiter.params.provider.MethodSource
 ;%0A%0A/
@@ -3354,11 +3354,598 @@
    %7D%0A  %7D
+%0A%0A  @ParameterizedTest%0A  @MethodSource%0A  void criteriaValue(String value, String sql) %7B%0A    Criteria criteria = new Criteria().addField(%22'f'%22).setOperation(%22=%22).setValue(value);%0A    assertThat(criteria.toString().replace(%22 %22, %22%22), is(%22(jsonb-%3E%3E'f')=%22 + sql));%0A  %7D%0A%0A  static Stream%3CArguments%3E criteriaValue() %7B%0A    return Stream.of(%0A        arguments(%22a%22,       %22'a'%22),%0A        //arguments(%22'a'%22,     %22'''a'''%22),%0A        //arguments(%22O'Kapi%22,  %22'O''Kapi'%22),%0A        //arguments(%22'%22,       %22''''%22),%0A        //arguments(%22Up/%5C%5CUp%22, %22'Up/%5C%5C%5C%5CUp'%22),%0A        arguments(%22%22,        %22''%22)%0A    );%0A%7D
 %0A%7D%0A

Add criteriaValue SQL masking unit tests