org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties Java Examples

The following examples show how to use org.apache.olingo.odata2.api.ep.EntityProviderWriteProperties. 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: CMODataAbapClient.java    From devops-cm-client with Apache License 2.0 7 votes vote down vote up
public void updateTransport(Transport transport) throws IOException, URISyntaxException, ODataException, UnexpectedHttpResponseException
{
  Edm edm = getEntityDataModel();
  try (CloseableHttpClient client = clientFactory.createClient()) {
    
    HttpPut put = requestBuilder.updateTransport(transport.getTransportID());
    put.setHeader("x-csrf-token", getCSRFToken());
    EdmEntityContainer entityContainer = edm.getDefaultEntityContainer();
    EdmEntitySet entitySet = entityContainer.getEntitySet(TransportRequestBuilder.getEntityKey());
    URI rootUri = new URI(endpoint.toASCIIString() + "/");
    EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(rootUri).build();
    ODataResponse response = null;
    try {
        response = EntityProvider.writeEntry(put.getHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue(), entitySet,transport.getValueMap(), properties);
        put.setEntity(EntityBuilder.create().setStream(response.getEntityAsStream()).build());
        try (CloseableHttpResponse httpResponse = client.execute(put)) {
            hasStatusOrFail(httpResponse, SC_OK, HttpStatus.SC_NO_CONTENT);
        }
    }  finally {
        if(response != null) response.close();
    }
  }
}
 
Example #2
Source File: JsonFeedWithDeltaLinkProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private String writeRoomData(final EdmEntitySet entitySet, final TombstoneCallback tombstoneCallback)
    throws URISyntaxException, EntityProviderException, IOException {
  Map<String, ODataCallback> callbacks = new HashMap<String, ODataCallback>();
  callbacks.put(TombstoneCallback.CALLBACK_KEY_TOMBSTONE, tombstoneCallback);

  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(new URI(BASE_URI)).callbacks(callbacks).build();

  final ODataResponse response = new JsonEntityProvider().writeFeed(entitySet, roomsData,
      properties);
  assertNotNull(response);
  assertNotNull(response.getEntity());
  assertNull("EntitypProvider must not set content header", response.getContentHeader());

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

  validate(json);

  return json;
}
 
Example #3
Source File: CMODataAbapClient.java    From devops-cm-client with Apache License 2.0 6 votes vote down vote up
public Transport createTransport(Map<String, Object> transport) throws IOException, URISyntaxException, ODataException, UnexpectedHttpResponseException
{
  try (CloseableHttpClient client = clientFactory.createClient()) {
    HttpPost post = requestBuilder.createTransport();
    post.setHeader("x-csrf-token", getCSRFToken());
    EdmEntityContainer entityContainer = getEntityDataModel().getDefaultEntityContainer();
    EdmEntitySet entitySet = entityContainer.getEntitySet(TransportRequestBuilder.getEntityKey());
    URI rootUri = new URI(endpoint.toASCIIString() + "/");
    EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(rootUri).build();
    ODataResponse marshaller = null;

    try {
      marshaller = EntityProvider.writeEntry(post.getHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue(), entitySet, transport, properties);
      post.setEntity(EntityBuilder.create().setStream(marshaller.getEntityAsStream()).build());

      try(CloseableHttpResponse httpResponse = client.execute(post)) {
        hasStatusOrFail(httpResponse, SC_CREATED);
        return getTransport(httpResponse);
      }
    } finally {
        if(marshaller != null) marshaller.close();
    }
  }
}
 
Example #4
Source File: XmlExpandProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void expandSelectedRoom() throws Exception {
  ExpandSelectTreeNode selectTree = getSelectExpandTree("Employees('1')", "ne_Room", "ne_Room");

  HashMap<String, ODataCallback> callbacksEmployee = createCallbacks("Employees");
  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(BASE_URI).expandSelectTree(selectTree).callbacks(callbacksEmployee)
          .build();
  AtomEntityProvider provider = createAtomEntityProvider();
  ODataResponse response =
      provider.writeEntry(MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees"),
          employeeData, properties);

  String xmlString = verifyResponse(response);
  verifyNavigationProperties(xmlString, F, T, F);
  assertXpathNotExists("/a:entry/m:properties", xmlString);
  verifyRoom(roomXPathString, xmlString);
}
 
