org.apache.olingo.odata2.api.edm.EdmTyped Java Examples

The following examples show how to use org.apache.olingo.odata2.api.edm.EdmTyped. 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: XmlPropertyProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeEmployeeId() throws Exception {
  AtomEntityProvider s = createAtomEntityProvider();
  EdmTyped edmTyped = MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("EmployeeId");
  EdmProperty edmProperty = (EdmProperty) edmTyped;

  ODataResponse response = s.writeProperty(edmProperty, employeeData.get("EmployeeId"));
  assertNotNull(response);
  assertNotNull(response.getEntity());

  String xml = StringHelper.inputStreamToString((InputStream) response.getEntity());
  assertNotNull(xml);

  assertXpathExists("/d:EmployeeId", xml);
  assertXpathEvaluatesTo("1", "/d:EmployeeId/text()", xml);
}
 
Example #2
Source File: ODataExpressionParser.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private static String getPropertyName(final CommonExpression whereExpression) throws EdmException,
    ODataJPARuntimeException {
  EdmTyped edmProperty = ((PropertyExpression) whereExpression).getEdmProperty();
  EdmMapping mapping;
  if (edmProperty instanceof EdmNavigationProperty) {
    EdmNavigationProperty edmNavigationProperty = (EdmNavigationProperty) edmProperty;
    mapping = edmNavigationProperty.getMapping();
  } else if(edmProperty instanceof EdmProperty) {
    EdmProperty property = (EdmProperty) edmProperty;
    mapping = property.getMapping();
  } else {
    throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.GENERAL, null);
  }

  return mapping != null ? mapping.getInternalName() : edmProperty.getName();
}
 
