Java Code Examples for org.apache.olingo.client.api.domain.ClientEntity#getProperty()

The following examples show how to use org.apache.olingo.client.api.domain.ClientEntity#getProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EntityTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void withEnums(final ContentType contentType) throws Exception {
  final InputStream input = getClass().getResourceAsStream("Products_5." + getSuffix(contentType));
  final ClientEntity entity = client.getBinder().getODataEntity(
      client.getDeserializer(contentType).toEntity(input));
  assertNotNull(entity);

  final ClientProperty skinColor = entity.getProperty("SkinColor");
  assertTrue(skinColor.hasEnumValue());
  assertEquals("Microsoft.Test.OData.Services.ODataWCFService.Color", skinColor.getEnumValue().getTypeName());
  assertEquals("Red", skinColor.getEnumValue().getValue());

  final ClientProperty coverColors = entity.getProperty("CoverColors");
  assertTrue(coverColors.hasCollectionValue());
  for (final Iterator<ClientValue> itor = coverColors.getCollectionValue().iterator(); itor.hasNext();) {
    final ClientValue item = itor.next();
    assertTrue(item.isEnum());
  }

  // operations won't get serialized
  entity.getOperations().clear();
  final ClientEntity written = client.getBinder().getODataEntity(
      new ResWrap<Entity>((URI) null, null, client.getBinder().getEntity(entity)));
  assertEquals(entity, written);
  input.close();
}
 
Example 2
Source File: BasicITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void createEntityWithEnumAndTypeDefinition() throws Exception {
  ClientEntity newEntity = getFactory().newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ETMixEnumDefCollComp"));
  final ODataEntityCreateRequest<ClientEntity> request = getClient().getCUDRequestFactory().getEntityCreateRequest(
      getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment("ESMixEnumDefCollComp").build(),
      newEntity);
  final ODataEntityCreateResponse<ClientEntity> response = request.execute();
  final ClientEntity createdEntity = response.getBody();
  assertNotNull(createdEntity);
  ClientProperty property = createdEntity.getProperty("PropertyEnumString");
  assertNotNull(property);
  // TODO: Improve client value types.
  assertEquals("String1", isJson() ? property.getPrimitiveValue().toValue() : property.getEnumValue().getValue());
  property = createdEntity.getProperty("PropertyDefString");
  assertNotNull(property);
  // TODO: Improve client value types.
  assertEquals("1", isJson() ?
      property.getPrimitiveValue().toValue() :
      property.getEnumValue().getValue());
}
 
Example 3
Source File: KeyAsSegmentTestITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void update(final ContentType contentType) {
  final ClientEntity changes = getClient().getObjectFactory().newEntity(
      new FullQualifiedName("Microsoft.Test.OData.Services.ODataWCFService.Customer"));
  final ClientProperty middleName = getClient().getObjectFactory().newPrimitiveProperty("MiddleName",
      getClient().getObjectFactory().newPrimitiveValueBuilder().buildString("middle"));
  changes.getProperties().add(middleName);

  final URI uri = getClient().newURIBuilder(testKeyAsSegmentServiceRootURL).
      appendEntitySetSegment("People").appendKeySegment(5).build();
  final ODataEntityUpdateRequest<ClientEntity> req = getClient().getCUDRequestFactory().
      getEntityUpdateRequest(uri, UpdateType.PATCH, changes);
  req.setFormat(contentType);

  final ODataEntityUpdateResponse<ClientEntity> res = req.execute();
  assertEquals(204, res.getStatusCode());

  final ClientEntity updated = getClient().getRetrieveRequestFactory().getEntityRequest(uri).execute().getBody();
  assertNotNull(updated);
  assertFalse(updated.getEditLink().toASCIIString().contains("("));
  assertFalse(updated.getEditLink().toASCIIString().contains(")"));

  final ClientProperty updatedMiddleName = updated.getProperty("MiddleName");
  assertNotNull(updatedMiddleName);
  assertEquals("middle", updatedMiddleName.getPrimitiveValue().toString());
}
 
