org.apache.olingo.commons.api.edm.EdmEntitySet Java Examples

The following examples show how to use org.apache.olingo.commons.api.edm.EdmEntitySet. 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: Storage.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public EntityIterator readEntitySetDataStreamed(EdmEntitySet edmEntitySet)throws ODataApplicationException{
	// actually, this is only required if we have more than one Entity Sets
	if(edmEntitySet.getName().equals(DemoEdmProvider.ES_PRODUCTS_NAME)){
		final Iterator<Entity> it = productList.iterator();
		return new EntityIterator() {
			@Override
			public boolean hasNext() {
				return it.hasNext();
			}

			@Override
			public Entity next() {
				return it.next();
			}
		};
	}
	return null;
}
 
Example #2
Source File: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void entitySetTwoPrimNoMetadata() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim");
  final EntityCollection entitySet = data.readAll(edmEntitySet);
  final String resultString = IOUtils.toString(serializerNoMetadata
      .entityCollection(metadata, edmEntitySet.getEntityType(), entitySet,
          EntityCollectionSerializerOptions.with()
              .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
              .build()).getContent());
  final String expectedResult = "{\"value\":["
      + "{\"PropertyInt16\":32766,\"PropertyString\":\"Test String1\"},"
      + "{\"PropertyInt16\":-365,\"PropertyString\":\"Test String2\"},"
      + "{\"PropertyInt16\":-32766,\"PropertyString\":null},"
      + "{\"PropertyInt16\":32767,\"PropertyString\":\"Test String4\"}]}";
  Assert.assertEquals(expectedResult, resultString);
}
 
Example #3
Source File: CarsProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private EdmEntitySet getEdmEntitySet(final UriInfoResource uriInfo) throws ODataApplicationException {
  final List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
  /*
   * To get the entity set we have to interpret all URI segments
   */
  if (!(resourcePaths.get(0) instanceof UriResourceEntitySet)) {
    throw new ODataApplicationException("Invalid resource type for first segment.",
        HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), Locale.ENGLISH);
  }

  /*
   * Here we should interpret the whole URI but in this example we do not support navigation so we throw an exception
   */

  final UriResourceEntitySet uriResource = (UriResourceEntitySet) resourcePaths.get(0);
  return uriResource.getEntitySet();
}
 
Example #4
Source File: MetadataTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void testOLINGO1100() {
  final Edm edm = client.getReader().readMetadata(getClass().getResourceAsStream("olingo1100.xml"));
  assertNotNull(edm);
  final EdmEntityContainer container = edm.getEntityContainer(
      new FullQualifiedName("Microsoft.Exchange.Services.OData.Model", "EntityContainer"));
  assertNotNull(container);
  final EdmEntitySet providers = container.getEntitySet("Provider");
  assertNotNull(providers);
  assertEquals(edm.getEntityType(new FullQualifiedName(container.getNamespace(), "Provider")),
      providers.getEntityType());
  assertEquals(container.getEntitySet("ProviderLicense"), providers.getRelatedBindingTarget("ProviderLicense"));
  assertNull(providers.getRelatedBindingTarget("ProviderLicensePractice"));
  assertNull(providers.getRelatedBindingTarget("Provider"));
  final EdmEntitySet providerLicenses = container.getEntitySet("ProviderLicense");
  assertEquals(edm.getEntityType(new FullQualifiedName(container.getNamespace(), "ProviderLicense")),
      providerLicenses.getEntityType());
  assertEquals(container.getEntitySet("ProviderLicensePractice"), 
      providerLicenses.getRelatedBindingTarget("ProviderLicensePractice"));
  assertNull(providerLicenses.getRelatedBindingTarget("ProviderLicense"));
  assertNull(providerLicenses.getRelatedBindingTarget("Provider"));
  final EdmEntitySet providerLicensePractices = container.getEntitySet("ProviderLicensePractice");
  assertNull(providerLicensePractices.getRelatedBindingTarget("ProviderLicensePractice"));
  assertNull(providerLicensePractices.getRelatedBindingTarget("Provider"));
  assertNull(providerLicenses.getRelatedBindingTarget("ProviderLicense"));
}
 
