@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());
  }
  private void readWithNext(final ContentType format) {
    final URIBuilder uriBuilder =
        client.newURIBuilder(testStaticServiceRootURL).appendEntitySetSegment("People");

    final ODataEntitySetRequest<ClientEntitySet> req =
        client.getRetrieveRequestFactory().getEntitySetRequest(uriBuilder.build());
    req.setFormat(format);
    req.setPrefer(client.newPreferences().maxPageSize(5));

    final ODataRetrieveResponse<ClientEntitySet> res = req.execute();
    final ClientEntitySet feed = res.getBody();

    assertNotNull(feed);

    assertEquals(5, feed.getEntities().size());
    assertNotNull(feed.getNext());

    final URI expected = URI.create(testStaticServiceRootURL + "/People?$skiptoken=5");
    final URI found = URIUtils.getURI(testStaticServiceRootURL, feed.getNext().toASCIIString());

    assertEquals(expected, found);
  }
  private ODataRetrieveResponse<ClientEntitySet> sendRequest(
      final String entitySet, final String filterString, final String cookie) {
    final URI uri =
        getClient()
            .newURIBuilder(SERVICE_URI)
            .appendEntitySetSegment(entitySet)
            .filter(filterString)
            .build();

    ODataEntitySetRequest<ClientEntitySet> request =
        getClient().getRetrieveRequestFactory().getEntitySetRequest(uri);
    if (cookie == null) {
      setCookieHeader(request);
    } else {
      request.addCustomHeader(HttpHeader.COOKIE, cookie);
    }

    final ODataRetrieveResponse<ClientEntitySet> response = request.execute();
    if (cookie == null) {
      saveCookieHeader(response);
    }
    return response;
  }
  @Test
  public void changesetWithReferences() throws EdmPrimitiveTypeException, URISyntaxException {
    // create your request
    final ODataBatchRequest request =
        getClient().getBatchRequestFactory().getBatchRequest(SERVICE_URI);
    final BatchManager streamManager = request.payloadManager();

    final ODataChangeset changeset = streamManager.addChangeset();
    final ClientEntity entityESAllPrim =
        getFactory().newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ESAllPrim"));

    entityESAllPrim
        .getProperties()
        .add(
            getFactory()
                .newPrimitiveProperty(
                    "PropertyDouble", getFactory().newPrimitiveValueBuilder().buildDouble(3.1415)));

    entityESAllPrim.addLink(
        getFactory()
            .newEntityNavigationLink(
                "NavPropertyETTwoPrimOne",
                getClient()
                    .newURIBuilder(SERVICE_URI)
                    .appendEntitySetSegment("ESTwoPrim")
                    .appendKeySegment(-365)
                    .build()));

    final URIBuilder uriBuilder =
        getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment("ESAllPrim");

    // add create request
    final ODataEntityCreateRequest<ClientEntity> createReq =
        getClient()
            .getCUDRequestFactory()
            .getEntityCreateRequest(uriBuilder.build(), entityESAllPrim);
    createReq.setFormat(ContentType.JSON);
    changeset.addRequest(createReq);

    // retrieve request reference
    int createRequestRef = changeset.getLastContentId();

    // add update request
    final ClientEntity entityUpdate = getFactory().newEntity(entityESAllPrim.getTypeName());
    entityUpdate.addLink(
        getFactory()
            .newEntitySetNavigationLink(
                "NavPropertyETTwoPrimMany",
                getClient()
                    .newURIBuilder(SERVICE_URI)
                    .appendEntitySetSegment("ESTwoPrim")
                    .appendKeySegment(32767)
                    .build()));

    final ODataEntityUpdateRequest<ClientEntity> updateReq =
        getClient()
            .getCUDRequestFactory()
            .getEntityUpdateRequest(
                URI.create("$" + createRequestRef), UpdateType.PATCH, entityUpdate);
    updateReq.setFormat(ContentType.JSON);

    changeset.addRequest(updateReq);

    final ODataBatchResponse response = streamManager.getResponse();
    assertEquals(HttpStatusCode.ACCEPTED.getStatusCode(), response.getStatusCode());
    final String cookie = response.getHeader(HttpHeader.SET_COOKIE).iterator().next();

    // verify response payload ...
    final Iterator<ODataBatchResponseItem> bodyIterator = response.getBody();
    final ODataBatchResponseItem item = bodyIterator.next();

    assertTrue(item instanceof ODataChangesetResponseItem);
    final ODataChangesetResponseItem chgitem = (ODataChangesetResponseItem) item;
    assertTrue(chgitem.hasNext());
    ODataResponse res = chgitem.next();
    assertEquals(HttpStatusCode.CREATED.getStatusCode(), res.getStatusCode());
    assertTrue(res instanceof ODataEntityCreateResponse);
    final ODataEntityCreateResponse<?> createResponse = ((ODataEntityCreateResponse<?>) res);

    res = chgitem.next();
    assertEquals(HttpStatusCode.OK.getStatusCode(), res.getStatusCode());
    assertTrue(res instanceof ODataEntityUpdateResponse);

    final ODataEntitySetRequest<ClientEntitySet> req =
        getClient()
            .getRetrieveRequestFactory()
            .getEntitySetRequest(
                new URI(
                    createResponse.getHeader(HttpHeader.LOCATION).iterator().next()
                        + "/NavPropertyETTwoPrimMany"));
    req.setFormat(ContentType.JSON);
    req.addCustomHeader(HttpHeader.COOKIE, cookie);
    final ODataRetrieveResponse<ClientEntitySet> getResponse = req.execute();

    assertEquals(
        32767,
        getResponse
            .getBody()
            .getEntities()
            .get(0)
            .getProperty("PropertyInt16")
            .getPrimitiveValue()
            .toValue());
  }