Java Code Examples for org.apache.olingo.commons.api.data.Property#asCollection()

The following examples show how to use org.apache.olingo.commons.api.data.Property#asCollection() . 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: RequestValidator.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void validatePropertyValue(final Property property, final EdmProperty edmProperty,
    final EdmBindingTarget edmBindingTarget, final List<String> path) throws DataProviderException {

  final ArrayList<String> newPath = new ArrayList<String>(path);
  newPath.add(edmProperty.getName());

  if (edmProperty.isCollection()) {
    if (edmProperty.getType() instanceof EdmComplexType && property != null) {
      for (final Object value : property.asCollection()) {
        validateComplexValue((ComplexValue) value,
            edmBindingTarget,
            (EdmComplexType) edmProperty.getType(),
            newPath);
      }
    }
  } else if (edmProperty.getType() instanceof EdmComplexType) {
    validateComplexValue((property == null) ? null : property.asComplex(),
        edmBindingTarget,
        (EdmComplexType) edmProperty.getType(),
        newPath);
  }
}
 
Example 2
Source File: DataProviderTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void esMixPrimCollComp() throws Exception {
  final DataProvider dataProvider = new DataProvider(oData, edm);
  
  EntityCollection outSet = dataProvider.readAll(esMixPrimCollComp);

  Assert.assertEquals(3, outSet.getEntities().size());
  Assert.assertEquals(4, outSet.getEntities().get(0).getProperties().size());
  Property complex = outSet.getEntities().get(0).getProperties().get(2);
  Assert.assertTrue(complex.isComplex());
  Assert.assertEquals(2, complex.asComplex().getValue().size());
  Property complexCollection = outSet.getEntities().get(0).getProperties().get(3);
  Assert.assertTrue(complexCollection.isCollection());
  List<?> linkedComplexValues = complexCollection.asCollection();
  Assert.assertEquals(3, linkedComplexValues.size());
  ComplexValue linkedComplexValue = (ComplexValue) linkedComplexValues.get(0);
  Assert.assertEquals(2, linkedComplexValue.getValue().size());
  Property lcProp = linkedComplexValue.getValue().get(0);
  Assert.assertFalse(lcProp.isCollection());
  Assert.assertEquals((short) 123, lcProp.getValue());
  //
  Assert.assertEquals(4, outSet.getEntities().get(1).getProperties().size());
  Assert.assertEquals(4, outSet.getEntities().get(2).getProperties().size());
}
 
Example 3
Source File: ODataXmlSerializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void writePrimitiveCollection(final EdmPrimitiveType type, final Property property,
    final Boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale,
    final Boolean isUnicode, final String xml10InvalidCharReplacement,
    final XMLStreamWriter writer) throws XMLStreamException, EdmPrimitiveTypeException, SerializerException {
  for (Object value : property.asCollection()) {
    writer.writeStartElement(METADATA, Constants.ELEM_ELEMENT, NS_METADATA);
    switch (property.getValueType()) {
    case COLLECTION_PRIMITIVE:
    case COLLECTION_ENUM:
      writePrimitiveValue(type, value, isNullable, maxLength, precision,
          scale, isUnicode, xml10InvalidCharReplacement, writer);
      break;
    case COLLECTION_GEOSPATIAL:
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
    default:
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
    }
    writer.writeEndElement();
  }
}
 
Example 4
Source File: JsonDeltaSerializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void writeComplexCollection(final ServiceMetadata metadata, final EdmComplexType type,
    final Property property,
    final Set<List<String>> selectedPaths, final JsonGenerator json)
    throws IOException, SerializerException {
  json.writeStartArray();
  for (Object value : property.asCollection()) {
    switch (property.getValueType()) {
    case COLLECTION_COMPLEX:
      json.writeStartObject();
      if (isODataMetadataFull) {
        json.writeStringField(Constants.JSON_TYPE, "#" +
            type.getFullQualifiedName().getFullQualifiedNameAsString());
      }
      writeComplexValue(metadata, type, ((ComplexValue) value).getValue(), selectedPaths, json);
      json.writeEndObject();
      break;
    default:
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
    }
  }
  json.writeEndArray();
}
 
Example 5
Source File: JsonDeltaSerializerWithNavigations.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void writeComplexCollection(final ServiceMetadata metadata, final EdmComplexType type,
    final Property property,
    final Set<List<String>> selectedPaths, final JsonGenerator json)
    throws IOException, SerializerException {
  json.writeStartArray();
  for (Object value : property.asCollection()) {
    switch (property.getValueType()) {
    case COLLECTION_COMPLEX:
      json.writeStartObject();
      if (isODataMetadataFull) {
        json.writeStringField(Constants.JSON_TYPE, "#" +
            type.getFullQualifiedName().getFullQualifiedNameAsString());
      }
      writeComplexValue(metadata, type, ((ComplexValue) value).getValue(), selectedPaths, json);
      json.writeEndObject();
      break;
    default:
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
    }
  }
  json.writeEndArray();
}
 