Example #5
Source File: TripPinHandler.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public void updateEntity(DataRequest request, Entity entity, boolean merge, String eTag,
    EntityResponse response) throws ODataLibraryException, ODataApplicationException {
  EdmEntitySet edmEntitySet = request.getEntitySet();
  
  Entity currentEntity = this.dataModel.getEntity(edmEntitySet.getName(), request.getKeyPredicates());
  if (currentEntity == null) {
    response.writeNotFound(true);
    return;
  }
  String key = edmEntitySet.getEntityType().getKeyPredicateNames().get(0);
  String baseURL = request.getODataRequest().getRawBaseUri();
  boolean updated = this.dataModel.updateEntity(edmEntitySet, eTag, key, currentEntity
      .getProperty(key).getValue(), merge, entity, baseURL);

  if (updated) {
    response.writeUpdatedEntity();
  } else {
    response.writeNotModified();
  }
}
 
Example #6
Source File: ODataXmlDeserializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void complexProperty() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixPrimCollComp");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("PropertyComp");
  final String payload = "<data:PropertyComp xmlns:data=\"http://docs.oasis-open.org/odata/ns/data\" "
      + " xmlns:metadata=\"http://docs.oasis-open.org/odata/ns/metadata\"\n"
      + " metadata:type=\"#olingo.odata.test1.CTTwoPrim\">\n"
      + "  <data:PropertyInt16>123</data:PropertyInt16>\n" 
      + "  <data:PropertyString metadata:null=\"true\"/>\n"
      + "</data:PropertyComp>";

  final Property result = deserializer.property(new ByteArrayInputStream(payload.getBytes()), edmProperty)
      .getProperty();

  Assert.assertEquals("PropertyComp", result.getName());
  Assert.assertTrue(result.isComplex());
  final ComplexValue cv = result.asComplex();
  Assert.assertEquals("olingo.odata.test1.CTTwoPrim", result.getType());
  Assert.assertEquals((short) 123, getCVProperty(cv, "PropertyInt16").asPrimitive());
  Assert.assertTrue(getCVProperty(cv, "PropertyString").isNull());    
}
 
Example #7
Source File: ContextURLHelperTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void buildExpandSelect() throws Exception {
  final EdmEntitySet entitySet = entityContainer.getEntitySet("ESTwoPrim");
  final ExpandItem expandItem1 = ExpandSelectMock.mockExpandItem(entitySet, "NavPropertyETAllPrimOne");
  final EdmEntitySet innerEntitySet = entityContainer.getEntitySet("ESAllPrim");
  ExpandItem expandItem2 = ExpandSelectMock.mockExpandItem(entitySet, "NavPropertyETAllPrimMany");
  final SelectOption innerSelect = ExpandSelectMock.mockSelectOption(Arrays.asList(
      ExpandSelectMock.mockSelectItem(innerEntitySet, "PropertyInt32")));
  Mockito.when(expandItem2.getSelectOption()).thenReturn(innerSelect);
  final ExpandOption expand = ExpandSelectMock.mockExpandOption(Arrays.asList(
      expandItem1, expandItem2));
  final SelectItem selectItem = ExpandSelectMock.mockSelectItem(entitySet, "PropertyString");
  final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList(selectItem));
  final ContextURL contextURL = ContextURL.with().entitySet(entitySet)
      .selectList(ContextURLHelper.buildSelectList(entitySet.getEntityType(), expand, select)).build();
  assertEquals("$metadata#ESTwoPrim(PropertyInt16,PropertyString,NavPropertyETAllPrimOne(),"
      + "NavPropertyETAllPrimMany(PropertyInt16,PropertyInt32))",
      ContextURLBuilder.create(contextURL).toASCIIString());
}
 
