org.apache.olingo.server.api.serializer.EntityCollectionSerializerOptions Java Examples

The following examples show how to use org.apache.olingo.server.api.serializer.EntityCollectionSerializerOptions. 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: JsonDeltaSerializerWithNavigations.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
protected void writeEntitySet(final ServiceMetadata metadata, final EdmEntityType entityType,
    final Delta entitySet, final EntityCollectionSerializerOptions options,
    final JsonGenerator json) throws IOException,
    SerializerException {
  json.writeStartArray();
  for (final Entity entity : entitySet.getEntities()) {
    writeAddedUpdatedEntity(metadata, entityType, entity, options.getExpand(), options.getSelect(),
        options.getContextURL(), false, options.getContextURL().getEntitySetOrSingletonOrType(), json,
        options.isFullRepresentation());
  }
  for (final DeletedEntity deletedEntity : entitySet.getDeletedEntities()) {
    writeDeletedEntity(deletedEntity, json);
  }
  for (final DeltaLink addedLink : entitySet.getAddedLinks()) {
    writeLink(addedLink, options, json, true);
  }
  for (final DeltaLink deletedLink : entitySet.getDeletedLinks()) {
    writeLink(deletedLink, options, json, false);
  }
  json.writeEndArray();
}
 
Example #2
Source File: JsonDeltaSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test(expected = SerializerException.class)
public void negativeLinkDeltaTest3() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  
  List<DeltaLink> addedLinks = new ArrayList<DeltaLink>();
  DeltaLink link1 = new DeltaLink();
  link1.setRelationship("NavPropertyETAllPrimOne");
  link1.setSource(new URI("ESDelta(100)"));
  addedLinks.add(link1 );
  delta.getAddedLinks().addAll(addedLinks );
     
  ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();      
   }
 
Example #3
Source File: JsonDeltaSerializerWithNavigationsTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test(expected = SerializerException.class)
public void negativeDeltaDeletedEntityTest2() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();   
    
  List<DeletedEntity> deletedEntity = new ArrayList<DeletedEntity>();
  DeletedEntity delEntity1 = new DeletedEntity();
  delEntity1.setId(new URI("ESDelta(100)"));
 
  deletedEntity.add(delEntity1);
  delta.getDeletedEntities().addAll(deletedEntity);    
 
  ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();
    
   }
 
Example #4
Source File: JsonDeltaSerializerWithNavigationsTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void deletedDeltaLink() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  List<DeltaLink> deletedLinks = new ArrayList<DeltaLink>();
  DeltaLink link1 = new DeltaLink();
  link1.setRelationship("NavPropertyETAllPrimOne");
  link1.setSource(new URI("ESDelta(100)"));
  link1.setTarget(new URI("ESAllPrim(0)"));
  deletedLinks.add(link1 );
  delta.getDeletedLinks().addAll(deletedLinks);
   InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();
     String jsonString = IOUtils.toString(stream);
     final String expectedResult = "{"
         + "\"@context\":\"$metadata#ESDelta/$delta\",\"value\":[{"
         + "\"@context\":\"#ESDelta/$deletedLink\",\"source\":\"ESDelta(100)\","
         + "\"relationship\":\"NavPropertyETAllPrimOne\","
         + "\"target\":\"ESAllPrim(0)\"}]"          
         + "}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example #5
Source File: JsonDeltaSerializerWithNavigationsTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void addedDeltaLink() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  List<DeltaLink> addedLinks = new ArrayList<DeltaLink>();
  DeltaLink link1 = new DeltaLink();
  link1.setRelationship("NavPropertyETAllPrimOne");
  link1.setSource(new URI("ESDelta(100)"));
  link1.setTarget(new URI("ESAllPrim(0)"));
  addedLinks.add(link1 );
  delta.getAddedLinks().addAll(addedLinks );
   InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();
     String jsonString = IOUtils.toString(stream);
     final String expectedResult = "{"
         + "\"@context\":\"$metadata#ESDelta/$delta\",\"value\":[{"
         + "\"@context\":\"#ESDelta/$link\",\"source\":\"ESDelta(100)\",\"relationship\":\"NavPropertyETAllPrimOne\","
         + "\"target\":\"ESAllPrim(0)\"}]"          
         + "}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example #6
Source File: JsonDeltaSerializerWithNavigationsTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test(expected = SerializerException.class)
public void negativeLinkDeltaTest3() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  
  List<DeltaLink> addedLinks = new ArrayList<DeltaLink>();
  DeltaLink link1 = new DeltaLink();
  link1.setRelationship("NavPropertyETAllPrimOne");
  link1.setSource(new URI("ESDelta(100)"));
  addedLinks.add(link1 );
  delta.getAddedLinks().addAll(addedLinks );
     
  ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();      
   }
 
Example #7
Source File: JsonDeltaSerializerWithNavigationsTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void emptynavigationToManyInDeltaEntityInFullRepresentation() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  final Entity entity = data.readAll(edmEntitySet).getEntities().get(2);
  final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList(
      ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETAllPrimMany")));
  List<Entity> addedEntity = new ArrayList<Entity>();
  addedEntity.add(entity);
  delta.getEntities().addAll(addedEntity);
   InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build()).expand(expand)
      .isFullRepresentation(true).build()).getContent();
     String jsonString = IOUtils.toString(stream);
     final String expectedResult = "{\"@context\":\"$metadata#ESDelta/$delta\","
         + "\"value\":[{\"@id\":\"ESDelta(0)\",\"PropertyInt16\":0,"
         + "\"PropertyString\":\"Number:0\",\"NavPropertyETAllPrimMany\":[]}]}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example #8
Source File: JsonDeltaSerializerWithNavigationsTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test(expected = SerializerException.class)
public void negativeDeltaEntityTest() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  
  final Entity entity2 = data.readAll(edmEntitySet).getEntities().get(1);
  List<Entity> addedEntity = new ArrayList<Entity>();
  Entity changedEntity = new Entity();
  changedEntity.addProperty(entity2.getProperty("PropertyString"));
  addedEntity.add(changedEntity);
  delta.getEntities().addAll(addedEntity);
   ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();
    
   }
 
Example #9
Source File: JsonDeltaSerializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
protected void writeEntitySet(final ServiceMetadata metadata, final EdmEntityType entityType,
    final Delta entitySet, final EntityCollectionSerializerOptions options,
    final JsonGenerator json) throws IOException,
    SerializerException {
  json.writeStartArray();
  for (final Entity entity : entitySet.getEntities()) {
    writeAddedUpdatedEntity(metadata, entityType, entity, options.getExpand(),
        options.getSelect(), options.getContextURL(), false, options.getContextURL()
            .getEntitySetOrSingletonOrType(), json);
  }
  for (final DeletedEntity deletedEntity : entitySet.getDeletedEntities()) {
    writeDeletedEntity(deletedEntity, json);
  }
  for (final DeltaLink addedLink : entitySet.getAddedLinks()) {
    writeLink(addedLink, options, json, true);
  }
  for (final DeltaLink deletedLink : entitySet.getDeletedLinks()) {
    writeLink(deletedLink, options, json, false);
  }
  json.writeEndArray();
}
 
Example #10
Source File: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void entitySetMedia() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMedia");
  final EntityCollection entitySet = data.readAll(edmEntitySet);
  final String resultString = IOUtils.toString(serializer.entityCollection(metadata,
      edmEntitySet.getEntityType(), entitySet,
      EntityCollectionSerializerOptions.with()
          .contextURL(ContextURL.with().entitySet(edmEntitySet).build()).build()).getContent());
  final String expectedResult = "{\"@odata.context\":\"$metadata#ESMedia\","
      + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":["
      + "{\"@odata.mediaEtag\":\"W/\\\"1\\\"\",\"@odata.mediaContentType\":\"image/svg+xml\","
      + "\"@odata.mediaEditLink\":\"ESMedia(1)/$value\",\"PropertyInt16\":1},"
      + "{\"@odata.mediaEtag\":\"W/\\\"2\\\"\",\"@odata.mediaContentType\":\"image/svg+xml\","
      + "\"@odata.mediaEditLink\":\"ESMedia(2)/$value\",\"PropertyInt16\":2},"
      + "{\"@odata.mediaEtag\":\"W/\\\"3\\\"\",\"@odata.mediaContentType\":\"image/svg+xml\","
      + "\"@odata.mediaEditLink\":\"ESMedia(3)/$value\",\"PropertyInt16\":3},"
      + "{\"@odata.mediaEtag\":\"W/\\\"4\\\"\",\"@odata.mediaContentType\":\"image/svg+xml\","
      + "\"@odata.mediaEditLink\":\"ESMedia(4)/$value\",\"PropertyInt16\":4}]}";
  Assert.assertEquals(expectedResult, resultString);
}
 
