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

The following examples show how to use org.apache.olingo.commons.api.edm.EdmProperty. 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: MetadataTest.java    From olingo-odata4 with Apache License 2.0 7 votes vote down vote up
@Test
public void readAnnotationOnComplexType() {
  final Edm edm = fetchEdm();
  assertNotNull(edm);
  EdmComplexType complexType = edm.getComplexTypeWithAnnotations(
      new FullQualifiedName("SEPMRA_SO_MAN2", "CTPrim"));
  assertEquals(2, complexType.getAnnotations().size());
  assertEquals("HeaderInfo", complexType.getAnnotations().get(0).getTerm().getName());
  // Annotations on complex type property
  EdmProperty complexTypeProp = (EdmProperty) complexType.getProperty("PropertyInt16");
  assertEquals(1, complexTypeProp.getAnnotations().size());
  assertEquals("HeaderInfo", complexTypeProp.getAnnotations().get(0).getTerm().getName());
  // Annotations on complex type navigation property
  EdmNavigationProperty complexTypeNavProp = complexType.getNavigationProperty(
      "NavPropertyDraftAdministrativeDataType");
  assertEquals(1, complexTypeNavProp.getAnnotations().size());
  assertEquals("HeaderInfo", complexTypeNavProp.getAnnotations().get(0).getTerm().getName());
  
  FullQualifiedName termName = new FullQualifiedName("Integration", "Extractable");
  EdmTerm term = edm.getTerm(termName);
  EdmAnnotation annotation = complexType.getAnnotation(term, null);
  assertNotNull(annotation);
}
 
Example #2
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 7 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("{"
      + "\"@context\":\"../$metadata#ESAllPrim(32767)/PropertyDecimal\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":\"34\"}",
      resultString);
}
 
Example #3
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 #4
Source File: ExpandSelectMock.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private static UriInfoResource mockResourceMultiLevelOnDerivedComplexTypes(final EdmEntitySet edmEntitySet, 
    final String pathSegmentBeforeCast,
    final String name, final EdmType derivedType, final String pathSegmentAfterCast) {
  EdmStructuredType type = edmEntitySet.getEntityType();
  List<UriResource> elements = new ArrayList<UriResource>();
  final EdmElement edmElement = type.getProperty(name);
  final EdmProperty property = (EdmProperty) edmElement;
  UriResourceComplexProperty element = Mockito.mock(UriResourceComplexProperty.class);
  Mockito.when(element.getProperty()).thenReturn(property);
  elements.add(element);
  
  if (pathSegmentBeforeCast != null) {
    mockComplexPropertyWithTypeFilter(pathSegmentBeforeCast, (EdmComplexType) derivedType, 
        (EdmStructuredType) edmElement.getType(), elements);
  }
  
  mockPropertyOnDerivedType(derivedType, pathSegmentAfterCast, elements);
  
  UriInfoResource resource = Mockito.mock(UriInfoResource.class);
  Mockito.when(resource.getUriResourceParts()).thenReturn(elements);
  return resource;
}
 
Example #5
Source File: TripPinHandler.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Override
public void upsertStreamProperty(DataRequest request, String entityETag, InputStream streamContent,
    NoContentResponse response) throws ODataLibraryException, ODataApplicationException {
  final EdmEntitySet edmEntitySet = request.getEntitySet();
  List<UriParameter> keys = request.getKeyPredicates();
  Entity entity = this.dataModel.getEntity(edmEntitySet.getName(), keys);

  EdmProperty property = request.getUriResourceProperty().getProperty();

  if (streamContent == null) {
    boolean deleted = this.dataModel.deleteStream(entity, property);
    if (deleted) {
      response.writeNoContent();
    } else {
      response.writeNotFound();
    }
  } else {
    boolean updated = this.dataModel.updateStream(entity, property, streamContent);
    if (updated) {
      response.writeNoContent();
    } else {
      response.writeServerError(true);
    }
  }
}
 
Example #6
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 #7
Source File: ExpressionVisitorImpl.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
@Override
public VisitorOperand visitMember(Member member) throws ExpressionVisitException, ODataApplicationException {
    final List<UriResource> uriResourceParts = member.getResourcePath().getUriResourceParts();
    int size = uriResourceParts.size();
    if (uriResourceParts.get(0) instanceof UriResourceProperty) {
        EdmProperty currentEdmProperty = ((UriResourceProperty) uriResourceParts.get(0)).getProperty();
        Property currentProperty = entity.getProperty(currentEdmProperty.getName());
        return new TypedOperand(currentProperty.getValue(), currentEdmProperty.getType(), currentEdmProperty);
    } else if (uriResourceParts.get(size - 1) instanceof UriResourceLambdaAll) {
        return throwNotImplemented();
    } else if (uriResourceParts.get(size - 1) instanceof UriResourceLambdaAny) {
        return throwNotImplemented();
    } else {
        return throwNotImplemented();
    }
}
 