Example #8
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public void createMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
    ContentType requestFormat, ContentType responseFormat) throws ODataApplicationException, ODataLibraryException {
  
  final EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
  final byte[] mediaContent = odata.createFixedFormatDeserializer().binary(request.getBody());
  
  final Entity entity = storage.createMediaEntity(edmEntitySet.getEntityType(), 
                                                  requestFormat.toContentTypeString(), 
                                                  mediaContent);
  
  final ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build();
  final EntitySerializerOptions opts = EntitySerializerOptions.with().contextURL(contextUrl).build();
  final SerializerResult serializerResult = odata.createSerializer(responseFormat).entity(serviceMetadata,
      edmEntitySet.getEntityType(), entity, opts);
  
  final String location = request.getRawBaseUri() + '/'
      + odata.createUriHelper().buildCanonicalURL(edmEntitySet, entity);
  response.setContent(serializerResult.getContent());
  response.setStatusCode(HttpStatusCode.CREATED.getStatusCode());
  response.setHeader(HttpHeader.LOCATION, location);
  response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example #9
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public void updateMediaEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo,
    ContentType requestFormat, ContentType responseFormat) throws ODataApplicationException, ODataLibraryException {
  
  final UriResource firstResoucePart = uriInfo.getUriResourceParts().get(0);
  if (firstResoucePart instanceof UriResourceEntitySet) {
    final EdmEntitySet edmEntitySet = Util.getEdmEntitySet(uriInfo);
    final UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) firstResoucePart;

    final Entity entity = storage.readEntityData(edmEntitySet, uriResourceEntitySet.getKeyPredicates());
    if (entity == null) {
      throw new ODataApplicationException("Entity not found", HttpStatusCode.NOT_FOUND.getStatusCode(),
          Locale.ENGLISH);
    }
    
    final byte[] mediaContent = odata.createFixedFormatDeserializer().binary(request.getBody());
    storage.updateMedia(entity, requestFormat.toContentTypeString(), mediaContent);
    
    response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
  } else {
    throw new ODataApplicationException("Not implemented", HttpStatusCode.NOT_IMPLEMENTED.getStatusCode(), 
        Locale.ENGLISH);
  }
}
 
Example #10
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void entityCollectionReference() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EntityCollection entityCollection = data.readAll(edmEntitySet);

  final SerializerResult serializerResult = serializer.referenceCollection(metadata,
      edmEntitySet,
      entityCollection,
      ReferenceCollectionSerializerOptions.with()
          .contextURL(ContextURL.with().asCollection().suffix(Suffix.REFERENCE).build())
          .build());

  final String resultString = IOUtils.toString(serializerResult.getContent());

  Assert.assertEquals("{\"@context\":\"../$metadata#Collection($ref)\","
      + "\"value\":[{\"@id\":\"ESAllPrim(32767)\"},"
      + "{\"@id\":\"ESAllPrim(-32768)\"},"
      + "{\"@id\":\"ESAllPrim(0)\"},{\"@id\":\"ESAllPrim(10)\"}]}",
      resultString);
}
 
Example #11
Source File: EdmAssistedJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void expandWithEdm() throws Exception {
  final EdmEntitySet entitySet = entityContainer.getEntitySet("ESTwoPrim");
  Entity entity = new Entity()
      .addProperty(new Property(null, "PropertyInt16", ValueType.PRIMITIVE, (short) 42))
      .addProperty(new Property(null, "PropertyString", ValueType.PRIMITIVE, "test"));
  final Entity target = new Entity()
      .addProperty(new Property(null, "PropertyInt16", ValueType.PRIMITIVE, (short) 2))
      .addProperty(new Property(null, "PropertyByte", ValueType.PRIMITIVE, 3L));
  Link link = new Link();
  link.setTitle("NavPropertyETAllPrimOne");
  link.setInlineEntity(target);
  entity.getNavigationLinks().add(link);
  EntityCollection entityCollection = new EntityCollection();
  entityCollection.getEntities().add(entity);
  Assert.assertEquals("{\"@odata.context\":\"$metadata#ESTwoPrim\",\"value\":[{\"@odata.id\":null,"
      + "\"PropertyInt16\":42,\"PropertyString\":\"test\","
      + "\"NavPropertyETAllPrimOne\":{\"@odata.id\":null,\"PropertyInt16\":2,\"PropertyByte\":3}}]}",
      serialize(serializer, metadata, entitySet, entityCollection, null));
}
 