Example #3
Source File: JPAProcessorImplTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private EdmTyped getEdmTypedMockedObj(final String propertyName) {
  EdmProperty mockedEdmProperty = EasyMock.createMock(EdmProperty.class);
  try {
    EasyMock.expect(mockedEdmProperty.getMapping()).andStubReturn(getEdmMappingMockedObj(propertyName));
    EdmType edmType = EasyMock.createMock(EdmType.class);
    EasyMock.expect(edmType.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
    EasyMock.replay(edmType);
    EasyMock.expect(mockedEdmProperty.getName()).andStubReturn("identifier");
    EasyMock.expect(mockedEdmProperty.getType()).andStubReturn(edmType);
    EasyMock.expect(mockedEdmProperty.getFacets()).andStubReturn(getEdmFacetsMockedObj());

    EasyMock.replay(mockedEdmProperty);
  } catch (EdmException e) {
    fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
  }
  return mockedEdmProperty;
}
 
Example #4
Source File: EdmMockUtil.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private static EdmTyped mockEdmPropertyOfSource2() {
  EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
  EdmType type = EasyMock.createMock(EdmType.class);
  EasyMock.expect(type.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
  EasyMock.replay(type);
  EdmMapping mapping = EasyMock.createMock(EdmMapping.class);
  EasyMock.expect(mapping.getInternalName()).andStubReturn("description");
  EasyMock.replay(mapping);
  try {
    EasyMock.expect(edmProperty.getName()).andStubReturn("description");
    EasyMock.expect(edmProperty.getType()).andStubReturn(type);
    EasyMock.expect(edmProperty.getMapping()).andStubReturn(mapping);
  } catch (EdmException e) {
    fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
  }
  EasyMock.replay(edmProperty);
  return edmProperty;
}
 
Example #5
Source File: CollectionSQLVisitor.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public Object visitProperty (PropertyExpression property_expression,
   String uri_literal, EdmTyped edm_property)
{
   if (edm_property == null)
      throw new IllegalArgumentException ("Property not found: " +
            uri_literal);

   if (uri_literal.equals (CollectionEntitySet.NAME))
      return new Member("name");

   if (uri_literal.equals (CollectionEntitySet.DESCRIPTION))
      return new Member ("description");

   throw new IllegalArgumentException ("Property not supported: " +
         uri_literal);
}
 
Example #6
Source File: ODataJPADefaultProcessorTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private EdmTyped getEdmTypedMockedObj(final String propertyName) {
  EdmProperty mockedEdmProperty = EasyMock.createMock(EdmProperty.class);
  try {
    EasyMock.expect(mockedEdmProperty.getMapping()).andStubReturn((EdmMapping) getEdmMappingMockedObj(propertyName));
    EdmType edmType = EasyMock.createMock(EdmType.class);
    EasyMock.expect(edmType.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
    EasyMock.replay(edmType);
    EasyMock.expect(mockedEdmProperty.getName()).andStubReturn("identifier");
    EasyMock.expect(mockedEdmProperty.getType()).andStubReturn(edmType);
    EasyMock.expect(mockedEdmProperty.getFacets()).andStubReturn(getEdmFacetsMockedObj());

    EasyMock.replay(mockedEdmProperty);
  } catch (EdmException e) {
    fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
  }
  return mockedEdmProperty;
}
 
Example #7
Source File: EdmEntityTypeImplProv.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public List<EdmProperty> getKeyProperties() throws EdmException {
  if (edmKeyProperties == null) {
    if (edmBaseType != null) {
      return ((EdmEntityType) edmBaseType).getKeyProperties();
    }

    if (keyProperties == null) {
      keyProperties = new HashMap<String, EdmProperty>();
      edmKeyProperties = new ArrayList<EdmProperty>();

      for (String keyPropertyName : getKeyPropertyNames()) {
        final EdmTyped edmProperty = getProperty(keyPropertyName);
        if (edmProperty != null && edmProperty instanceof EdmProperty) {
          keyProperties.put(keyPropertyName, (EdmProperty) edmProperty);
          edmKeyProperties.add((EdmProperty) edmProperty);
        } else {
          throw new EdmException(EdmException.COMMON);
        }
      }
    }
  }

  return edmKeyProperties;
}
 
Example #8
Source File: AtomEntryProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeWithoutFacetsValidation() throws Exception {
  Edm edm = MockFacade.getMockEdm();
  EdmTyped roomNameProperty = edm.getEntityType("RefScenario", "Room").getProperty("Name");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getMaxLength()).thenReturn(3);
  when(((EdmProperty) roomNameProperty).getFacets()).thenReturn(facets);

  String name = "1234567";
  roomData.put("Name", name);
  AtomEntityProvider ser = createAtomEntityProvider();
  EntityProviderWriteProperties properties = EntityProviderWriteProperties
      .fromProperties(DEFAULT_PROPERTIES).validatingFacets(false).build();
  ODataResponse response =
      ser.writeEntry(edm.getDefaultEntityContainer().getEntitySet("Rooms"), roomData, properties);
  assertNotNull(response);


  assertNotNull(response.getEntity());
  String xmlString = StringHelper.inputStreamToString((InputStream) response.getEntity());

  assertXpathEvaluatesTo(name, "/a:entry/a:content/m:properties/d:Name/text()", xmlString);
}
 
Example #9
Source File: AtomEntryProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeETagEncoding() throws IOException, XpathException, SAXException, XMLStreamException,
    FactoryConfigurationError, ODataException {
  Edm edm = MockFacade.getMockEdm();
  EdmTyped roomIdProperty = edm.getEntityType("RefScenario", "Room").getProperty("Id");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getConcurrencyMode()).thenReturn(EdmConcurrencyMode.Fixed);
  when(facets.getMaxLength()).thenReturn(3);
  when(((EdmProperty) roomIdProperty).getFacets()).thenReturn(facets);

  roomData.put("Id", "<\">");
  AtomEntityProvider ser = createAtomEntityProvider();
  ODataResponse response =
      ser.writeEntry(edm.getDefaultEntityContainer().getEntitySet("Rooms"), roomData, DEFAULT_PROPERTIES);

  assertNotNull(response);
  assertNotNull(response.getEntity());
  assertNull("EntityProvider should not set content header", response.getContentHeader());
  assertEquals("W/\"<\">.3\"", response.getETag());

  String xmlString = StringHelper.inputStreamToString((InputStream) response.getEntity());

  assertXpathExists("/a:entry", xmlString);
  assertXpathExists("/a:entry/@m:etag", xmlString);
  assertXpathEvaluatesTo("W/\"<\">.3\"", "/a:entry/@m:etag", xmlString);
}
 
Example #10
Source File: AtomEntryProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithoutCompositeKeyWithOneKeyNull() throws Exception {
  Edm edm = MockFacade.getMockEdm();
  EdmEntitySet entitySet = edm.getEntityContainer("Container2").getEntitySet("Photos");
  
  final EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(BASE_URI).build();

  Map<String, Object> photoData = new HashMap<String, Object>();
  photoData.put("Name", "Mona Lisa");
  photoData.put("Id", Integer.valueOf(1));
  
  EdmTyped typeProperty = edm.getEntityContainer("Container2").getEntitySet("Photos").
      getEntityType().getProperty("Type");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getConcurrencyMode()).thenReturn(EdmConcurrencyMode.Fixed);
  when(facets.getMaxLength()).thenReturn(3);
  when(((EdmProperty) typeProperty).getFacets()).thenReturn(facets);

  AtomEntityProvider ser = createAtomEntityProvider();
  try {
  ser.writeEntry(entitySet, photoData, properties);
  } catch (EntityProviderProducerException e) {
    assertTrue(e.getMessage().contains("The metadata do not allow a null value for property 'Type'"));
  }
}
 