Example #8
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 #9
Source File: ODataXmlSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void complexProperty() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixPrimCollComp");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("PropertyComp");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty("PropertyComp");

  final String resultString = IOUtils.toString(serializer
      .complex(metadata, (EdmComplexType) edmProperty.getType(), property,
          ComplexSerializerOptions.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\" "
      + "xmlns:d=\"http://docs.oasis-open.org/odata/ns/data\" "
      + "m:type=\"#olingo.odata.test1.CTTwoPrim\" "
      + "m:context=\"../$metadata#ESMixPrimCollComp(32767)/PropertyComp\" "
      + "m:metadata-etag=\"metadataETag\">"
      + "<d:PropertyInt16 m:type=\"Int16\">111</d:PropertyInt16>"
      + "<d:PropertyString>TEST A</d:PropertyString>"
      + "</m:value>";
  checkXMLEqual(expected, resultString);
}
 
Example #10
Source File: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void complexCollectionPropertyWithSelectNoMetadata() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESKeyNav");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("CollPropertyComp");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty(edmProperty.getName());
  
  final EdmComplexType complexType = metadata.getEdm().getComplexType(
      new FullQualifiedName("olingo.odata.test1", "CTPrimComp"));
  final EdmProperty propertyWithinCT = (EdmProperty) complexType.getProperty("PropertyInt16"); 
  
  final UriInfoResource resource = ExpandSelectMock.mockComplexTypeResource(propertyWithinCT);
  final SelectItem selectItem = ExpandSelectMock.mockSelectItemForColComplexProperty(resource);
  final SelectOption selectOption = ExpandSelectMock.mockSelectOption(Arrays.asList(selectItem));
  
  final String resultString = IOUtils.toString(serializerNoMetadata
      .complexCollection(metadata, (EdmComplexType) edmProperty.getType(), property, ComplexSerializerOptions.with()
          .contextURL(ContextURL.with()
              .entitySet(edmEntitySet).keyPath("1")
              .navOrPropertyPath("CollPropertyComp")
              .build()).select(selectOption).build()).getContent());
  Assert.assertEquals("{\"value\":[{\"PropertyInt16\":1},{\"PropertyInt16\":2},{\"PropertyInt16\":3}]}",
      resultString);
}
 
Example #11
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 #12
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 #13
Source File: ContextURLHelperTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void buildCompoundKey() throws Exception {
  final EdmEntitySet entitySet = entityContainer.getEntitySet("ESTwoKeyNav");
  final EdmProperty edmProperty = entitySet.getEntityType().getStructuralProperty("PropertyInt16");
  UriParameter key1 = Mockito.mock(UriParameter.class);
  Mockito.when(key1.getName()).thenReturn(edmProperty.getName());
  Mockito.when(key1.getText()).thenReturn("1");
  UriParameter key2 = Mockito.mock(UriParameter.class);
  Mockito.when(key2.getName()).thenReturn("PropertyString");
  Mockito.when(key2.getText()).thenReturn("'2'");
  final ContextURL contextURL = ContextURL.with().entitySet(entitySet)
      .keyPath(ContextURLHelper.buildKeyPredicate(Arrays.asList(key1, key2)))
      .navOrPropertyPath(edmProperty.getName()).build();
  assertEquals("../$metadata#ESTwoKeyNav(PropertyInt16=1,PropertyString='2')/PropertyInt16",
      ContextURLBuilder.create(contextURL).toASCIIString());
}
 
Example #14
Source File: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void 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 #15
Source File: ODataJsonSerializerTest.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("{"
      + "\"@odata.context\":\"../$metadata#ESAllPrim(32767)/PropertyInt64\","
      + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":\"" + Long.MAX_VALUE + "\"}",
      resultString);
}
 