Example #5
Source File: MapProcessor.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse readEntity(final GetEntityUriInfo uriInfo, final String contentType) throws ODataException {
  final EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(getContext().getPathInfo().getServiceRoot()).build();

  // query
  final String mappedKeyName =
      (String) uriInfo.getTargetEntitySet().getEntityType().getKeyProperties().get(0).getMapping().getObject();
  final String keyValue = uriInfo.getKeyPredicates().get(0).getLiteral();
  final int index = indexOf(mappedKeyName, keyValue);
  if ((index < 0) || (index > records.size())) {
    throw new ODataNotFoundException(ODataNotFoundException.ENTITY.addContent(keyValue));
  }
  final HashMap<String, String> record = records.get(index);

  final HashMap<String, Object> data = new HashMap<String, Object>();
  for (final String pName : uriInfo.getTargetEntitySet().getEntityType().getPropertyNames()) {
    final EdmProperty property = (EdmProperty) uriInfo.getTargetEntitySet().getEntityType().getProperty(pName);
    final String mappedPropertyName = (String) property.getMapping().getObject();
    data.put(pName, record.get(mappedPropertyName));
  }

  final ODataResponse response =
      EntityProvider.writeEntry(contentType, uriInfo.getTargetEntitySet(), data, properties);
  return response;
}
 
Example #6
Source File: JsonFeedEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void nextLink() throws Exception {
  final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  Map<String, Object> roomData = new HashMap<String, Object>();
  roomData.put("Id", "1");
  roomData.put("Seats", 123);
  roomData.put("Version", 1);
  List<Map<String, Object>> roomsData = new ArrayList<Map<String, Object>>();
  roomsData.add(roomData);

  final ODataResponse response = new JsonEntityProvider().writeFeed(entitySet, roomsData,
      EntityProviderWriteProperties.serviceRoot(URI.create(BASE_URI)).nextLink("Rooms?$skiptoken=2").build());
  assertNotNull(response);
  assertNotNull(response.getEntity());
  assertNull("EntitypProvider must not set content header", response.getContentHeader());

  final String json = StringHelper.inputStreamToString((InputStream) response.getEntity());
  assertNotNull(json);
  assertEquals("{\"d\":{\"results\":[{\"__metadata\":{\"id\":\"" + BASE_URI + "Rooms('1')\","
      + "\"uri\":\"" + BASE_URI + "Rooms('1')\",\"type\":\"RefScenario.Room\",\"etag\":\"W/\\\"1\\\"\"},"
      + "\"Id\":\"1\",\"Name\":null,\"Seats\":123,\"Version\":1,"
      + "\"nr_Employees\":{\"__deferred\":{\"uri\":\"" + BASE_URI + "Rooms('1')/nr_Employees\"}},"
      + "\"nr_Building\":{\"__deferred\":{\"uri\":\"" + BASE_URI + "Rooms('1')/nr_Building\"}}}],"
      + "\"__next\":\"Rooms?$skiptoken=2\"}}",
      json);
}
 
Example #7
Source File: ODataJPAResponseBuilderDefault.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private static EntityProviderWriteProperties getEntityProviderProperties(final ODataJPAContext odataJPAContext,
    final GetEntityUriInfo resultsView) throws ODataJPARuntimeException {
  ODataEntityProviderPropertiesBuilder entityFeedPropertiesBuilder = null;
  ExpandSelectTreeNode expandSelectTree = null;
  try {
    entityFeedPropertiesBuilder =
        EntityProviderWriteProperties.serviceRoot(odataJPAContext.getODataContext().getPathInfo().getServiceRoot());
    expandSelectTree = UriParser.createExpandSelectTree(resultsView.getSelect(), resultsView.getExpand());
    entityFeedPropertiesBuilder.expandSelectTree(expandSelectTree);
    entityFeedPropertiesBuilder.callbacks(JPAExpandCallBack.getCallbacks(odataJPAContext.getODataContext()
        .getPathInfo().getServiceRoot(), expandSelectTree, resultsView.getExpand()));
  } catch (ODataException e) {
    throw ODataJPARuntimeException.throwException(ODataJPARuntimeException.INNER_EXCEPTION, e);
  }

  return entityFeedPropertiesBuilder.build();
}
 
