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

The following examples show how to use org.apache.olingo.commons.api.edm.EdmPrimitiveType. 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: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 7 votes vote down vote up
@Test
public void primitivePropertyNull() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("PropertyString");
  final Property property = new Property("Edm.String", edmProperty.getName(), ValueType.PRIMITIVE, null);
  final String resultString = IOUtils
      .toString(serializer.primitive(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
      PrimitiveSerializerOptions.with()
          .contextURL(ContextURL.with()
              .entitySet(edmEntitySet).keyPath("4242").navOrPropertyPath(edmProperty.getName())
              .build())
          .build()).getContent());
  Assert.assertEquals(
      "{\"@odata.context\":\"../$metadata#ESAllPrim(4242)/PropertyString\","
          +"\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\",\"value\":null}",
      resultString);
}
 
Example #2
Source File: ODataXmlSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void testXML10ReplacementChar() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("PropertyString");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty(edmProperty.getName());
  property.setValue(ValueType.PRIMITIVE, "ab\u0000cd\u0001");
  final String resultString = IOUtils.toString(serializer
      .primitive(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
          PrimitiveSerializerOptions.with()
              .contextURL(ContextURL.with()
                  .entitySet(edmEntitySet).keyPath("32767").navOrPropertyPath(edmProperty.getName())
                  .build())
              .xml10InvalidCharReplacement("XX")
              .unicode(Boolean.TRUE)
              .build()).getContent());

  String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
      + "<m:value xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\" "
      + "m:context=\"../$metadata#ESAllPrim(32767)/PropertyString\" "
      + "m:metadata-etag=\"metadataETag\">"
      + "abXXcdXX</m:value>";
  Assert.assertEquals(expected, resultString);
}
 
Example #3
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitiveCollectionPropertyIEEE754CompatibleInt64() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCollAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("CollPropertyInt64");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty(edmProperty.getName());

  final String resultString = IOUtils.toString(serializerIEEECompatible
      .primitiveCollection(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
          PrimitiveSerializerOptions.with()
              .contextURL(ContextURL.with()
                  .entitySet(edmEntitySet).keyPath("1").navOrPropertyPath(edmProperty.getName()).build())
              .build()).getContent());
  Assert.assertEquals("{"
      + "\"@context\":\"../$metadata#ESCollAllPrim(1)/CollPropertyInt64\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":[\"929292929292\",\"333333333333\",\"444444444444\"]}",
      resultString);
}
 
Example #4
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitiveCollectionPropertyIEEE754CompatibleDecimal() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCollAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("CollPropertyDecimal");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty(edmProperty.getName());

  final String resultString = IOUtils.toString(serializerIEEECompatible
      .primitiveCollection(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
          PrimitiveSerializerOptions.with()
              .contextURL(ContextURL.with()
                  .entitySet(edmEntitySet).keyPath("1").navOrPropertyPath(edmProperty.getName()).build())
              .build()).getContent());
  Assert.assertEquals("{"
      + "\"@context\":\"../$metadata#ESCollAllPrim(1)/CollPropertyDecimal\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":[\"12\",\"-2\",\"1234\"]}",
      resultString);
}
 
Example #5
Source File: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitivePropertyIEEE754CompatibleDecimal() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("PropertyDecimal");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty(edmProperty.getName());
  final String resultString = IOUtils.toString(serializerIEEECompatible
      .primitive(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
          PrimitiveSerializerOptions.with()
              .contextURL(ContextURL.with()
                  .entitySet(edmEntitySet).keyPath("32767").navOrPropertyPath(edmProperty.getName()).build())
              .build()).getContent());
  Assert.assertEquals("{"
      + "\"@odata.context\":\"../$metadata#ESAllPrim(32767)/PropertyDecimal\","
      + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":\"34\"}",
      resultString);
}
 