Example 4
Source File: FunctionImportITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void entityWithoutEntitySet() throws Exception {
  ODataInvokeRequest<ClientEntity> request = getClient().getInvokeRequestFactory()
      .getFunctionInvokeRequest(
          getClient().newURIBuilder(TecSvcConst.BASE_URI).appendOperationCallSegment("FICRTETKeyNav").build(),
          ClientEntity.class);
  assertNotNull(request);
  setCookieHeader(request);

  final ODataInvokeResponse<ClientEntity> response = request.execute();
  saveCookieHeader(response);
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());

  final ClientEntity entity = response.getBody();
  assertNotNull(entity);
  final ClientProperty property = entity.getProperty("PropertyInt16");
  assertNotNull(property);
  assertShortOrInt(1, property.getPrimitiveValue().toValue());
}
 
Example 5
Source File: BasicITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void readEntity() throws Exception {
  ODataEntityRequest<ClientEntity> request = getClient().getRetrieveRequestFactory()
      .getEntityRequest(getClient().newURIBuilder(SERVICE_URI)
          .appendEntitySetSegment("ESCollAllPrim").appendKeySegment(1).build());
  assertNotNull(request);
  setCookieHeader(request);

  final ODataRetrieveResponse<ClientEntity> response = request.execute();
  saveCookieHeader(response);
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
  assertContentType(response.getContentType());
  assertNotNull(response.getRawResponse());
  final ClientEntity entity = response.getBody();
  assertNotNull(entity);
  final ClientProperty property = entity.getProperty("CollPropertyInt16");
  assertNotNull(property);
  assertNotNull(property.getCollectionValue());
  assertEquals(3, property.getCollectionValue().size());
  Iterator<ClientValue> iterator = property.getCollectionValue().iterator();
  assertShortOrInt(1000, iterator.next().asPrimitive().toValue());
  assertShortOrInt(2000, iterator.next().asPrimitive().toValue());
  assertShortOrInt(30112, iterator.next().asPrimitive().toValue());
}
 
Example 6
Source File: FunctionImportITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void entityCollectionWithAppendedKey() {
  // .../odata.svc/FICRTCollESMedia()(1)
  ODataInvokeRequest<ClientEntity> request = getClient().getInvokeRequestFactory()
      .getFunctionInvokeRequest(
          getClient().newURIBuilder(TecSvcConst.BASE_URI).appendOperationCallSegment("FICRTCollESMedia")
              .appendKeySegment(getFactory().newPrimitiveValueBuilder().buildInt32(1))
              .build(),
          ClientEntity.class);
  assertNotNull(request);
  setCookieHeader(request);

  final ODataInvokeResponse<ClientEntity> response = request.execute();
  saveCookieHeader(response);
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());

  final ClientEntity entity = response.getBody();
  assertNotNull(entity);
  final ClientProperty property = entity.getProperty("PropertyInt16");
  assertNotNull(property);
  assertNotNull(property.getPrimitiveValue());
  assertShortOrInt(1, property.getPrimitiveValue().toValue());
}
 
Example 7
Source File: NavigationITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void twoLevelsToEntitySet() throws Exception {
  final ODataRetrieveResponse<ClientEntitySet> response =
      getClient().getRetrieveRequestFactory().getEntitySetRequest(
          getClient().newURIBuilder(TecSvcConst.BASE_URI)
              .appendEntitySetSegment("ESTwoPrim").appendKeySegment(32767)
              .appendNavigationSegment("NavPropertyETAllPrimOne")
              .appendNavigationSegment("NavPropertyETTwoPrimMany").build())
          .execute();
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());

  final ClientEntitySet entitySet = response.getBody();
  assertNotNull(entitySet);
  assertEquals(1, entitySet.getEntities().size());
  final ClientEntity entity = entitySet.getEntities().get(0);
  assertNotNull(entity);
  final ClientProperty property = entity.getProperty("PropertyString");
  assertNotNull(property);
  assertNotNull(property.getPrimitiveValue());
  assertEquals("Test String2", property.getPrimitiveValue().toValue());
}
 