Example #12
Source File: TransactionalEntityManager.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Entity copyEntityRecursively(final EdmEntitySet edmEntitySet, final Entity entity) {
  // Check if entity is already copied
  if(containsEntityInCopyMap(edmEntitySet.getName(), entity)) {
    return getEntityFromCopyMap(edmEntitySet.getName(), entity);
  } else {
    final Entity newEntity = copyEntity(entity);
    addEntityToCopyMap(edmEntitySet.getName(), entity, newEntity);
    
    // Create nested entities recursively
    for(final Link link : entity.getNavigationLinks()) {
      newEntity.getNavigationLinks().add(copyLink(edmEntitySet, link));
    }
    
    return newEntity;
  }
}
 
Example #13
Source File: ContextURLHelperTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void buildSelectComplex() throws Exception {
  final EdmEntitySet entitySet = entityContainer.getEntitySet("ESCompMixPrimCollComp");
  final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList(
      ExpandSelectMock.mockSelectItem(entitySet,
          "PropertyMixedPrimCollComp", "PropertyComp", "PropertyString"),
          ExpandSelectMock.mockSelectItem(entitySet,
              "PropertyMixedPrimCollComp", "PropertyComp", "PropertyInt16"),
              ExpandSelectMock.mockSelectItem(entitySet, "PropertyMixedPrimCollComp", "CollPropertyString"),
              ExpandSelectMock.mockSelectItem(entitySet, "PropertyMixedPrimCollComp", "CollPropertyComp"),
              ExpandSelectMock.mockSelectItem(entitySet, "PropertyInt16")));
  final ContextURL contextURL = ContextURL.with().entitySet(entitySet)
      .selectList(ContextURLHelper.buildSelectList(entitySet.getEntityType(), null, select)).build();
  assertEquals("$metadata#ESCompMixPrimCollComp("
      + "PropertyInt16,"
      + "PropertyMixedPrimCollComp/CollPropertyString,"
      + "PropertyMixedPrimCollComp/PropertyComp/PropertyInt16,"
      + "PropertyMixedPrimCollComp/PropertyComp/PropertyString,"
      + "PropertyMixedPrimCollComp/CollPropertyComp)",
      ContextURLBuilder.create(contextURL).toASCIIString());
}
 
Example #14
Source File: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void selectAll() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim");
  final Entity entity = data.readAll(edmEntitySet).getEntities().get(0);
  final SelectItem selectItem1 = ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertyString");
  SelectItem selectItem2 = Mockito.mock(SelectItem.class);
  Mockito.when(selectItem2.isStar()).thenReturn(true);
  final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList(selectItem1, selectItem2));
  InputStream result = serializer.entity(metadata, edmEntitySet.getEntityType(), entity,
      EntitySerializerOptions.with()
          .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build())
          .select(select)
          .build()).getContent();
  final String resultString = IOUtils.toString(result);
  final String expectedResult = "{\"@odata.context\":\"$metadata#ESTwoPrim/$entity\","
      + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"PropertyInt16\":32766,\"PropertyString\":\"Test String1\"}";
  Assert.assertEquals(expectedResult, resultString);
}
 
Example #15
Source File: ODataXmlSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitiveProperty() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("PropertyString");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty(edmProperty.getName());
  final String resultString = IOUtils.toString(serializer
      .primitive(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
          PrimitiveSerializerOptions.with()
              .contextURL(ContextURL.with()
                  .entitySet(edmEntitySet).keyPath("32767").navOrPropertyPath(edmProperty.getName())
                  .build())
              .build()).getContent());

  String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
      + "<m:value xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\" "
      + "m:context=\"../$metadata#ESAllPrim(32767)/PropertyString\" "
      + "m:metadata-etag=\"metadataETag\">"
      + "First Resource - positive values</m:value>";
  Assert.assertEquals(expected, resultString);
}
 