Example #11
Source File: XmlPropertyProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeImageUrl() throws Exception {
  AtomEntityProvider s = createAtomEntityProvider();

  EdmTyped edmTyped = MockFacade.getMockEdm().getEntityType("RefScenario", "Employee").getProperty("ImageUrl");
  EdmProperty edmProperty = (EdmProperty) edmTyped;

  ODataResponse response = s.writeProperty(edmProperty, employeeData.get("ImageUrl"));
  assertNotNull(response);
  assertNotNull(response.getEntity());

  String xml = StringHelper.inputStreamToString((InputStream) response.getEntity());
  assertNotNull(xml);

  assertXpathExists("/d:ImageUrl", xml);
  assertXpathExists("/d:ImageUrl/@m:null", xml);
  assertXpathEvaluatesTo("true", "/d:ImageUrl/@m:null", xml);
}
 
Example #12
Source File: JsonEntryEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeWithoutFacetsValidation() throws Exception {
  Edm edm = MockFacade.getMockEdm();
  EdmTyped roomNameProperty = edm.getEntityType("RefScenario", "Room").getProperty("Name");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getMaxLength()).thenReturn(3);
  when(((EdmProperty) roomNameProperty).getFacets()).thenReturn(facets);
  EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Rooms");

  String name = "1234567890";
  Map<String, Object> roomData = new HashMap<String, Object>();
  roomData.put("Id", "4711");
  roomData.put("Name", name);
  EntityProviderWriteProperties properties = EntityProviderWriteProperties
      .fromProperties(DEFAULT_PROPERTIES).validatingFacets(false).build();
  final ODataResponse response = new JsonEntityProvider().writeEntry(entitySet, roomData, properties);
  final String json = verifyResponse(response);
  assertNotNull(response);
  assertEquals("{\"d\":{\"__metadata\":{\"id\":\"http://host:80/service/Rooms('4711')\"," +
      "\"uri\":\"http://host:80/service/Rooms('4711')\",\"type\":\"RefScenario.Room\"}," +
      "\"Id\":\"4711\",\"Name\":\"1234567890\",\"Seats\":null,\"Version\":null," +
      "\"nr_Employees\":{\"__deferred\":{\"uri\":\"http://host:80/service/Rooms('4711')/nr_Employees\"}}," +
      "\"nr_Building\":{\"__deferred\":{\"uri\":\"http://host:80/service/Rooms('4711')/nr_Building\"}}}}",
      json);
}
 
Example #13
Source File: JsonEntryEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithoutCompositeKeyWithOneKeyNull() throws Exception {
  Edm edm = MockFacade.getMockEdm();
  EdmEntitySet entitySet = edm.getEntityContainer("Container2").getEntitySet("Photos");
  
  final EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(URI.create(BASE_URI)).build();

  Map<String, Object> photoData = new HashMap<String, Object>();
  photoData.put("Name", "Mona Lisa");
  photoData.put("Id", Integer.valueOf(1));
  
  EdmTyped typeProperty = edm.getEntityContainer("Container2").getEntitySet("Photos").
      getEntityType().getProperty("Type");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getConcurrencyMode()).thenReturn(EdmConcurrencyMode.Fixed);
  when(facets.getMaxLength()).thenReturn(3);
  when(((EdmProperty) typeProperty).getFacets()).thenReturn(facets);

  JsonEntityProvider ser = new JsonEntityProvider();
  try {
  ser.writeEntry(entitySet, photoData, properties);
  } catch (EntityProviderProducerException e) {
    assertTrue(e.getMessage().contains("The metadata do not allow a null value for property 'Type'"));
  }
}
 