Example #8
Source File: ExpandSelectProducerWithBuilderTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void expandBuilding() throws Exception {
  EdmEntitySet roomsSet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  List<String> expandedNavigationProperties = new ArrayList<String>();
  expandedNavigationProperties.add("nr_Building");

  ExpandSelectTreeNode expandSelectTree =
      ExpandSelectTreeNode.entitySet(roomsSet).expandedLinks(expandedNavigationProperties).build();

  Map<String, ODataCallback> callbacks = new HashMap<String, ODataCallback>();
  callbacks.put("nr_Building", new LocalCallback());
  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.fromProperties(DEFAULT_PROPERTIES).callbacks(callbacks).expandSelectTree(
          expandSelectTree).build();
  ODataResponse entry = EntityProvider.writeEntry("application/xml", roomsSet, roomData, properties);

  String xml = StringHelper.inputStreamToString((InputStream) entry.getEntity());
  assertXpathExists("/a:entry/a:content/m:properties", xml);
  assertXpathExists("/a:entry/a:link[@type]/m:inline", xml);
}
 
Example #9
Source File: Expander.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public WriteEntryCallbackResult retrieveEntryResult(WriteEntryCallbackContext context)
      throws ODataApplicationException
{
   EntityProviderWriteProperties inlineProperties = EntityProviderWriteProperties
         .serviceRoot(this.serviceRoot)
         .expandSelectTree(context.getCurrentExpandSelectTreeNode())
         .build();

   WriteEntryCallbackResult result = new WriteEntryCallbackResult();
   result.setInlineProperties(inlineProperties);

   List<Map<String, Object>> data = getData(context);
   if (data.size() > 1)
   {
      throw new IllegalStateException("cannot expand a feed as an entity");
   }
   if (data.size() == 1)
   {
      result.setEntryData(data.get(0));
   }

   return result;
}
 
Example #10
Source File: XmlExpandProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void deepExpandSelectedEmployeesWithRoomId() throws Exception {
  ExpandSelectTreeNode selectTree =
      getSelectExpandTree("Rooms('1')", "nr_Employees/ne_Room/Id", "nr_Employees/ne_Room");

  HashMap<String, ODataCallback> callbacksRoom = createCallbacks("Rooms");
  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(BASE_URI).expandSelectTree(selectTree).callbacks(callbacksRoom)
          .build();
  AtomEntityProvider provider = createAtomEntityProvider();
  ODataResponse response =
      provider.writeEntry(MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms"), roomData,
          properties);

  String xmlString = verifyResponse(response);
  assertXpathNotExists("/a:entry/m:properties", xmlString);
  assertXpathExists(employeeXPathString, xmlString);
  assertXpathExists(employeeXPathString + "/m:inline/a:feed" + roomXPathString, xmlString);
  assertXpathExists(employeeXPathString + "/m:inline/a:feed" + roomXPathString + "/m:inline", xmlString);
  assertXpathExists(employeeXPathString + "/m:inline/a:feed" + roomXPathString + "/m:inline/a:entry", xmlString);
  assertXpathExists(employeeXPathString + "/m:inline/a:feed" + roomXPathString
      + "/m:inline/a:entry/a:content/m:properties/d:Id", xmlString);
}
 
