Java Code Examples for org.apache.olingo.commons.api.data.Entity#addProperty()

The following examples show how to use org.apache.olingo.commons.api.data.Entity#addProperty() . 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: JSONTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * @return
 */
private Entity createEntity() {
  Entity en = new Entity();
  Property p1 = new Property();
  p1.setName("Id");
  p1.setType("Int64");
  p1.setValue(ValueType.PRIMITIVE, Long.valueOf(123));
  en.addProperty(p1);
  
  Property p2 = new Property();
  p2.setName("Name");
  p2.setType("String");
  p2.setValue(ValueType.PRIMITIVE, "ABC");
  en.addProperty(p2);
  return en;
}
 
Example 2
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void nullComplexValueButInDataMapAndNullCollectionsNotInDataMap() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixEnumDefCollComp");
  Entity entity = new Entity();
  entity.setId(URI.create("id"));
  entity.addProperty(new Property(null, "PropertyEnumString", ValueType.ENUM, 6));
  entity.addProperty(new Property(null, "PropertyDefString", ValueType.PRIMITIVE, "Test"));
  entity.addProperty(new Property(null, "PropertyCompMixedEnumDef", ValueType.COMPLEX, null));
  final String resultString = IOUtils.toString(serializer.entity(metadata, edmEntitySet.getEntityType(), entity,
      EntitySerializerOptions.with()
          .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build())
          .build()).getContent());
  Assert.assertEquals("{\"@context\":\"$metadata#ESMixEnumDefCollComp/$entity\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"PropertyEnumString\":\"String2,String3\","
      + "\"CollPropertyEnumString\":[],"
      + "\"PropertyDefString\":\"Test\","
      + "\"CollPropertyDefString\":[],"
      + "\"PropertyCompMixedEnumDef\":null,"
      + "\"CollPropertyCompMixedEnumDef\":[]}",
      resultString);
}
 
Example 3
Source File: ODataJsonDeserializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void consumeEntityProperties(final EdmEntityType edmEntityType, final ObjectNode node,
    final Entity entity) throws DeserializerException {
  List<String> propertyNames = edmEntityType.getPropertyNames();
  for (String propertyName : propertyNames) {
    JsonNode jsonNode = node.get(propertyName);
    if (jsonNode != null) {
      EdmProperty edmProperty = (EdmProperty) edmEntityType.getProperty(propertyName);
      if (jsonNode.isNull() && !edmProperty.isNullable()) {
        throw new DeserializerException("Property: " + propertyName + " must not be null.",
            DeserializerException.MessageKeys.INVALID_NULL_PROPERTY, propertyName);
      }
      Property property = consumePropertyNode(edmProperty.getName(), edmProperty.getType(),
          edmProperty.isCollection(), edmProperty.isNullable(), edmProperty.getMaxLength(),
          edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(), edmProperty.getMapping(),
          jsonNode);
      entity.addProperty(property);
      node.remove(propertyName);
    }
  }
}
 
Example 4
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 5
Source File: EdmAssistedJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void expand() throws Exception {
  final Entity relatedEntity1 = new Entity().addProperty(new Property(null, "Related1", ValueType.PRIMITIVE, 1.5));
  final Entity relatedEntity2 = new Entity().addProperty(new Property(null, "Related1", ValueType.PRIMITIVE, 2.75));
  EntityCollection target = new EntityCollection();
  target.getEntities().add(relatedEntity1);
  target.getEntities().add(relatedEntity2);
  Link link = new Link();
  link.setTitle("NavigationProperty");
  link.setInlineEntitySet(target);
  Entity entity = new Entity();
  entity.setId(null);
  entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, (short) 1));
  entity.getNavigationLinks().add(link);
  EntityCollection entityCollection = new EntityCollection();
  entityCollection.getEntities().add(entity);
  Assert.assertEquals("{\"@odata.context\":\"$metadata#EntitySet(Property1,NavigationProperty(Related1))\","
      + "\"value\":[{\"@odata.id\":null,"
      + "\"[email protected]\":\"#Int16\",\"Property1\":1,"
      + "\"NavigationProperty\":["
      + "{\"@odata.id\":null,\"[email protected]\":\"#Double\",\"Related1\":1.5},"
      + "{\"@odata.id\":null,\"[email protected]\":\"#Double\",\"Related1\":2.75}]}]}",
      serialize(serializer, metadata, null, entityCollection, "Property1,NavigationProperty(Related1)"));
}
 