Example #6
Source File: ODataXmlSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitiveCollectionProperty() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCollAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("CollPropertyString");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty(edmProperty.getName());

  final String resultString = IOUtils.toString(serializer
      .primitiveCollection(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
          PrimitiveSerializerOptions.with()
              .contextURL(ContextURL.with()
                  .entitySet(edmEntitySet).keyPath("1").navOrPropertyPath(edmProperty.getName())
                  .build())
              .build()).getContent());
  String expected = "<?xml version='1.0' encoding='UTF-8'?>"
      + "<m:value xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\" "
      + "m:context=\"../$metadata#ESCollAllPrim(1)/CollPropertyString\" "
      + "m:metadata-etag=\"metadataETag\"  m:type=\"#Collection(String)\">"
      + "<m:element>[email protected]</m:element>"
      + "<m:element>[email protected]</m:element>"
      + "<m:element>[email protected]</m:element>"
      + "</m:value>";
  checkXMLEqual(expected, resultString);
}
 
Example #7
Source File: TypedOperand.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public TypedOperand asTypedOperand() throws ODataApplicationException {
  if (isNull()) {
    return this;
  } else if (type instanceof EdmPrimitiveType && !(value instanceof Collection)) {
    return value.getClass() == getDefaultType((EdmPrimitiveType) type) ?
        this :
        asTypedOperand((EdmPrimitiveType) type);
  } else if (type instanceof EdmPrimitiveType && value instanceof Collection) {
    return value.getClass() == getDefaultType((EdmPrimitiveType) type) ?
        this :
        asTypedOperandForCollection((EdmPrimitiveType) type);
  } else {
    throw new ODataApplicationException("A single primitive-type instance is expected.",
        HttpStatusCode.BAD_REQUEST.getStatusCode(), Locale.ROOT);
  }
}
 
Example #8
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void checkEqualityTypes(final Expression left, final Expression right) throws UriParserException {
  checkNoCollection(left);
  checkNoCollection(right);

  final EdmType leftType = getType(left);
  final EdmType rightType = getType(right);
  if (leftType == null || rightType == null || leftType.equals(rightType)) {
    return;
  }

  // Numeric promotion for Edm.Byte and Edm.SByte
  if (isType(leftType, EdmPrimitiveTypeKind.Byte, EdmPrimitiveTypeKind.SByte)
      && isType(rightType, EdmPrimitiveTypeKind.Byte, EdmPrimitiveTypeKind.SByte)) {
    return;
  }

  if (leftType.getKind() != EdmTypeKind.PRIMITIVE
      || rightType.getKind() != EdmTypeKind.PRIMITIVE
      || !(((EdmPrimitiveType) leftType).isCompatible((EdmPrimitiveType) rightType)
      || ((EdmPrimitiveType) rightType).isCompatible((EdmPrimitiveType) leftType))) {
    throw new UriParserSemanticException("Incompatible types.",
        UriParserSemanticException.MessageKeys.TYPES_NOT_COMPATIBLE,
        leftType.getFullQualifiedName().getFullQualifiedNameAsString(),
        rightType.getFullQualifiedName().getFullQualifiedNameAsString());
  }
}
 
Example #9
Source File: ODataAdapter.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This method returns the object which is the value of the property.
 *
 * @param edmProperty EdmProperty
 * @param value       String value
 * @return Object
 * @throws ODataApplicationException
 */
private Object readPrimitiveValue(EdmProperty edmProperty, String value) throws ODataApplicationException {
    if (value == null) {
        return null;
    }
    try {
        if (value.startsWith("'") && value.endsWith("'")) {
            value = value.substring(1, value.length() - 1);
        }
        EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmProperty.getType();
        Class<?> javaClass = getJavaClassForPrimitiveType(edmProperty, edmPrimitiveType);
        return edmPrimitiveType.valueOfString(value, edmProperty.isNullable(), edmProperty.getMaxLength(),
                                              edmProperty.getPrecision(), edmProperty.getScale(),
                                              edmProperty.isUnicode(), javaClass);
    } catch (EdmPrimitiveTypeException e) {
        throw new ODataApplicationException("Invalid value: " + value + " for property: " + edmProperty.getName(),
                                            HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
                                            Locale.getDefault());
    }
}
 