Example #11
Source File: JsonEntryEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void entryWithExpandedEntryButNullDataOmitData() throws Exception {
  final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  Map<String, Object> roomData = new HashMap<String, Object>();
  roomData.put("Id", "1");
  roomData.put("Version", 1);

  ExpandSelectTreeNode node1 = createRoomNode();
  Map<String, ODataCallback> callbacks = new HashMap<String, ODataCallback>();
  callbacks.put("nr_Building", new NullEntryCallback());

  final ODataResponse response =
      new JsonEntityProvider().writeEntry(entitySet, roomData,
          EntityProviderWriteProperties.serviceRoot(URI.create(BASE_URI)).expandSelectTree(node1)
          .callbacks(callbacks).omitInlineForNullData(true).build());
  assertNotNull(response);
  assertNotNull(response.getEntity());
  assertNull("EntitypProvider must not set content header", response.getContentHeader());

  Map<String, Object> roomEntry = checkRoom(response);
  assertTrue(roomEntry.containsKey("nr_Building"));
  assertNotNull(roomEntry.get("nr_Building"));
  assertTrue(((Map<String, Object>) roomEntry.get("nr_Building")).size() == 1);
  assertTrue(((Map<String, Object>) roomEntry.get("nr_Building")).containsKey("__deferred"));
}
 
Example #12
Source File: XmlExpandProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void expandSelectedEmployeesIgnoreFacets() 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);

  ExpandSelectTreeNode selectTree = getSelectExpandTree("Rooms('1')", "nr_Employees", "nr_Employees");

  HashMap<String, ODataCallback> callbacksRoom = createCallbacks("Rooms");
  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(BASE_URI).expandSelectTree(selectTree).callbacks(callbacksRoom)
          .callbacks(callbacksRoom).validatingFacets(false)
          .build();
  AtomEntityProvider provider = createAtomEntityProvider();
  ODataResponse response =
      provider.writeEntry(edm.getDefaultEntityContainer().getEntitySet("Rooms"), roomData,
          properties);

  String xmlString = verifyResponse(response);
  assertXpathNotExists("/a:entry/m:properties", xmlString);
  assertXpathExists("/a:entry/a:link", xmlString);
  verifyEmployees(employeeXPathString, xmlString);
}
 
Example #13
Source File: AtomEntryProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithoutCompositeKey() throws Exception {
  EdmEntitySet entitySet = MockFacade.getMockEdm().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");

  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 'Id'"));
  }
}
 
Example #14
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 #15
Source File: JsonLinkEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void omitJsonWrapperOnLink() throws Exception {
  final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");
  Map<String, Object> employeeData = new HashMap<String, Object>();
  employeeData.put("EmployeeId", "1");

  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.fromProperties(DEFAULT_PROPERTIES).omitJsonWrapper(true).build();
  final ODataResponse response = new JsonEntityProvider().writeLink(entitySet, employeeData, properties);
  assertNotNull(response);
  assertNotNull(response.getEntity());

  final String json = StringHelper.inputStreamToString((InputStream) response.getEntity());
  assertNotNull(json);
  assertEquals("{\"uri\":\"" + BASE_URI + "Employees('1')\"}", json);
}
 
Example #16
Source File: JsonEntityProvider.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Override
public ODataResponse writeFunctionImport(final EdmFunctionImport functionImport, final Object data,
    final EntityProviderWriteProperties properties) throws EntityProviderException {
  try {
    if(functionImport.getReturnType() !=null){
      if (functionImport.getReturnType().getType().getKind() == EdmTypeKind.ENTITY) {
        @SuppressWarnings("unchecked")
        Map<String, Object> map = (Map<String, Object>) data;
        return writeEntry(functionImport.getEntitySet(), map, properties);
      }

      final EntityPropertyInfo info = EntityInfoAggregator.create(functionImport);
      if (functionImport.getReturnType().getMultiplicity() == EdmMultiplicity.MANY) {
        return writeCollection(info, (List<?>) data);
      } else {
        return writeSingleTypedElement(info, data);
      }
    }else{
      return ODataResponse.newBuilder().status(HttpStatusCodes.ACCEPTED).build();
    }
  } catch (final EdmException e) {
    throw new EntityProviderProducerException(e.getMessageReference(), e);
  }
}
 