Example 6
Source File: ODataJsonSerializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void writePrimitiveCollection(final EdmPrimitiveType type, final Property property,
    final Boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale,
    final Boolean isUnicode, final JsonGenerator json)
    throws IOException, SerializerException {
  json.writeStartArray();
  for (Object value : property.asCollection()) {
    switch (property.getValueType()) {
    case COLLECTION_PRIMITIVE:
    case COLLECTION_ENUM:
    case COLLECTION_GEOSPATIAL:
      try {
        writePrimitiveValue(property.getName(), type, value, isNullable,
            maxLength, precision, scale, isUnicode, json);
      } catch (EdmPrimitiveTypeException e) {
        throw new SerializerException("Wrong value for property!", e,
            SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
            property.getName(), property.getValue().toString());
      }
      break;
    default:
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
    }
  }
  json.writeEndArray();
}
 
Example 7
Source File: DataProvider.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void updateProperty(final EdmProperty edmProperty, Property property, final Property newProperty,
    final boolean patch) throws DataProviderException {
  if(property == null){
    throw new DataProviderException("Cannot update type of the entity",
        HttpStatusCode.BAD_REQUEST);
  }
  final EdmType type = edmProperty.getType();
  if (edmProperty.isCollection()) {
    // Updating collection properties means replacing all entries with the given ones.
    property.asCollection().clear();

    if (newProperty != null) {
      if (type.getKind() == EdmTypeKind.COMPLEX) {
        // Create each complex value.
        for (final ComplexValue complexValue : (List<ComplexValue>) newProperty.asCollection()) {
          ((List<ComplexValue>) property.asCollection()).add(createComplexValue(edmProperty, complexValue, patch));
        }
      } else {
        // Primitive type
        ((List<Object>) property.asCollection()).addAll(newProperty.asCollection());
      }
    }
  } else if (type.getKind() == EdmTypeKind.COMPLEX) {
    for (final String propertyName : ((EdmComplexType) type).getPropertyNames()) {
      final List<Property> newProperties = newProperty == null || newProperty.asComplex() == null ? null :
          newProperty.asComplex().getValue();
      updateProperty(((EdmComplexType) type).getStructuralProperty(propertyName),
          findProperty(propertyName, property.asComplex().getValue()),
          newProperties == null ? null : findProperty(propertyName, newProperties),
          patch);
    }
  } else {
    if (newProperty != null || !patch) {
      final Object value = newProperty == null ? null : newProperty.getValue();
      updatePropertyValue(property, value);
    }
  }
}
 
Example 8
Source File: JsonDeltaSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void writePrimitiveCollection(final EdmPrimitiveType type, final Property property,
    final Boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale,
    final Boolean isUnicode, final JsonGenerator json)
    throws IOException, SerializerException {
  json.writeStartArray();
  for (Object value : property.asCollection()) {
    switch (property.getValueType()) {
    case COLLECTION_PRIMITIVE:
    case COLLECTION_ENUM:
      try {
        writePrimitiveValue(property.getName(), type, value, isNullable,
            maxLength, precision, scale, isUnicode, json);
      } catch (EdmPrimitiveTypeException e) {
        throw new SerializerException("Wrong value for property!", e,
            SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
            property.getName(), property.getValue().toString());
      }
      break;
    case COLLECTION_GEOSPATIAL:
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
    default:
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
    }
  }
  json.writeEndArray();
}
 
Example 9
Source File: JsonDeltaSerializerWithNavigations.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void writePrimitiveCollection(final EdmPrimitiveType type, final Property property,
    final Boolean isNullable, final Integer maxLength, final Integer precision, final Integer scale,
    final Boolean isUnicode, final JsonGenerator json)
    throws IOException, SerializerException {
  json.writeStartArray();
  for (Object value : property.asCollection()) {
    switch (property.getValueType()) {
    case COLLECTION_PRIMITIVE:
    case COLLECTION_ENUM:
      try {
        writePrimitiveValue(property.getName(), type, value, isNullable,
            maxLength, precision, scale, isUnicode, json);
      } catch (EdmPrimitiveTypeException e) {
        throw new SerializerException("Wrong value for property!", e,
            SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
            property.getName(), property.getValue().toString());
      }
      break;
    case COLLECTION_GEOSPATIAL:
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
    default:
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
    }
  }
  json.writeEndArray();
}
 