Example #10
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 #11
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitivePropertyWithMetadataFull() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType()
      .getProperty("PropertyString");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0)
      .getProperty(edmProperty.getName());
  final String resultString = IOUtils
      .toString(serializerFullMetadata
          .primitive(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
              PrimitiveSerializerOptions.with()
                  .contextURL(ContextURL.with().entitySet(edmEntitySet)
                      .keyPath("32767")
                      .navOrPropertyPath(edmProperty.getName()).build())
                  .build())
          .getContent());
  Assert.assertEquals(
      "{\"@context\":\"../$metadata#ESAllPrim(32767)/PropertyString\","
          + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
          + "\"value\":\"First Resource - positive values\"}",
      resultString);
}
 
Example #12
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitiveProperty() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("PropertyString");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty(edmProperty.getName());
  final String resultString = IOUtils.toString(serializer
      .primitive(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
          PrimitiveSerializerOptions.with()
              .contextURL(ContextURL.with()
                  .entitySet(edmEntitySet).keyPath("32767").navOrPropertyPath(edmProperty.getName())
                  .build())
              .build()).getContent());
  Assert.assertEquals("{"
      + "\"@context\":\"../$metadata#ESAllPrim(32767)/PropertyString\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":\"First Resource - positive values\"}",
      resultString);
}
 
Example #13
Source File: EdmTypeDefinitionImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public EdmPrimitiveType getUnderlyingType() {
  if (edmPrimitiveTypeInstance == null) {
    try {
      if (typeDefinition.getUnderlyingType() == null) {
        throw new EdmException("Underlying Type for type definition: "
            + typeName.getFullQualifiedNameAsString() + " must not be null.");
      }
      edmPrimitiveTypeInstance = EdmPrimitiveTypeFactory.getInstance(
          EdmPrimitiveTypeKind.valueOfFQN(typeDefinition.getUnderlyingType()));
    } catch (IllegalArgumentException e) {
      throw new EdmException("Invalid underlying type: " + typeDefinition.getUnderlyingType(), e);
    }
  }
  return edmPrimitiveTypeInstance;
}
 
Example #14
Source File: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitiveCollectionProperty() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESCollAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("CollPropertyString");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty(edmProperty.getName());

  final String resultString = IOUtils.toString(serializer
      .primitiveCollection(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
          PrimitiveSerializerOptions.with()
              .contextURL(ContextURL.with()
                  .entitySet(edmEntitySet).keyPath("1").navOrPropertyPath(edmProperty.getName())
                  .build())
              .build()).getContent());
  Assert.assertEquals("{"
      + "\"@odata.context\":\"../$metadata#ESCollAllPrim(1)/CollPropertyString\","
      + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":[\"[email protected]\",\"[email protected]\",\"[email protected]\"]}",
      resultString);
}
 
