@Test
  public void clientEscaping() {
    final String filter =
        getClient()
            .getFilterFactory()
            .eq(
                getClient().getFilterFactory().getArgFactory().property("PropertyString"),
                getClient()
                    .getFilterFactory()
                    .getArgFactory()
                    .literal("First Resource - positive values"))
            .build();

    final URI uri =
        getClient()
            .newURIBuilder(SERVICE_URI)
            .appendEntitySetSegment(ES_ALL_PRIM)
            .filter(filter)
            .build();

    ODataEntitySetRequest<ClientEntitySet> request =
        getClient().getRetrieveRequestFactory().getEntitySetRequest(uri);
    setCookieHeader(request);
    final ODataRetrieveResponse<ClientEntitySet> response = request.execute();
    saveCookieHeader(response);

    assertEquals(1, response.getBody().getEntities().size());
    ClientEntity clientEntity = response.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
 @Test
 public void castEdm64ToDouble() {
   double value = Float.MAX_VALUE + 1;
   ODataRetrieveResponse<ClientEntitySet> result =
       sendRequest(ES_ALL_PRIM, "PropertyInt64 lt " + value);
   assertEquals(3, result.getBody().getEntities().size());
 }
  @Test
  public void numericPromotionToInt64() {
    ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "PropertyInt64 eq 0");
    assertEquals(1, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(0, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void dateSubDate() {
    ODataRetrieveResponse<ClientEntitySet> response =
        sendRequest(ES_ALL_PRIM, "PropertyDuration eq 2012-12-04 sub 2012-12-04");
    assertEquals(1, response.getBody().getEntities().size());

    final ClientEntity clientEntity = response.getBody().getEntities().get(0);
    assertShortOrInt(0, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void stringProperty() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_TWO_KEY_NAV, "PropertyString eq '2'");

    assertEquals(1, result.getBody().getEntities().size());
    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertEquals("2", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());
  }
  @Test
  public void binaryIntegerOperations() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 add 1 eq (1 sub 3) div 2 mul 3 add 7");

    assertEquals(1, result.getBody().getEntities().size());
    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());
  }
  @Test
  public void durationSubDuration() {
    ODataRetrieveResponse<ClientEntitySet> response =
        sendRequest(ES_ALL_PRIM, "PropertyDuration sub duration'PT2S' eq duration'PT4S'");
    assertEquals(1, response.getBody().getEntities().size());

    final ClientEntity clientEntity = response.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void indexOf() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_ALL_PRIM, "indexof(PropertyString,'positive') eq 17");
    assertEquals(1, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void secondFunctionTimeOfDay() {
    ODataRetrieveResponse<ClientEntitySet> response =
        sendRequest(ES_ALL_PRIM, "second(PropertyTimeOfDay) eq 14");
    assertEquals(1, response.getBody().getEntities().size());

    ClientEntity clientEntity = response.getBody().getEntities().get(0);
    assertShortOrInt(
        -32768, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void dateTimeOffsetLiteral() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_ALL_PRIM, "PropertyDateTimeOffset eq 2012-12-03T07:16:23Z");
    assertEquals(1, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void trim() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_ALL_PRIM, "trim(substring(PropertyString,0,6)) eq 'First'");
    assertEquals(1, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void decimalDiv() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_ALL_PRIM, "PropertyDouble eq 0 sub (358000 div 2)");
    assertEquals(1, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(
        -32768, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void minuteFunctionDateTimeOffset() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_ALL_PRIM, "minute(PropertyDateTimeOffset) eq 17");
    assertEquals(1, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(
        -32768, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void timeOfDayLiteral() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_ALL_PRIM, "PropertyTimeOfDay eq 03:26:05");
    assertEquals(1, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void toUpper() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_ALL_PRIM, "contains(PropertyString,concat(toupper('f'),'irst'))");
    assertEquals(1, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void dateAddDuration() {
    ODataRetrieveResponse<ClientEntitySet> response =
        sendRequest(ES_ALL_PRIM, "PropertyDate eq 2012-12-01 add duration'P1DT27H16M23S'");
    assertEquals(1, response.getBody().getEntities().size());

    final ClientEntity clientEntity = response.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void round() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 eq round(2.5)");
    assertEquals(1, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());

    result = sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 eq round(2.4)");
    assertEquals(1, result.getBody().getEntities().size());

    clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(2, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());

    result = sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 eq round(2.6)");
    assertEquals(1, result.getBody().getEntities().size());

    clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());

    result = sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 eq round(3.1)");
    assertEquals(1, result.getBody().getEntities().size());

    clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());
  }
  @Test
  public void comparisonOnStringOperands() {
    // If check if the expression is true => All entry are returned
    ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_ALL_PRIM, "'Tes' lt 'Test'");
    assertEquals(3, result.getBody().getEntities().size());

    result = sendRequest(ES_ALL_PRIM, "'Test' le 'Test'");
    assertEquals(3, result.getBody().getEntities().size());

    result = sendRequest(ES_ALL_PRIM, "'Test1' le 'Test'");
    assertEquals(0, result.getBody().getEntities().size());

    result = sendRequest(ES_ALL_PRIM, "'Test1' gt 'Test'");
    assertEquals(3, result.getBody().getEntities().size());

    result = sendRequest(ES_ALL_PRIM, "'Tes' gt 'Test'");
    assertEquals(0, result.getBody().getEntities().size());

    result = sendRequest(ES_ALL_PRIM, "'Test' ge 'Test'");
    assertEquals(3, result.getBody().getEntities().size());

    result = sendRequest(ES_ALL_PRIM, "'Test' eq 'Test'");
    assertEquals(3, result.getBody().getEntities().size());

    result = sendRequest(ES_ALL_PRIM, "'Test1' ne 'Test'");
    assertEquals(3, result.getBody().getEntities().size());
  }
  @Test
  public void numericComparisonOperators() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 ge 1");
    assertEquals(4, result.getBody().getEntities().size());

    result = sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 gt 1");
    assertEquals(2, result.getBody().getEntities().size());

    result = sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 lt 2");
    assertEquals(2, result.getBody().getEntities().size());

    result = sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 le 2");
    assertEquals(3, result.getBody().getEntities().size());

    result = sendRequest(ES_ALL_PRIM, "PropertyDouble ge -179000");
    assertEquals(2, result.getBody().getEntities().size());

    result = sendRequest(ES_ALL_PRIM, "PropertyDouble gt -179000");
    assertEquals(1, result.getBody().getEntities().size());

    result = sendRequest(ES_ALL_PRIM, "PropertyDouble lt -179000");
    assertEquals(1, result.getBody().getEntities().size());

    result = sendRequest(ES_ALL_PRIM, "PropertyDouble le -179000");
    assertEquals(2, result.getBody().getEntities().size());
  }
  @Test
  public void unaryWithNullLiterals() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_TWO_KEY_NAV, "PropertyComp/PropertyComp/PropertyBoolean eq not null");
    assertEquals(0, result.getBody().getEntities().size());

    result =
        sendRequest(
            ES_TWO_KEY_NAV, "PropertyComp/PropertyComp/PropertyInt16 eq 0 add -(5 add null)");
    assertEquals(0, result.getBody().getEntities().size());
  }
  @Test
  public void int64Literal() {
    long value = Integer.MAX_VALUE + 1L;
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_ALL_PRIM, "PropertyInt64 gt " + value);
    assertEquals(1, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void booleanOperatorWithNull() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 eq null");
    assertEquals(0, result.getBody().getEntities().size());

    result = sendRequest(ES_TWO_KEY_NAV, "null eq null");
    assertEquals(4, result.getBody().getEntities().size());

    result = sendRequest(ES_TWO_KEY_NAV, "null ne null");
    assertEquals(0, result.getBody().getEntities().size());
  }
  @Test
  public void doubleLiteral() {
    Double value = -17900000000000000000.0;

    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_ALL_PRIM, "PropertyDouble le " + value.toString());
    assertEquals(1, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void substringStartAndEndGiven() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(
            ES_ALL_PRIM, "substring(PropertyString,length('First') add 1,8) eq ('Resource')");

    assertEquals(1, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void binaryOperationIntegerDecimalWithPromotion() {
    String filterString =
        ""
            + "PropertyInt16 mod 2 eq " // Choose mod 2 == 1 => { 1, 3, .. }
            + "(((5 sub 1) div 5) " // Integer Division 4 / 5 == 0
            + "add 1) " // 0 + 1 = 1
            + "and "
            + "PropertyComp/PropertyInt16 eq " // Complex Property
            + "5.5 mul 2"; // Single * Int16 => Single => Int16 eq Single => Single eq Single

    ODataRetrieveResponse<ClientEntitySet> result = sendRequest(ES_TWO_KEY_NAV, filterString);
    assertEquals(3, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(1, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());
    assertShortOrInt(
        11,
        clientEntity
            .getProperty("PropertyComp")
            .getComplexValue()
            .get("PropertyInt16")
            .getPrimitiveValue()
            .toValue());

    clientEntity = result.getBody().getEntities().get(1);
    assertShortOrInt(1, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("2", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());
    assertShortOrInt(
        11,
        clientEntity
            .getProperty("PropertyComp")
            .getComplexValue()
            .get("PropertyInt16")
            .getPrimitiveValue()
            .toValue());

    clientEntity = result.getBody().getEntities().get(2);
    assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());
    assertShortOrInt(
        11,
        clientEntity
            .getProperty("PropertyComp")
            .getComplexValue()
            .get("PropertyInt16")
            .getPrimitiveValue()
            .toValue());
  }
  @Test
  public void notOperator() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_TWO_KEY_NAV, "not (PropertyInt16 eq 1)");
    assertEquals(2, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(2, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());

    clientEntity = result.getBody().getEntities().get(1);
    assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());
  }
  @Test
  public void unaryMinusOperatorDecimal() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_TWO_KEY_NAV, "PropertyInt16 gt -2.0 add --3.0");
    assertEquals(2, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(2, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());

    clientEntity = result.getBody().getEntities().get(1);
    assertShortOrInt(3, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("1", clientEntity.getProperty("PropertyString").getPrimitiveValue().toValue());
  }
  @Test
  public void endsWith() {
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_ALL_PRIM, "endswith(PropertyString,'values')");
    assertEquals(2, result.getBody().getEntities().size());

    ClientEntity clientEntity = result.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());

    clientEntity = result.getBody().getEntities().get(1);
    assertShortOrInt(
        -32768, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
  }
  @Test
  public void fractionalsecondsDateTimeOffset() {
    ODataRetrieveResponse<ClientEntitySet> response =
        sendRequest(
            ES_COMP_ALL_PRIM,
            "fractionalseconds(PropertyComp/PropertyDateTimeOffset) eq 0.1234567");
    assertEquals(2, response.getBody().getEntities().size());

    ClientEntity clientEntity = response.getBody().getEntities().get(0);
    assertShortOrInt(
        32767, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("W/\"32767\"", clientEntity.getETag());

    clientEntity = response.getBody().getEntities().get(1);
    assertShortOrInt(0, clientEntity.getProperty("PropertyInt16").getPrimitiveValue().toValue());
    assertEquals("W/\"0\"", clientEntity.getETag());
  }
  @Test
  public void methodCallsWithNull() {
    // One representative of "stringFuntion" "residue class"
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest(ES_ALL_PRIM, "endswith(PropertyString,null) eq null"); // null eq null => true
    assertEquals(3, result.getBody().getEntities().size());

    // One representative of "stringifiedValueFunction" "residue class"
    result =
        sendRequest(ES_ALL_PRIM, "substring(PropertyString,null) eq null"); // null eq null => true
    assertEquals(3, result.getBody().getEntities().size());

    // Substring
    result = sendRequest(ES_ALL_PRIM, "hour(null) eq null"); // null eq null => true
    assertEquals(3, result.getBody().getEntities().size());

    result =
        sendRequest(
            ES_ALL_PRIM, "substring(PropertyString,0,null) eq null"); // null eq null => true
    assertEquals(3, result.getBody().getEntities().size());
  }