Example 10
Source File: ODataJsonSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void writeComplexCollection(final ServiceMetadata metadata, final EdmComplexType type,
    final Property property,
    final Set<List<String>> selectedPaths, final JsonGenerator json, 
    Set<List<String>> expandedPaths, Linked linked, ExpandOption expand)
    throws IOException, SerializerException {
  json.writeStartArray();
  EdmComplexType derivedType = type;
  Set<List<String>> expandedPaths1 = expandedPaths != null && !expandedPaths.isEmpty() ? 
      expandedPaths : ExpandSelectHelper.getExpandedItemsPath(expand);
  for (Object value : property.asCollection()) {
    expandedPaths = expandedPaths1;
    derivedType = ((ComplexValue) value).getTypeName()!=null ? metadata.getEdm().getComplexType
        (new FullQualifiedName(((ComplexValue) value).getTypeName())): type;          
    switch (property.getValueType()) {
    case COLLECTION_COMPLEX:
      json.writeStartObject();
      if (isODataMetadataFull || (!isODataMetadataNone && !derivedType.equals(type))) {
           json.writeStringField(constants.getType(), "#" + 
               derivedType.getFullQualifiedName().getFullQualifiedNameAsString());
      }
      expandedPaths = expandedPaths == null || expandedPaths.isEmpty() ? null :
        ExpandSelectHelper.getReducedExpandItemsPaths(expandedPaths, property.getName());
      writeComplexValue(metadata, derivedType, ((ComplexValue) value).getValue(), 
          selectedPaths, json, expandedPaths, (ComplexValue) value, expand, property.getName());
      json.writeEndObject();
      break;
    default:
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
    }
  }
  json.writeEndArray();
}
 
Example 11
Source File: ODataXmlDeserializerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void complexCollectionProperty() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixPrimCollComp");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("CollPropertyComp");
  String payload = "<metadata:value xmlns:metadata=\"http://docs.oasis-open.org/odata/ns/metadata\"\n" + 
      "xmlns:data=\"http://docs.oasis-open.org/odata/ns/data\" "
      + "metadata:type=\"#Collection(olingo.odata.test1.CTTwoPrim)\">\n"+ 
      "  <metadata:element>\n" + 
      "    <data:PropertyInt16>123</data:PropertyInt16>\n" + 
      "    <data:PropertyString>TEST 1</data:PropertyString>\n" + 
      "  </metadata:element>\n" + 
      "  <metadata:element>\n" + 
      "    <data:PropertyInt16>456</data:PropertyInt16>\n" + 
      "    <data:PropertyString>TEST 2</data:PropertyString>\n" + 
      "  </metadata:element>\n" + 
      "  <metadata:element>\n" + 
      "    <data:PropertyInt16>789</data:PropertyInt16>\n" + 
      "    <data:PropertyString>TEST 3</data:PropertyString>\n" + 
      "  </metadata:element>\n" + 
      "</metadata:value>";
  Property result = deserializer.property(new ByteArrayInputStream(payload.getBytes()), edmProperty).getProperty();

  List<?> complexCollection = result.asCollection();

  Assert.assertEquals(3, complexCollection.size());
  Assert.assertEquals("Collection(olingo.odata.test1.CTTwoPrim)", result.getType());
  Assert.assertEquals((short) 123,
      getCVProperty((ComplexValue) complexCollection.get(0), "PropertyInt16").asPrimitive());
  Assert.assertEquals("TEST 1",
      getCVProperty((ComplexValue) complexCollection.get(0), "PropertyString").asPrimitive());
  Assert.assertEquals((short) 789,
      getCVProperty((ComplexValue) complexCollection.get(2), "PropertyInt16").asPrimitive());
  Assert.assertEquals("TEST 3",
      getCVProperty((ComplexValue) complexCollection.get(2), "PropertyString").asPrimitive());
}
 
Example 12
Source File: ODataJsonDeserializerEntityTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void eTMixPrimCollCompMissingPropertyInComplexType() throws Exception {
  final String entityString = "{"
      + "\"PropertyComp\":{\"PropertyInt16\":111},"
      + "\"CollPropertyComp\":["
      + "{\"PropertyInt16\":123},"
      + "{\"PropertyInt16\":456},"
      + "{\"PropertyInt16\":789}]}";
  final Entity entity = deserialize(entityString, "ETMixPrimCollComp");
  assertNotNull(entity);
  List<Property> properties = entity.getProperties();
  assertNotNull(properties);
  assertEquals(2, properties.size());

  Property complexProperty = entity.getProperty("PropertyComp");
  assertEquals(ValueType.COMPLEX, complexProperty.getValueType());
  List<Property> complexPropertyValues = complexProperty.asComplex().getValue();
  assertEquals(1, complexPropertyValues.size());

  Property property = entity.getProperty("CollPropertyComp");
  assertEquals(ValueType.COLLECTION_COMPLEX, property.getValueType());

  assertTrue(property.getValue() instanceof List);
  List<? extends Object> asCollection = property.asCollection();
  assertEquals(3, asCollection.size());

  for (Object arrayElement : asCollection) {
    assertTrue(arrayElement instanceof ComplexValue);
    List<Property> castedArrayElement = ((ComplexValue) arrayElement).getValue();
    assertEquals(1, castedArrayElement.size());
  }
}
 