Example 8
Source File: BasicITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void updateEntity() throws Exception {
  ClientEntity newEntity = getFactory().newEntity(ET_ALL_PRIM);
  newEntity.getProperties().add(getFactory().newPrimitiveProperty(PROPERTY_INT64,
      getFactory().newPrimitiveValueBuilder().buildInt64((long) 42)));

  final URI uri = getClient().newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM).appendKeySegment(32767)
      .build();
  final ODataEntityUpdateRequest<ClientEntity> request = getClient().getCUDRequestFactory().getEntityUpdateRequest(
      uri, UpdateType.REPLACE, newEntity);
  HttpUriRequest req = request.getHttpRequest();
  final ODataEntityUpdateResponse<ClientEntity> response = request.execute();
  assertNotNull(req);
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());

  // Check that the updated properties have changed and that other properties have their default values.
  final ClientEntity entity = response.getBody();
  assertNotNull(entity);
  final ClientProperty property1 = entity.getProperty(PROPERTY_INT64);
  assertNotNull(property1);
  assertShortOrInt(42, property1.getPrimitiveValue().toValue());
  final ClientProperty property2 = entity.getProperty(PROPERTY_DECIMAL);
  assertNotNull(property2);
  assertNull(property2.getPrimitiveValue().toValue());
}
 
Example 9
Source File: NavigationITCase.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void oneLevelToEntity() throws Exception {
  final ODataRetrieveResponse<ClientEntity> response =
      getClient().getRetrieveRequestFactory().getEntityRequest(
          getClient().newURIBuilder(TecSvcConst.BASE_URI)
              .appendEntitySetSegment("ESAllPrim").appendKeySegment(32767)
              .appendNavigationSegment("NavPropertyETTwoPrimOne").build())
          .execute();
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());

  final ClientEntity entity = response.getBody();
  assertNotNull(entity);
  final ClientProperty property = entity.getProperty("PropertyString");
  assertNotNull(property);
  assertNotNull(property.getPrimitiveValue());
  assertEquals("Test String4", property.getPrimitiveValue().toValue());
}
 
Example 10
Source File: ExpandSelectITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void readSelect() {
  ODataEntityRequest<ClientEntity> request = getClient().getRetrieveRequestFactory()
      .getEntityRequest(getClient().newURIBuilder(TecSvcConst.BASE_URI)
          .appendEntitySetSegment("ESAllPrim").appendKeySegment(Short.MAX_VALUE)
          .select("PropertyInt32,PropertyInt16")
          .build());
  assertNotNull(request);
  setCookieHeader(request);

  final ODataRetrieveResponse<ClientEntity> response = request.execute();
  saveCookieHeader(response);
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());

  final ClientEntity entity = response.getBody();
  assertNotNull(entity);

  assertNotNull(entity.getProperties());
  assertEquals(2, entity.getProperties().size());
  assertNull(entity.getProperty("PropertyString"));

  ClientProperty property = entity.getProperty("PropertyInt16");
  assertNotNull(property);
  assertNotNull(property.getPrimitiveValue());
  assertShortOrInt(Integer.valueOf(Short.MAX_VALUE), property.getPrimitiveValue().toValue());

  property = entity.getProperty("PropertyInt32");
  assertNotNull(property);
  assertNotNull(property.getPrimitiveValue());
  assertEquals(Integer.MAX_VALUE, property.getPrimitiveValue().toValue());
}
 
Example 11
Source File: BasicITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void readEntitySet() {
  ODataEntitySetRequest<ClientEntitySet> request = getClient().getRetrieveRequestFactory()
      .getEntitySetRequest(getClient().newURIBuilder(SERVICE_URI)
          .appendEntitySetSegment(ES_MIX_PRIM_COLL_COMP).build());
  assertNotNull(request);
  setCookieHeader(request);

  final ODataRetrieveResponse<ClientEntitySet> response = request.execute();
  saveCookieHeader(response);
  assertNotNull(response.getHeaderNames());
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
  assertContentType(response.getContentType());

  final ClientEntitySet entitySet = response.getBody();
  assertNotNull(entitySet);

  assertNull(entitySet.getCount());
  assertNull(entitySet.getNext());
  assertEquals(Collections.<ClientAnnotation> emptyList(), entitySet.getAnnotations());
  assertNull(entitySet.getDeltaLink());

  final List<ClientEntity> entities = entitySet.getEntities();
  assertNotNull(entities);
  assertEquals(3, entities.size());
  final ClientEntity entity = entities.get(2);
  assertNotNull(entity);
  final ClientProperty property = entity.getProperty(PROPERTY_INT16);
  assertNotNull(property);
  assertNotNull(property.getPrimitiveValue());
  assertShortOrInt(0, property.getPrimitiveValue().toValue());
}
 