Example #15
Source File: ODataJsonSerializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void writePrimitive(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 EdmPrimitiveTypeException, IOException, SerializerException {
  if (property.isPrimitive()) {
    writePrimitiveValue(property.getName(), type, property.asPrimitive(),
        isNullable, maxLength, precision, scale, isUnicode, json);
  } else if (property.isGeospatial()) {
    writeGeoValue(property.getName(), type, property.asGeospatial(), isNullable, json, null);
  } else if (property.isEnum()) {
    writePrimitiveValue(property.getName(), type, property.asEnum(),
        isNullable, maxLength, precision, scale, isUnicode, json);
  } else {
    throw new SerializerException("Inconsistent property type!",
        SerializerException.MessageKeys.INCONSISTENT_PROPERTY_TYPE, property.getName());
  }
}
 
Example #16
Source File: ParserHelper.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private static UriParameter keyValuePair(UriTokenizer tokenizer,
    final String keyPredicateName, final EdmEntityType edmEntityType,
    final Edm edm, final EdmType referringType, final Map<String, AliasQueryOption> aliases)
    throws UriParserException, UriValidationException {
  final EdmKeyPropertyRef keyPropertyRef = edmEntityType.getKeyPropertyRef(keyPredicateName);
  final EdmProperty edmProperty = keyPropertyRef == null ? null : keyPropertyRef.getProperty();
  if (edmProperty == null) {
    throw new UriValidationException(keyPredicateName + " is not a valid key property name.",
        UriValidationException.MessageKeys.INVALID_KEY_PROPERTY, keyPredicateName);
  }
  ParserHelper.requireNext(tokenizer, TokenKind.EQ);
  if (tokenizer.next(TokenKind.COMMA) || tokenizer.next(TokenKind.CLOSE) || tokenizer.next(TokenKind.EOF)) {
    throw new UriParserSyntaxException("Key value expected.", UriParserSyntaxException.MessageKeys.SYNTAX);
  }
  if (nextPrimitiveTypeValue(tokenizer, (EdmPrimitiveType) edmProperty.getType(), edmProperty.isNullable())) {
    return createUriParameter(edmProperty, keyPredicateName, tokenizer.getText(), edm, referringType, aliases);
  } else {
    throw new UriParserSemanticException(keyPredicateName + " has not a valid  key value.",
        UriParserSemanticException.MessageKeys.INVALID_KEY_VALUE, keyPredicateName);
  }
}
 
Example #17
Source File: TripPinDataModel.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
static Object readPrimitiveValue(EdmProperty edmProperty, String value)
    throws ODataApplicationException {
  if (value == null) {
    return null;
  }
  try {
    if (value.startsWith("'") && value.endsWith("'")) {
      value = value.substring(1,value.length()-1);
    }
    EdmPrimitiveType edmPrimitiveType = (EdmPrimitiveType) edmProperty.getType();
    Class<?> javaClass = getJavaClassForPrimitiveType(edmProperty, edmPrimitiveType);
    return edmPrimitiveType.valueOfString(value, edmProperty.isNullable(),
        edmProperty.getMaxLength(), edmProperty.getPrecision(), edmProperty.getScale(),
        edmProperty.isUnicode(), javaClass);
  } catch (EdmPrimitiveTypeException e) {
    throw new ODataApplicationException("Invalid value: " + value + " for property: "
        + edmProperty.getName(), 500, Locale.getDefault());
  }
}
 
Example #18
Source File: ODataXmlSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitiveProperty() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("PropertyString");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty(edmProperty.getName());
  final String resultString = IOUtils.toString(serializer
      .primitive(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
          PrimitiveSerializerOptions.with()
              .contextURL(ContextURL.with()
                  .entitySet(edmEntitySet).keyPath("32767").navOrPropertyPath(edmProperty.getName())
                  .build())
              .build()).getContent());

  String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
      + "<m:value xmlns:m=\"http://docs.oasis-open.org/odata/ns/metadata\" "
      + "m:context=\"../$metadata#ESAllPrim(32767)/PropertyString\" "
      + "m:metadata-etag=\"metadataETag\">"
      + "First Resource - positive values</m:value>";
  Assert.assertEquals(expected, resultString);
}
 
Example #19
Source File: ExpressionParser.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private Expression parsePrimitive(final TokenKind primitiveTokenKind) throws UriParserException {
  final String primitiveValueLiteral = tokenizer.getText();
  if (primitiveTokenKind == TokenKind.EnumValue) {
    return createEnumExpression(primitiveValueLiteral);
  } else {
    EdmPrimitiveTypeKind primitiveTypeKind = ParserHelper.tokenToPrimitiveType.get(primitiveTokenKind);
    if (primitiveTypeKind == EdmPrimitiveTypeKind.Int64) {
      primitiveTypeKind = determineIntegerType(primitiveValueLiteral);
    }

    final EdmPrimitiveType type = primitiveTypeKind == null ?
        // Null handling
        null :
        odata.createPrimitiveTypeInstance(primitiveTypeKind);
    return new LiteralImpl(primitiveValueLiteral, type);
  }
}
 