Example 13
Source File: ODataJsonDeserializerEntityTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void deriveEntityETMixPrimCollComp() throws Exception {
  final String entityString = "{"
      + "\"PropertyInt16\":32767,"
      + "\"CollPropertyString\":"
      + "[\"[email protected]\",\"[email protected]\",\"[email protected]\"],"
      + "\"PropertyComp\":{\"@odata.type\": \"#olingo.odata.test1.CTBase\","
      + "\"PropertyInt16\":111,\"PropertyString\":\"TEST A\",\"AdditionalPropString\":\"Additional\"},"
      + "\"CollPropertyComp\":["
      + "{\"@odata.type\": \"#olingo.odata.test1.CTBase\",\n"
      + "\"PropertyInt16\":123,\"PropertyString\":\"TEST 1\",\"AdditionalPropString\":\"Additional\"},"
      + "{\"PropertyInt16\":456,\"PropertyString\":\"TEST 2\"},"
      + "{\"PropertyInt16\":789,\"PropertyString\":\"TEST 3\"}]}";
  final Entity entity = deserialize(entityString, "ETMixPrimCollComp");
  assertNotNull(entity);
  List<Property> properties = entity.getProperties();
  assertNotNull(properties);
  assertEquals(4, properties.size());
  Property prop = entity.getProperty("PropertyComp");
  ComplexValue asComp = prop.asComplex();
  assertEquals(3,asComp.getValue().size());
  assertEquals("olingo.odata.test1.CTBase",prop.getType());
  Property property = entity.getProperty("CollPropertyComp");
  assertEquals(ValueType.COLLECTION_COMPLEX, property.getValueType());

  assertTrue(property.getValue() instanceof List);
  List<? extends Object> asCollection = property.asCollection();
  assertEquals(3, asCollection.size());
  assertEquals(3,((ComplexValue)asCollection.get(0)).getValue().size());
  assertEquals(2,((ComplexValue)asCollection.get(1)).getValue().size());
  assertEquals(2,((ComplexValue)asCollection.get(2)).getValue().size());
}
 
Example 14
Source File: ODataJsonDeserializerEntityTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void simpleEntityETMixPrimCollComp() throws Exception {
  final String entityString = "{"
      + "\"PropertyInt16\":32767,"
      + "\"CollPropertyString\":"
      + "[\"[email protected]\",\"[email protected]\",\"[email protected]\"],"
      + "\"PropertyComp\":{\"PropertyInt16\":111,\"PropertyString\":\"TEST A\"},"
      + "\"CollPropertyComp\":["
      + "{\"PropertyInt16\":123,\"PropertyString\":\"TEST 1\"},"
      + "{\"PropertyInt16\":456,\"PropertyString\":\"TEST 2\"},"
      + "{\"PropertyInt16\":789,\"PropertyString\":\"TEST 3\"}]}";
  final Entity entity = deserialize(entityString, "ETMixPrimCollComp");
  assertNotNull(entity);
  List<Property> properties = entity.getProperties();
  assertNotNull(properties);
  assertEquals(4, properties.size());

  Property property = entity.getProperty("CollPropertyComp");
  assertEquals(ValueType.COLLECTION_COMPLEX, property.getValueType());

  assertTrue(property.getValue() instanceof List);
  List<? extends Object> asCollection = property.asCollection();
  assertEquals(3, asCollection.size());

  for (Object arrayElement : asCollection) {
    assertTrue(arrayElement instanceof ComplexValue);
    List<Property> castedArrayElement = ((ComplexValue) arrayElement).getValue();
    assertEquals(2, castedArrayElement.size());
  }
}
 