Example 6
Source File: EdmAssistedJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void entityCollectionWithComplexPropertyMetadataMin() throws Exception {
  Entity entity = new Entity();
  entity.setId(null);
  entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, 1L));
  ComplexValue complexValue = new ComplexValue();
  complexValue.getValue().add(new Property(null, "Inner1", ValueType.PRIMITIVE,
      BigDecimal.TEN.scaleByPowerOfTen(-5)));
  Calendar time = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
  time.clear();
  time.set(Calendar.HOUR_OF_DAY, 13);
  time.set(Calendar.SECOND, 59);
  time.set(Calendar.MILLISECOND, 999);
  complexValue.getValue().add(new Property("Edm.TimeOfDay", "Inner2", ValueType.PRIMITIVE, time));
  entity.addProperty(new Property("Namespace.ComplexType", "Property2", ValueType.COMPLEX, complexValue));
  EntityCollection entityCollection = new EntityCollection();
  entityCollection.getEntities().add(entity);
  Assert.assertEquals("{\"@odata.context\":\"$metadata#EntitySet(Property1,Property2)\","
      + "\"value\":[{"
      + "\"Property1\":1,"
      + "\"Property2\":{"
      + "\"Inner1\":0.00010,"
      + "\"Inner2\":\"13:00:59.999\"}}]}",
      serialize(serializerMin, metadata, null, entityCollection, null));
}
 
Example 7
Source File: EdmAssistedJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void expandMetadataMin() throws Exception {
  final Entity relatedEntity1 = new Entity().addProperty(new Property(null, "Related1", ValueType.PRIMITIVE, 1.5));
  final Entity relatedEntity2 = new Entity().addProperty(new Property(null, "Related1", ValueType.PRIMITIVE, 2.75));
  EntityCollection target = new EntityCollection();
  target.getEntities().add(relatedEntity1);
  target.getEntities().add(relatedEntity2);
  Link link = new Link();
  link.setTitle("NavigationProperty");
  link.setInlineEntitySet(target);
  Entity entity = new Entity();
  entity.setId(null);
  entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, (short) 1));
  entity.getNavigationLinks().add(link);
  EntityCollection entityCollection = new EntityCollection();
  entityCollection.getEntities().add(entity);
  Assert.assertEquals("{\"@odata.context\":\"$metadata#EntitySet(Property1,NavigationProperty(Related1))\","
      + "\"value\":[{"
      + "\"Property1\":1,"
      + "\"NavigationProperty\":["
      + "{\"Related1\":1.5},"
      + "{\"Related1\":2.75}]}]}",
      serialize(serializerMin, metadata, null, entityCollection, "Property1,NavigationProperty(Related1)"));
}
 
Example 8
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private List<Entity> cloneEntityCollection(final List<Entity> entities) {
  final List<Entity> clonedEntities = new ArrayList<Entity>();
  
  for(final Entity entity : entities) {
    final Entity clonedEntity = new Entity();
    
    clonedEntity.setId(entity.getId());
    for(final Property property : entity.getProperties()) {
      clonedEntity.addProperty(new Property(property.getType(), 
                                            property.getName(), 
                                            property.getValueType(), 
                                            property.getValue()));
    }
     
    clonedEntities.add(clonedEntity);
  }
  
  return clonedEntities;
}
 
Example 9
Source File: EdmAssistedJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void metadataMin() throws Exception {
  final ServiceMetadata metadata = oData.createServiceMetadata(null, Collections.<EdmxReference> emptyList(),
      new MetadataETagSupport("W/\"42\""));
  Entity entity = new Entity();
  entity.setType("Namespace.EntityType");
  entity.setId(URI.create("ID"));
  entity.setETag("W/\"1000\"");
  Link link = new Link();
  link.setHref("editLink");
  entity.setEditLink(link);
  entity.setMediaContentSource(URI.create("media"));
  entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE,
      UUID.fromString("12345678-ABCD-1234-CDEF-123456789012")));
  EntityCollection entityCollection = new EntityCollection();
  entityCollection.getEntities().add(entity);
  Assert.assertEquals("{\"@odata.context\":\"$metadata#EntitySet(Property1)\","
      + "\"@odata.metadataEtag\":\"W/\\\"42\\\"\",\"value\":[{"
      + "\"@odata.etag\":\"W/\\\"1000\\\"\","
      + "\"Property1\":\"12345678-abcd-1234-cdef-123456789012\","
      + "\"@odata.editLink\":\"editLink\","
      + "\"@odata.mediaReadLink\":\"editLink/$value\"}]}",
      serialize(serializerMin, metadata, null, entityCollection, null));
}
 