Example #17
Source File: JsonEntryEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void entryWithExpandedEntryButNoRegisteredCallback() throws Exception {
  final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  Map<String, Object> roomData = new HashMap<String, Object>();
  roomData.put("Id", "1");
  roomData.put("Version", 1);

  ExpandSelectTreeNode node1 = createRoomNode();

  final ODataResponse response = new JsonEntityProvider().writeEntry(entitySet, roomData,
      EntityProviderWriteProperties.serviceRoot(URI.create(BASE_URI)).expandSelectTree(node1).build());
  final String json = verifyResponse(response);
  assertEquals("{\"d\":{\"__metadata\":{\"id\":\"" + BASE_URI + "Rooms('1')\","
      + "\"uri\":\"" + BASE_URI + "Rooms('1')\",\"type\":\"RefScenario.Room\",\"etag\":\"W/\\\"1\\\"\"},"
      + "\"nr_Building\":{\"__deferred\":{\"uri\":\"" + BASE_URI + "Rooms('1')/nr_Building\"}}}}",
      json);
}
 
Example #18
Source File: AtomEntryProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testExceptionWithNonNullablePropertyIsNull() throws Exception {
  EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Organizations");
  EdmProperty nameProperty = (EdmProperty) entitySet.getEntityType().getProperty("Name");
  EdmFacets facets = nameProperty.getFacets();
  when(facets.isNullable()).thenReturn(new Boolean(false));
  final EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(BASE_URI).omitETag(true).
      isDataBasedPropertySerialization(true).build();
  AtomEntityProvider ser = createAtomEntityProvider();

  Map<String, Object> orgData = new HashMap<String, Object>();
  orgData.put("Id", "1");
  try {
  ser.writeEntry(entitySet, orgData, properties);
  } catch (EntityProviderProducerException e) {
    assertTrue(e.getMessage().contains("The metadata do not allow a null value for property 'Name'"));
  }
}
 
Example #19
Source File: JsonEntryEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithoutKey() throws Exception {
  EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");
  List<String> selectedPropertyNames = new ArrayList<String>();
  selectedPropertyNames.add("ManagerId");
  ExpandSelectTreeNode select =
      ExpandSelectTreeNode.entitySet(entitySet).selectedProperties(selectedPropertyNames).build();

  final EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(URI.create(BASE_URI)).expandSelectTree(select).build();

  Map<String, Object> localEmployeeData = new HashMap<String, Object>();
  localEmployeeData.put("ManagerId", "1");

  JsonEntityProvider ser = new JsonEntityProvider();
  try {
  ser.writeEntry(entitySet, localEmployeeData, properties);
  } catch (EntityProviderProducerException e) {
    assertTrue(e.getMessage().contains("The metadata do not allow a null value for property 'EmployeeId'"));
  }
}
 
Example #20
Source File: AtomEntryProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
/**
 * Test serialization of empty syndication title property. EmployeeName is set to NULL after the update (which is
 * allowed because EmployeeName has default Nullable behavior which is true).
 * Write of an empty atom title tag is allowed within RFC4287 (http://tools.ietf.org/html/rfc4287#section-4.2.14).
 */
@Test
public void serializeEmployeeWithNullSyndicationTitleProperty() throws IOException, XpathException, SAXException,
    XMLStreamException, FactoryConfigurationError, ODataException {
  AtomEntityProvider ser = createAtomEntityProvider();
  EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(BASE_URI).build();
  employeeData.put("EmployeeName", null);
  ODataResponse response =
      ser.writeEntry(MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees"), employeeData,
          properties);
  String xmlString = verifyResponse(response);

  assertXpathExists("/a:entry/a:title", xmlString);
  assertXpathEvaluatesTo("", "/a:entry/a:title", xmlString);

  assertXpathExists("/a:entry", xmlString);
  assertXpathEvaluatesTo(BASE_URI.toASCIIString(), "/a:entry/@xml:base", xmlString);

  assertXpathExists("/a:entry/a:content", xmlString);
  assertXpathEvaluatesTo("Employees('1')/$value", "/a:entry/a:content/@src", xmlString);
  assertXpathExists("/a:entry/m:properties", xmlString);
}
 
