Search in sources :

Example 6 with ODataEntry

use of org.apache.olingo.odata2.api.ep.entry.ODataEntry in project camel by apache.

the class Olingo2AppAPITest method prettyPrint.

private static String prettyPrint(Map<String, Object> properties, int level) {
    StringBuilder b = new StringBuilder();
    Set<Map.Entry<String, Object>> entries = properties.entrySet();
    for (Map.Entry<String, Object> entry : entries) {
        indent(b, level);
        b.append(entry.getKey()).append(": ");
        Object value = entry.getValue();
        if (value instanceof Map) {
            @SuppressWarnings("unchecked") final Map<String, Object> objectMap = (Map<String, Object>) value;
            value = prettyPrint(objectMap, level + 1);
            b.append(value).append(NEW_LINE);
        } else if (value instanceof Calendar) {
            Calendar cal = (Calendar) value;
            value = DateFormat.getInstance().format(cal.getTime());
            b.append(value).append(NEW_LINE);
        } else if (value instanceof ODataDeltaFeed) {
            ODataDeltaFeed feed = (ODataDeltaFeed) value;
            List<ODataEntry> inlineEntries = feed.getEntries();
            b.append("{");
            for (ODataEntry oDataEntry : inlineEntries) {
                value = prettyPrint(oDataEntry.getProperties(), level + 1);
                b.append("\n[\n").append(value).append("\n],");
            }
            b.deleteCharAt(b.length() - 1);
            indent(b, level);
            b.append("}\n");
        } else {
            b.append(value).append(NEW_LINE);
        }
    }
    // remove last line break
    b.deleteCharAt(b.length() - 1);
    return b.toString();
}
Also used : Calendar(java.util.Calendar) ODataDeltaFeed(org.apache.olingo.odata2.api.ep.feed.ODataDeltaFeed) ODataEntry(org.apache.olingo.odata2.api.ep.entry.ODataEntry) ODataEntry(org.apache.olingo.odata2.api.ep.entry.ODataEntry) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with ODataEntry

use of org.apache.olingo.odata2.api.ep.entry.ODataEntry in project camel by apache.

the class Olingo2AppAPITest method testBatchRequest.

@Test
public void testBatchRequest() throws Exception {
    final List<Olingo2BatchRequest> batchParts = new ArrayList<Olingo2BatchRequest>();
    // Edm query
    batchParts.add(Olingo2BatchQueryRequest.resourcePath(Olingo2AppImpl.METADATA).build());
    // feed query
    batchParts.add(Olingo2BatchQueryRequest.resourcePath(MANUFACTURERS).build());
    // read
    batchParts.add(Olingo2BatchQueryRequest.resourcePath(TEST_MANUFACTURER).build());
    // read with expand
    final HashMap<String, String> queryParams = new HashMap<String, String>();
    queryParams.put(SystemQueryOption.$expand.toString(), CARS);
    batchParts.add(Olingo2BatchQueryRequest.resourcePath(TEST_MANUFACTURER).queryParams(queryParams).build());
    // create
    final Map<String, Object> data = getEntityData();
    batchParts.add(Olingo2BatchChangeRequest.resourcePath(MANUFACTURERS).contentId(TEST_RESOURCE_CONTENT_ID).operation(Operation.CREATE).body(data).build());
    // update
    final Map<String, Object> updateData = new HashMap<String, Object>(data);
    @SuppressWarnings("unchecked") Map<String, Object> address = (Map<String, Object>) updateData.get(ADDRESS);
    updateData.put("Name", "MyCarManufacturer Renamed");
    address.put("Street", "Main Street");
    batchParts.add(Olingo2BatchChangeRequest.resourcePath(TEST_RESOURCE).operation(Operation.UPDATE).body(updateData).build());
    // delete
    batchParts.add(Olingo2BatchChangeRequest.resourcePath(TEST_RESOURCE).operation(Operation.DELETE).build());
    final TestOlingo2ResponseHandler<List<Olingo2BatchResponse>> responseHandler = new TestOlingo2ResponseHandler<List<Olingo2BatchResponse>>();
    // read to verify delete
    batchParts.add(Olingo2BatchQueryRequest.resourcePath(TEST_CREATE_MANUFACTURER).build());
    olingoApp.batch(edm, batchParts, responseHandler);
    final List<Olingo2BatchResponse> responseParts = responseHandler.await(15, TimeUnit.MINUTES);
    assertEquals("Batch responses expected", 8, responseParts.size());
    assertNotNull(responseParts.get(0).getBody());
    final ODataFeed feed = (ODataFeed) responseParts.get(1).getBody();
    assertNotNull(feed);
    LOG.info("Batch feed:  {}", prettyPrint(feed));
    ODataEntry dataEntry = (ODataEntry) responseParts.get(2).getBody();
    assertNotNull(dataEntry);
    LOG.info("Batch read entry:  {}", prettyPrint(dataEntry));
    dataEntry = (ODataEntry) responseParts.get(3).getBody();
    assertNotNull(dataEntry);
    LOG.info("Batch read entry with expand:  {}", prettyPrint(dataEntry));
    dataEntry = (ODataEntry) responseParts.get(4).getBody();
    assertNotNull(dataEntry);
    LOG.info("Batch create entry:  {}", prettyPrint(dataEntry));
    assertEquals(HttpStatusCodes.NO_CONTENT.getStatusCode(), responseParts.get(5).getStatusCode());
    assertEquals(HttpStatusCodes.NO_CONTENT.getStatusCode(), responseParts.get(6).getStatusCode());
    assertEquals(HttpStatusCodes.NOT_FOUND.getStatusCode(), responseParts.get(7).getStatusCode());
    final Exception exception = (Exception) responseParts.get(7).getBody();
    assertNotNull(exception);
    LOG.info("Batch retrieve deleted entry:  {}", exception);
}
Also used : ODataFeed(org.apache.olingo.odata2.api.ep.feed.ODataFeed) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Olingo2BatchRequest(org.apache.camel.component.olingo2.api.batch.Olingo2BatchRequest) Olingo2BatchResponse(org.apache.camel.component.olingo2.api.batch.Olingo2BatchResponse) ArrayList(java.util.ArrayList) List(java.util.List) ODataEntry(org.apache.olingo.odata2.api.ep.entry.ODataEntry) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 8 with ODataEntry