Example #14
Source File: JsonFeedEntitySerializerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void unbalancedPropertyEntryWithoutKeyWithNullInlineFeed() throws Exception {
  Edm edm = MockFacade.getMockEdm();
  EdmTyped imageUrlProperty = edm.getEntityType("RefScenario", "Employee").getProperty("ImageUrl");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getMaxLength()).thenReturn(1);
  when(((EdmProperty) imageUrlProperty).getFacets()).thenReturn(facets);

  Entity roomData = new Entity();
  roomData.addProperty("Name", "Neu Schwanstein");
  roomData.addProperty("Seats", new Integer(20));

  roomData.setWriteProperties(EntitySerializerProperties.serviceRoot(URI.create(BASE_URI))
      .build());
  roomData.addNavigation("nr_Employees", null);

  EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Rooms");
  try {
    new JsonSerializerDeserializer().writeEntry(entitySet, roomData);
  } catch (EntityProviderException e) {
    assertEquals(ERROR_MSG, e.getMessage());
  }
}
 
Example #15
Source File: AtomEntrySerializerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithoutCompositeKeyWithOneKeyNull() throws Exception {
  Edm edm = MockFacade.getMockEdm();
  EdmEntitySet entitySet = edm.getEntityContainer("Container2").getEntitySet("Photos");
  
  Entity photoData = new Entity();
  photoData.addProperty("Name", "Mona Lisa");
  photoData.addProperty("Id", Integer.valueOf(1));
  photoData.setWriteProperties(
      EntitySerializerProperties.serviceRoot(BASE_URI).build());
  
  EdmTyped typeProperty = edm.getEntityContainer("Container2").getEntitySet("Photos").
      getEntityType().getProperty("Type");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getConcurrencyMode()).thenReturn(EdmConcurrencyMode.Fixed);
  when(facets.getMaxLength()).thenReturn(3);
  when(((EdmProperty) typeProperty).getFacets()).thenReturn(facets);

  try {
    createAtomEntityProvider().writeEntry(entitySet, photoData);
  } catch (EntityProviderProducerException e) {
    assertTrue(e.getMessage().contains("The metadata do not allow a null value for property 'Type'"));
  }
}
 
Example #16
Source File: JsonEntryEntitySerializerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test(expected = EdmSimpleTypeException.class)
public void serializeWithFacetsValidation() throws Throwable {
  Edm edm = MockFacade.getMockEdm();
  EdmTyped roomNameProperty = edm.getEntityType("RefScenario", "Room").getProperty("Name");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getMaxLength()).thenReturn(3);
  when(((EdmProperty) roomNameProperty).getFacets()).thenReturn(facets);
  EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Rooms");

  String name = "1234567";
  Entity roomData = new Entity();
  roomData.addProperty("Id", "4711");
  roomData.addProperty("Name", name);
  EntitySerializerProperties properties = EntitySerializerProperties
      .fromProperties(DEFAULT_PROPERTIES).validatingFacets(true).build();
  roomData.setWriteProperties(properties);
  try {
    final ODataResponse response = new JsonSerializerDeserializer().writeEntry(entitySet, roomData);
    final String json = verifyResponse(response);
    assertNotNull(response);
    assertEquals("{\"Id\":\"1\",\"Name\":null,\"isScrumTeam\":true}", json);
  } catch (EntityProviderException e) {
    throw e.getCause();
  }
}
 