Example 12
Source File: MediaITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void create() throws Exception {
  ODataMediaEntityCreateRequest<ClientEntity> request =
      getClient().getCUDRequestFactory().getMediaEntityCreateRequest(
          getClient().newURIBuilder(TecSvcConst.BASE_URI).appendEntitySetSegment("ESMedia").build(),
          IOUtils.toInputStream("just a test"));
  request.setContentType(ContentType.TEXT_PLAIN.toContentTypeString());
  assertNotNull(request);

  final ODataMediaEntityCreateResponse<ClientEntity> response = request.payloadManager().getResponse();
  assertEquals(HttpStatusCode.CREATED.getStatusCode(), response.getStatusCode());
  assertEquals(request.getURI() + "(5)", response.getHeader(HttpHeader.LOCATION).iterator().next());
  final ClientEntity entity = response.getBody();
  assertNotNull(entity);
  final ClientProperty property = entity.getProperty("PropertyInt16");
  assertNotNull(property);
  assertNotNull(property.getPrimitiveValue());
  assertShortOrInt(5, property.getPrimitiveValue().toValue());

  // Check that the media stream has been created.
  // This check has to be in the same session in order to access the same data provider.
  ODataMediaRequest mediaRequest = getClient().getRetrieveRequestFactory().getMediaRequest(
      getClient().newURIBuilder(TecSvcConst.BASE_URI).appendEntitySetSegment("ESMedia")
          .appendKeySegment(5).appendValueSegment().build());
  mediaRequest.addCustomHeader(HttpHeader.COOKIE, response.getHeader(HttpHeader.SET_COOKIE).iterator().next());
  ODataRetrieveResponse<InputStream> mediaResponse = mediaRequest.execute();
  assertEquals(HttpStatusCode.OK.getStatusCode(), mediaResponse.getStatusCode());
  assertEquals(ContentType.TEXT_PLAIN.toContentTypeString(), mediaResponse.getContentType());
  assertEquals("just a test", IOUtils.toString(mediaResponse.getBody()));
}
 
Example 13
Source File: BasicITCase.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void createEntity() throws Exception {
  ClientEntity newEntity = getFactory().newEntity(ET_ALL_PRIM);
  newEntity.getProperties().add(getFactory().newPrimitiveProperty(PROPERTY_INT64,
      getFactory().newPrimitiveValueBuilder().buildInt64((long) 42)));
  final ODataClient client = getClient();
  newEntity.addLink(getFactory().newEntityNavigationLink(NAV_PROPERTY_ET_TWO_PRIM_ONE,
      client.newURIBuilder(SERVICE_URI)
          .appendEntitySetSegment(ES_TWO_PRIM)
          .appendKeySegment(32766)
          .build()));

  final ODataEntityCreateRequest<ClientEntity> createRequest = client.getCUDRequestFactory().getEntityCreateRequest(
      client.newURIBuilder(SERVICE_URI).appendEntitySetSegment(ES_ALL_PRIM).build(),
      newEntity);
  assertNotNull(createRequest);
  final ODataEntityCreateResponse<ClientEntity> createResponse = createRequest.execute();
  assertNotNull(createRequest.getHttpRequest());
  assertNotNull(((AbstractODataBasicRequest)createRequest).getPayload());
  assertEquals(HttpStatusCode.CREATED.getStatusCode(), createResponse.getStatusCode());
  assertEquals(SERVICE_URI + ES_ALL_PRIM + "(1)", createResponse.getHeader(HttpHeader.LOCATION).iterator().next());
  final ClientEntity createdEntity = createResponse.getBody();
  assertNotNull(createdEntity);
  final ClientProperty property1 = createdEntity.getProperty(PROPERTY_INT64);
  assertNotNull(property1);
  assertShortOrInt(42, property1.getPrimitiveValue().toValue());
  final ClientProperty property2 = createdEntity.getProperty(PROPERTY_DECIMAL);
  assertNotNull(property2);
  assertNull(property2.getPrimitiveValue().toValue());
}
 