Example 15
Source File: ODataXmlSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void writeComplexCollection(final ServiceMetadata metadata,
    final EdmComplexType type, final Property property, final Set<List<String>> selectedPaths,
    final String xml10InvalidCharReplacement, final XMLStreamWriter writer, 
    Set<List<String>> expandedPaths, Linked linked, ExpandOption expand)
    throws XMLStreamException, SerializerException {
  EdmComplexType complexType = type;
  Set<List<String>> expandedPaths1 = expandedPaths != null && !expandedPaths.isEmpty() ? 
      expandedPaths : ExpandSelectHelper.getExpandedItemsPath(expand);
  for (Object value : property.asCollection()) {
    expandedPaths = expandedPaths1;
    writer.writeStartElement(METADATA, Constants.ELEM_ELEMENT, NS_METADATA);
    String typeName = ((ComplexValue)value).getTypeName();
    String propertyType = typeName != null ? typeName :property.getType();
    if (derivedComplexType(type, propertyType ) != null) {
      writer.writeAttribute(METADATA, NS_METADATA, Constants.ATTR_TYPE, propertyType);
    }
    if(typeName!=null && !propertyType.equals(type.getFullQualifiedName().getFullQualifiedNameAsString())){
      complexType = (EdmComplexType) (metadata.getEdm().getComplexType(new FullQualifiedName(propertyType)));
    }else{
      complexType = type;
    }
    switch (property.getValueType()) {
    case COLLECTION_COMPLEX:
      expandedPaths = expandedPaths == null || expandedPaths.isEmpty() ? null :
        ExpandSelectHelper.getReducedExpandItemsPaths(expandedPaths, property.getName());
      writeComplexValue(metadata, complexType,
          ((ComplexValue) value).getValue(), selectedPaths,
          xml10InvalidCharReplacement, writer, expandedPaths, (ComplexValue) value, expand, property.getName());
      break;
    default:
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
    }
    writer.writeEndElement();
  }
}
 
Example 16
Source File: ODataXmlDeserializerTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void derivedEntityESCompCollDerivedNullEmpty() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollDerived");
  final String payload = "<?xml version='1.0' encoding='UTF-8'?>\n" + 
      "<a:entry xmlns:a=\"http://www.w3.org/2005/Atom\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\"\n" + 
      "xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" m:context=\"$metadata#ESCompCollDerived/$entity\"\n" +  
      "m:metadata-etag=\"W/&quot;1c2796fd-da13-4741-9da2-99cac365f296&quot;\">\n" + 
      "    <a:id>ESCompCollDerived(32767)</a:id>\n" + 
      "    <a:title/>\n" + 
      "    <a:summary/>\n" + 
      "    <a:updated>2017-07-18T13:18:13Z</a:updated>\n" + 
      "    <a:author>\n" + 
      "        <a:name/>\n" + 
      "    </a:author>\n" + 
      "    <a:link rel=\"edit\" href=\"ESCompCollDerived(32767)\"/>\n" + 
      "    <a:category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\"\n" +
      "    term=\"#olingo.odata.test1.ETDeriveCollComp\"/>\n" + 
      "    <a:content type=\"application/xml\">\n" + 
      "        <m:properties>\n" + 
      "            <d:PropertyInt16 m:type=\"Int16\">32767</d:PropertyInt16>\n" + 
      "            <d:CollPropertyCompAno>\n" + 
      "                <m:element m:type=\"#olingo.odata.test1.CTBaseAno\">\n" + 
      "                    <d:PropertyString/>\n" + 
      "                   <d:AdditionalPropString/>\n" +
      "                </m:element>\n" + 
      "                <m:element  m:type=\"#olingo.odata.test1.CTBaseAno\">\n" + 
      "                </m:element>\n" + 
      "            </d:CollPropertyCompAno>\n" + 
      "        </m:properties>\n" + 
      "    </a:content>\n" + 
      "</a:entry>\n" ;

  Entity result = deserializer.entity(new ByteArrayInputStream(payload.getBytes()), 
      edmEntitySet.getEntityType()).getEntity();

  Assert.assertEquals(2, result.getProperties().size());
  Assert.assertEquals(0, result.getNavigationLinks().size());


  Property comp = result.getProperty("CollPropertyCompAno");
  Assert.assertEquals("Collection(olingo.odata.test1.CTTwoPrimAno)", comp.getType());
  List<? extends Object> cv = comp.asCollection();
  
  Assert.assertEquals(2, cv.size());
  for (Object arrayElement : cv) {

    assertTrue(arrayElement instanceof ComplexValue);
    List<Property> castedArrayElement = ((ComplexValue) arrayElement).getValue();
    if(castedArrayElement.size()>0){
      assertEquals(2, castedArrayElement.size());
      assertNull(castedArrayElement.get(0).getValue());
    }
  }
}
 