Example #17
Source File: JsonEntryEntitySerializerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeWithoutFacetsValidation() throws Exception {
  Edm edm = MockFacade.getMockEdm();
  EdmTyped roomNameProperty = edm.getEntityType("RefScenario", "Room").getProperty("Name");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getMaxLength()).thenReturn(3);
  when(((EdmProperty) roomNameProperty).getFacets()).thenReturn(facets);
  EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Rooms");

  String name = "1234567890";
  Entity roomData = new Entity();
  roomData.addProperty("Id", "4711");
  roomData.addProperty("Name", name);
  EntitySerializerProperties properties = EntitySerializerProperties
      .fromProperties(DEFAULT_PROPERTIES).validatingFacets(false).build();
  roomData.setWriteProperties(properties);
  final ODataResponse response = new JsonSerializerDeserializer().writeEntry(entitySet, roomData);
  final String json = verifyResponse(response);
  assertNotNull(response);
  assertEquals("{\"Id\":\"4711\",\"Name\":\"1234567890\"}", json);
}
 
Example #18
Source File: JsonEntryEntitySerializerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithoutCompositeKeyWithOneKeyNull() throws Exception {
  Edm edm = MockFacade.getMockEdm();
  EdmEntitySet entitySet = edm.getEntityContainer("Container2").getEntitySet("Photos");
  
  Entity photoData = new Entity();
  photoData.addProperty("Name", "Mona Lisa");
  photoData.addProperty("Id", Integer.valueOf(1));
  photoData.setWriteProperties(
      EntitySerializerProperties.serviceRoot(URI.create(BASE_URI)).build());
  
  EdmTyped typeProperty = edm.getEntityContainer("Container2").getEntitySet("Photos").
      getEntityType().getProperty("Type");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getConcurrencyMode()).thenReturn(EdmConcurrencyMode.Fixed);
  when(facets.getMaxLength()).thenReturn(3);
  when(((EdmProperty) typeProperty).getFacets()).thenReturn(facets);

  try {
    new JsonSerializerDeserializer().writeEntry(entitySet, photoData);
  } catch (EntityProviderProducerException e) {
    assertTrue(e.getMessage().contains("The metadata do not allow a null value for property 'Type'"));
  }
}
 
Example #19
Source File: JsonFeedEntitySerializerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void unbalancedPropertyEntryWithEmptyInlineFeed() throws Exception {
  Edm edm = MockFacade.getMockEdm();
  EdmTyped imageUrlProperty = edm.getEntityType("RefScenario", "Employee").getProperty("ImageUrl");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getMaxLength()).thenReturn(1);
  when(((EdmProperty) imageUrlProperty).getFacets()).thenReturn(facets);

  Entity roomData = new Entity();
  roomData.addProperty("Id", "1");
  roomData.addProperty("Name", "Neu Schwanstein");
  roomData.addProperty("Seats", new Integer(20));

  roomData.setWriteProperties(EntitySerializerProperties.serviceRoot(URI.create(BASE_URI))
      .build());
  EntityCollection innerData = new EntityCollection();
  roomData.addNavigation("nr_Employees", innerData);

  EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Rooms");
  ODataResponse response = new JsonSerializerDeserializer().writeEntry(entitySet, roomData);
  assertNotNull(response);
  assertNotNull(response.getEntity());
  final String json = StringHelper.inputStreamToString((InputStream) response.getEntity());
  assertEquals("{\"Id\":\"1\",\"Name\":\"Neu Schwanstein\",\"Seats\":20,\"nr_Employees\":"
      + "[]}", json);
}
 
Example #20
Source File: JsonFeedEntitySerializerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void unbalancedPropertyEntryWithNullInlineFeed() throws Exception {
  Edm edm = MockFacade.getMockEdm();
  EdmTyped imageUrlProperty = edm.getEntityType("RefScenario", "Employee").getProperty("ImageUrl");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getMaxLength()).thenReturn(1);
  when(((EdmProperty) imageUrlProperty).getFacets()).thenReturn(facets);

  Entity roomData = new Entity();
  roomData.addProperty("Id", "1");
  roomData.addProperty("Name", "Neu Schwanstein");
  roomData.addProperty("Seats", new Integer(20));

  roomData.setWriteProperties(EntitySerializerProperties.serviceRoot(URI.create(BASE_URI))
      .build());
  roomData.addNavigation("nr_Employees", null);

  EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Rooms");
  try {
    new JsonSerializerDeserializer().writeEntry(entitySet, roomData);
  } catch (EntityProviderException e) {
    assertEquals(ERROR_MSG, e.getMessage());
  }
}
 