Example 14
Source File: CoreUtils.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public static Object getKey(
    final EdmEnabledODataClient client,
    final EntityInvocationHandler typeHandler,
    final Class<?> entityTypeRef,
    final ClientEntity entity) {

  Object res = null;

  if (!entity.getProperties().isEmpty()) {
    final Class<?> keyRef = ClassUtils.getCompoundKeyRef(entityTypeRef);
    if (keyRef == null) {
      final ClientProperty property = entity.getProperty(firstValidEntityKey(entityTypeRef));
      if (property != null && property.hasPrimitiveValue()) {
        res = primitiveValueToObject(
            property.getPrimitiveValue(), getPropertyClass(entityTypeRef, property.getName()));
      }
    } else {
      try {
        res = keyRef.newInstance();
        populate(client, typeHandler, res, CompoundKeyElement.class, entity.getProperties().iterator());
      } catch (Exception e) {
        LOG.error("Error population compound key {}", keyRef.getSimpleName(), e);
        throw new IllegalArgumentException("Cannot populate compound key");
      }
    }
  }

  return res;
}
 
Example 15
Source File: ExpandWithComplexPropertyITCase.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void readExpandHavingComplexProperty3() {
  ODataEntityRequest<ClientEntity> request = getClient().getRetrieveRequestFactory()
      .getEntityRequest(getClient().newURIBuilder(TecSvcConst.BASE_URI)
          .appendEntitySetSegment("ESCompMixPrimCollComp").appendKeySegment(1)
          .expand("PropertyMixedPrimCollComp/NavPropertyETTwoKeyNavOne,"
              + "PropertyMixedPrimCollComp/PropertyComp/NavPropertyETMediaOne,"
              + "PropertyMixedPrimCollComp/PropertyComp/NavPropertyETTwoKeyNavOne")
          .build());
  assertNotNull(request);
  setCookieHeader(request);

  final ODataRetrieveResponse<ClientEntity> response = request.execute();
  saveCookieHeader(response);
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());

  final ClientEntity entity = response.getBody();
  assertNotNull(entity);

  assertNotNull(entity.getProperties());
  assertEquals(2, entity.getProperties().size());
  assertNotNull(entity.getProperty("PropertyMixedPrimCollComp"));

  ClientProperty property = entity.getProperty("PropertyInt16");
  assertNotNull(property);
  assertNotNull(property.getPrimitiveValue());
  assertShortOrInt(Integer.valueOf(1), property.getPrimitiveValue().toValue());

  property = entity.getProperty("PropertyMixedPrimCollComp");
  assertNotNull(property);
  ClientComplexValue complexValue = property.getComplexValue();
  assertNotNull(complexValue);
  if (isJson()) {
    assertNotNull(complexValue.get("NavPropertyETTwoKeyNavOne"));
  } else {
    assertNotNull(complexValue.getNavigationLink("NavPropertyETTwoKeyNavOne"));
  }
  property = complexValue.get("PropertyComp");
  assertNotNull(property);
  complexValue = property.getComplexValue();
  assertNotNull(complexValue);
  if (isJson()) {
    assertNotNull(complexValue.get("NavPropertyETMediaOne"));
    assertNotNull(complexValue.get("NavPropertyETTwoKeyNavOne"));
  } else {
    assertNotNull(complexValue.getNavigationLink("NavPropertyETMediaOne"));
    assertNotNull(complexValue.getNavigationLink("NavPropertyETMediaOne").asInlineEntity().getEntity());
    assertNotNull(complexValue.getNavigationLink("NavPropertyETTwoKeyNavOne"));
    assertNotNull(complexValue.getNavigationLink("NavPropertyETTwoKeyNavOne").asInlineEntity().getEntity());
  }
}
 