Example 17
Source File: ODataXmlDeserializerTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void derivedEntityESCompCollDerived() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCompCollDerived");
  final String payload = "<?xml version='1.0' encoding='UTF-8'?>\n" + 
      "<a:entry xmlns:a=\"http://www.w3.org/2005/Atom\" xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\"\n" + 
      "xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" m:context=\"$metadata#ESCompCollDerived/$entity\"\n" +  
      "m:metadata-etag=\"W/&quot;1c2796fd-da13-4741-9da2-99cac365f296&quot;\">\n" + 
      "    <a:id>ESCompCollDerived(32767)</a:id>\n" + 
      "    <a:title/>\n" + 
      "    <a:summary/>\n" + 
      "    <a:updated>2017-07-18T13:18:13Z</a:updated>\n" + 
      "    <a:author>\n" + 
      "        <a:name/>\n" + 
      "    </a:author>\n" + 
      "    <a:link rel=\"edit\" href=\"ESCompCollDerived(32767)\"/>\n" + 
      "    <a:category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\"\n" +
      "    term=\"#olingo.odata.test1.ETDeriveCollComp\"/>\n" + 
      "    <a:content type=\"application/xml\">\n" + 
      "        <m:properties>\n" + 
      "            <d:PropertyInt16 m:type=\"Int16\">32767</d:PropertyInt16>\n" + 
      "            <d:CollPropertyCompAno>\n" + 
      "                <m:element m:type=\"#olingo.odata.test1.CTTwoPrimAno\">\n" + 
      "                    <d:PropertyString>TEST9876</d:PropertyString>\n" + 
      "                </m:element>\n" + 
      "                <m:element  m:type=\"#olingo.odata.test1.CTBaseAno\">\n" + 
      "                    <d:AdditionalPropString>TEST9889</d:AdditionalPropString>\n" + 
      "                    <d:PropertyString>TEST9889</d:PropertyString>\n" + 
      "                </m:element>\n" + 
      "            </d:CollPropertyCompAno>\n" + 
      "        </m:properties>\n" + 
      "    </a:content>\n" + 
      "</a:entry>\n" ;

  Entity result = deserializer.entity(new ByteArrayInputStream(payload.getBytes()), 
      edmEntitySet.getEntityType()).getEntity();

  Assert.assertEquals(2, result.getProperties().size());
  Assert.assertEquals(0, result.getNavigationLinks().size());

  Assert.assertEquals(("[[PropertyString=TEST9876], [AdditionalPropString=TEST9889, PropertyString=TEST9889]]"), 
      result.getProperty("CollPropertyCompAno").getValue().toString());

  Property comp = result.getProperty("CollPropertyCompAno");
  Assert.assertEquals("Collection(olingo.odata.test1.CTTwoPrimAno)", comp.getType());
  List<? extends Object> cv = comp.asCollection();
  
  Assert.assertEquals(2, cv.size());
  for (Object arrayElement : cv) {

    assertTrue(arrayElement instanceof ComplexValue);
    List<Property> castedArrayElement = ((ComplexValue) arrayElement).getValue();
    if(castedArrayElement.size() == 1){
      assertEquals("PropertyString=TEST9876", castedArrayElement.get(0).toString());
    }else{
      assertEquals(2, castedArrayElement.size());
      assertEquals("AdditionalPropString=TEST9889", castedArrayElement.get(0).toString());
    }
  
  }
}
 