use of org.apache.olingo.odata2.api.ep.entry.ODataEntry in project camel by apache.

the class Olingo2AppAPITest method testReadEntry.

@Test
public void testReadEntry() throws Exception {
    final TestOlingo2ResponseHandler<ODataEntry> responseHandler = new TestOlingo2ResponseHandler<ODataEntry>();
    olingoApp.read(edm, TEST_MANUFACTURER, null, responseHandler);
    ODataEntry entry = responseHandler.await();
    LOG.info("Single Entry:  {}", prettyPrint(entry));
    responseHandler.reset();
    olingoApp.read(edm, TEST_CAR, null, responseHandler);
    entry = responseHandler.await();
    LOG.info("Single Entry:  {}", prettyPrint(entry));
    responseHandler.reset();
    final Map<String, String> queryParams = new HashMap<String, String>();
    queryParams.put(SystemQueryOption.$expand.toString(), CARS);
    olingoApp.read(edm, TEST_MANUFACTURER, queryParams, responseHandler);
    ODataEntry entryExpanded = responseHandler.await();
    LOG.info("Single Entry with expanded Cars relation:  {}", prettyPrint(entryExpanded));
}
Also used : HashMap(java.util.HashMap) ODataEntry(org.apache.olingo.odata2.api.ep.entry.ODataEntry) Test(org.junit.Test)

Example 9 with ODataEntry

use of org.apache.olingo.odata2.api.ep.entry.ODataEntry in project camel by apache.

the class Olingo2ComponentTest method testCreateUpdateDelete.

@Test
public void testCreateUpdateDelete() throws Exception {
    final Map<String, Object> data = getEntityData();
    Map<String, Object> address;
    final ODataEntry manufacturer = requestBody("direct://CREATE", data);
    assertNotNull("Created Manufacturer", manufacturer);
    final Map<String, Object> properties = manufacturer.getProperties();
    assertEquals("Created Manufacturer Id", "123", properties.get(ID_PROPERTY));
    LOG.info("Created Manufacturer: {}", properties);
    // update
    data.put("Name", "MyCarManufacturer Renamed");
    address = (Map<String, Object>) data.get("Address");
    address.put("Street", "Main Street");
    HttpStatusCodes status = requestBody("direct://UPDATE", data);
    assertNotNull("Update status", status);
    assertEquals("Update status", HttpStatusCodes.NO_CONTENT.getStatusCode(), status.getStatusCode());
    LOG.info("Update status: {}", status);
    // delete
    status = requestBody("direct://DELETE", null);
    assertNotNull("Delete status", status);
    assertEquals("Delete status", HttpStatusCodes.NO_CONTENT.getStatusCode(), status.getStatusCode());
    LOG.info("Delete status: {}", status);
}
Also used : HttpStatusCodes(org.apache.olingo.odata2.api.commons.HttpStatusCodes) ODataEntry(org.apache.olingo.odata2.api.ep.entry.ODataEntry) Test(org.junit.Test)

Example 10 with ODataEntry

use of org.apache.olingo.odata2.api.ep.entry.ODataEntry in project camel by apache.

the class Olingo2ComponentTest method testBatch.