Example 10
Source File: JsonDeltaSerializerWithNavigationsTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testIdAnnotationWithNavigationOneInDelta() 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);
  final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList(
      ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETBaseContTwoContOne")));
  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()).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\","
         + "\"NavPropertyETBaseContTwoContOne@delta\":"
         + "{\"@id\":\"ESDelta(32767)/NavPropertyETBaseContTwoContOne"
         + "(PropertyInt16=111,PropertyString='TEST%20A')\",\"PropertyInt16\":111,"
         + "\"PropertyString\":\"TEST A\"}},{\"@id\":\"ESDelta(-32768)\","
         + "\"PropertyString\":\"Number:-32768\",\"NavPropertyETBaseContTwoContOne\":null}]}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example 11
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void enumAndTypeDefinition() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixEnumDefCollComp");
  Entity entity = new Entity();
  entity.setId(URI.create("id"));
  entity.addProperty(new Property(null, "PropertyEnumString", ValueType.ENUM, 6));
  entity.addProperty(new Property(null, "CollPropertyEnumString", ValueType.COLLECTION_ENUM,
      Arrays.asList(2, 4, 6)));
  entity.addProperty(new Property(null, "PropertyDefString", ValueType.PRIMITIVE, "Test"));
  entity.addProperty(new Property(null, "CollPropertyDefString", ValueType.COLLECTION_PRIMITIVE,
      Arrays.asList("Test1", "Test2")));
  ComplexValue complexValue = new ComplexValue();
  complexValue.getValue().add(entity.getProperty("PropertyEnumString"));
  complexValue.getValue().add(entity.getProperty("CollPropertyEnumString"));
  complexValue.getValue().add(entity.getProperty("PropertyDefString"));
  complexValue.getValue().add(entity.getProperty("CollPropertyDefString"));
  entity.addProperty(new Property(null, "PropertyCompMixedEnumDef", ValueType.COMPLEX, complexValue));
  entity.addProperty(new Property(null, "CollPropertyCompMixedEnumDef", ValueType.COLLECTION_COMPLEX,
      Collections.singletonList(complexValue)));
  final String resultString = IOUtils.toString(serializer.entity(metadata, edmEntitySet.getEntityType(), entity,
      EntitySerializerOptions.with()
          .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build())
          .build()).getContent());
  Assert.assertEquals("{\"@context\":\"$metadata#ESMixEnumDefCollComp/$entity\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"PropertyEnumString\":\"String2,String3\","
      + "\"CollPropertyEnumString\":[\"String2\",\"String3\",\"String2,String3\"],"
      + "\"PropertyDefString\":\"Test\","
      + "\"CollPropertyDefString\":[\"Test1\",\"Test2\"],"
      + "\"PropertyCompMixedEnumDef\":{\"PropertyEnumString\":\"String2,String3\","
      + "\"CollPropertyEnumString\":[\"String2\",\"String3\",\"String2,String3\"],"
      + "\"PropertyDefString\":\"Test\",\"CollPropertyDefString\":[\"Test1\",\"Test2\"]},"
      + "\"CollPropertyCompMixedEnumDef\":[{\"PropertyEnumString\":\"String2,String3\","
      + "\"CollPropertyEnumString\":[\"String2\",\"String3\",\"String2,String3\"],"
      + "\"PropertyDefString\":\"Test\",\"CollPropertyDefString\":[\"Test1\",\"Test2\"]}]}",
      resultString);
}
 