Example 18
Source File: ODataXmlDeserializerTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void derivedEntityMixPrimCollComp() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixPrimCollComp");
  final String payload = "<?xml version='1.0' encoding='UTF-8'?>\n" + 
      "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\"\n" + 
      "  xmlns:metadata=\"http://docs.oasis-open.org/odata/ns/metadata\"\n" + 
      "  xmlns:data=\"http://docs.oasis-open.org/odata/ns/data\" \n" + 
      "  metadata:metadata-etag=\"WmetadataETag\">\n" + 
      "  <atom:category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\"\n" + 
      "    term=\"#olingo.odata.test1.ETMixPrimCollComp\" />\n" + 
      "  <atom:content type=\"application/xml\">\n" + 
      "    <metadata:properties>\n" + 
      "      <data:PropertyInt16>32767</data:PropertyInt16>\n" + 
      "      <data:CollPropertyString type=\"#Collection(String)\">\n" + 
      "        <metadata:element>[email protected]</metadata:element>\n" + 
      "        <metadata:element>[email protected]</metadata:element>\n" + 
      "        <metadata:element>[email protected]</metadata:element>\n" + 
      "      </data:CollPropertyString>\n" + 
      "      <data:PropertyComp metadata:type=\"#olingo.odata.test1.CTBase\">\n" + 
      "        <data:PropertyInt16>111</data:PropertyInt16>\n" + 
      "        <data:PropertyString>TEST A</data:PropertyString>\n" + 
      "        <data:AdditionalPropString>Additional</data:AdditionalPropString>\n" + 
      "      </data:PropertyComp>\n" + 
      "       <data:CollPropertyComp metadata:type=\"#Collection(olingo.odata.test1.CTTwoPrim)\">\n" + 
      "          <metadata:element  metadata:type=\"olingo.odata.test1.CTBase\">\n" + 
      "            <data:PropertyInt16>123</data:PropertyInt16>\n" + 
      "            <data:PropertyString>TEST 1</data:PropertyString>\n" + 
      "            <data:AdditionalPropString>Additional test</data:AdditionalPropString>\n" + 
      "          </metadata:element>\n" + 
      "          <metadata:element>\n" + 
      "            <data:PropertyInt16>456</data:PropertyInt16>\n" + 
      "            <data:PropertyString>TEST 2</data:PropertyString>\n" + 
      "          </metadata:element>\n" + 
      "          <metadata:element>\n" + 
      "            <data:PropertyInt16>789</data:PropertyInt16>\n" + 
      "            <data:PropertyString>TEST 3</data:PropertyString>\n" + 
      "          </metadata:element>\n" + 
      "        </data:CollPropertyComp>\n" + 
      "    </metadata:properties>\n" + 
      "  </atom:content>\n" + 
      "</atom:entry>\n"; 

  Entity result = deserializer.entity(new ByteArrayInputStream(payload.getBytes()), 
      edmEntitySet.getEntityType()).getEntity();

  Assert.assertEquals(4, result.getProperties().size());
  Assert.assertEquals(0, result.getNavigationLinks().size());

  Assert.assertEquals(Arrays.asList("[email protected]", "[email protected]",
      "[email protected]"), result.getProperty("CollPropertyString").getValue());

  Property comp = result.getProperty("PropertyComp");
  Assert.assertEquals("olingo.odata.test1.CTBase", comp.getType());
  ComplexValue cv = comp.asComplex();
  
  Assert.assertEquals(3, cv.getValue().size());
  Assert.assertEquals((short) 111, getCVProperty(cv, "PropertyInt16").asPrimitive());
  Assert.assertEquals("TEST A", getCVProperty(cv, "PropertyString").asPrimitive());
  Assert.assertEquals("Additional", getCVProperty(cv, "AdditionalPropString").asPrimitive());
  
  comp = result.getProperty("CollPropertyComp");
  Assert.assertEquals("Collection(olingo.odata.test1.CTTwoPrim)", comp.getType());

  List<?> properties = comp.asCollection();
  Assert.assertEquals(3, properties.size());
  
  Assert.assertEquals((short) 123, getCVProperty((ComplexValue) properties.get(0), "PropertyInt16").asPrimitive());
  Assert.assertEquals("TEST 1", getCVProperty((ComplexValue) properties.get(0), "PropertyString").asPrimitive());
  Assert.assertEquals("Additional test", getCVProperty((ComplexValue) properties.get(0), "AdditionalPropString")
      .asPrimitive());

  Assert.assertEquals((short) 789, getCVProperty((ComplexValue) properties.get(2), "PropertyInt16").asPrimitive());
  Assert.assertEquals("TEST 3", getCVProperty((ComplexValue) properties.get(2), "PropertyString").asPrimitive());
}
 
Example 19
Source File: ODataXmlDeserializerTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void entityMixPrimCollComp() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixPrimCollComp");
  final String payload = "<?xml version='1.0' encoding='UTF-8'?>\n" + 
      "<atom:entry xmlns:atom=\"http://www.w3.org/2005/Atom\"\n" + 
      "  xmlns:metadata=\"http://docs.oasis-open.org/odata/ns/metadata\"\n" + 
      "  xmlns:data=\"http://docs.oasis-open.org/odata/ns/data\" \n" + 
      "  metadata:metadata-etag=\"WmetadataETag\">\n" + 
      "  <atom:category scheme=\"http://docs.oasis-open.org/odata/ns/scheme\"\n" + 
      "    term=\"#olingo.odata.test1.ETMixPrimCollComp\" />\n" + 
      "  <atom:content type=\"application/xml\">\n" + 
      "    <metadata:properties>\n" + 
      "      <data:PropertyInt16>32767</data:PropertyInt16>\n" + 
      "      <data:CollPropertyString type=\"#Collection(String)\">\n" + 
      "        <metadata:element>[email protected]</metadata:element>\n" + 
      "        <metadata:element>[email protected]</metadata:element>\n" + 
      "        <metadata:element>[email protected]</metadata:element>\n" + 
      "      </data:CollPropertyString>\n" + 
      "      <data:PropertyComp metadata:type=\"#olingo.odata.test1.CTTwoPrim\">\n" + 
      "        <data:PropertyInt16>111</data:PropertyInt16>\n" + 
      "        <data:PropertyString>TEST A</data:PropertyString>\n" + 
      "      </data:PropertyComp>\n" + 
      "       <data:CollPropertyComp metadata:type=\"#Collection(olingo.odata.test1.CTTwoPrim)\">\n" + 
      "          <metadata:element>\n" + 
      "            <data:PropertyInt16>123</data:PropertyInt16>\n" + 
      "            <data:PropertyString>TEST 1</data:PropertyString>\n" + 
      "          </metadata:element>\n" + 
      "          <metadata:element>\n" + 
      "            <data:PropertyInt16>456</data:PropertyInt16>\n" + 
      "            <data:PropertyString>TEST 2</data:PropertyString>\n" + 
      "          </metadata:element>\n" + 
      "          <metadata:element>\n" + 
      "            <data:PropertyInt16>789</data:PropertyInt16>\n" + 
      "            <data:PropertyString>TEST 3</data:PropertyString>\n" + 
      "          </metadata:element>\n" + 
      "        </data:CollPropertyComp>\n" + 
      "    </metadata:properties>\n" + 
      "  </atom:content>\n" + 
      "</atom:entry>\n"; 

  Entity result = deserializer.entity(new ByteArrayInputStream(payload.getBytes()), 
      edmEntitySet.getEntityType()).getEntity();

  Assert.assertEquals(4, result.getProperties().size());
  Assert.assertEquals(0, result.getNavigationLinks().size());

  Assert.assertEquals(Arrays.asList("[email protected]", "[email protected]",
      "[email protected]"), result.getProperty("CollPropertyString").getValue());

  Property comp = result.getProperty("PropertyComp");
  Assert.assertEquals("olingo.odata.test1.CTTwoPrim", comp.getType());
  ComplexValue cv = comp.asComplex();
  
  Assert.assertEquals(2, cv.getValue().size());
  Assert.assertEquals((short) 111, getCVProperty(cv, "PropertyInt16").asPrimitive());
  Assert.assertEquals("TEST A", getCVProperty(cv, "PropertyString").asPrimitive());
  
  comp = result.getProperty("CollPropertyComp");
  Assert.assertEquals("Collection(olingo.odata.test1.CTTwoPrim)", comp.getType());

  List<?> properties = comp.asCollection();
  Assert.assertEquals(3, properties.size());
  
  Assert.assertEquals((short) 123, getCVProperty((ComplexValue) properties.get(0), "PropertyInt16").asPrimitive());
  Assert.assertEquals("TEST 1", getCVProperty((ComplexValue) properties.get(0), "PropertyString").asPrimitive());

  Assert.assertEquals((short) 789, getCVProperty((ComplexValue) properties.get(2), "PropertyInt16").asPrimitive());
  Assert.assertEquals("TEST 3", getCVProperty((ComplexValue) properties.get(2), "PropertyString").asPrimitive());
}
 