Example #16
Source File: EdmAssistedJsonSerializer.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private void collection(final JsonGenerator json, final EdmType itemType, final String typeName,
    final EdmProperty edmProperty, final ValueType valueType, final List<?> value)
    throws IOException, SerializerException {

  json.writeStartArray();

  for (final Object item : value) {
    switch (valueType) {
    case COLLECTION_PRIMITIVE:
      primitiveValue(json, (EdmPrimitiveType) itemType, typeName, edmProperty, item);
      break;

    case COLLECTION_GEOSPATIAL:
    case COLLECTION_ENUM:
      throw new SerializerException("Geo and enum types are not supported.", MessageKeys.NOT_IMPLEMENTED);

    case COLLECTION_COMPLEX:
      complexValue(json, (EdmComplexType) itemType, typeName, (ComplexValue) item);
      break;

    default:
    }
  }

  json.writeEndArray();
}
 
Example #17
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void complexProperty() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixPrimCollComp");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("PropertyComp");
  final Property property = data.readAll(edmEntitySet).getEntities().get(0).getProperty("PropertyComp");

  final String resultString = IOUtils.toString(serializer
      .complex(metadata, (EdmComplexType) edmProperty.getType(), property,
          ComplexSerializerOptions.with()
              .contextURL(ContextURL.with()
                  .entitySet(edmEntitySet).keyPath("32767").navOrPropertyPath(edmProperty.getName())
                  .build())
              .build()).getContent());
  Assert.assertEquals("{"
      + "\"@context\":\"../$metadata#ESMixPrimCollComp(32767)/PropertyComp\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"PropertyInt16\":111,\"PropertyString\":\"TEST A\"}",
      resultString);
}
 
Example #18
Source File: ODataJsonSerializerv01Test.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("{"
      + "\"@context\":\"../$metadata#ESCollAllPrim(1)/CollPropertyString\","
      + "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":[\"[email protected]\",\"[email protected]\",\"[email protected]\"]}",
      resultString);
}
 
Example #19
Source File: EdmMappingTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void getDataClassForPrimTypeViaMapping() {
  CsdlMapping mapping = new CsdlMapping().setMappedJavaClass(Date.class);
  CsdlProperty property = new CsdlProperty()
      .setType(EdmPrimitiveTypeKind.DateTimeOffset.getFullQualifiedName())
      .setMapping(mapping);
  EdmProperty edmProperty = new EdmPropertyImpl(null, property);

  assertNotNull(edmProperty.getMapping());
  assertEquals(Date.class, edmProperty.getMapping().getMappedJavaClass());

  CsdlParameter parameter = new CsdlParameter()
      .setType(EdmPrimitiveTypeKind.DateTimeOffset.getFullQualifiedName())
      .setMapping(mapping);
  EdmParameter edmParameter = new EdmParameterImpl(null, parameter);

  assertNotNull(edmParameter.getMapping());
  assertEquals(Date.class, edmParameter.getMapping().getMappedJavaClass());
}
 
Example #20
Source File: ODataJsonSerializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitiveCollectionPropertyWithMetadataFull() 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(serializerFullMetadata
              .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\\\"\"," +
                  "\"@odata.type\":\"#Collection(String)\",\"value\":[\"[email protected]\"," +
                  "\"[email protected]\",\"[email protected]\"]}", resultString);
}
 
Example #21
Source File: ODataXmlDeserializerTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void complexProperty() throws Exception {
  final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESMixPrimCollComp");
  final EdmProperty edmProperty = (EdmProperty) edmEntitySet.getEntityType().getProperty("PropertyComp");
  final String payload = "<data:PropertyComp xmlns:data=\"http://docs.oasis-open.org/odata/ns/data\" "
      + " xmlns:metadata=\"http://docs.oasis-open.org/odata/ns/metadata\"\n"
      + " metadata:type=\"#olingo.odata.test1.CTTwoPrim\">\n"
      + "  <data:PropertyInt16>123</data:PropertyInt16>\n" 
      + "  <data:PropertyString metadata:null=\"true\"/>\n"
      + "</data:PropertyComp>";

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

  Assert.assertEquals("PropertyComp", result.getName());
  Assert.assertTrue(result.isComplex());
  final ComplexValue cv = result.asComplex();
  Assert.assertEquals("olingo.odata.test1.CTTwoPrim", result.getType());
  Assert.assertEquals((short) 123, getCVProperty(cv, "PropertyInt16").asPrimitive());
  Assert.assertTrue(getCVProperty(cv, "PropertyString").isNull());    
}
 
Example #22
Source File: ODataJsonSerializerv01Test.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void primitiveCollectionPropertyWithMetadataFull() 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(serializerFullMetadata
              .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)/CollPropertyString\"," +
                  "\"@metadataEtag\":\"W/\\\"metadataETag\\\"\"," +
                  "\"@type\":\"#Collection(String)\",\"value\":[\"[email protected]\"," +
                  "\"[email protected]\",\"[email protected]\"]}", resultString);
}
 