Example 12
Source File: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void enumAndTypeDefinition() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixEnumDefCollComp");
  Entity entity = new Entity();
  entity.setId(URI.create("id"));
  entity.addProperty(new Property(null, "PropertyEnumString", ValueType.ENUM, 6));
  entity.addProperty(new Property(null, "CollPropertyEnumString", ValueType.COLLECTION_ENUM,
      Arrays.asList(2, 4, 6)));
  entity.addProperty(new Property(null, "PropertyDefString", ValueType.PRIMITIVE, "Test"));
  entity.addProperty(new Property(null, "CollPropertyDefString", ValueType.COLLECTION_PRIMITIVE,
      Arrays.asList("Test1", "Test2")));
  ComplexValue complexValue = new ComplexValue();
  complexValue.getValue().add(entity.getProperty("PropertyEnumString"));
  complexValue.getValue().add(entity.getProperty("CollPropertyEnumString"));
  complexValue.getValue().add(entity.getProperty("PropertyDefString"));
  complexValue.getValue().add(entity.getProperty("CollPropertyDefString"));
  entity.addProperty(new Property(null, "PropertyCompMixedEnumDef", ValueType.COMPLEX, complexValue));
  entity.addProperty(new Property(null, "CollPropertyCompMixedEnumDef", ValueType.COLLECTION_COMPLEX,
      Collections.singletonList(complexValue)));
  final String resultString = IOUtils.toString(serializer.entity(metadata, edmEntitySet.getEntityType(), entity,
      EntitySerializerOptions.with()
          .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build())
          .build()).getContent());
  Assert.assertEquals("{\"@odata.context\":\"$metadata#ESMixEnumDefCollComp/$entity\","
      + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"PropertyEnumString\":\"String2,String3\","
      + "\"CollPropertyEnumString\":[\"String2\",\"String3\",\"String2,String3\"],"
      + "\"PropertyDefString\":\"Test\","
      + "\"CollPropertyDefString\":[\"Test1\",\"Test2\"],"
      + "\"PropertyCompMixedEnumDef\":{\"PropertyEnumString\":\"String2,String3\","
      + "\"CollPropertyEnumString\":[\"String2\",\"String3\",\"String2,String3\"],"
      + "\"PropertyDefString\":\"Test\",\"CollPropertyDefString\":[\"Test1\",\"Test2\"]},"
      + "\"CollPropertyCompMixedEnumDef\":[{\"PropertyEnumString\":\"String2,String3\","
      + "\"CollPropertyEnumString\":[\"String2\",\"String3\",\"String2,String3\"],"
      + "\"PropertyDefString\":\"Test\",\"CollPropertyDefString\":[\"Test1\",\"Test2\"]}]}",
      resultString);
}
 
Example 13
Source File: JsonDeltaSerializerTest.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 = "{"         
         +  "\"@odata.context\":\"$metadata#ESDelta/$delta\",\"value\":"
         + "[{\"@odata.id\":\"ESDelta(32767)\",\"PropertyInt16\":32767,\"PropertyString\":"
         + "\"Number:32767\"},{\"@odata.id\":\"ESDelta(100)\",\"PropertyInt16\":100,"
         + "\"PropertyString\":\"Number:100\"}]"
         + "}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }
 
Example 14
Source File: JsonDeltaSerializerWithNavigationsTest.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("{"
      +"\"@context\":\"$metadata#ESDelta(PropertyInt16,PropertyString)/$entity/$delta\","
      + "\"value\":[{\"@id\":\"ESDelta(32767)\",\"PropertyString\":\"Number:32767\"},"
      + "{\"@id\":\"ESDelta(-32768)\",\"PropertyString\":\"Number:-32768\"}]}",
      jsonString);
}
 
Example 15
Source File: EdmAssistedJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void entityCollectionSimpleMetadataNone() throws Exception {
  Entity entity = new Entity();
  entity.setId(null);
  entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, 1.25F));
  EntityCollection entityCollection = new EntityCollection();
  entityCollection.getEntities().add(entity);
  Assert.assertEquals("{\"value\":[{\"Property1\":1.25}]}",
      serialize(serializerNone, metadata, null, entityCollection, null));
}
 
Example 16
Source File: EdmAssistedJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void entityCollectionSimpleMetadataMin() throws Exception {
  Entity entity = new Entity();
  entity.setId(null);
  entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, 1.25F));
  EntityCollection entityCollection = new EntityCollection();
  entityCollection.getEntities().add(entity);
  Assert.assertEquals("{\"@odata.context\":\"$metadata#EntitySet(Property1)\","
      + "\"value\":[{\"Property1\":1.25}]}",
      serialize(serializerMin, metadata, null, entityCollection, null));
}
 