Example #16
Source File: ContextURLHelperTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void buildSelectWithAction() throws Exception {
  final EdmEntitySet entitySet = entityContainer.getEntitySet("ESTwoKeyNav");
  final EdmAction action = edm.getBoundAction(
      new FullQualifiedName("olingo.odata.test1.BAESTwoKeyNavRTESTwoKeyNav"), 
      new FullQualifiedName("olingo.odata.test1.ETTwoKeyNav"), true);
  final SelectItem selectItem = ExpandSelectMock.mockSelectItemHavingAction(
      entitySet, action);
  final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList(
      selectItem));
  final ContextURL contextURL = ContextURL.with().entitySet(entitySet)
      .selectList(ContextURLHelper.buildSelectList(entitySet.getEntityType(), null, select)).build();
  assertEquals("$metadata#ESTwoKeyNav(PropertyInt16,PropertyString,"
      + "olingo.odata.test1.BAESTwoKeyNavRTESTwoKeyNav)",
      ContextURLBuilder.create(contextURL).toASCIIString());
}
 
Example #17
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
public void deleteEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo)
         throws ODataApplicationException {
	
	// 1. Retrieve the entity set which belongs to the requested entity 
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. delete the data in backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	storage.deleteEntityData(edmEntitySet, keyPredicates);
	
	//3. configure the response object
	response.setStatusCode(HttpStatusCode.NO_CONTENT.getStatusCode());
}
 
Example #18
Source File: TechnicalPrimitiveComplexProcessor.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private ContextURL getContextUrl(final EdmEntitySet entitySet, final Entity entity, final List<String> path,
    final EdmType type, final RepresentationType representationType,
    final ExpandOption expand, final SelectOption select) throws ODataLibraryException {
  final UriHelper helper = odata.createUriHelper();
  Builder builder = ContextURL.with();
  builder = entitySet == null ?
      representationType == RepresentationType.PRIMITIVE || representationType == RepresentationType.COMPLEX ?
          builder.type(type) :
          builder.type(type).asCollection() :
      builder.entitySet(entitySet).keyPath(helper.buildKeyPredicate(entitySet.getEntityType(), entity));
  if (entitySet != null && !path.isEmpty()) {
    builder = builder.navOrPropertyPath(buildPropertyPath(path));
  }
  builder = builder.selectList(
      type.getKind() == EdmTypeKind.PRIMITIVE
          || type.getKind() == EdmTypeKind.ENUM
          || type.getKind() == EdmTypeKind.DEFINITION ?
              null :
              helper.buildContextURLSelectList((EdmStructuredType) type, expand, select));
  return builder.build();
}
 
Example #19
Source File: QueryHandler.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This method applies server-side paging to the given entity collection.
 *
 * @param skipTokenOption   Current skip token option (from a previous response's next link)
 * @param entityCollection  Entity collection
 * @param edmEntitySet      EDM entity set to decide whether paging must be done
 * @param rawRequestUri     Request URI (used to construct the next link)
 * @param preferredPageSize Preference for page size
 * @return Chosen page size
 * @throws ODataApplicationException
 */
public static Integer applyServerSidePaging(final SkipTokenOption skipTokenOption,
                                            EntityCollection entityCollection, final EdmEntitySet edmEntitySet,
                                            final String rawRequestUri, final Integer preferredPageSize)
        throws ODataApplicationException {
    if (edmEntitySet != null) {
        final int pageSize = getPageSize(preferredPageSize);
        final int page = getPage(skipTokenOption);
        final int itemsToSkip = pageSize * page;
        if (itemsToSkip <= entityCollection.getEntities().size()) {
            popAtMost(entityCollection, itemsToSkip);
            final int remainingItems = entityCollection.getEntities().size();
            reduceToSize(entityCollection, pageSize);
            // Determine if a new next Link has to be provided.
            if (remainingItems > pageSize) {
                entityCollection.setNext(createNextLink(rawRequestUri, edmEntitySet, page + 1));
            }
        } else {
            throw new ODataApplicationException("Nothing found.", HttpStatusCode.NOT_FOUND.getStatusCode(),
                                                Locale.ROOT);
        }
        return pageSize;
    }
    return null;
}
 
Example #20
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test(expected = UriParserSemanticException.class)
public void testPropertyPathExpWithoutProperty() throws Exception {
  final String entitySetName = "ESName";
  final String keyPropertyName = "a";
  EdmProperty keyProperty = mockProperty(keyPropertyName, 
      OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String));
  EdmKeyPropertyRef keyPropertyRef = mockKeyPropertyRef(keyPropertyName, keyProperty);
  EdmEntityType entityType = mockEntityType(keyPropertyName, keyPropertyRef);
  Mockito.when(entityType.getFullQualifiedName()).thenReturn(new FullQualifiedName("test.ETName"));
  EdmEntitySet entitySet = mockEntitySet(entitySetName, entityType);
  EdmEntityContainer container = mockContainer(entitySetName, entitySet);
  Edm mockedEdm = Mockito.mock(Edm.class);
  Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container);
  
  UriTokenizer tokenizer = new UriTokenizer("a eq \'abc\'");
  new ExpressionParser(mockedEdm, odata).parse(tokenizer, entityType, null, null);
}
 