Example #23
Source File: UriResourceImplTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void uriResourceComplexPropertyImpl() {
  EdmEntityType entityType = edm.getEntityType(EntityTypeProvider.nameETKeyNav);
  EdmProperty property = (EdmProperty) entityType.getProperty("PropertyCompNav");
  UriResourceComplexPropertyImpl impl = new UriResourceComplexPropertyImpl(property);
  assertEquals(UriResourceKind.complexProperty, impl.getKind());
  assertEquals(property, impl.getProperty());
  assertEquals(property.getName(), impl.toString());
  assertFalse(impl.isCollection());
  assertEquals(property.getType(), impl.getType());
  assertEquals(property.getType(), impl.getComplexType());
  impl.getComplexType();

  EdmComplexType complexTypeImplType = edm.getComplexType(ComplexTypeProvider.nameCTBasePrimCompNav);

  impl.setTypeFilter(complexTypeImplType);
  assertEquals(complexTypeImplType, impl.getTypeFilter());
  assertEquals(complexTypeImplType, impl.getComplexTypeFilter());
  impl.getComplexTypeFilter();

}
 
Example #24
Source File: DebugTabUriTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void select() throws Exception {
  EdmProperty edmProperty = mock(EdmProperty.class);
  when(edmProperty.getName()).thenReturn("property");
  final DebugTabUri tab = new DebugTabUri(new UriInfoImpl().setKind(UriInfoKind.all)
      .setSystemQueryOption(new SelectOptionImpl().setSelectItems(Arrays.asList(
          (SelectItem) new SelectItemImpl().setStar(true),
          new SelectItemImpl().setResourcePath(
              new UriInfoImpl().setKind(UriInfoKind.resource)
                  .addResourcePart(new UriResourcePrimitivePropertyImpl(edmProperty)))))));
  assertEquals("{\"kind\":\"all\",\"select\":[\"*\",\"property\"]}", createJson(tab));

  assertEquals("<h2>Kind</h2>\n"
      + "<p>all</p>\n"
      + "<h2>Selected Properties</h2>\n"
      + "<ul>\n"
      + "<li>*</li>\n"
      + "<li>property</li>\n"
      + "</ul>\n",
      createHtml(tab));
}
 
Example #25
Source File: ODataBinderImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
private EdmTypeInfo buildTypeInfo(final ContextURL contextURL, final String metadataETag,
    final String propertyName, final String propertyType) {

  FullQualifiedName typeName = null;
  final EdmType type = findType(null, contextURL, metadataETag);
  if (type instanceof EdmStructuredType) {
    final EdmProperty edmProperty = ((EdmStructuredType) type).getStructuralProperty(propertyName);
    if (edmProperty != null) {
      typeName = edmProperty.getType().getFullQualifiedName();
    }
  }
  if (typeName == null && type != null) {
    typeName = type.getFullQualifiedName();
  }

  return buildTypeInfo(typeName, propertyType);
}
 
Example #26
Source File: MetadataTest.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
@Test
public void readPropertyAnnotationsTest() {
  List<InputStream> streams = new ArrayList<InputStream>();
  streams.add(getClass().getResourceAsStream("VOC_Core.xml"));
  final Edm edm = client.getReader().readMetadata(getClass().getResourceAsStream("edmxWithCsdlAnnotationPath.xml"),
      streams);
  assertNotNull(edm);
  
  final EdmEntityType person = edm.getEntityType(
      new FullQualifiedName("Microsoft.Exchange.Services.OData.Model", "Person"));
  assertNotNull(person);
  EdmProperty userName = (EdmProperty) person.getProperty("UserName");
  List<EdmAnnotation> userNameAnnotations = userName.getAnnotations();
  for (EdmAnnotation annotation : userNameAnnotations) {
    EdmTerm term = annotation.getTerm();
    assertNotNull(term);
    assertEquals("Permissions", term.getName());
    assertEquals("Org.OData.Core.V1.Permissions",
        term.getFullQualifiedName().getFullQualifiedNameAsString());
    EdmExpression expression = annotation.getExpression();
    assertNotNull(expression);
    assertTrue(expression.isDynamic());
    assertEquals("AnnotationPath", expression.asDynamic().getExpressionName());
  }
}
 
