@Test
  public void testMissingEntity() {
    // Update an existing entity, use a URI to a not existing entity
    // Perform the request to a single navigation property and a collection navigation property as
    // well.
    // Expected: Not Found (404)

    final ODataClient client = getClient();
    final URI entityURI =
        client
            .newURIBuilder(SERVICE_URI)
            .appendEntitySetSegment(ES_KEY_NAV)
            .appendKeySegment(1)
            .build();
    final ClientObjectFactory of = client.getObjectFactory();

    // Request to single (non collection) navigation property
    ClientEntity entity = of.newEntity(ET_KEY_NAV);
    final ClientLink navLinkOne =
        of.newEntityNavigationLink(
            NAV_PROPERTY_ET_KEY_NAV_ONE,
            client
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment(ES_KEY_NAV)
                .appendKeySegment(42)
                .build());
    entity.addLink(navLinkOne);

    try {
      client
          .getCUDRequestFactory()
          .getEntityUpdateRequest(entityURI, UpdateType.PATCH, entity)
          .execute();
      fail();
    } catch (ODataClientErrorException e) {
      assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), e.getStatusLine().getStatusCode());
    }

    // Request to collection navigation propetry
    entity = of.newEntity(ET_KEY_NAV);
    final ClientLink navLinkMany =
        of.newEntitySetNavigationLink(
            NAV_PROPERTY_ET_KEY_NAV_MANY,
            client
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment(ES_KEY_NAV)
                .appendKeySegment(3)
                .build());
    entity.addLink(navLinkMany);

    try {
      client
          .getCUDRequestFactory()
          .getEntityUpdateRequest(entityURI, UpdateType.PATCH, entity)
          .execute();
    } catch (ODataClientErrorException e) {
      assertEquals(HttpStatusCode.NOT_FOUND.getStatusCode(), e.getStatusLine().getStatusCode());
    }
  }
  @Test
  public void numericBinaryOperationWithNullValues() {
    // Create new Entries
    final String filterString = "PropertyString eq null";

    ClientEntity entity =
        getFactory().newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ETAllPrim"));

    entity
        .getProperties()
        .add(
            getFactory()
                .newPrimitiveProperty(
                    "PropertyInt16",
                    getFactory().newPrimitiveValueBuilder().buildInt16((short) 1)));
    entity.addLink(
        getFactory()
            .newEntityNavigationLink(
                "NavPropertyETTwoPrimOne",
                getClient()
                    .newURIBuilder(SERVICE_URI)
                    .appendEntitySetSegment("ESTwoPrim")
                    .appendKeySegment(32766)
                    .build()));

    final URI uri =
        getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM).build();
    ODataEntityCreateResponse<ClientEntity> createResponse =
        getClient().getCUDRequestFactory().getEntityCreateRequest(uri, entity).execute();

    ODataRetrieveResponse<ClientEntitySet> filterResponse =
        sendRequest(
            ES_ALL_PRIM,
            filterString,
            createResponse.getHeader(HttpHeader.SET_COOKIE).iterator().next());
    assertEquals(1, filterResponse.getBody().getEntities().size());
  }
  @Test
  public void nullComplexProperty() {
    // Create a new entry.The complex property PropertyCompComp is set to null. So the structure of
    // the property
    // is still there, but filled is null values (primitive types)
    // We define a filter, which returns all entry where PropertyCompComp/PropertyComp/PropertyInt16
    // is equals to 1

    ClientEntity newEntity =
        getFactory().newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ETKeyNav"));
    newEntity.getProperties().add(getFactory().newComplexProperty("PropertyCompCompNav", null));
    newEntity
        .getProperties()
        .add(
            getFactory()
                .newPrimitiveProperty(
                    "PropertyInt16",
                    getFactory().newPrimitiveValueBuilder().buildInt16((short) 4)));
    newEntity
        .getProperties()
        .add(
            getFactory()
                .newPrimitiveProperty(
                    "PropertyString", getFactory().newPrimitiveValueBuilder().buildString("Test")));
    newEntity
        .getProperties()
        .add(
            getFactory()
                .newComplexProperty(
                    "PropertyCompAllPrim",
                    getFactory()
                        .newComplexValue(SERVICE_NAMESPACE + "." + "CTAllPrim")
                        .add(
                            getFactory()
                                .newPrimitiveProperty(
                                    "PropertyString",
                                    getFactory()
                                        .newPrimitiveValueBuilder()
                                        .buildString("Test 3")))));

    newEntity
        .getProperties()
        .add(
            getFactory()
                .newComplexProperty(
                    "PropertyCompTwoPrim",
                    getFactory()
                        .newComplexValue(SERVICE_NAMESPACE + "." + "CTTwoPrim")
                        .add(
                            getFactory()
                                .newPrimitiveProperty(
                                    "PropertyInt16",
                                    getFactory().newPrimitiveValueBuilder().buildInt16((short) 1)))
                        .add(
                            getFactory()
                                .newPrimitiveProperty(
                                    "PropertyString",
                                    getFactory()
                                        .newPrimitiveValueBuilder()
                                        .buildString("Test2")))));

    newEntity.addLink(
        getFactory()
            .newEntityNavigationLink(
                "NavPropertyETTwoKeyNavOne",
                getClient()
                    .newURIBuilder(SERVICE_URI)
                    .appendEntitySetSegment(ES_TWO_KEY_NAV)
                    .appendKeySegment(
                        new LinkedHashMap<String, Object>() {
                          private static final long serialVersionUID = 1L;

                          {
                            put("PropertyInt16", 1);
                            put("PropertyString", "1");
                          }
                        })
                    .build()));

    final URI uri =
        getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment("ESKeyNav").build();
    ODataEntityCreateRequest<ClientEntity> request =
        getClient().getCUDRequestFactory().getEntityCreateRequest(uri, newEntity);
    ODataEntityCreateResponse<ClientEntity> response = request.execute();
    assertEquals(HttpStatusCode.CREATED.getStatusCode(), response.getStatusCode());

    final String cookie = response.getHeader(HttpHeader.SET_COOKIE).iterator().next();

    // Do the filter request
    ODataRetrieveResponse<ClientEntitySet> result =
        sendRequest("ESKeyNav", "PropertyCompCompNav/PropertyCompNav/PropertyInt16 eq 1", cookie);
    assertEquals(3, result.getBody().getEntities().size());

    // Try filter all entries where PropertyCompComp is null
    result =
        sendRequest(
            "ESKeyNav", "PropertyCompCompNav/PropertyCompNav/PropertyInt16 eq null", cookie);
    assertEquals(1, result.getBody().getEntities().size());
  }
  @Test
  public void changesetBatchRequest() throws URISyntaxException {
    final ODataBatchRequest request =
        getClient().getBatchRequestFactory().getBatchRequest(SERVICE_URI);
    final BatchManager payload = request.payloadManager();
    // -----------------------------
    // - Append get request
    // -----------------------------
    appendGetRequest(payload, "ESAllPrim", 32767, false); // Without error

    // -----------------------------
    // - Append change set
    // -----------------------------
    final ODataChangeset changeset = payload.addChangeset();

    // ------------------------
    // POST request (Insert)
    URIBuilder targetURI =
        getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment("ESAllPrim");
    URI editLink = targetURI.build();

    ClientObjectFactory factory = getFactory();
    ClientEntity postEntity =
        factory.newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ESAllPrim"));
    postEntity.addLink(
        factory.newEntityNavigationLink(
            "NavPropertyETTwoPrimOne",
            getClient()
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment("ESTwoPrim")
                .appendKeySegment(32766)
                .build()));

    postEntity
        .getProperties()
        .add(
            factory.newPrimitiveProperty(
                "PropertyDouble", factory.newPrimitiveValueBuilder().buildDouble(3.1415)));

    final ODataEntityCreateRequest<ClientEntity> createRequest =
        getClient().getCUDRequestFactory().getEntityCreateRequest(editLink, postEntity);
    createRequest.setFormat(ContentType.JSON);

    changeset.addRequest(createRequest);

    // ------------------------
    // Patch request (Update)
    targetURI =
        getClient()
            .newURIBuilder(SERVICE_URI)
            .appendEntitySetSegment("ESAllPrim")
            .appendKeySegment(0);
    editLink = targetURI.build();

    ClientEntity patchEntity =
        factory.newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ESAllPrim"));
    patchEntity.setEditLink(editLink);

    patchEntity
        .getProperties()
        .add(
            factory.newPrimitiveProperty(
                "PropertyDouble", factory.newPrimitiveValueBuilder().buildDouble(3.1415)));

    ODataEntityUpdateRequest<ClientEntity> changeReq =
        getClient().getCUDRequestFactory().getEntityUpdateRequest(UpdateType.PATCH, patchEntity);
    changeReq.setFormat(ContentType.JSON);
    changeset.addRequest(changeReq);

    // ------------------------
    // Patch request (Upsert)
    targetURI =
        getClient()
            .newURIBuilder(SERVICE_URI)
            .appendEntitySetSegment("ESAllPrim")
            .appendKeySegment(15);
    editLink = targetURI.build();

    patchEntity = factory.newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ESAllPrim"));
    patchEntity.setEditLink(editLink);

    patchEntity
        .getProperties()
        .add(
            factory.newPrimitiveProperty(
                "PropertyDouble", factory.newPrimitiveValueBuilder().buildDouble(3.1415)));

    patchEntity.addLink(
        factory.newEntityNavigationLink(
            "NavPropertyETTwoPrimOne",
            getClient()
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment("ESTwoPrim")
                .appendKeySegment(32766)
                .build()));

    changeReq =
        getClient().getCUDRequestFactory().getEntityUpdateRequest(UpdateType.PATCH, patchEntity);
    changeReq.setFormat(ContentType.JSON);
    changeset.addRequest(changeReq);

    // -----------------------------
    // - Append get request
    // -----------------------------
    appendGetRequest(payload, "ESAllPrim", 0, false); // Without error

    // -----------------------------
    // - Fetch result
    // -----------------------------
    final ODataBatchResponse response = payload.getResponse();
    assertEquals(HttpStatusCode.ACCEPTED.getStatusCode(), response.getStatusCode());
    final Iterator<ODataBatchResponseItem> bodyIterator = response.getBody();

    // Check first get request
    assertTrue(bodyIterator.hasNext());
    ODataBatchResponseItem item = bodyIterator.next();
    assertFalse(item.isChangeset());
    assertTrue(item.hasNext());
    final ODataResponse response0 = item.next();
    assertTrue(response0 instanceof ODataRetrieveResponse);
    @SuppressWarnings("unchecked")
    ODataRetrieveResponse<ClientEntity> retrieveResponse =
        (ODataRetrieveResponse<ClientEntity>) response0;
    assertEquals(
        34,
        retrieveResponse.getBody().getProperty("PropertyDecimal").getPrimitiveValue().toValue());

    // Check change set
    assertTrue(bodyIterator.hasNext());
    item = bodyIterator.next();
    assertTrue(item.isChangeset());

    // Insert
    assertTrue(item.hasNext());
    final ODataResponse response1 = item.next();
    assertEquals(HttpStatusCode.CREATED.getStatusCode(), response1.getStatusCode());
    assertTrue(response1 instanceof ODataEntityCreateResponse);
    assertEquals(
        3.1415,
        ((ODataEntityCreateResponse<?>) response1)
            .getBody()
            .getProperty("PropertyDouble")
            .getPrimitiveValue()
            .toValue());
    // Update
    assertTrue(item.hasNext());
    final ODataResponse response2 = item.next();
    assertEquals(HttpStatusCode.OK.getStatusCode(), response2.getStatusCode());
    assertTrue(response2 instanceof ODataEntityUpdateResponse);

    // Upsert
    assertTrue(item.hasNext());
    final ODataResponse response3 = item.next();
    assertEquals(HttpStatusCode.CREATED.getStatusCode(), response3.getStatusCode());
    assertTrue(response3 instanceof ODataEntityUpdateResponse);
    assertEquals(
        3.1415,
        ((ODataEntityUpdateResponse<?>) response3)
            .getBody()
            .getProperty("PropertyDouble")
            .getPrimitiveValue()
            .toValue());

    // Check second get request
    assertTrue(bodyIterator.hasNext());
    item = bodyIterator.next();
    assertFalse(item.isChangeset());
    assertTrue(item.hasNext());
    final ODataResponse response4 = item.next();
    assertTrue(response4 instanceof ODataRetrieveResponse);
    @SuppressWarnings("unchecked")
    final ODataRetrieveResponse<ClientEntity> retrieveResponse2 =
        (ODataRetrieveResponse<ClientEntity>) response4;
    assertEquals(
        3.1415,
        retrieveResponse2.getBody().getProperty("PropertyDouble").getPrimitiveValue().toValue());
  }
  @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());
  }
  @Test
  public void testCreateBindingSimple() throws EdmPrimitiveTypeException {
    final ODataClient client = getClient();
    final URI createURI =
        client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_KEY_NAV).build();
    final ClientObjectFactory of = client.getObjectFactory();

    // Create entity (EntitySet: ESKeyNav, Type: ETKeyNav)
    final ClientEntity entity = of.newEntity(ET_KEY_NAV);
    entity
        .getProperties()
        .add(
            of.newPrimitiveProperty(
                PROPERTY_INT16, of.newPrimitiveValueBuilder().buildInt16((short) 42)));
    entity
        .getProperties()
        .add(
            of.newPrimitiveProperty(
                PROPERTY_STRING, of.newPrimitiveValueBuilder().buildString("42")));
    entity
        .getProperties()
        .add(
            of.newComplexProperty(
                PROPERTY_COMP_NAV,
                of.newComplexValue(CT_NAV_FIVE_PROP)
                    .add(
                        of.newPrimitiveProperty(
                            PROPERTY_INT16,
                            of.newPrimitiveValueBuilder().buildInt16((short) 42)))));
    entity
        .getProperties()
        .add(
            of.newComplexProperty(
                PROPERTY_COMP_ALL_PRIM,
                of.newComplexValue(CT_ALL_PRIM)
                    .add(
                        of.newPrimitiveProperty(
                            PROPERTY_STRING, of.newPrimitiveValueBuilder().buildString("42")))));
    entity
        .getProperties()
        .add(
            of.newComplexProperty(
                PROPERTY_COMP_TWO_PRIM,
                of.newComplexValue(CT_TWO_PRIM)
                    .add(
                        of.newPrimitiveProperty(
                            PROPERTY_INT16, of.newPrimitiveValueBuilder().buildInt16((short) 42)))
                    .add(
                        of.newPrimitiveProperty(
                            PROPERTY_STRING, of.newPrimitiveValueBuilder().buildString("42")))));
    entity
        .getProperties()
        .add(
            of.newComplexProperty(
                PROPERTY_COMP_COMP_NAV,
                of.newComplexValue(CT_PRIM_COMP)
                    .add(
                        of.newPrimitiveProperty(
                            PROPERTY_STRING, of.newPrimitiveValueBuilder().buildString("42")))
                    .add(
                        of.newComplexProperty(
                            PROPERTY_COMP_NAV,
                            of.newComplexValue(CT_NAV_FIVE_PROP)
                                .add(
                                    of.newPrimitiveProperty(
                                        PROPERTY_INT16,
                                        of.newPrimitiveValueBuilder().buildInt16((short) 42)))))));

    // Bind existing entities via binding synatx
    entity.addLink(
        of.newEntityNavigationLink(
            NAV_PROPERTY_ET_TWO_KEY_NAV_ONE,
            client
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment(ES_TWO_KEY_NAV)
                .appendKeySegment(
                    new LinkedHashMap<String, Object>() {
                      private static final long serialVersionUID = 3109256773218160485L;

                      {
                        put(PROPERTY_INT16, 3);
                        put(PROPERTY_STRING, "1");
                      }
                    })
                .build()));

    final ClientLink navLinkOne =
        of.newEntityNavigationLink(
            NAV_PROPERTY_ET_KEY_NAV_ONE,
            client
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment(ES_KEY_NAV)
                .appendKeySegment(1)
                .build());
    final ClientLink navLinkMany1 =
        of.newEntitySetNavigationLink(
            NAV_PROPERTY_ET_KEY_NAV_MANY,
            client
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment(ES_KEY_NAV)
                .appendKeySegment(2)
                .build());
    final ClientLink navLinkMany2 =
        of.newEntitySetNavigationLink(
            NAV_PROPERTY_ET_KEY_NAV_MANY,
            client
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment(ES_KEY_NAV)
                .appendKeySegment(3)
                .build());

    final HashMap<String, Object> combinedKey = new HashMap<String, Object>();
    combinedKey.put(PROPERTY_INT16, 1);
    combinedKey.put(PROPERTY_STRING, "1");
    final ClientLink navLink2Many =
        of.newEntitySetNavigationLink(
            NAV_PROPERTY_ET_TWO_KEY_NAV_MANY,
            client
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment(ES_TWO_KEY_NAV)
                .appendKeySegment(combinedKey)
                .build());

    entity.addLink(navLinkOne);
    entity.addLink(navLinkMany1);
    entity.addLink(navLinkMany2);
    entity.addLink(navLink2Many);

    final ODataEntityCreateResponse<ClientEntity> createResponse =
        client.getCUDRequestFactory().getEntityCreateRequest(createURI, entity).execute();
    final String cookie = createResponse.getHeader(HttpHeader.SET_COOKIE).iterator().next();
    final Short entityInt16Key =
        createResponse
            .getBody()
            .getProperty(PROPERTY_INT16)
            .getPrimitiveValue()
            .toCastValue(Short.class);

    // Check the just created entity
    final URI entityGetURI =
        client
            .newURIBuilder(SERVICE_URI)
            .appendEntitySetSegment(ES_KEY_NAV)
            .appendKeySegment(entityInt16Key)
            .expand(
                NAV_PROPERTY_ET_KEY_NAV_ONE,
                NAV_PROPERTY_ET_KEY_NAV_MANY,
                NAV_PROPERTY_ET_TWO_KEY_NAV_MANY)
            .build();

    final ODataEntityRequest<ClientEntity> entityGetRequest =
        client.getRetrieveRequestFactory().getEntityRequest(entityGetURI);
    entityGetRequest.addCustomHeader(HttpHeader.COOKIE, cookie);
    final ODataRetrieveResponse<ClientEntity> entityGetResponse = entityGetRequest.execute();

    // NAV_PROPERTY_ET_KEY_NAV_ONE
    assertEquals(
        Short.valueOf((short) 1),
        entityGetResponse
            .getBody()
            .getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_ONE)
            .asInlineEntity()
            .getEntity()
            .getProperty(PROPERTY_INT16)
            .getPrimitiveValue()
            .toCastValue(Short.class));

    // NAV_PROPERTY_ET_KEY_NAV_MANY(0)
    Iterator<ClientEntity> iterator =
        entityGetResponse
            .getBody()
            .getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_MANY)
            .asInlineEntitySet()
            .getEntitySet()
            .getEntities()
            .iterator();
    assertEquals(
        Short.valueOf((short) 2),
        iterator.next().getProperty(PROPERTY_INT16).getPrimitiveValue().toCastValue(Short.class));

    // NAV_PROPERTY_ET_KEY_NAV_MANY(1)
    assertEquals(
        Short.valueOf((short) 3),
        iterator.next().getProperty(PROPERTY_INT16).getPrimitiveValue().toCastValue(Short.class));

    // NAV_PROPERTY_ET_TWO_KEY_NAV_MANY(0)
    assertEquals(
        Short.valueOf((short) 1),
        entityGetResponse
            .getBody()
            .getNavigationLink(NAV_PROPERTY_ET_TWO_KEY_NAV_MANY)
            .asInlineEntitySet()
            .getEntitySet()
            .getEntities()
            .iterator()
            .next()
            .getProperty(PROPERTY_INT16)
            .getPrimitiveValue()
            .toCastValue(Short.class));
    assertEquals(
        "1",
        entityGetResponse
            .getBody()
            .getNavigationLink(NAV_PROPERTY_ET_TWO_KEY_NAV_MANY)
            .asInlineEntitySet()
            .getEntitySet()
            .getEntities()
            .iterator()
            .next()
            .getProperty(PROPERTY_STRING)
            .getPrimitiveValue()
            .toValue());

    // Check if partner navigation link has been set up
    final URI etTwoKeyNavEntityURI =
        client
            .newURIBuilder(SERVICE_URI)
            .appendEntitySetSegment(ES_TWO_KEY_NAV)
            .appendKeySegment(combinedKey)
            .expand(NAV_PROPERTY_ET_KEY_NAV_ONE)
            .build();
    final ODataEntityRequest<ClientEntity> etTwoKeyNavEntityRequest =
        client.getRetrieveRequestFactory().getEntityRequest(etTwoKeyNavEntityURI);
    etTwoKeyNavEntityRequest.addCustomHeader(HttpHeader.COOKIE, cookie);
    final ODataRetrieveResponse<ClientEntity> etTwoKeyNavEntityResponse =
        etTwoKeyNavEntityRequest.execute();

    assertEquals(
        entityInt16Key,
        etTwoKeyNavEntityResponse
            .getBody()
            .getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_ONE)
            .asInlineEntity()
            .getEntity()
            .getProperty(PROPERTY_INT16)
            .getPrimitiveValue()
            .toCastValue(Short.class));
  }
  @Test
  public void testDeepInsertWithBindingSameNavigationProperty() {
    final EdmEnabledODataClient client = getClient();
    client.getConfiguration().setDefaultPubFormat(ContentType.JSON);
    final ClientObjectFactory of = client.getObjectFactory();

    final ClientEntity entity = of.newEntity(ET_KEY_NAV);
    entity
        .getProperties()
        .add(
            of.newPrimitiveProperty(
                PROPERTY_STRING, of.newPrimitiveValueBuilder().buildString("1")));
    entity
        .getProperties()
        .add(
            of.newComplexProperty(
                PROPERTY_COMP_TWO_PRIM,
                of.newComplexValue(CT_TWO_PRIM)
                    .add(
                        of.newPrimitiveProperty(
                            PROPERTY_INT16, of.newPrimitiveValueBuilder().buildInt16((short) 1)))
                    .add(
                        of.newPrimitiveProperty(
                            PROPERTY_STRING, of.newPrimitiveValueBuilder().buildString("1")))));

    final ClientEntity innerEntity = of.newEntity(ET_KEY_NAV);
    innerEntity
        .getProperties()
        .add(
            of.newPrimitiveProperty(
                PROPERTY_STRING, of.newPrimitiveValueBuilder().buildString("2")));
    innerEntity
        .getProperties()
        .add(
            of.newComplexProperty(
                PROPERTY_COMP_TWO_PRIM,
                of.newComplexValue(CT_TWO_PRIM)
                    .add(
                        of.newPrimitiveProperty(
                            PROPERTY_INT16, of.newPrimitiveValueBuilder().buildInt16((short) 1)))
                    .add(
                        of.newPrimitiveProperty(
                            PROPERTY_STRING, of.newPrimitiveValueBuilder().buildString("2")))));
    innerEntity.addLink(
        of.newEntityNavigationLink(
            NAV_PROPERTY_ET_TWO_KEY_NAV_ONE,
            client
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment(ES_TWO_KEY_NAV)
                .appendKeySegment(
                    new LinkedHashMap<String, Object>() {
                      private static final long serialVersionUID = 1L;

                      {
                        put(PROPERTY_INT16, 3);
                        put(PROPERTY_STRING, "1");
                      }
                    })
                .build()));

    final ClientInlineEntity inlineLink =
        of.newDeepInsertEntity(NAV_PROPERTY_ET_KEY_NAV_ONE, innerEntity);
    entity.addLink(inlineLink);

    entity.addLink(
        of.newEntityNavigationLink(
            NAV_PROPERTY_ET_TWO_KEY_NAV_ONE,
            client
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment(ES_TWO_KEY_NAV)
                .appendKeySegment(
                    new LinkedHashMap<String, Object>() {
                      private static final long serialVersionUID = 3109256773218160485L;

                      {
                        put(PROPERTY_INT16, 3);
                        put(PROPERTY_STRING, "1");
                      }
                    })
                .build()));

    final URI bindingURI =
        client
            .newURIBuilder(SERVICE_URI)
            .appendEntitySetSegment(ES_KEY_NAV)
            .appendKeySegment(3)
            .build();

    entity.addLink(of.newEntityNavigationLink(NAV_PROPERTY_ET_KEY_NAV_ONE, bindingURI));

    final URI targetURI =
        client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_KEY_NAV).build();
    final ODataEntityCreateResponse<ClientEntity> response =
        client.getCUDRequestFactory().getEntityCreateRequest(targetURI, entity).execute();

    assertEquals(HttpStatusCode.CREATED.getStatusCode(), response.getStatusCode());

    assertEquals(
        1,
        response
            .getBody()
            .getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_ONE)
            .asInlineEntity()
            .getEntity()
            .getProperty(PROPERTY_COMP_TWO_PRIM)
            .getComplexValue()
            .get(PROPERTY_INT16)
            .getPrimitiveValue()
            .toValue());
  }
  @Test
  public void testUpdateBinding() throws Exception {
    // The entity MUST NOT contain related entities as inline content. It MAY contain binding
    // information
    // for navigation properties. For single-valued navigation properties this replaces the
    // relationship.
    // For collection-valued navigation properties this adds to the relationship.

    final ODataClient client = getClient();
    final URI entityURI =
        client
            .newURIBuilder(SERVICE_URI)
            .appendEntitySetSegment(ES_KEY_NAV)
            .appendKeySegment(1)
            .build();
    final ClientObjectFactory of = client.getObjectFactory();

    // ESKeyNav(1).NavPropertyETKeyNavOne = ESKeyNav(2)
    // ESKeyNav(1).NavPropertyETKeyNavMany = { ESKeyNav(1), ESKeyNav(2) }
    // => Replace NavPropertyETKeyNavOne with ESKeyNav(3)
    // => Add to NavPropertyETKeyNavOne ESKeyNav(3)
    final ClientEntity entity = of.newEntity(ET_KEY_NAV);
    final ClientLink navLinkOne =
        of.newEntityNavigationLink(
            NAV_PROPERTY_ET_KEY_NAV_ONE,
            client
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment(ES_KEY_NAV)
                .appendKeySegment(3)
                .build());
    final ClientLink navLinkMany =
        of.newEntitySetNavigationLink(
            NAV_PROPERTY_ET_KEY_NAV_MANY,
            client
                .newURIBuilder(SERVICE_URI)
                .appendEntitySetSegment(ES_KEY_NAV)
                .appendKeySegment(3)
                .build());
    entity.addLink(navLinkOne);
    entity.addLink(navLinkMany);

    final ODataEntityUpdateResponse<ClientEntity> updateResponse =
        client
            .getCUDRequestFactory()
            .getEntityUpdateRequest(entityURI, UpdateType.PATCH, entity)
            .execute();
    final String cookie = updateResponse.getHeader(HttpHeader.SET_COOKIE).iterator().next();

    // Check if update was successful
    final URI entityGetURI =
        client
            .newURIBuilder(SERVICE_URI)
            .appendEntitySetSegment(ES_KEY_NAV)
            .appendKeySegment(1)
            .expand(NAV_PROPERTY_ET_KEY_NAV_ONE, NAV_PROPERTY_ET_KEY_NAV_MANY)
            .build();
    final ODataEntityRequest<ClientEntity> entityRequest =
        client.getRetrieveRequestFactory().getEntityRequest(entityGetURI);
    entityRequest.addCustomHeader(HttpHeader.COOKIE, cookie);
    final ODataRetrieveResponse<ClientEntity> entityResponse = entityRequest.execute();

    assertEquals(
        Short.valueOf((short) 3),
        entityResponse
            .getBody()
            .getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_ONE)
            .asInlineEntity()
            .getEntity()
            .getProperty(PROPERTY_INT16)
            .getPrimitiveValue()
            .toCastValue(Short.class));
    assertEquals(
        3,
        entityResponse
            .getBody()
            .getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_MANY)
            .asInlineEntitySet()
            .getEntitySet()
            .getEntities()
            .size());

    Iterator<ClientEntity> iterator =
        entityResponse
            .getBody()
            .getNavigationLink(NAV_PROPERTY_ET_KEY_NAV_MANY)
            .asInlineEntitySet()
            .getEntitySet()
            .getEntities()
            .iterator();
    assertEquals(
        Short.valueOf((short) 1),
        iterator.next().getProperty(PROPERTY_INT16).getPrimitiveValue().toCastValue(Short.class));
    assertEquals(
        Short.valueOf((short) 2),
        iterator.next().getProperty(PROPERTY_INT16).getPrimitiveValue().toCastValue(Short.class));
    assertEquals(
        Short.valueOf((short) 3),
        iterator.next().getProperty(PROPERTY_INT16).getPrimitiveValue().toCastValue(Short.class));
  }