Example #20
Source File: JsonDeltaSerializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void writePrimitive(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 EdmPrimitiveTypeException, IOException, SerializerException {
  if (property.isPrimitive()) {
    writePrimitiveValue(property.getName(), type, property.asPrimitive(),
        isNullable, maxLength, precision, scale, isUnicode, json);
  } else if (property.isGeospatial()) {
    throw new SerializerException("Property type not yet supported!",
        SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, property.getName());
  } else if (property.isEnum()) {
    writePrimitiveValue(property.getName(), type, property.asEnum(),
        isNullable, maxLength, precision, scale, isUnicode, json);
  } else {
    throw new SerializerException("Inconsistent property type!",
        SerializerException.MessageKeys.INCONSISTENT_PROPERTY_TYPE, property.getName());
  }
}
 
Example #21
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitivePropertyIEEE754CompatibleInt64() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("PropertyInt64");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty(edmProperty.getName());
  final String resultString = IOUtils.toString(serializerIEEECompatible
      .primitive(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
          PrimitiveSerializerOptions.with()
              .contextURL(ContextURL.with()
                  .entitySet(edmEntitySet).keyPath("32767").navOrPropertyPath(edmProperty.getName()).build())
              .build()).getContent());
  Assert.assertEquals("{"
      + "\"@context\":\"../$metadata#ESAllPrim(32767)/PropertyInt64\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":\"" + Long.MAX_VALUE + "\"}",
      resultString);
}
 
Example #22
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitivePropertyNull() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESAllPrim");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("PropertyString");
  final Property property = new Property("Edm.String", edmProperty.getName(), ValueType.PRIMITIVE, null);
  final String resultString = IOUtils
      .toString(serializer.primitive(metadata, (EdmPrimitiveType) edmProperty.getType(), property,
      PrimitiveSerializerOptions.with()
          .contextURL(ContextURL.with()
              .entitySet(edmEntitySet).keyPath("4242").navOrPropertyPath(edmProperty.getName())
              .build())
          .build()).getContent());
  Assert.assertEquals(
      "{\"@context\":\"../$metadata#ESAllPrim(4242)/PropertyString\","
          +"\"@metadataEtag\":\"W/\\\"metadataETag\\\"\",\"value\":null}",
      resultString);
}
 
Example #23
Source File: ParserHelper.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private static UriParameter createUriParameter(final EdmProperty edmProperty, final String parameterName,
    final String literalValue, final Edm edm, final EdmType referringType,
    final Map<String, AliasQueryOption> aliases) throws UriParserException, UriValidationException {
  final AliasQueryOption alias = literalValue.startsWith("@") ?
      getKeyAlias(literalValue, edmProperty, edm, referringType, aliases) :
      null;
  final String value = alias == null ? literalValue : alias.getText();
  final EdmPrimitiveType primitiveType = (EdmPrimitiveType) edmProperty.getType();
  try {
    if (!(primitiveType.validate(primitiveType.fromUriLiteral(value), edmProperty.isNullable(),
        edmProperty.getMaxLength(), edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode()))) {
      throw new UriValidationException("Invalid key property",
          UriValidationException.MessageKeys.INVALID_KEY_PROPERTY, parameterName);
    }
  } catch (final EdmPrimitiveTypeException e) {
    throw new UriValidationException("Invalid key property", e,
        UriValidationException.MessageKeys.INVALID_KEY_PROPERTY, parameterName);
  }

  return new UriParameterImpl()
      .setName(parameterName)
      .setText("null".equals(literalValue) ? null : literalValue)
      .setAlias(alias == null ? null : literalValue)
      .setExpression(alias == null ? null :
          alias.getValue() == null ? new LiteralImpl(value, primitiveType) : alias.getValue());
}
 
Example #24
Source File: UntypedOperand.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings( "unchecked")
@Override
public TypedOperand asTypedOperandForCollection(EdmPrimitiveType type) throws ODataApplicationException {
  List<Object> newValue = new ArrayList<Object>();
  List<Object> list = (List<Object>) value;
  for (Object val : list) {
    final String literal = (String) val;
    
    // First try the null literal.
    if (null != tryCast(literal, primNull)) {
      newValue.add(tryCast(literal, primNull));
      type = primNull;
    }
    // Then try the given type.
    if (null != tryCast(literal, type)) {
      newValue.add(tryCast(literal, type));
    }
  }
  if (!newValue.isEmpty()) {
    return new TypedOperand(newValue, type);
  } 
  throw new ODataApplicationException("Cast failed", HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
      Locale.ROOT);
}
 