Example #21
Source File: AtomEntryProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void contentOnlyWithoutKeyWithoutSelectedProperties() throws Exception {
  HashMap<String, Object> employeeData = new HashMap<String, Object>();
  employeeData.put("ManagerId", "1");
  employeeData.put("Age", new Integer(52));
  employeeData.put("RoomId", "1");
  employeeData.put("TeamId", "42");

  List<String> selectedProperties = new ArrayList<String>();
  selectedProperties.add("ManagerId");
  selectedProperties.add("Age");
  selectedProperties.add("RoomId");
  selectedProperties.add("TeamId");
  final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");

  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.fromProperties(DEFAULT_PROPERTIES).contentOnly(true).build();
  AtomEntityProvider ser = createAtomEntityProvider();
  try {
    ser.writeEntry(entitySet, employeeData, properties);
  } catch (EntityProviderProducerException e) {
    assertTrue(e.getMessage().contains("The metadata do not allow a null value for property 'EmployeeId'"));
  }
}
 
Example #22
Source File: JsonLinksEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void omitJsonWrapperSingleLink() throws Exception {
  final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");
  Map<String, Object> employeeData = new HashMap<String, Object>();
  employeeData.put("EmployeeId", "1");
  ArrayList<Map<String, Object>> employeesData = new ArrayList<Map<String, Object>>();
  employeesData.add(employeeData);

  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.fromProperties(DEFAULT_PROPERTIES).omitJsonWrapper(true).build();
  final ODataResponse response = new JsonEntityProvider().writeLinks(entitySet, employeesData, properties);
  assertNotNull(response);
  assertNotNull(response.getEntity());
  assertNull("EntitypProvider must not set content header", response.getContentHeader());

  final String json = StringHelper.inputStreamToString((InputStream) response.getEntity());
  assertNotNull(json);
  assertEquals("[{\"uri\":\"" + BASE_URI + "Employees('1')\"}]", json);
}
 
Example #23
Source File: XmlSelectProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void selectEmployeeIdEmployeeNameImageUrl() throws Exception {
  ExpandSelectTreeNode selectTree = getSelectExpandTree("EmployeeId, EmployeeName, ImageUrl", null);

  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(BASE_URI).expandSelectTree(selectTree).build();
  AtomEntityProvider provider = createAtomEntityProvider();
  ODataResponse response =
      provider.writeEntry(MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees"),
          employeeData, properties);

  String xmlString = verifyResponse(response);

  verifyNavigationProperties(xmlString, F, F, F);
  assertXpathExists("/a:entry/m:properties", xmlString);
  verifyKeyProperties(xmlString, T, F, F, F);
  verifySingleProperties(xmlString, T, F, F, T);
  verifyComplexProperties(xmlString, F);
}
 
Example #24
Source File: JsonEntryEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void omitJsonWrapper() throws Exception {
  final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Teams");
  Map<String, Object> teamData = new HashMap<String, Object>();
  teamData.put("Id", "1");
  teamData.put("isScrumTeam", true);

  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.fromProperties(DEFAULT_PROPERTIES).omitJsonWrapper(true).build();
  final ODataResponse response = new JsonEntityProvider().writeEntry(entitySet, teamData, properties);
  final String json = verifyResponse(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);
}
 
Example #25
Source File: XmlLinksEntityProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void linksWithInlineCount() throws Exception {
  final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms");
  initializeRoomData(1);

  final ODataResponse response = createAtomEntityProvider().writeLinks(entitySet, roomsData,
      EntityProviderWriteProperties.serviceRoot(BASE_URI).inlineCount(3).build());
  assertNotNull(response);
  assertNotNull(response.getEntity());

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

  assertXpathExists("/d:links", xml);
  assertXpathEvaluatesTo("3", "/d:links/m:count/text()", xml);
  assertXpathEvaluatesTo(BASE_URI.toString() + "Rooms('1')", "/d:links/d:uri/text()", xml);
}
 
Example #26
Source File: AtomFeedProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testFeedMandatoryParts() throws Exception {
  AtomEntityProvider ser = createAtomEntityProvider();
  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(BASE_URI).build();
  ODataResponse response = ser.writeFeed(view.getTargetEntitySet(), roomsData, properties);
  String xmlString = verifyResponse(response);

  assertXpathExists("/a:feed/a:id", xmlString);
  assertXpathEvaluatesTo(BASE_URI.toASCIIString() + "Rooms", "/a:feed/a:id/text()", xmlString);

  assertXpathExists("/a:feed/a:title", xmlString);
  assertXpathEvaluatesTo("Rooms", "/a:feed/a:title/text()", xmlString);

  assertXpathExists("/a:feed/a:updated", xmlString);
  assertXpathExists("/a:feed/a:author", xmlString);
  assertXpathExists("/a:feed/a:author/a:name", xmlString);
}
 