Example #11
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void entityWithStreamMetadataMinimal() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESWithStream");
  final EntityCollection collection = data.readAll(edmEntitySet);
  InputStream result = serializer.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\":[{\"PropertyInt16\":32767},"
      + "{\"PropertyInt16\":7,\"PropertyStream@mediaEtag\":\"eTag\","
      + "\"PropertyStream@mediaContentType\":\"image/jpeg\"}]}";
  Assert.assertEquals(expectedResult, resultString);
}
 
Example #12
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void entityWithStreamExpand() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESWithStream");
  final EntityCollection collection = data.readAll(edmEntitySet);
  final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList(
      ExpandSelectMock.mockExpandItem(edmEntitySet, "PropertyStream")));
  InputStream result = serializer.entityCollection(metadata, edmEntitySet.getEntityType(), collection,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .expand(expand).build()).getContent();
  final String resultString = IOUtils.toString(result);
  final String expectedResult =  "{\"@context\":\"$metadata#ESWithStream\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":[{\"PropertyInt16\":32767,\"PropertyStream\":\"�ioz�\\\"�\"},"
      + "{\"PropertyInt16\":7,\"PropertyStream@mediaEtag\":\"eTag\","
      + "\"PropertyStream@mediaContentType\":\"image/jpeg\",\"PropertyStream\":\"�ioz�\\\"�\"}]}";
  Assert.assertEquals(expectedResult, resultString);
}
 
Example #13
Source File: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void entityWithStreamMetadataMinimal() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESWithStream");
  final EntityCollection collection = data.readAll(edmEntitySet);
  InputStream result = serializer.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\":[{\"PropertyInt16\":32767},"
      + "{\"PropertyInt16\":7,\"[email protected]\":\"eTag\","
      + "\"[email protected]\":\"image/jpeg\"}]}";
  Assert.assertEquals(expectedResult, resultString);
}
 
Example #14
Source File: ODataJsonSerializerv01Test.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 #15
Source File: JsonDeltaSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeltaForStream() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESWithStream");
  Delta delta = new Delta();
  List<DeltaLink> addedLinks = new ArrayList<DeltaLink>();
  DeltaLink link1 = new DeltaLink();
  link1.setRelationship("NavPropertyETAllPrimOne");
  link1.setSource(new URI("ESDelta(100)"));
  link1.setTarget(new URI("ESAllPrim(0)"));
  addedLinks.add(link1 );
  delta.getAddedLinks().addAll(addedLinks );
   InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();
     String jsonString = IOUtils.toString(stream);
     final String expectedResult = "{"
         + "\"@odata.context\":\"$metadata#ESWithStream/$delta\","
         + "\"value\":[{\"@odata.context\":\"#ESWithStream/$link\","
         + "\"source\":\"ESDelta(100)\",\"relationship\":\"NavPropertyETAllPrimOne\","
         + "\"target\":\"ESAllPrim(0)\"}]"      
         + "}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example #16
Source File: JsonDeltaSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test(expected = SerializerException.class)
public void negativeLinkDeltaTest1() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  
  List<DeltaLink> addedLinks = new ArrayList<DeltaLink>();
  DeltaLink link1 = new DeltaLink();
  link1.setSource(new URI("ESDelta(100)"));
  link1.setTarget(new URI("ESAllPrim(0)"));
  addedLinks.add(link1 );
  delta.getAddedLinks().addAll(addedLinks );
     
  ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();      
   }
 