Example 16
Source File: ExpandSelectITCase.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void readExpandSelect() {
  ODataEntityRequest<ClientEntity> request = getEdmEnabledClient().getRetrieveRequestFactory()
      .getEntityRequest(getClient().newURIBuilder(TecSvcConst.BASE_URI)
          .appendEntitySetSegment("ESTwoPrim").appendKeySegment(-365)
          .expand("NavPropertyETAllPrimMany($select=PropertyTimeOfDay,PropertySByte)")
          .select("PropertyString")
          .build());
  assertNotNull(request);
  setCookieHeader(request);

  final ODataRetrieveResponse<ClientEntity> response = request.execute();
  saveCookieHeader(response);
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());

  final ClientEntity entity = response.getBody();
  assertNotNull(entity);

  assertNotNull(entity.getProperty("PropertyInt16"));

  final ClientProperty property = entity.getProperty("PropertyString");
  assertNotNull(property);
  assertNotNull(property.getPrimitiveValue());
  assertEquals("Test String2", property.getPrimitiveValue().toValue());

  if (isJson()) {
    assertNull(entity.getNavigationLink("NavPropertyETAllPrimOne"));
  } else {
    // in xml the links will be always present; but the content will not be if no $expand unlike
    // json;metadata=minimal; json=full is same as application/xml
    assertFalse(entity.getNavigationLink("NavPropertyETAllPrimOne") instanceof ClientInlineEntity);
  }

  final ClientLink link = entity.getNavigationLink("NavPropertyETAllPrimMany");
  assertNotNull(link);
  assertEquals(ClientLinkType.ENTITY_SET_NAVIGATION, link.getType());
  final ClientInlineEntitySet inlineEntitySet = link.asInlineEntitySet();
  assertNotNull(inlineEntitySet);
  final List<? extends ClientEntity> entities = inlineEntitySet.getEntitySet().getEntities();
  assertNotNull(entities);
  assertEquals(2, entities.size());
  final ClientEntity inlineEntity = entities.get(0);
  assertEquals(3, inlineEntity.getProperties().size());
  assertShortOrInt(-128, inlineEntity.getProperty("PropertySByte").getPrimitiveValue().toValue());
  Calendar time = Calendar.getInstance();
  time.clear();
  time.set(1970, Calendar.JANUARY, 1, 23, 49, 14);
  assertEquals(new java.sql.Timestamp(time.getTimeInMillis()),
      inlineEntity.getProperty("PropertyTimeOfDay").getPrimitiveValue().toValue());
}
 
Example 17
Source File: AsyncSupportITCase.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void createEntity() throws Exception {
  ODataClient client = getClient();
  URI uri = client.newURIBuilder(SERVICE_URI)
      .appendEntitySetSegment(ES_ALL_PRIM).build();

  ClientEntity newEntity = getFactory().newEntity(new FullQualifiedName(SERVICE_NAMESPACE, "ETAllPrim"));
  newEntity.getProperties().add(getFactory().newPrimitiveProperty("PropertyInt64",
          getFactory().newPrimitiveValueBuilder().buildInt32(42)));
  newEntity.addLink(getFactory().newEntityNavigationLink(NAV_PROPERTY_ET_TWO_PRIM_ONE,
          client.newURIBuilder(SERVICE_URI)
                  .appendEntitySetSegment("ESTwoPrim")
                  .appendKeySegment(32766)
                  .build()));

  final ODataEntityCreateRequest<ClientEntity> createRequest =
      client.getCUDRequestFactory().getEntityCreateRequest(uri, newEntity);
  createRequest.setPrefer(PreferenceName.RESPOND_ASYNC + "; " + TEC_ASYNC_SLEEP + "=1");
  assertNotNull(createRequest);
  AsyncResponseWrapper<ODataResponse> asyncResponse =
      client.getAsyncRequestFactory().getAsyncRequestWrapper(createRequest).execute();

  assertTrue(asyncResponse.isPreferenceApplied());
  assertFalse(asyncResponse.isDone());

  waitTillDone(asyncResponse, 10);

  @SuppressWarnings("unchecked")
  final ODataEntityCreateResponse<ClientEntity> createResponse =
      (ODataEntityCreateResponse<ClientEntity>) asyncResponse.getODataResponse();

  assertEquals(HttpStatusCode.CREATED.getStatusCode(), createResponse.getStatusCode());
  assertEquals(SERVICE_URI + "ESAllPrim(1)", createResponse.getHeader(HttpHeader.LOCATION).iterator().next());
  final ClientEntity createdEntity = createResponse.getBody();
  assertNotNull(createdEntity);
  final ClientProperty property1 = createdEntity.getProperty("PropertyInt64");
  assertNotNull(property1);
  assertShortOrInt(42, property1.getPrimitiveValue().toValue());
  final ClientProperty property2 = createdEntity.getProperty("PropertyDecimal");
  assertNotNull(property2);
  assertNull(property2.getPrimitiveValue().toValue());
}
 