Example #27
Source File: AtomEntryProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeEmployeeAndCheckOrderOfPropertyTags() throws IOException, XpathException, SAXException,
    XMLStreamException, FactoryConfigurationError, ODataException {
  AtomEntityProvider ser = createAtomEntityProvider();
  EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(BASE_URI).build();
  EdmEntitySet employeeEntitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");
  ODataResponse response = ser.writeEntry(employeeEntitySet, employeeData, properties);
  String xmlString = verifyResponse(response);

  // log.debug(xmlString);

  assertXpathExists("/a:entry", xmlString);
  assertXpathExists("/a:entry/a:content", xmlString);
  // verify properties
  assertXpathExists("/a:entry/m:properties", xmlString);
  assertXpathEvaluatesTo("9", "count(/a:entry/m:properties/*)", xmlString);

  // verify order of tags
  List<String> expectedPropertyNamesFromEdm = employeeEntitySet.getEntityType().getPropertyNames();
  verifyTagOrdering(xmlString, expectedPropertyNamesFromEdm.toArray(new String[0]));
}
 
Example #28
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 #29
Source File: AtomFeedProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void unbalancedPropertyFeedWithSelect() throws Exception {
  final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Companys");
  List<Map<String, Object>> originalData = createData(true);
  List<String> selectedPropertyNames = new ArrayList<String>();
  selectedPropertyNames.add("Id");
  selectedPropertyNames.add("Location");
  ExpandSelectTreeNode select =
      ExpandSelectTreeNode.entitySet(entitySet).selectedProperties(selectedPropertyNames).build();
  
  ODataResponse response = new AtomEntityProvider().writeFeed(entitySet, originalData,
      EntityProviderWriteProperties.serviceRoot(BASE_URI).expandSelectTree(select).
      isDataBasedPropertySerialization(true).build());

  EntityProviderReadProperties readProperties = EntityProviderReadProperties.init().mergeSemantic(false).build();
  XmlEntityConsumer consumer = new XmlEntityConsumer();
  ODataFeed feed = consumer.readFeed(entitySet, (InputStream) response.getEntity(), readProperties);

  compareList(originalData, feed.getEntries());
}
 
Example #30
Source File: AtomEntryProducerTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void contentOnly() throws Exception {
  final EntityProviderWriteProperties properties =
      EntityProviderWriteProperties.serviceRoot(BASE_URI).contentOnly(true).build();

  AtomEntityProvider ser = createAtomEntityProvider();
  ODataResponse response =
      ser.writeEntry(MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees"), employeeData,
          properties);
  String xmlString = verifyResponse(response);
  assertXpathExists("/a:entry", xmlString);
  assertXpathEvaluatesTo(BASE_URI.toASCIIString(), "/a:entry/@xml:base", xmlString);

  assertXpathNotExists("/a:entry[@m:etag]", xmlString);
  assertXpathNotExists("/a:entry/a:id", xmlString);
  assertXpathNotExists("/a:entry/a:title", xmlString);
  assertXpathNotExists("/a:entry/a:updated", xmlString);
  assertXpathNotExists("/a:entry/a:category", xmlString);
  assertXpathNotExists("/a:entry/a:link[@title=\"ne_Team\"and @href=\"Employees('1')/ne_Team\"]", xmlString);
  assertXpathNotExists("/a:entry/a:link[@title=\"ne_Room\"and @href=\"Employees('1')/ne_Room\"]", xmlString);
  assertXpathNotExists("/a:entry/a:link[@title=\"ne_Manager\" and @href=\"Employees('1')/ne_Manager\"]", xmlString);
  assertXpathNotExists("/a:entry/a:content", xmlString);

  assertXpathExists("/a:entry/m:properties", xmlString);
}