Example #21
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public Entity readEntityData(EdmEntitySet edmEntitySet, List<UriParameter> keyParams)
				throws ODataApplicationException{

	EdmEntityType edmEntityType = edmEntitySet.getEntityType();

	// actually, this is only required if we have more than one Entity Type
	if(edmEntityType.getName().equals(DemoEdmProvider.ET_PRODUCT_NAME)){
		return getProduct(edmEntityType, keyParams);
	}

	return null;
}
 
Example #22
Source File: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void entityWithStreamMetadataFull() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESWithStream");
  final EntityCollection collection = data.readAll(edmEntitySet);
  InputStream result = serializerFullMetadata.entityCollection(metadata, edmEntitySet.getEntityType(), collection,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();
  final String resultString = IOUtils.toString(result);
  final String expectedResult = "{\"@odata.context\":\"$metadata#ESWithStream\","
      + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":[{"
      + "\"@odata.type\":\"#olingo.odata.test1.ETWithStream\","
      + "\"@odata.id\":\"ESWithStream(32767)\","
      + "\"[email protected]\":\"#Int16\","
      + "\"PropertyInt16\":32767,"
      + "\"[email protected]\":\"#Stream\","
      + "\"[email protected]\":\"readLink\"},"
      + "{"
      + "\"@odata.type\":\"#olingo.odata.test1.ETWithStream\","
      + "\"@odata.id\":\"ESWithStream(7)\","
      + "\"[email protected]\":\"#Int16\","
      + "\"PropertyInt16\":7,"
      + "\"[email protected]\":\"#Stream\","
      + "\"[email protected]\":\"eTag\","
      + "\"[email protected]\":\"image/jpeg\","
      + "\"[email protected]\":\"http://mediaserver:1234/editLink\""
      + "}]}";
  Assert.assertEquals(expectedResult, resultString);
}
 
Example #23
Source File: ContextURLHelperTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void buildSelectAll() throws Exception {
  final EdmEntitySet entitySet = entityContainer.getEntitySet("ESAllPrim");
  final SelectItem selectItem1 = ExpandSelectMock.mockSelectItem(entitySet, "PropertyGuid");
  SelectItem selectItem2 = Mockito.mock(SelectItem.class);
  Mockito.when(selectItem2.isStar()).thenReturn(true);
  final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList(selectItem1, selectItem2));
  final ContextURL contextURL = ContextURL.with().entitySet(entitySet)
      .selectList(ContextURLHelper.buildSelectList(entitySet.getEntityType(), null, select)).build();
  assertEquals("$metadata#ESAllPrim(*)", ContextURLBuilder.create(contextURL).toASCIIString());
}
 