Example #21
Source File: JsonFeedEntitySerializerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test(expected=EntityProviderException.class)
public void unbalancedPropertyEntryWithoutKeyWithEmptyInlineFeedIncludingMetadata() throws Exception {
  Edm edm = MockFacade.getMockEdm();
  EdmTyped imageUrlProperty = edm.getEntityType("RefScenario", "Employee").getProperty("ImageUrl");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getMaxLength()).thenReturn(1);
  when(((EdmProperty) imageUrlProperty).getFacets()).thenReturn(facets);

  Entity roomData = new Entity();
  roomData.addProperty("Name", "Neu Schwanstein");
  roomData.addProperty("Seats", new Integer(20));

  roomData.setWriteProperties(EntitySerializerProperties.serviceRoot(URI.create(BASE_URI))
      .includeMetadata(true).build());
  EntityCollection innerData = new EntityCollection();
  roomData.addNavigation("nr_Employees", innerData);

  EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Rooms");
  new JsonSerializerDeserializer().writeEntry(entitySet, roomData);
}
 
Example #22
Source File: JsonFeedEntitySerializerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void unbalancedPropertyEntryWithInlineFeedWithPropertiesInParent() throws Exception {
  Edm edm = MockFacade.getMockEdm();
  EdmTyped imageUrlProperty = edm.getEntityType("RefScenario", "Employee").getProperty("ImageUrl");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getMaxLength()).thenReturn(1);
  when(((EdmProperty) imageUrlProperty).getFacets()).thenReturn(facets);

  Entity roomData = new Entity();
  roomData.addProperty("Id", "1");
  roomData.addProperty("Name", "Neu Schwanstein");
  roomData.addProperty("Seats", new Integer(20));

  roomData.setWriteProperties(EntitySerializerProperties.serviceRoot(URI.create(BASE_URI))
      .build());
  EntityCollection innerData = new EntityCollection();
  Entity data = new Entity();
  data.addProperty("EmployeeId", "1");
  data.addProperty("EmployeeName", "EmpName1");
  data.addProperty("RoomId", "1");
  innerData.addEntity(data);
  
  data = new Entity();
  data.addProperty("EmployeeId", "1");
  data.addProperty("RoomId", "1");
  innerData.addEntity(data);
  roomData.addNavigation("nr_Employees", innerData);

  EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Rooms");
  final ODataResponse response =
      new JsonSerializerDeserializer().writeEntry(entitySet, roomData);
  assertNotNull(response);
  assertNotNull(response.getEntity());

  final String json = StringHelper.inputStreamToString((InputStream) response.getEntity());
  assertNotNull(json);
  assertEquals("{\"Id\":\"1\",\"Name\":\"Neu Schwanstein\",\"Seats\":20,\"nr_Employees\":"
      + "[{\"EmployeeId\":\"1\",\"EmployeeName\":\"EmpName1\",\"RoomId\":\"1\"},"
      + "{\"EmployeeId\":\"1\",\"RoomId\":\"1\"}]}", json);
}
 
Example #23
Source File: AtomEntrySerializerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void noneSyndicationWithNullUri() throws Exception {
  // prepare Mock
  EdmEntitySet employeesSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");
  EdmCustomizableFeedMappings employeeCustomPropertyMapping = mock(EdmCustomizableFeedMappings.class);
  when(employeeCustomPropertyMapping.isFcKeepInContent()).thenReturn(Boolean.TRUE);
  when(employeeCustomPropertyMapping.getFcNsPrefix()).thenReturn("customPre");
  EdmTyped employeeEntryDateProperty = employeesSet.getEntityType().getProperty("EmployeeName");
  when(((EdmProperty) employeeEntryDateProperty).getCustomizableFeedMappings()).thenReturn(
      employeeCustomPropertyMapping);

  Map<String, String> prefixMap = new HashMap<String, String>();
  prefixMap.put("a", Edm.NAMESPACE_ATOM_2005);
  prefixMap.put("d", Edm.NAMESPACE_D_2007_08);
  prefixMap.put("m", Edm.NAMESPACE_M_2007_08);
  prefixMap.put("xml", Edm.NAMESPACE_XML_1998);
  prefixMap.put("customPre", "http://customUri.com");
  XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(prefixMap));

  AtomSerializerDeserializer ser = createAtomEntityProvider();
  employeeData.setWriteProperties(DEFAULT_PROPERTIES);
  boolean thrown = false;
  try {
    ser.writeEntry(employeesSet, employeeData);
  } catch (EntityProviderException e) {
    verifyRootCause(EntityProviderProducerException.class, EntityProviderException.INVALID_NAMESPACE.getKey(), e);
    thrown = true;
  }
  if (!thrown) {
    fail("Exception should have been thrown");
  }
}
 