Example #25
Source File: EdmDouble.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isCompatible(final EdmPrimitiveType primitiveType) {
  return primitiveType instanceof EdmByte
      || primitiveType instanceof EdmSByte
      || primitiveType instanceof EdmInt16
      || primitiveType instanceof EdmInt32
      || primitiveType instanceof EdmInt64
      || primitiveType instanceof EdmSingle
      || primitiveType instanceof EdmDouble;
}
 
Example #26
Source File: EdmTypeConvertor.java    From syndesis with Apache License 2.0 5 votes vote down vote up
public PropertyMetadata visit(EdmPrimitiveType type) {
    switch (type.getName()) {
        case "Boolean":
            return property(Boolean.class);
        case "Byte":
        case "SByte":
        case "Int16":
        case "Int32":
        case "Int64":
        case "Single":
        case "Double":
        case "Decimal":
            return property(Number.class);
        case "String":
        case "Binary":
        case "Date":
        case "DateTimeOffset":
        case "Duration":
        case "Guid":
        case "TimeOfDay":
            return property(String.class);
        default:
            if (LOG.isWarnEnabled()) {
                LOG.warn("The primitive edm type '{}' is not supported. Returning as string", type.getName());
            }
            return property(String.class);
    }
}
 
Example #27
Source File: PropertyResponse.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void writePrimitiveProperty(EdmPrimitiveType type, Property property)
    throws SerializerException {
  if(this.collection) {
    this.response.setContent(
        this.serializer.primitiveCollection(metadata, type, property, this.primitiveOptions).getContent());
  } else {
    this.response.setContent(
        this.serializer.primitive(metadata, type, property, this.primitiveOptions).getContent());
  }
  writeOK(responseContentType);
  close();
}
 
Example #28
Source File: EdmInt64.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isCompatible(final EdmPrimitiveType primitiveType) {
  return primitiveType instanceof EdmByte
      || primitiveType instanceof EdmSByte
      || primitiveType instanceof EdmInt16
      || primitiveType instanceof EdmInt32
      || primitiveType instanceof EdmInt64;
}
 
Example #29
Source File: JsonDeltaSerializerWithNavigations.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void writePropertyValue(final ServiceMetadata metadata, final EdmProperty edmProperty,
    final Property property, final Set<List<String>> selectedPaths, final JsonGenerator json)
    throws IOException, SerializerException {
  final EdmType type = edmProperty.getType();
  try {
    if (edmProperty.isPrimitive()
        || type.getKind() == EdmTypeKind.ENUM || type.getKind() == EdmTypeKind.DEFINITION) {
      if (edmProperty.isCollection()) {
        writePrimitiveCollection((EdmPrimitiveType) type, property,
            edmProperty.isNullable(), edmProperty.getMaxLength(),
            edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(), json);
      } else {
        writePrimitive((EdmPrimitiveType) type, property,
            edmProperty.isNullable(), edmProperty.getMaxLength(),
            edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode(), json);
      }
    } else if (property.isComplex()) {
      if (edmProperty.isCollection()) {
        writeComplexCollection(metadata, (EdmComplexType) type, property, selectedPaths, json);
      } else {
        writeComplex(metadata, (EdmComplexType) type, property, selectedPaths, json);
      }
    } else {
      throw new SerializerException("Property type not yet supported!",
          SerializerException.MessageKeys.UNSUPPORTED_PROPERTY_TYPE, edmProperty.getName());
    }
  } catch (final EdmPrimitiveTypeException e) {
    throw new SerializerException("Wrong value for property!", e,
        SerializerException.MessageKeys.WRONG_PROPERTY_VALUE,
        edmProperty.getName(), property.getValue().toString());
  }
}
 
Example #30
Source File: PrimitiveTypeBaseTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void expectErrorInValueOfString(final EdmPrimitiveType instance,
    final String value, final Boolean isNullable, final Integer maxLength, final Integer precision,
    final Integer scale, final Boolean isUnicode, final Class<?> returnType,
    final String message) {

  try {
    instance.valueOfString(value, isNullable, maxLength, precision, scale, isUnicode, returnType);
    fail("Expected exception not thrown");
  } catch (final EdmPrimitiveTypeException e) {
    assertNotNull(e.getLocalizedMessage());
    assertThat(e.getLocalizedMessage(), containsString(message));
  }
}