Example #24
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void expandSelect() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoPrim");
  final EdmEntityType entityType = edmEntitySet.getEntityType();
  final Entity entity = data.readAll(edmEntitySet).getEntities().get(3);
  final SelectOption select = ExpandSelectMock.mockSelectOption(Collections.singletonList(
      ExpandSelectMock.mockSelectItem(entityContainer.getEntitySet("ESAllPrim"), "PropertyDate")));
  ExpandItem expandItem = ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETAllPrimOne");
  Mockito.when(expandItem.getSelectOption()).thenReturn(select);
  final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList(expandItem));
  final String resultString = IOUtils.toString(serializer
      .entity(metadata, entityType, entity,
          EntitySerializerOptions.with()
              .contextURL(ContextURL.with().entitySet(edmEntitySet)
                  .selectList(helper.buildContextURLSelectList(entityType, expand, select))
                  .suffix(Suffix.ENTITY).build())
              .expand(expand)
              .build()).getContent());
  Assert.assertEquals("{"
      + "\"@context\":\"$metadata#ESTwoPrim(PropertyInt16,"
      + "NavPropertyETAllPrimOne(PropertyInt16,PropertyDate))/$entity\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"PropertyInt16\":32767,\"PropertyString\":\"Test String4\","
      + "\"NavPropertyETAllPrimOne\":{\"@id\":\"ESAllPrim(32767)\","
      + "\"PropertyInt16\":32767,\"PropertyDate\":\"2012-12-03\"}}",
      resultString);
}
 
Example #25
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void selectComplexTwice() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESFourKeyAlias");
  final EdmEntityType entityType = edmEntitySet.getEntityType();
  final EntityCollection entitySet = data.readAll(edmEntitySet);
  final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList(
      ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertyComp", "PropertyString"),
      ExpandSelectMock.mockSelectItem(edmEntitySet, "PropertyCompComp", "PropertyComp")));
  final String resultString = IOUtils.toString(serializer
      .entityCollection(metadata, entityType, entitySet,
          EntityCollectionSerializerOptions.with()
              .contextURL(ContextURL.with().entitySet(edmEntitySet)
                  .selectList(helper.buildContextURLSelectList(entityType, null, select))
                  .build())
              .select(select)
              .build()).getContent());
  
  String expected = "{"
          + "\"@context\":\"$metadata#ESFourKeyAlias"
          +   "(PropertyInt16,PropertyComp/PropertyString,PropertyCompComp/PropertyComp)\","
          + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
          + "\"value\":[{"
              + "\"@id\":\"ESFourKeyAlias(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')\","
              + "\"PropertyInt16\":1,\"PropertyComp\":{"
                  + "\"PropertyString\":\"Num11\""
              + "},"
              + "\"PropertyCompComp\":{"
                  + "\"PropertyComp\":{"
                      + "\"@type\":\"#olingo.odata.test1.CTBase\","
                      + "\"PropertyInt16\":111,"
                      + "\"PropertyString\":\"Num111\","
                      + "\"AdditionalPropString\":\"Test123\""
          + "}}}]}";
  
  Assert.assertEquals(expected, resultString);
}
 
Example #26
Source File: MetadataTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void readAnnotationFetchingAllEntitySets() {
  final Edm edm = fetchEdm();
  assertNotNull(edm);
  EdmEntityContainer container = edm.getEntityContainer();
  List<EdmEntitySet> entitySets = container.getEntitySets();
  assertEquals(4, entitySets.size());
  
  FullQualifiedName termName = new FullQualifiedName("UI", "HeaderInfo");
  EdmTerm term = edm.getTerm(termName);
  EdmAnnotation annotation = entitySets.get(0).getAnnotation(term, null);
  assertNotNull(annotation);
}
 