Example 18
Source File: ExpandWithComplexPropertyITCase.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void readExpandHavingComplexProperty1() {
  ODataEntityRequest<ClientEntity> request = getClient().getRetrieveRequestFactory()
      .getEntityRequest(getClient().newURIBuilder(TecSvcConst.BASE_URI)
          .appendEntitySetSegment("ESCompMixPrimCollComp").appendKeySegment(1)
          .expand("PropertyMixedPrimCollComp/PropertyComp/NavPropertyETTwoKeyNavOne($expand=NavPropertySINav),"
              + "PropertyMixedPrimCollComp/PropertyComp/NavPropertyETMediaOne")
          .build());
  assertNotNull(request);
  setCookieHeader(request);

  final ODataRetrieveResponse<ClientEntity> response = request.execute();
  saveCookieHeader(response);
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());

  final ClientEntity entity = response.getBody();
  assertNotNull(entity);

  assertNotNull(entity.getProperties());
  assertEquals(2, entity.getProperties().size());
  assertNotNull(entity.getProperty("PropertyMixedPrimCollComp"));

  ClientProperty property = entity.getProperty("PropertyInt16");
  assertNotNull(property);
  assertNotNull(property.getPrimitiveValue());
  assertShortOrInt(Integer.valueOf(1), property.getPrimitiveValue().toValue());

  property = entity.getProperty("PropertyMixedPrimCollComp");
  assertNotNull(property);
  ClientComplexValue complexValue = property.getComplexValue();
  assertNotNull(complexValue);
  property = complexValue.get("PropertyComp");
  assertNotNull(property);
  complexValue = property.getComplexValue();
  assertNotNull(complexValue);
  if (isJson()) {
    property = complexValue.get("NavPropertyETTwoKeyNavOne");
    assertNotNull(property);
    assertNotNull(complexValue.get("NavPropertyETMediaOne"));
    complexValue = property.getComplexValue();
    assertNotNull(complexValue);
    assertNotNull(complexValue.get("NavPropertySINav"));
  } else {
    ClientLink etkeyNavOneLink = complexValue.getNavigationLink("NavPropertyETTwoKeyNavOne");
    assertNotNull(etkeyNavOneLink);
    ClientEntity navEntity = etkeyNavOneLink.asInlineEntity().getEntity();
    assertNotNull(navEntity.getNavigationLink("NavPropertySINav"));
    assertNotNull(navEntity.getNavigationLink("NavPropertySINav").asInlineEntity().getEntity());
    assertNotNull(complexValue.getNavigationLink("NavPropertyETMediaOne"));
  }
}
 