Example #27
Source File: ODataJsonSerializerTest.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("{"
      + "\"@odata.context\":\"../$metadata#ESCollAllPrim(1)/CollPropertyInt64\","
      + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\","
      + "\"value\":[\"929292929292\",\"333333333333\",\"444444444444\"]}",
      resultString);
}
 
Example #28
Source File: ExpressionParserTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertyPathExp() throws Exception {
  final String entitySetName = "ESName";
  final String keyPropertyName = "a";
  EdmProperty keyProperty = mockProperty(keyPropertyName,
      OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.String));
  EdmKeyPropertyRef keyPropertyRef = mockKeyPropertyRef(keyPropertyName, keyProperty);
  EdmEntityType entityType = mockEntityType(keyPropertyName, keyPropertyRef);
  Mockito.when(entityType.getPropertyNames()).thenReturn(Collections.singletonList(keyPropertyName));
  Mockito.when(entityType.getProperty(keyPropertyName)).thenReturn(keyProperty);
  EdmEntitySet entitySet = mockEntitySet(entitySetName, entityType);
  EdmEntityContainer container = mockContainer(entitySetName, entitySet);
  Edm mockedEdm = Mockito.mock(Edm.class);
  Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container);
  
  UriTokenizer tokenizer = new UriTokenizer("a eq \'abc\'");
  Expression expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, 
      entityType, null, null);
  assertNotNull(expression);
  assertEquals("{[a] EQ \'abc\'}", expression.toString());
  
  tokenizer = new UriTokenizer("a in (\'abc\', \'xyz\')");
  expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, 
      entityType, null, null);
  assertNotNull(expression);
  assertEquals("{[a] IN [\'abc\', \'xyz\']}", expression.toString());
  try {
    tokenizer = new UriTokenizer("a in (\'abc\', 10)");
    expression = new ExpressionParser(mockedEdm, odata).parse(tokenizer, 
        entityType, null, null);
  } catch (UriParserSemanticException e) {
    assertEquals("Incompatible types.", e.getMessage());
  }
}
 
Example #29
Source File: UriHelperImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Override
public String buildKeyPredicate(final EdmEntityType edmEntityType, final Entity entity) throws SerializerException {
  StringBuilder result = new StringBuilder();
  final List<String> keyNames = edmEntityType.getKeyPredicateNames();
  boolean first = true;
  for (final String keyName : keyNames) {
    EdmKeyPropertyRef refType = edmEntityType.getKeyPropertyRef(keyName);
    if (first) {
      first = false;
    } else {
      result.append(',');
    }
    if (keyNames.size() > 1) {
      result.append(Encoder.encode(keyName)).append('=');
    }
    final EdmProperty edmProperty =  refType.getProperty();
    if (edmProperty == null) {
      throw new SerializerException("Property not found (possibly an alias): " + keyName,
          SerializerException.MessageKeys.MISSING_PROPERTY, keyName);
    }
    final EdmPrimitiveType type = (EdmPrimitiveType) edmProperty.getType();
    final Object propertyValue = findPropertyRefValue(entity, refType);
    try {
      final String value = type.toUriLiteral(
          type.valueToString(propertyValue,
              edmProperty.isNullable(), edmProperty.getMaxLength(),
              edmProperty.getPrecision(), edmProperty.getScale(), edmProperty.isUnicode()));
      result.append(Encoder.encode(value));
    } catch (final EdmPrimitiveTypeException e) {
      throw new SerializerException("Wrong key value!", e,
          SerializerException.MessageKeys.WRONG_PROPERTY_VALUE, edmProperty.getName(), 
          propertyValue != null ? propertyValue.toString(): null);
    }
  }
  return result.toString();
}
 
Example #30
Source File: EdmKeyPropertyRefImplTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test(expected = EdmException.class)
public void aliasForPropertyInComplexPropertyButWrongPath2() {
  CsdlPropertyRef providerRef = new CsdlPropertyRef().setName("wrong/Id").setAlias("alias");
  EdmEntityType etMock = mock(EdmEntityType.class);
  EdmProperty keyPropertyMock = mock(EdmProperty.class);
  EdmElement compMock = mock(EdmProperty.class);
  EdmComplexType compTypeMock = mock(EdmComplexType.class);
  when(compTypeMock.getProperty("Id")).thenReturn(keyPropertyMock);
  when(compMock.getType()).thenReturn(compTypeMock);
  when(etMock.getProperty("comp")).thenReturn(compMock);
  new EdmKeyPropertyRefImpl(etMock, providerRef).getProperty();
}