Example #27
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void entityWithStreamMetadataFull() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESWithStream");
  final EntityCollection collection = data.readAll(edmEntitySet);
  InputStream result = serializerFullMetadata.entityCollection(metadata, edmEntitySet.getEntityType(), collection,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();
  final String resultString = IOUtils.toString(result);
  final String expectedResult = "{\"@context\":\"$metadata#ESWithStream\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":[{"
      + "\"@type\":\"#olingo.odata.test1.ETWithStream\","
      + "\"@id\":\"ESWithStream(32767)\","
      + "\"PropertyInt16@type\":\"#Int16\","
      + "\"PropertyInt16\":32767,"
      + "\"PropertyStream@type\":\"#Stream\","
      + "\"PropertyStream@mediaReadLink\":\"readLink\"},"
      + "{"
      + "\"@type\":\"#olingo.odata.test1.ETWithStream\","
      + "\"@id\":\"ESWithStream(7)\","
      + "\"PropertyInt16@type\":\"#Int16\","
      + "\"PropertyInt16\":7,"
      + "\"PropertyStream@type\":\"#Stream\","
      + "\"PropertyStream@mediaEtag\":\"eTag\","
      + "\"PropertyStream@mediaContentType\":\"image/jpeg\","
      + "\"PropertyStream@mediaEditLink\":\"http://mediaserver:1234/editLink\""
      + "}]}";
  Assert.assertEquals(expectedResult, resultString);
}
 
Example #28
Source File: DemoEntityProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void readEntity(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat)
						throws ODataApplicationException, SerializerException {

	// 1. retrieve the Entity Type
	List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
	// Note: only in our example we can assume that the first segment is the EntitySet
	UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0);
	EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

	// 2. retrieve the data from backend
	List<UriParameter> keyPredicates = uriResourceEntitySet.getKeyPredicates();
	Entity entity = storage.readEntityData(edmEntitySet, keyPredicates);

	// 3. serialize
	EdmEntityType entityType = edmEntitySet.getEntityType();

	ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).suffix(ContextURL.Suffix.ENTITY).build();
 	// expand and select currently not supported
	EntitySerializerOptions options = EntitySerializerOptions.with().contextURL(contextUrl).build();

	ODataSerializer serializer = this.odata.createSerializer(responseFormat);
	SerializerResult serializerResult = serializer.entity(serviceMetadata, entityType, entity, options);
	InputStream entityStream = serializerResult.getContent();

	//4. configure the response object
	response.setContent(entityStream);
	response.setStatusCode(HttpStatusCode.OK.getStatusCode());
	response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example #29
Source File: ContextURLHelperTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void buildEntityTypeCastInSelect2() throws Exception {
  final EdmEntitySet entitySet = entityContainer.getEntitySet("ESTwoKeyNav");
  final EdmEntityType derivedEntityType = edm.getEntityType(
      new FullQualifiedName("olingo.odata.test1.ETBaseTwoKeyNav"));
  final SelectItem selectItem = ExpandSelectMock.mockSelectItemOnDerivedEntityTypes( 
      "PropertyDate", derivedEntityType);
  Mockito.when(selectItem.getStartTypeFilter()).thenReturn(derivedEntityType);
  final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList(selectItem));
  final ContextURL contextURL = ContextURL.with().entitySet(entitySet)
      .selectList(ContextURLHelper.buildSelectList(entitySet.getEntityType(), null, select)).build();
  assertEquals("$metadata#ESTwoKeyNav(PropertyInt16,PropertyString,"
      + "olingo.odata.test1.ETBaseTwoKeyNav/PropertyDate)", 
      ContextURLBuilder.create(contextURL).toASCIIString());
}
 
Example #30
Source File: Util.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public static EdmEntitySet getEdmEntitySet(UriInfoResource uriInfo) throws ODataApplicationException {

    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    // To get the entity set we have to interpret all URI segments
    if (!(resourcePaths.get(0) instanceof UriResourceEntitySet)) {
      // Here we should interpret the whole URI but in this example we do not support navigation so we throw an
      // exception
      throw new ODataApplicationException("Invalid resource type for first segment.", HttpStatusCode.NOT_IMPLEMENTED
          .getStatusCode(), Locale.ENGLISH);
    }

    UriResourceEntitySet uriResource = (UriResourceEntitySet) resourcePaths.get(0);

    return uriResource.getEntitySet();
  }