Example 19
Source File: EntityWithStreamITCase.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void readEntitySetWithStreamProperty() {
  ODataEntitySetRequest<ClientEntitySet> request = getClient().getRetrieveRequestFactory()
      .getEntitySetRequest(getClient().newURIBuilder(SERVICE_URI)
          .appendEntitySetSegment("ESWithStream").build());    
  assertNotNull(request);
  setCookieHeader(request);

  final ODataRetrieveResponse<ClientEntitySet> response = request.execute();
  saveCookieHeader(response);
  assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
  assertEquals("application/json; odata.metadata=full", response.getContentType());

  final ClientEntitySet entitySet = response.getBody();
  assertNotNull(entitySet);

  assertNull(entitySet.getCount());
  assertNull(entitySet.getNext());
  assertEquals(Collections.<ClientAnnotation> emptyList(), entitySet.getAnnotations());
  assertNull(entitySet.getDeltaLink());

  final List<ClientEntity> entities = entitySet.getEntities();
  assertNotNull(entities);
  assertEquals(2, entities.size());
  
  ClientEntity entity = entities.get(0);
  assertNotNull(entity);
  ClientProperty property = entity.getProperty(PROPERTY_INT16);
  assertNotNull(property);
  assertNotNull(property.getPrimitiveValue());
  assertShortOrInt(Short.MAX_VALUE, property.getPrimitiveValue().toValue());
  
  ClientLink link = entity.getMediaEditLinks().get(0);
  assertNotNull(link);
  
  assertEquals("/readLink", link.getLink().toASCIIString());
  assertEquals(ClientLinkType.MEDIA_READ, link.getType());
  
  entity = entities.get(1);
  assertNotNull(entity);
  property = entity.getProperty(PROPERTY_INT16);
  assertNotNull(property);
  assertNotNull(property.getPrimitiveValue());
  assertShortOrInt(7, property.getPrimitiveValue().toValue());
  
  assertEquals(1, entity.getMediaEditLinks().size());
  
  link = entity.getMediaEditLinks().get(0);
  assertNotNull(link);
  assertEquals("http://mediaserver:1234/editLink", link.getLink().toASCIIString());
  assertEquals(ClientLinkType.fromString(Constants.NS_MEDIA_EDIT_LINK_REL, "image/jpeg").name(), 
      link.getType().name());
  assertEquals("eTag", link.getMediaETag());    
}
 
Example 20
Source File: EntityTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
private void singleton(final ContentType contentType) throws Exception {
  final InputStream input = getClass().getResourceAsStream("VipCustomer." + getSuffix(contentType));
  final ClientEntity entity = client.getBinder().getODataEntity(
      client.getDeserializer(contentType).toEntity(input));
  assertNotNull(entity);

  assertEquals("Microsoft.Test.OData.Services.ODataWCFService.Customer", entity.getTypeName().toString());

  final ClientProperty birthday = entity.getProperty("Birthday");
  assertTrue(birthday.hasPrimitiveValue());
  assertEquals(EdmDateTimeOffset.getInstance(), birthday.getPrimitiveValue().getType());

  final ClientProperty timeBetweenLastTwoOrders = entity.getProperty("TimeBetweenLastTwoOrders");
  assertTrue(timeBetweenLastTwoOrders.hasPrimitiveValue());
  assertEquals(EdmDuration.getInstance(), timeBetweenLastTwoOrders.getPrimitiveValue().getType());

  int checked = 0;
  for (ClientLink link : entity.getNavigationLinks()) {
    if ("Parent".equals(link.getName())) {
      checked++;
      assertEquals(ClientLinkType.ENTITY_NAVIGATION, link.getType());
    }
    if ("Orders".equals(link.getName())) {
      checked++;
      if (contentType.isCompatible(ContentType.APPLICATION_ATOM_SVC)
          || contentType.isCompatible(ContentType.APPLICATION_ATOM_XML)) {
        assertEquals(ClientLinkType.ENTITY_SET_NAVIGATION, link.getType());
      }
    }
    if ("Company".equals(link.getName())) {
      checked++;
      assertEquals(ClientLinkType.ENTITY_NAVIGATION, link.getType());
    }
  }
  assertEquals(3, checked);

  assertEquals(2, entity.getOperations().size());
  assertEquals("#Microsoft.Test.OData.Services.ODataWCFService.ResetAddress",
      entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.ResetAddress").getMetadataAnchor());
  assertEquals("#Microsoft.Test.OData.Services.ODataWCFService.GetHomeAddress",
      entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.GetHomeAddress").getMetadataAnchor());

  // operations won't get serialized
  entity.getOperations().clear();
  final ClientEntity written = client.getBinder().getODataEntity(
      new ResWrap<Entity>((URI) null, null, client.getBinder().getEntity(entity)));
  assertEquals(entity, written);
  input.close();
}