Example #17
Source File: JsonDeltaSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test(expected = SerializerException.class)
public void negativeDeltaEntityTest() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  
  final Entity entity2 = data.readAll(edmEntitySet).getEntities().get(1);
  List<Entity> addedEntity = new ArrayList<Entity>();
  Entity changedEntity = new Entity();
  changedEntity.addProperty(entity2.getProperty("PropertyString"));
  addedEntity.add(changedEntity);
  delta.getEntities().addAll(addedEntity);
   ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();
    
   }
 
Example #18
Source File: JsonDeltaSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void addedDifferentdDeltaEntity() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EdmEntitySet edmEntitySet2 = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  final Entity entity = data.readAll(edmEntitySet).getEntities().get(0);
  List<Entity> addedEntity = new ArrayList<Entity>();
  addedEntity.add(entity);
  delta.getEntities().addAll(addedEntity);
   InputStream stream = ser.entityCollection(metadata, edmEntitySet2.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet2).build())
      .build()).getContent();
     String jsonString = IOUtils.toString(stream);
     final String expectedResult = "{"
       +"\"@odata.context\":\"$metadata#ESDelta/$delta\",\"value\":[{"
       + "\"@odata.context\":\"#ESAllPrim/$entity\",\"@odata.id\":\"ESAllPrim(32767)\","
       + "\"PropertyInt16\":32767,\"PropertyString\":\"First Resource - positive values\"}]"
       + "}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example #19
Source File: JsonDeltaSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void addedDeltaLink() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  List<DeltaLink> addedLinks = new ArrayList<DeltaLink>();
  DeltaLink link1 = new DeltaLink();
  link1.setRelationship("NavPropertyETAllPrimOne");
  link1.setSource(new URI("ESDelta(100)"));
  link1.setTarget(new URI("ESAllPrim(0)"));
  addedLinks.add(link1 );
  delta.getAddedLinks().addAll(addedLinks );
   InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();
     String jsonString = IOUtils.toString(stream);
     final String expectedResult = "{"
         + "\"@odata.context\":\"$metadata#ESDelta/$delta\",\"value\":[{"
         + "\"@odata.context\":\"#ESDelta/$link\",\"source\":\"ESDelta(100)\","
         + "\"relationship\":\"NavPropertyETAllPrimOne\","
         + "\"target\":\"ESAllPrim(0)\"}]"          
         + "}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example #20
Source File: DemoEntityCollectionProcessor.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void readEntityCollection(ODataRequest request, ODataResponse response, 
        UriInfo uriInfo, ContentType responseFormat) throws ODataApplicationException, SerializerException {

    // 1st we have retrieve the requested EntitySet from the uriInfo object 
    // (representation of the parsed service URI)
    List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
    // In our example, the first segment is the EntitySet
    UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); 
    EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

    // 2nd: fetch the data from backend for this requested EntitySetName 
    // it has to be delivered as EntitySet object
    EntityCollection entitySet = getData(edmEntitySet);

    // 3rd: create a serializer based on the requested format (json)
    ODataSerializer serializer = odata.createSerializer(responseFormat);
    
    // 4th: Now serialize the content: transform from the EntitySet object to InputStream
    EdmEntityType edmEntityType = edmEntitySet.getEntityType();
    ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

    final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
    EntityCollectionSerializerOptions opts =
            EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
    SerializerResult serializedContent = serializer.entityCollection(serviceMetadata, 
        edmEntityType, entitySet, opts);

    // Finally: configure the response object: set the body, headers and status code
    response.setContent(serializedContent.getContent());
    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
    response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
}
 
