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();
}
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);
}
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));
}
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);
}
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);
}
Aggregations