Example 17
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 18
Source File: ODataXmlSerializerTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void enumAndTypeDefinition() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixEnumDefCollComp");
  Entity entity = new Entity();
  entity.setId(URI.create("id"));
  entity.addProperty(new Property(null, "PropertyEnumString", ValueType.ENUM, 6));
  entity.addProperty(new Property(null, "CollPropertyEnumString", ValueType.COLLECTION_ENUM,
      Arrays.asList(2, 4, 6)));
  entity.addProperty(new Property(null, "PropertyDefString", ValueType.PRIMITIVE, "Test"));
  entity.addProperty(new Property(null, "CollPropertyDefString", ValueType.COLLECTION_PRIMITIVE,
      Arrays.asList("Test1", "Test2")));
  ComplexValue complexValue = new ComplexValue();
  complexValue.getValue().add(entity.getProperty("PropertyEnumString"));
  complexValue.getValue().add(entity.getProperty("CollPropertyEnumString"));
  complexValue.getValue().add(entity.getProperty("PropertyDefString"));
  complexValue.getValue().add(entity.getProperty("CollPropertyDefString"));
  entity.addProperty(new Property(null, "PropertyCompMixedEnumDef", ValueType.COMPLEX, complexValue));
  entity.addProperty(new Property(null, "CollPropertyCompMixedEnumDef", ValueType.COLLECTION_COMPLEX,
      Collections.singletonList(complexValue)));
  final long currentTimeMillis = System.currentTimeMillis();
  final String resultString = IOUtils.toString(serializer.entity(metadata, edmEntitySet.getEntityType(), entity,
      EntitySerializerOptions.with()
          .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build())
          .build()).getContent());
  checkXMLEqual(resultString,
      "<?xml version='1.0' encoding='UTF-8'?>\n"
      + "<a:entry xmlns:a=\"" + Constants.NS_ATOM + "\""
      + "  xmlns:m=\"" + Constants.NS_METADATA + "\" xmlns:d=\"" + Constants.NS_DATASERVICES + "\""
      + " m:context=\"$metadata#ESMixEnumDefCollComp/$entity\" m:metadata-etag=\"metadataETag\">\n"
      + "  <a:id>id</a:id>"
      + "  <a:title /> <a:summary />\n"
      + "  <a:updated>" + UPDATED_FORMAT.format(new Date(currentTimeMillis)) + "</a:updated>\n"
      + "  <a:author> <a:name /> </a:author>\n"
      + "  <a:link rel=\"edit\" href=\"id\" />"
      + "  <a:category scheme=\"" + Constants.NS_SCHEME + "\"\n"
      + "    term=\"#olingo.odata.test1.ETMixEnumDefCollComp\" />\n"
      + "  <a:content type=\"application/xml\">\n"
      + "    <m:properties>\n"
      + "      <d:PropertyEnumString m:type=\"#olingo.odata.test1.ENString\">String2,String3"
      + "</d:PropertyEnumString>\n"
      + "      <d:CollPropertyEnumString m:type=\"#Collection(olingo.odata.test1.ENString)\">\n"
      + "        <m:element>String2</m:element>\n"
      + "        <m:element>String3</m:element>\n"
      + "        <m:element>String2,String3</m:element>\n"
      + "      </d:CollPropertyEnumString>\n"
      + "      <d:PropertyDefString m:type=\"#olingo.odata.test1.TDString\">Test</d:PropertyDefString>\n"
      + "      <d:CollPropertyDefString m:type=\"#Collection(olingo.odata.test1.TDString)\">\n"
      + "        <m:element>Test1</m:element>\n"
      + "        <m:element>Test2</m:element>\n"
      + "      </d:CollPropertyDefString>\n"
      + "      <d:PropertyCompMixedEnumDef m:type=\"#olingo.odata.test1.CTMixEnumDef\">\n"
      + "        <d:PropertyEnumString m:type=\"#olingo.odata.test1.ENString\">String2,String3"
      + "</d:PropertyEnumString>\n"
      + "        <d:CollPropertyEnumString m:type=\"#Collection(olingo.odata.test1.ENString)\">\n"
      + "          <m:element>String2</m:element>\n"
      + "          <m:element>String3</m:element>\n"
      + "          <m:element>String2,String3</m:element>\n"
      + "        </d:CollPropertyEnumString>\n"
      + "        <d:PropertyDefString m:type=\"#olingo.odata.test1.TDString\">Test</d:PropertyDefString>\n"
      + "        <d:CollPropertyDefString m:type=\"#Collection(olingo.odata.test1.TDString)\">\n"
      + "          <m:element>Test1</m:element>\n"
      + "          <m:element>Test2</m:element>\n"
      + "        </d:CollPropertyDefString>\n"
      + "      </d:PropertyCompMixedEnumDef>\n"
      + "      <d:CollPropertyCompMixedEnumDef m:type=\"#Collection(olingo.odata.test1.CTMixEnumDef)\">\n"
      + "        <m:element>\n"
      + "          <d:PropertyEnumString m:type=\"#olingo.odata.test1.ENString\">String2,String3"
      + "</d:PropertyEnumString>\n"
      + "          <d:CollPropertyEnumString m:type=\"#Collection(olingo.odata.test1.ENString)\">\n"
      + "            <m:element>String2</m:element>\n"
      + "            <m:element>String3</m:element>\n"
      + "            <m:element>String2,String3</m:element>\n"
      + "          </d:CollPropertyEnumString>\n"
      + "          <d:PropertyDefString m:type=\"#olingo.odata.test1.TDString\">Test</d:PropertyDefString>\n"
      + "          <d:CollPropertyDefString m:type=\"#Collection(olingo.odata.test1.TDString)\">\n"
      + "            <m:element>Test1</m:element>\n"
      + "            <m:element>Test2</m:element>\n"
      + "          </d:CollPropertyDefString>\n"
      + "        </m:element>\n"
      + "      </d:CollPropertyCompMixedEnumDef>\n"
      + "    </m:properties>\n"
      + "  </a:content>\n"
      + "</a:entry>");
}
 