Example #21
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 #22
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void selectComplex() 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, "PropertyInt16"),
      ExpandSelectMock.mockSelectItem(edmEntitySet,"PropertyCompComp", "PropertyComp", "PropertyString")));
  InputStream result = serializer
      .entityCollection(metadata, entityType, entitySet,
          EntityCollectionSerializerOptions.with()
              .contextURL(ContextURL.with().entitySet(edmEntitySet)
                  .selectList(helper.buildContextURLSelectList(entityType, null, select))
                  .build())
              .select(select)
              .build()).getContent();
  final String resultString = IOUtils.toString(result);
  final String expected = "{"
      +     "\"@context\":\"$metadata#ESFourKeyAlias"
      +        "(PropertyInt16,PropertyCompComp/PropertyComp/PropertyString)\"," 
      +     "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      +     "\"value\":[" 
      +     "{" 
      +         "\"@id\":\""
      +         "ESFourKeyAlias(PropertyInt16=1,KeyAlias1=11,KeyAlias2='Num11',KeyAlias3='Num111')\"," 
      +         "\"PropertyInt16\":1," 
      +         "\"PropertyCompComp\":{" 
      +             "\"PropertyComp\":{" 
      +             "\"@type\":\"#olingo.odata.test1.CTBase\"," 
      +             "\"PropertyString\":\"Num111\"" 
      +     "}}}]}";

 Assert.assertEquals(expected, resultString);
}
 
Example #23
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 #24
Source File: JsonDeltaSerializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testDeltaToken() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  List<DeltaLink> addedLinks = new ArrayList<DeltaLink>();
  DeltaLink link1 = new DeltaLink();
  link1.setRelationship("NavPropertyETAllPrimOne");
  link1.setSource(new URI("ESDelta(100)"));
  link1.setTarget(new URI("ESAllPrim(0)"));
  addedLinks.add(link1 );
  delta.getAddedLinks().addAll(addedLinks );
  delta.setDeltaLink(new URI("23042017"));
   InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();
     String jsonString = IOUtils.toString(stream);
     final String expectedResult = "{"
         + "\"@odata.context\":\"$metadata#ESDelta/$delta\",\"value\":[{"
         + "\"@odata.context\":\"#ESDelta/$link\",\"source\":\"ESDelta(100)\","
         + "\"relationship\":\"NavPropertyETAllPrimOne\","
         + "\"target\":\"ESAllPrim(0)\"}],"          
         + "\"@odata.deltaLink\":\"23042017\""
         + "}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example #25
Source File: JsonDeltaSerializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void addedChangedDeltaEntity() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  final Entity entity = data.readAll(edmEntitySet).getEntities().get(0);
  final Entity entity2 = data.readAll(edmEntitySet).getEntities().get(1);
  List<Entity> addedEntity = new ArrayList<Entity>();
  Entity changedEntity = new Entity();
  changedEntity.setId(entity2.getId());
  changedEntity.addProperty(entity2.getProperty("PropertyString"));
  addedEntity.add(entity);
  addedEntity.add(changedEntity);
  delta.getEntities().addAll(addedEntity);
   InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build())
      .build()).getContent();
     String jsonString = IOUtils.toString(stream);
     final String expectedResult = "{"
         + "\"@odata.context\":\"$metadata#ESDelta/$delta\",\"value\":[{"
         + "\"@odata.id\":\"ESDelta(32767)\",\"PropertyInt16\":32767,"
         + "\"PropertyString\":\"Number:32767\"},{\"@odata.id\":\"ESDelta(-32768)\","
         + "\"PropertyString\":\"Number:-32768\"}]"
         + "}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example #26
Source File: EntitySetResponse.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private EntitySetResponse(ServiceMetadata metadata, ODataResponse response, ODataSerializer serializer,
    EntityCollectionSerializerOptions options,
    ContentType responseContentType, Map<String, String> preferences) {
  super(metadata, response, preferences);
  this.serializer = serializer;
  this.options = options;
  this.responseContentType = responseContentType;
}
 