Example #24
Source File: JsonEntryEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test(expected = EdmSimpleTypeException.class)
public void serializeWithFacetsValidation() throws Throwable {
  Edm edm = MockFacade.getMockEdm();
  EdmTyped roomNameProperty = edm.getEntityType("RefScenario", "Room").getProperty("Name");
  EdmFacets facets = mock(EdmFacets.class);
  when(facets.getMaxLength()).thenReturn(3);
  when(((EdmProperty) roomNameProperty).getFacets()).thenReturn(facets);
  EdmEntitySet entitySet = edm.getDefaultEntityContainer().getEntitySet("Rooms");

  String name = "1234567";
  Map<String, Object> roomData = new HashMap<String, Object>();
  roomData.put("Id", "4711");
  roomData.put("Name", name);
  EntityProviderWriteProperties properties = EntityProviderWriteProperties
      .fromProperties(DEFAULT_PROPERTIES).validatingFacets(true).build();
  try {
    final ODataResponse response = new JsonEntityProvider().writeEntry(entitySet, roomData, properties);
    final String json = verifyResponse(response);
    assertNotNull(response);
    assertEquals("{\"__metadata\":{\"id\":\"" + BASE_URI + "Teams('1')\","
        + "\"uri\":\"" + BASE_URI + "Teams('1')\",\"type\":\"RefScenario.Team\"},"
        + "\"Id\":\"1\",\"Name\":null,\"isScrumTeam\":true,"
        + "\"nt_Employees\":{\"__deferred\":{\"uri\":\"" + BASE_URI + "Teams('1')/nt_Employees\"}}}",
        json);
  } catch (EntityProviderException e) {
    throw e.getCause();
  }
}
 
Example #25
Source File: XmlPropertyProducerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void serializeLocation() throws Exception {
  AtomEntityProvider s = createAtomEntityProvider();

  EdmEntityType edmEntityType = MockFacade.getMockEdm().getEntityType("RefScenario", "Employee");
  EdmTyped edmTyped = edmEntityType.getProperty("Location");
  EdmProperty edmProperty = (EdmProperty) edmTyped;

  ODataResponse response = s.writeProperty(edmProperty, employeeData.get("Location"));
  assertNotNull(response);
  assertNotNull(response.getEntity());

  String xml = StringHelper.inputStreamToString((InputStream) response.getEntity());
  assertNotNull(xml);

  assertXpathExists("/d:Location", xml);
  assertXpathExists("/d:Location/d:City", xml);
  assertXpathExists("/d:Location/d:City/d:PostalCode", xml);
  assertXpathExists("/d:Location/d:City/d:CityName", xml);
  assertXpathExists("/d:Location/d:Country", xml);

  // verify order of tags
  // first outer tags (city/country)
  XMLUnitHelper.verifyTagOrdering(xml, "City", "Country");
  // then inner tags (postalcode/cityname)
  XMLUnitHelper.verifyTagOrdering(xml, "PostalCode", "CityName");

  assertXpathEvaluatesTo("RefScenario.c_Location", "/d:Location/@m:type", xml);

  assertXpathEvaluatesTo("33470", "/d:Location/d:City/d:PostalCode/text()", xml);
  assertXpathEvaluatesTo("Duckburg", "/d:Location/d:City/d:CityName/text()", xml);
  assertXpathEvaluatesTo("Calisota", "/d:Location/d:Country/text()", xml);
}
 
