From 2824f15a0a58afebff940e4026dd07338974b959 Mon Sep 17 00:00:00 2001
From: Shwetha GS <sshivalingamurthy@hortonworks.com>
Date: Mon, 11 Apr 2016 17:15:29 +0530
Subject: [PATCH] ATLAS-586 While updating the multiple attributes, Atlas returns the response with escape characters (dkantor via shwethags)

---
 release-log.txt                                                                 |  1 +
 webapp/src/main/java/org/apache/atlas/web/resources/EntityResource.java         |  9 ++++++---
 webapp/src/test/java/org/apache/atlas/web/resources/EntityJerseyResourceIT.java | 52 ++++++++++++++++++++++++++++++++++++++++++----------
 3 files changed, 49 insertions(+), 13 deletions(-)

diff --git a/release-log.txt b/release-log.txt
index 4b02f34..d687990 100644
--- a/release-log.txt
+++ b/release-log.txt
@@ -14,6 +14,7 @@ ATLAS-409 Atlas will not import avro tables with schema read from a file (dosset
 ATLAS-379 Create sqoop and falcon metadata addons (venkatnrangan,bvellanki,sowmyaramesh via shwethags)
 
 ALL CHANGES:
+ATLAS-586 While updating the multiple attributes, Atlas returns the response with escape characters (dkantor via shwethags)
 ATLAS-582 Move Atlas UI to use backboneJS (kevalbhatt18 via shwethags)
 ATLAS-540 API to retrieve entity version events (shwethags)
 ATLAS-529 support drop database (sumasai)
diff --git a/webapp/src/main/java/org/apache/atlas/web/resources/EntityResource.java b/webapp/src/main/java/org/apache/atlas/web/resources/EntityResource.java
index 8e69378..b14aa80 100755
--- a/webapp/src/main/java/org/apache/atlas/web/resources/EntityResource.java
+++ b/webapp/src/main/java/org/apache/atlas/web/resources/EntityResource.java
@@ -19,6 +19,7 @@
 package org.apache.atlas.web.resources;
 
 import com.google.common.base.Preconditions;
+
 import org.apache.atlas.AtlasClient;
 import org.apache.atlas.AtlasException;
 import org.apache.atlas.EntityAuditEvent;
@@ -58,6 +59,7 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
+
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -165,9 +167,10 @@ public class EntityResource {
 
             JSONObject response = new JSONObject();
             response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
-            response.put(AtlasClient.GUID, new JSONArray(guids));
-            response.put(AtlasClient.DEFINITION, metadataService.getEntityDefinition(new JSONArray(guids).getString(0)));
-
+            JSONArray guidsArray = new JSONArray(guids);
+            response.put(AtlasClient.GUID, guidsArray);
+            String entityDefinition = metadataService.getEntityDefinition(guidsArray.getString(0));
+            response.put(AtlasClient.DEFINITION, new JSONObject(entityDefinition));
             return Response.ok(response).build();
         } catch(EntityExistsException e) {
             LOG.error("Unique constraint violation", e);
diff --git a/webapp/src/test/java/org/apache/atlas/web/resources/EntityJerseyResourceIT.java b/webapp/src/test/java/org/apache/atlas/web/resources/EntityJerseyResourceIT.java
index 83db46f..23f6874 100755
--- a/webapp/src/test/java/org/apache/atlas/web/resources/EntityJerseyResourceIT.java
+++ b/webapp/src/test/java/org/apache/atlas/web/resources/EntityJerseyResourceIT.java
@@ -20,6 +20,8 @@ package org.apache.atlas.web.resources;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
+import com.google.gson.Gson;
+import com.google.gson.JsonSyntaxException;
 import com.google.inject.Inject;
 import com.sun.jersey.api.client.ClientResponse;
 import com.sun.jersey.api.client.WebResource;
@@ -50,6 +52,7 @@ import org.apache.atlas.typesystem.types.utils.TypesUtil;
 import org.apache.atlas.web.util.Servlets;
 import org.apache.commons.lang.RandomStringUtils;
 import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONException;
 import org.codehaus.jettison.json.JSONObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -368,14 +371,19 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
     }
 
     private String getEntityDefinition(ClientResponse clientResponse) throws Exception {
-        Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
-        JSONObject response = new JSONObject(clientResponse.getEntity(String.class));
+        JSONObject response = getEntity(clientResponse);
         final String definition = response.getString(AtlasClient.DEFINITION);
         Assert.assertNotNull(definition);
 
         return definition;
     }
 
+    private JSONObject getEntity(ClientResponse clientResponse) throws JSONException {
+        Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
+        JSONObject response = new JSONObject(clientResponse.getEntity(String.class));
+        return response;
+    }
+
     @Test
     public void testGetInvalidEntityDefinition() throws Exception {
         WebResource resource = service.path(ENTITIES).path("blah");
@@ -730,12 +738,28 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
         columns.add(ref1);
         columns.add(ref2);
         tableInstance.set("columns", columns);
-
+        String entityJson = InstanceSerialization.toJson(tableInstance, true);
+        JSONArray entityArray = new JSONArray(1);
+        entityArray.put(entityJson);
         LOG.debug("Replacing entity= " + tableInstance);
-        serviceClient.updateEntities(tableInstance);
+        ClientResponse clientResponse = service.path(ENTITIES).
+            accept(Servlets.JSON_MEDIA_TYPE).type(Servlets.JSON_MEDIA_TYPE).
+            method(HttpMethod.PUT, ClientResponse.class, entityArray);
+        Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
 
-        ClientResponse response = getEntityDefinition(tableId._getId());
-        String definition = getEntityDefinition(response);
+        // ATLAS-586: verify response entity can be parsed by GSON.
+        String entity = clientResponse.getEntity(String.class);
+        Gson gson = new Gson();
+        UpdateEntitiesResponse updateEntitiesResponse = null;
+        try {
+            updateEntitiesResponse = gson.fromJson(entity, UpdateEntitiesResponse.class);
+        }
+        catch (JsonSyntaxException e) {
+            Assert.fail("Response entity from " + service.path(ENTITIES).getURI() + " not parseable by GSON", e);
+        }
+        
+        clientResponse = getEntityDefinition(tableId._getId());
+        String definition = getEntityDefinition(clientResponse);
         Referenceable getReferenceable = InstanceSerialization.fromJsonReferenceable(definition, true);
         List<Referenceable> refs = (List<Referenceable>) getReferenceable.get("columns");
         Assert.assertEquals(refs.size(), 2);
@@ -744,6 +768,17 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
         Assert.assertTrue(refs.get(1).equalsContents(columns.get(1)));
     }
     
+    private static class UpdateEntitiesResponse {
+        String requestId;
+        String[] GUID;
+        AtlasEntity definition;
+    }
+    
+    private static class AtlasEntity {
+        String typeName;
+        final Map<String, Object> values = new HashMap<String, Object>();
+    }
+    
     @Test
     public void testDeleteEntitiesViaRestApi() throws Exception {
         // Create 2 database entities
@@ -761,10 +796,7 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
             queryParam(AtlasClient.GUID.toLowerCase(), db1Id._getId()).
             queryParam(AtlasClient.GUID.toLowerCase(), db2Id._getId()).
             accept(Servlets.JSON_MEDIA_TYPE).type(Servlets.JSON_MEDIA_TYPE).method(HttpMethod.DELETE, ClientResponse.class);
-        Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
-
-        // Verify that response has guids for both database entities
-        JSONObject response = new JSONObject(clientResponse.getEntity(String.class));
+        JSONObject response = getEntity(clientResponse);
         final String deletedGuidsJson = response.getString(AtlasClient.GUID);
         Assert.assertNotNull(deletedGuidsJson);
         JSONArray guidsArray = new JSONArray(deletedGuidsJson);
--
libgit2 0.27.1