Example #27
Source File: JsonDeltaSerializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void selectInDelta() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  final EdmEntityType entityType = edmEntitySet.getEntityType();
  final UriHelper helper = odata.createUriHelper();
  final SelectOption select = ExpandSelectMock.mockSelectOption(Collections.singletonList(
      ExpandSelectMock.mockSelectItem(entityContainer.getEntitySet("ESAllPrim"), "PropertyString")));
  
  final Entity entity = data.readAll(edmEntitySet).getEntities().get(0);
  final Entity entity2 = data.readAll(edmEntitySet).getEntities().get(1);
     
     Delta delta = new Delta();
     List<Entity> addedEntity = new ArrayList<Entity>();
     Entity changedEntity = new Entity();
     changedEntity.setId(entity2.getId());
     changedEntity.addProperty(entity2.getProperty("PropertyString"));
     changedEntity.addProperty(entity2.getProperty("PropertyInt16"));
     addedEntity.add(entity);
     addedEntity.add(changedEntity);
     delta.getEntities().addAll(addedEntity);
     InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
         EntityCollectionSerializerOptions.with()
         .contextURL(ContextURL.with().entitySet(edmEntitySet)
             .selectList(helper.buildContextURLSelectList(entityType, null, select))
             .suffix(Suffix.ENTITY).build())
         .select(select).build()).getContent();
        String jsonString = IOUtils.toString(stream);
  Assert.assertEquals("{"
      +"\"@odata.context\":\"$metadata#ESDelta(PropertyInt16,PropertyString)/$entity/$delta\","
      + "\"value\":[{\"@odata.id\":\"ESDelta(32767)\",\"PropertyString\":\"Number:32767\"},"
      + "{\"@odata.id\":\"ESDelta(-32768)\",\"PropertyString\":\"Number:-32768\"}]}",
      jsonString);
}
 
Example #28
Source File: JsonDeltaSerializerWithNavigationsTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void navigationEntityInDeltaEntityWithFullRepresentation() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  final Entity entity = data.readAll(edmEntitySet).getEntities().get(0);
  final Entity entity2 = data.readAll(edmEntitySet).getEntities().get(3);
  final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList(
      ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETAllPrimOne")));
  List<Entity> addedEntity = new ArrayList<Entity>();
  Entity changedEntity = new Entity();
  changedEntity.setId(entity.getId());
  changedEntity.addProperty(entity.getProperty("PropertyString"));
  addedEntity.add(entity);
  addedEntity.add(entity2);
  delta.getEntities().addAll(addedEntity);
   InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build()).expand(expand)
      .isFullRepresentation(true)
      .build()).getContent();
     String jsonString = IOUtils.toString(stream);
     final String expectedResult = "{"
         + "\"@context\":\"$metadata#ESDelta/$delta\",\"value\":[{\"@id\":\"ESDelta(32767)\","
         + "\"PropertyInt16\":32767,\"PropertyString\":\"Number:32767\",\"NavPropertyETAllPrimOne\":null},"
         + "{\"@id\":\"ESDelta(100)\","
         + "\"PropertyInt16\":100,\"PropertyString\":\"Number:100\","
         + "\"NavPropertyETAllPrimOne\":"
         + "{\"@id\":\"ESAllPrim(32767)\",\"PropertyInt16\":32767,\"PropertyString\":"
         + "\"First Resource - positive values\","
         + "\"PropertyBoolean\":true,\"PropertyByte\":255,\"PropertySByte\":127,\"PropertyInt32\":2147483647,"
         + "\"PropertyInt64\":9223372036854775807,\"PropertySingle\":1.79E20,\"PropertyDouble\":-1.79E19,"
         + "\"PropertyDecimal\":34,\"PropertyBinary\":\"ASNFZ4mrze8=\",\"PropertyDate\":\"2012-12-03\","
         + "\"PropertyDateTimeOffset\":\"2012-12-03T07:16:23Z\",\"PropertyDuration\":\"PT6S\",\"PropertyGuid\":"
         + "\"01234567-89ab-cdef-0123-456789abcdef\",\"PropertyTimeOfDay\":\"03:26:05\"}}]"
         + "}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example #29
Source File: JsonDeltaSerializerWithNavigationsTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void navigationEntityInDeltaEntity() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESDelta");
  Delta delta = new Delta();
  final Entity entity = data.readAll(edmEntitySet).getEntities().get(0);
  final Entity entity2 = data.readAll(edmEntitySet).getEntities().get(3);
  final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList(
      ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETAllPrimOne")));
  List<Entity> addedEntity = new ArrayList<Entity>();
  Entity changedEntity = new Entity();
  changedEntity.setId(entity.getId());
  changedEntity.addProperty(entity.getProperty("PropertyString"));
  addedEntity.add(entity);
  addedEntity.add(entity2);
  delta.getEntities().addAll(addedEntity);
   InputStream stream = ser.entityCollection(metadata, edmEntitySet.getEntityType(), delta ,
      EntityCollectionSerializerOptions.with()
      .contextURL(ContextURL.with().entitySet(edmEntitySet).build()).expand(expand)
      .build()).getContent();
     String jsonString = IOUtils.toString(stream);
     final String expectedResult = "{"
         + "\"@context\":\"$metadata#ESDelta/$delta\",\"value\":[{\"@id\":\"ESDelta(32767)\","
         + "\"PropertyInt16\":32767,\"PropertyString\":\"Number:32767\",\"NavPropertyETAllPrimOne\":null},"
         + "{\"@id\":\"ESDelta(100)\","
         + "\"PropertyInt16\":100,\"PropertyString\":\"Number:100\","
         + "\"NavPropertyETAllPrimOne@delta\":"
         + "{\"@id\":\"ESAllPrim(32767)\",\"PropertyInt16\":32767,\"PropertyString\":"
         + "\"First Resource - positive values\","
         + "\"PropertyBoolean\":true,\"PropertyByte\":255,\"PropertySByte\":127,\"PropertyInt32\":2147483647,"
         + "\"PropertyInt64\":9223372036854775807,\"PropertySingle\":1.79E20,\"PropertyDouble\":-1.79E19,"
         + "\"PropertyDecimal\":34,\"PropertyBinary\":\"ASNFZ4mrze8=\",\"PropertyDate\":\"2012-12-03\","
         + "\"PropertyDateTimeOffset\":\"2012-12-03T07:16:23Z\",\"PropertyDuration\":\"PT6S\",\"PropertyGuid\":"
         + "\"01234567-89ab-cdef-0123-456789abcdef\",\"PropertyTimeOfDay\":\"03:26:05\"}}]"
         + "}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example #30
Source File: DemoEntityCollectionProcessor.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
public void readEntityCollection(ODataRequest request, ODataResponse response, UriInfo uriInfo, ContentType responseFormat) throws ODataApplicationException, SerializerException {

		// 1st retrieve the requested EntitySet from the uriInfo (representation of the parsed URI)
		List<UriResource> resourcePaths = uriInfo.getUriResourceParts();
		UriResourceEntitySet uriResourceEntitySet = (UriResourceEntitySet) resourcePaths.get(0); // in our example, the first segment is the EntitySet
		EdmEntitySet edmEntitySet = uriResourceEntitySet.getEntitySet();

		// 2nd: fetch the data from backend for this requested EntitySetName and deliver as EntitySet
		EntityCollection entityCollection = storage.readEntitySetData(edmEntitySet);

		// 3rd: create a serializer based on the requested format (json)
		ODataSerializer serializer = odata.createSerializer(responseFormat);

		// and serialize the content: transform from the EntitySet object to InputStream
		EdmEntityType edmEntityType = edmEntitySet.getEntityType();
		ContextURL contextUrl = ContextURL.with().entitySet(edmEntitySet).build();

		final String id = request.getRawBaseUri() + "/" + edmEntitySet.getName();
		EntityCollectionSerializerOptions opts =
				EntityCollectionSerializerOptions.with().id(id).contextURL(contextUrl).build();
		SerializerResult serializerResult = serializer.entityCollection(serviceMetadata, edmEntityType, entityCollection, opts);
		InputStream serializedContent = serializerResult.getContent();

		// 4th: configure the response object: set the body, headers and status code
		response.setContent(serializedContent);
		response.setStatusCode(HttpStatusCode.OK.getStatusCode());
		response.setHeader(HttpHeader.CONTENT_TYPE, responseFormat.toContentTypeString());
	}