@Test
public void testBatch() throws Exception {
    final List<Olingo2BatchRequest> batchParts = new ArrayList<Olingo2BatchRequest>();
    // 1. Edm query
    batchParts.add(Olingo2BatchQueryRequest.resourcePath(Olingo2AppImpl.METADATA).build());
    // 2. feed query
    batchParts.add(Olingo2BatchQueryRequest.resourcePath(MANUFACTURERS).build());
    // 3. read
    batchParts.add(Olingo2BatchQueryRequest.resourcePath(TEST_MANUFACTURER).build());
    // 4. read with expand
    final HashMap<String, String> queryParams = new HashMap<String, String>();
    queryParams.put(SystemQueryOption.$expand.toString(), CARS);
    batchParts.add(Olingo2BatchQueryRequest.resourcePath(TEST_MANUFACTURER).queryParams(queryParams).build());
    // 5. create
    final Map<String, Object> data = getEntityData();
    batchParts.add(Olingo2BatchChangeRequest.resourcePath(MANUFACTURERS).contentId(TEST_RESOURCE_CONTENT_ID).operation(Operation.CREATE).body(data).build());
    // 6. update address in created entry
    final Map<String, Object> updateData = new HashMap<String, Object>(data);
    Map<String, Object> address = (Map<String, Object>) updateData.get(ADDRESS);
    address.put("Street", "Main Street");
    batchParts.add(Olingo2BatchChangeRequest.resourcePath(TEST_RESOURCE_ADDRESS).operation(Operation.UPDATE).body(address).build());
    // 7. update
    updateData.put("Name", "MyCarManufacturer Renamed");
    batchParts.add(Olingo2BatchChangeRequest.resourcePath(TEST_RESOURCE).operation(Operation.UPDATE).body(updateData).build());
    // 8. delete
    batchParts.add(Olingo2BatchChangeRequest.resourcePath(TEST_RESOURCE).operation(Operation.DELETE).build());
    // 9. read to verify delete
    batchParts.add(Olingo2BatchQueryRequest.resourcePath(TEST_CREATE_MANUFACTURER).build());
    // execute batch request
    final List<Olingo2BatchResponse> responseParts = requestBody("direct://BATCH", batchParts);
    assertNotNull("Batch response", responseParts);
    assertEquals("Batch responses expected", 9, responseParts.size());
    final Edm edm = (Edm) responseParts.get(0).getBody();
    assertNotNull(edm);
    LOG.info("Edm entity sets: {}", edm.getEntitySets());
    final ODataFeed feed = (ODataFeed) responseParts.get(1).getBody();
    assertNotNull(feed);
    LOG.info("Read feed: {}", feed.getEntries());
    ODataEntry dataEntry = (ODataEntry) responseParts.get(2).getBody();
    assertNotNull(dataEntry);
    LOG.info("Read entry: {}", dataEntry.getProperties());
    dataEntry = (ODataEntry) responseParts.get(3).getBody();
    assertNotNull(dataEntry);
    LOG.info("Read entry with $expand: {}", dataEntry.getProperties());
    dataEntry = (ODataEntry) responseParts.get(4).getBody();
    assertNotNull(dataEntry);
    LOG.info("Created entry: {}", dataEntry.getProperties());
    int statusCode = responseParts.get(5).getStatusCode();
    assertEquals(HttpStatusCodes.NO_CONTENT.getStatusCode(), statusCode);
    LOG.info("Update address status: {}", statusCode);
    statusCode = responseParts.get(6).getStatusCode();
    assertEquals(HttpStatusCodes.NO_CONTENT.getStatusCode(), statusCode);
    LOG.info("Update entry status: {}", statusCode);
    statusCode = responseParts.get(7).getStatusCode();
    assertEquals(HttpStatusCodes.NO_CONTENT.getStatusCode(), statusCode);
    LOG.info("Delete status: {}", statusCode);
    assertEquals(HttpStatusCodes.NOT_FOUND.getStatusCode(), responseParts.get(8).getStatusCode());
    final Exception exception = (Exception) responseParts.get(8).getBody();
    assertNotNull(exception);
    LOG.info("Read deleted entry exception: {}", exception);
}
Also used : ODataFeed(org.apache.olingo.odata2.api.ep.feed.ODataFeed) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Olingo2BatchRequest(org.apache.camel.component.olingo2.api.batch.Olingo2BatchRequest) Edm(org.apache.olingo.odata2.api.edm.Edm) Olingo2BatchResponse(org.apache.camel.component.olingo2.api.batch.Olingo2BatchResponse) ODataEntry(org.apache.olingo.odata2.api.ep.entry.ODataEntry) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ODataEntry (org.apache.olingo.odata2.api.ep.entry.ODataEntry)9 HashMap (java.util.HashMap)8 Test (org.junit.Test)7 Map (java.util.Map)5 ArrayList (java.util.ArrayList)3 Olingo2BatchRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchRequest)3 Olingo2BatchResponse (org.apache.camel.component.olingo2.api.batch.Olingo2BatchResponse)3 HttpStatusCodes (org.apache.olingo.odata2.api.commons.HttpStatusCodes)3 ODataFeed (org.apache.olingo.odata2.api.ep.feed.ODataFeed)3 InputStream (java.io.InputStream)2 List (java.util.List)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URISyntaxException (java.net.URISyntaxException)1 Calendar (java.util.Calendar)1 Iterator (java.util.Iterator)1 Olingo2BatchChangeRequest (org.apache.camel.component.olingo2.api.batch.Olingo2BatchChangeRequest)1 Header (org.apache.http.Header)1 HttpResponse (org.apache.http.HttpResponse)1