Example 19
Source File: Storage.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
public void updateMedia(final Entity entity, final String mediaContentType, final byte[] data) {
  entity.getProperties().remove(entity.getProperty(MEDIA_PROPERTY_NAME));
  entity.addProperty(new Property(null, MEDIA_PROPERTY_NAME, ValueType.PRIMITIVE, data));
  entity.setMediaContentType(mediaContentType);
}
 
Example 20
Source File: JsonDeltaSerializerWithNavigationsTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void navigationInDeltaEntityInFullRepresentation() 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);
  final ExpandOption expand = ExpandSelectMock.mockExpandOption(Collections.singletonList(
      ExpandSelectMock.mockExpandItem(edmEntitySet, "NavPropertyETAllPrimMany")));
  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()).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\","
       + "\"NavPropertyETAllPrimMany\":["
       + "{\"@id\":\"ESAllPrim(-32768)\",\"PropertyInt16\":-32768,"
       + "\"PropertyString\":\"Second Resource - negative values\","
       + "\"PropertyBoolean\":false,\"PropertyByte\":0,\"PropertySByte\":-128,\"PropertyInt32\":-2147483648,"
       + "\"PropertyInt64\":-9223372036854775808,\"PropertySingle\":-1.79E8,\"PropertyDouble\":-179000.0,"
       + "\"PropertyDecimal\":-34,\"PropertyBinary\":\"ASNFZ4mrze8=\",\"PropertyDate\":\"2015-11-05\","
       + "\"PropertyDateTimeOffset\":\"2005-12-03T07:17:08Z\",\"PropertyDuration\":\"PT9S\","
       + "\"PropertyGuid\":\"76543201-23ab-cdef-0123-456789dddfff\",\"PropertyTimeOfDay\":\"23:49:14\"},"
       + "{\"@id\":\"ESAllPrim(0)\",\"PropertyInt16\":0,\"PropertyString\":\"\","
       + "\"PropertyBoolean\":false,\"PropertyByte\":0,\"PropertySByte\":0,\"PropertyInt32\":0,"
       + "\"PropertyInt64\":0,\"PropertySingle\":0.0,\"PropertyDouble\":0.0,"
       + "\"PropertyDecimal\":0,"
       + "\"PropertyBinary\":\"\",\"PropertyDate\":\"1970-01-01\","
       + "\"PropertyDateTimeOffset\":\"2005-12-03T00:00:00Z\","
       + "\"PropertyDuration\":\"PT0S\",\"PropertyGuid\":"
       + "\"76543201-23ab-cdef-0123-456789cccddd\","
       + "\"PropertyTimeOfDay\":\"00:01:01\"}]},{\"@id\":\"ESDelta(-32768)\","
       + "\"PropertyString\":\"Number:-32768\",\"NavPropertyETAllPrimMany\":[]}]}";
     Assert.assertNotNull(jsonString);
     Assert.assertEquals(expectedResult, jsonString);
   }