Repository: brooklyn-server
Updated Branches:
  refs/heads/master 56f7258e8 -> 53f485826


rest api: entity/spec returns unquoted text_plain

Previously it returned json:
* it wrapped it in quotes.
* it did strange things with \n: the result rendered as “\n” rather than
  actual new-line characters (though it was more complicated than that
  in that an angular UI did recognise the “\n” as a single character).


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/15d10147
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/15d10147
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/15d10147

Branch: refs/heads/master
Commit: 15d1014789f9a4945330ff8f6acc6f5dae27f95c
Parents: e9d84bf
Author: Aled Sage <aled.s...@gmail.com>
Authored: Thu Jul 28 14:59:39 2016 +0100
Committer: Aled Sage <aled.s...@gmail.com>
Committed: Thu Jul 28 15:02:04 2016 +0100

----------------------------------------------------------------------
 .../org/apache/brooklyn/rest/api/EntityApi.java |  2 +
 .../brooklyn/rest/resources/EntityResource.java |  2 +-
 .../rest/resources/EntityResourceTest.java      | 42 ++++++++++++++++++++
 3 files changed, 45 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/15d10147/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/EntityApi.java
----------------------------------------------------------------------
diff --git 
a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/EntityApi.java 
b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/EntityApi.java
index e5683ed..b3cae2f 100644
--- a/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/EntityApi.java
+++ b/rest/rest-api/src/main/java/org/apache/brooklyn/rest/api/EntityApi.java
@@ -221,9 +221,11 @@ public interface EntityApi {
             @ApiParam(value = "Entity ID or name", required = true)
             @PathParam("entity") String entity);
 
+    // see http://stackoverflow.com/questions/332129/yaml-mime-type for 
"@Produces"
     @GET
     @Path("/{entity}/spec")
     @ApiOperation(value = "Get the YAML spec used to create the entity, if 
available")
+    @Produces({"text/x-yaml", "application/x-yaml", "text/yaml", "text/plain", 
"application/yaml", MediaType.TEXT_PLAIN})
     @ApiResponses(value = {
             @ApiResponse(code = 404, message = "Application or entity missing")
     })

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/15d10147/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/EntityResource.java
----------------------------------------------------------------------
diff --git 
a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/EntityResource.java
 
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/EntityResource.java
index 4d263da..c67eaaa 100644
--- 
a/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/EntityResource.java
+++ 
b/rest/rest-resources/src/main/java/org/apache/brooklyn/rest/resources/EntityResource.java
@@ -218,6 +218,6 @@ public class EntityResource extends 
AbstractBrooklynRestResource implements Enti
         NamedStringTag spec = 
BrooklynTags.findFirst(BrooklynTags.YAML_SPEC_KIND, entity.tags().getTags());
         if (spec == null)
             return null;
-        return (String) 
WebResourceUtils.getValueForDisplay(spec.getContents(), true, true);
+        return (String) 
WebResourceUtils.getValueForDisplay(spec.getContents(), false, true);
     }
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/15d10147/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/EntityResourceTest.java
----------------------------------------------------------------------
diff --git 
a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/EntityResourceTest.java
 
b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/EntityResourceTest.java
index decbcef..62ee189 100644
--- 
a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/EntityResourceTest.java
+++ 
b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/resources/EntityResourceTest.java
@@ -18,6 +18,8 @@
  */
 package org.apache.brooklyn.rest.resources;
 
+import static org.testng.Assert.assertEquals;
+
 import java.util.Collection;
 import java.util.List;
 import java.util.Map;
@@ -30,6 +32,7 @@ import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.core.entity.EntityInternal;
 import org.apache.brooklyn.core.test.entity.TestEntity;
 import org.apache.brooklyn.entity.stock.BasicApplication;
+import org.apache.brooklyn.entity.stock.BasicEntity;
 import org.apache.brooklyn.rest.domain.ApplicationSpec;
 import org.apache.brooklyn.rest.domain.EntitySpec;
 import org.apache.brooklyn.rest.domain.TaskSummary;
@@ -46,6 +49,7 @@ import org.testng.annotations.Test;
 
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.base.Joiner;
 import com.google.common.base.Predicate;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
@@ -85,6 +89,44 @@ public class EntityResourceTest extends 
BrooklynRestResourceTest {
     }
 
     @Test
+    public void testGetSpecAcceptingXYaml() throws Exception {
+        testGetSpec("text/x-yaml");
+    }
+    
+    @Test
+    public void testGetSpecWithNoExplicitAccepts() throws Exception {
+        testGetSpec(null);
+    }
+    
+    protected void testGetSpec(String acceptMimeType) throws Exception {
+        String appName = "ent-with-spec";
+        
+        // Create an app with a yaml spec
+        String yaml = Joiner.on("\n").join(
+                "name: " + appName,
+                "services:",
+                "- type: "+BasicEntity.class.getName());
+        Response appResponse = client().path("/applications")
+                .header("Content-Type", "text/x-yaml")
+                .post(yaml);
+        waitForApplicationToBeRunning(appResponse.getLocation());
+
+        // Retrieve the yaml spec, and confirm it is as expected (not wrapped 
in quotes, and treating \n sensibly)
+        Response response;
+        if (acceptMimeType != null) {
+            response = client().path("/applications/" + appName + "/entities/" 
+ appName + "/spec")
+                    .accept(acceptMimeType)
+                    .get();
+        } else {
+            response = client().path("/applications/" + appName + "/entities/" 
+ appName + "/spec")
+                    .get();
+        }
+        String data = response.readEntity(String.class);
+
+        assertEquals(data.trim(), yaml.trim());
+    }
+    
+    @Test
     public void testTagsSanity() throws Exception {
         entity.tags().addTag("foo");
         

Reply via email to