You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2014/09/02 13:50:07 UTC

[47/50] [abbrv] git commit: [OLINGO-412] OmitJson Wrapper of link and links

[OLINGO-412] OmitJson Wrapper of link and links


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/7852087f
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/7852087f
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/7852087f

Branch: refs/heads/Olingo-129_PocJpaDataStore
Commit: 7852087f9523ee1674d44d215ffd5f9a7caca2be
Parents: 7092ad3
Author: Christian Amend <ch...@apache.org>
Authored: Tue Aug 19 13:30:09 2014 +0200
Committer: Christian Amend <ch...@apache.org>
Committed: Tue Aug 19 13:30:09 2014 +0200

----------------------------------------------------------------------
 .../ep/producer/JsonLinkEntityProducer.java     | 10 ++-
 .../ep/producer/JsonLinksEntityProducer.java    | 11 +--
 .../ep/producer/JsonLinkEntityProducerTest.java | 17 +++++
 .../producer/JsonLinksEntityProducerTest.java   | 76 ++++++++++++++++++++
 4 files changed, 107 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/7852087f/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonLinkEntityProducer.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonLinkEntityProducer.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonLinkEntityProducer.java
index 69a0c5a..9ddfddd 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonLinkEntityProducer.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonLinkEntityProducer.java
@@ -47,10 +47,14 @@ public class JsonLinkEntityProducer {
     final String uri = (properties.getServiceRoot() == null ? "" : properties.getServiceRoot().toASCIIString())
         + AtomEntryEntityProducer.createSelfLink(entityInfo, data, null);
     try {
-      jsonStreamWriter.beginObject()
-          .name(FormatJson.D);
+      if (!properties.isOmitJsonWrapper()) {
+        jsonStreamWriter.beginObject()
+            .name(FormatJson.D);
+      }
       appendUri(jsonStreamWriter, uri);
-      jsonStreamWriter.endObject();
+      if (!properties.isOmitJsonWrapper()) {
+        jsonStreamWriter.endObject();
+      }
     } catch (final IOException e) {
       throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass()
           .getSimpleName()), e);

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/7852087f/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonLinksEntityProducer.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonLinksEntityProducer.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonLinksEntityProducer.java
index a3a5e79..858456c 100644
--- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonLinksEntityProducer.java
+++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonLinksEntityProducer.java
@@ -47,8 +47,10 @@ public class JsonLinksEntityProducer {
     JsonStreamWriter jsonStreamWriter = new JsonStreamWriter(writer);
 
     try {
-      jsonStreamWriter.beginObject()
-          .name(FormatJson.D);
+      if (!properties.isOmitJsonWrapper()) {
+        jsonStreamWriter.beginObject()
+            .name(FormatJson.D);
+      }
 
       if (properties.getInlineCountType() == InlineCount.ALLPAGES) {
         final int inlineCount = properties.getInlineCount() == null ? 0 : properties.getInlineCount();
@@ -75,8 +77,9 @@ public class JsonLinksEntityProducer {
       if (properties.getInlineCountType() == InlineCount.ALLPAGES) {
         jsonStreamWriter.endObject();
       }
-
-      jsonStreamWriter.endObject();
+      if (!properties.isOmitJsonWrapper()) {
+        jsonStreamWriter.endObject();
+      }
     } catch (final IOException e) {
       throw new EntityProviderException(EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass()
           .getSimpleName()), e);

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/7852087f/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonLinkEntityProducerTest.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonLinkEntityProducerTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonLinkEntityProducerTest.java
index b23ac7b..91785b5 100644
--- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonLinkEntityProducerTest.java
+++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonLinkEntityProducerTest.java
@@ -44,6 +44,23 @@ public class JsonLinkEntityProducerTest extends BaseTest {
       EntityProviderWriteProperties.serviceRoot(URI.create(BASE_URI)).build();
 
   @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);
+  }
+
+  @Test
   public void serializeEmployeeLink() throws Exception {
     final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");
     Map<String, Object> employeeData = new HashMap<String, Object>();

http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/7852087f/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonLinksEntityProducerTest.java
----------------------------------------------------------------------
diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonLinksEntityProducerTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonLinksEntityProducerTest.java
index bef9ecb..2365d29 100644
--- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonLinksEntityProducerTest.java
+++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonLinksEntityProducerTest.java
@@ -47,6 +47,82 @@ public class JsonLinksEntityProducerTest extends BaseTest {
       EntityProviderWriteProperties.serviceRoot(URI.create(BASE_URI)).build();
 
   @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);
+  }
+
+  @Test
+  public void omitJsonWrapperSingleLinks() throws Exception {
+    final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");
+    Map<String, Object> employee1 = new HashMap<String, Object>();
+    employee1.put("EmployeeId", "1");
+    Map<String, Object> employee2 = new HashMap<String, Object>();
+    employee2.put("EmployeeId", "2");
+    Map<String, Object> employee3 = new HashMap<String, Object>();
+    employee3.put("EmployeeId", "3");
+    ArrayList<Map<String, Object>> employeesData = new ArrayList<Map<String, Object>>();
+    employeesData.add(employee1);
+    employeesData.add(employee2);
+    employeesData.add(employee3);
+
+    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')\"},"
+        + "{\"uri\":\"" + BASE_URI + "Employees('2')\"},"
+        + "{\"uri\":\"" + BASE_URI + "Employees('3')\"}]",
+        json);
+  }
+
+  @Test
+  public void serializeLinksAndInlineCountWithOmitJsonWrapper() throws Exception {
+    final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");
+    Map<String, Object> employee1 = new HashMap<String, Object>();
+    employee1.put("EmployeeId", "1");
+    Map<String, Object> employee2 = new HashMap<String, Object>();
+    employee2.put("EmployeeId", "2");
+    ArrayList<Map<String, Object>> employeesData = new ArrayList<Map<String, Object>>();
+    employeesData.add(employee1);
+    employeesData.add(employee2);
+
+    EntityProviderWriteProperties properties =
+        EntityProviderWriteProperties.fromProperties(DEFAULT_PROPERTIES).omitJsonWrapper(true).inlineCountType(
+            InlineCount.ALLPAGES).inlineCount(42).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("{\"__count\":\"42\",\"results\":["
+        + "{\"uri\":\"" + BASE_URI + "Employees('1')\"},"
+        + "{\"uri\":\"" + BASE_URI + "Employees('2')\"}]}",
+        json);
+  }
+
+  @Test
   public void serializeEmployeeLink() throws Exception {
     final EdmEntitySet entitySet = MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Employees");
     Map<String, Object> employeeData = new HashMap<String, Object>();