Example 20
Source File: ODataJsonDeserializerEntityTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void derivedEntityESCompCollDerived() throws Exception {
  final String payload = "{\n" +
      "   \"@odata.context\": \"$metadata#ESCompCollDerived/$entity\",\n" +
      "   \"@odata.metadataEtag\":\"W/metadataETag\",\n" +
      "   \"@odata.etag\":\"W/32767\",\n" +
      "   \"PropertyInt16\":32767,\n" +
      "   \"[email protected]\": \"#Collection(olingo.odata.test1.CTTwoPrimAno)\",\n" +
      "   \"CollPropertyCompAno\": [\n" +
      "      {\n" +
      "     \"@odata.type\": \"#olingo.odata.test1.CTBaseAno\",\n" +
      "            \"PropertyString\": \"TEST9876\",\n" +
      "            \"AdditionalPropString\": \"Additional9876\"\n" +
      "        },\n" +
      "        {\n" +
      "            \"@odata.type\": \"#olingo.odata.test1.CTTwoPrimAno\",\n" +
      "            \"PropertyString\": \"TEST1234\"\n" +
      "        }\n" +
      "    ]\n" +
      "   \n" +
      "}";
  final Entity entity = deserialize(payload, "ETDeriveCollComp");
  Assert.assertNotNull(entity);
  List<Property> properties = entity.getProperties();
  assertNotNull(properties);
  assertEquals(2, properties.size());
  assertEquals("PropertyInt16=32767", properties.get(0).toString());
  for (Property prop : properties) {
    assertNotNull(prop);
    if (prop.isCollection()) {
      Property property = entity.getProperty("CollPropertyCompAno");
      assertEquals(ValueType.COLLECTION_COMPLEX, property.getValueType());

      assertTrue(property.getValue() instanceof List);
      List<? extends Object> asCollection = property.asCollection();
      assertEquals(2, asCollection.size());

      for (Object arrayElement : asCollection) {
        assertTrue(arrayElement instanceof ComplexValue);
        List<Property> castedArrayElement = ((ComplexValue) arrayElement).getValue();
        if(castedArrayElement.size() == 1){
          assertEquals("PropertyString=TEST1234", castedArrayElement.get(0).toString());
          assertEquals("olingo.odata.test1.CTTwoPrimAno", ((ComplexValue) arrayElement).getTypeName());
        }else{
          assertEquals(2, castedArrayElement.size());
          assertEquals("AdditionalPropString=Additional9876", castedArrayElement.get(1).toString());
          assertEquals("olingo.odata.test1.CTBaseAno", ((ComplexValue) arrayElement).getTypeName());
        }
      }
    }
  }
}