Example #26
Source File: AtomEntrySerializerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void noneSyndicationKeepInContentTrueMustShowInProperties() throws Exception {
  // prepare Mock
  EdmEntitySet employeesSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");
  EdmCustomizableFeedMappings employeeCustomPropertyMapping = mock(EdmCustomizableFeedMappings.class);
  when(employeeCustomPropertyMapping.isFcKeepInContent()).thenReturn(Boolean.TRUE);
  when(employeeCustomPropertyMapping.getFcNsPrefix()).thenReturn("customPre");
  when(employeeCustomPropertyMapping.getFcNsUri()).thenReturn("http://customUri.com");
  EdmTyped employeeEntryDateProperty = employeesSet.getEntityType().getProperty("EmployeeName");
  when(((EdmProperty) employeeEntryDateProperty).getCustomizableFeedMappings()).thenReturn(
      employeeCustomPropertyMapping);

  Map<String, String> prefixMap = new HashMap<String, String>();
  prefixMap.put("a", Edm.NAMESPACE_ATOM_2005);
  prefixMap.put("d", Edm.NAMESPACE_D_2007_08);
  prefixMap.put("m", Edm.NAMESPACE_M_2007_08);
  prefixMap.put("xml", Edm.NAMESPACE_XML_1998);
  prefixMap.put("customPre", "http://customUri.com");
  XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(prefixMap));
  employeeData.setWriteProperties(DEFAULT_PROPERTIES);

  AtomSerializerDeserializer ser = createAtomEntityProvider();
  ODataResponse response = ser.writeEntry(employeesSet, employeeData);
  String xmlString = verifyResponse(response);

  assertXpathExists("/a:entry/customPre:EmployeeName", xmlString);
  assertXpathExists("/a:entry/m:properties/d:EmployeeName", xmlString);
}
 
Example #27
Source File: XmlPropertyProducerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void serializeRoomIdWithFacets() throws Exception {
  AtomEntityProvider s = createAtomEntityProvider();
  EdmTyped edmTyped = MockFacade.getMockEdm().getEntityType("RefScenario", "Room").getProperty("Id");
  EdmProperty edmProperty = (EdmProperty) edmTyped;

  String id = StringHelper.generateData(1000);
  try {
    ODataResponse response = s.writeProperty(edmProperty, id);
    assertNotNull(response);
  } catch(EntityProviderException e) {
    assertNotNull(e.getCause());
    assertTrue(e.getCause() instanceof EdmSimpleTypeException);
  }
}
 
Example #28
Source File: EdmFunctionImportImplProv.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
public EdmTyped getReturnType() throws EdmException {
  if (edmReturnType == null) {
    final ReturnType returnType = functionImport.getReturnType();
    if (returnType != null) {
      edmReturnType =
          new EdmTypedImplProv(edm, functionImport.getName(), returnType.getTypeName(), returnType.getMultiplicity());
    }
  }
  return edmReturnType;
}
 
Example #29
Source File: JsonPropertyProducerTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void serializeRoomIdWithFacets() throws Exception {
  EdmTyped edmTyped = MockFacade.getMockEdm().getEntityType("RefScenario", "Room").getProperty("Id");
  EdmProperty edmProperty = (EdmProperty) edmTyped;

  String id = StringHelper.generateData(1000);
  try {
    final ODataResponse response = new JsonEntityProvider().writeProperty(edmProperty, id);
    assertNotNull(response);
  } catch(EntityProviderException e) {
    assertNotNull(e.getCause());
    assertTrue(e.getCause() instanceof EdmSimpleTypeException);
  }
}
 
Example #30
Source File: EdmMock.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private static EdmFunctionImport createFunctionImportMock(final EdmEntityContainer container, final String name,
    final EdmType type, final EdmMultiplicity multiplicity) throws EdmException {
  EdmTyped returnType = mock(EdmTyped.class);
  when(returnType.getType()).thenReturn(type);
  when(returnType.getMultiplicity()).thenReturn(multiplicity);

  EdmFunctionImport functionImport = mock(EdmFunctionImport.class);
  when(functionImport.getName()).thenReturn(name);
  when(functionImport.getReturnType()).thenReturn(returnType);
  when(functionImport.getHttpMethod()).thenReturn(ODataHttpMethod.GET.name());

  when(container.getFunctionImport(name)).thenReturn(functionImport);

  return functionImport;
}