brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjcorb...@apache.org
Subject [1/4] git commit: Fixes PolicyResource.addPolicy.
Date Tue, 19 Aug 2014 15:54:58 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 8071a9b9e -> 124fff8a3


Fixes PolicyResource.addPolicy.


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/96449efd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/96449efd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/96449efd

Branch: refs/heads/master
Commit: 96449efdb0133c94a2225ac11e9719707f7b3a0e
Parents: ed87ada
Author: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Authored: Wed Aug 13 15:40:18 2014 +0100
Committer: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Committed: Wed Aug 13 15:40:18 2014 +0100

----------------------------------------------------------------------
 .../main/java/brooklyn/rest/api/PolicyApi.java  |  8 +++---
 .../brooklyn/rest/resources/PolicyResource.java | 27 ++++++++++----------
 .../rest/resources/ApplicationResourceTest.java |  5 +++-
 .../rest/resources/ErrorResponseTest.java       |  6 ++++-
 .../rest/resources/PolicyResourceTest.java      | 26 +++++++++++--------
 5 files changed, 42 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/96449efd/usage/rest-api/src/main/java/brooklyn/rest/api/PolicyApi.java
----------------------------------------------------------------------
diff --git a/usage/rest-api/src/main/java/brooklyn/rest/api/PolicyApi.java b/usage/rest-api/src/main/java/brooklyn/rest/api/PolicyApi.java
index 903055b..56a03b6 100644
--- a/usage/rest-api/src/main/java/brooklyn/rest/api/PolicyApi.java
+++ b/usage/rest-api/src/main/java/brooklyn/rest/api/PolicyApi.java
@@ -64,12 +64,12 @@ public interface PolicyApi {
       @PathParam("entity") String entityToken) ;
   
   @POST
-  @ApiOperation(value = "Add a policy", notes="Returns ID of policy added; policy type must
have no-arg constructor " +
-  		"and setConfig(Map) method should be available if non-empty config is supplied")
+  @ApiOperation(value = "Add a policy", notes = "Returns a summary of the new policy")
   @ApiErrors(value = {
-      @ApiError(code = 404, reason = "Could not find application, entity or policy")
+      @ApiError(code = 404, reason = "Could not find application or entity"),
+      @ApiError(code = 400, reason = "Type is not a class implementing Policy")
   })
-  public String addPolicy(
+  public PolicySummary addPolicy(
       @ApiParam(name = "application", value = "Application ID or name", required = true)
       @PathParam("application") String application,
       

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/96449efd/usage/rest-server/src/main/java/brooklyn/rest/resources/PolicyResource.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/brooklyn/rest/resources/PolicyResource.java b/usage/rest-server/src/main/java/brooklyn/rest/resources/PolicyResource.java
index 6b77193..7b4317d 100644
--- a/usage/rest-server/src/main/java/brooklyn/rest/resources/PolicyResource.java
+++ b/usage/rest-server/src/main/java/brooklyn/rest/resources/PolicyResource.java
@@ -18,7 +18,6 @@
  */
 package brooklyn.rest.resources;
 
-import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 
@@ -30,6 +29,7 @@ import org.slf4j.LoggerFactory;
 import brooklyn.entity.Entity;
 import brooklyn.entity.basic.EntityLocal;
 import brooklyn.policy.Policy;
+import brooklyn.policy.PolicySpec;
 import brooklyn.policy.basic.Policies;
 import brooklyn.rest.api.PolicyApi;
 import brooklyn.rest.domain.PolicySummary;
@@ -37,6 +37,7 @@ import brooklyn.rest.domain.Status;
 import brooklyn.rest.domain.SummaryComparators;
 import brooklyn.rest.transform.ApplicationTransformer;
 import brooklyn.rest.transform.PolicyTransformer;
+import brooklyn.rest.util.WebResourceUtils;
 import brooklyn.util.exceptions.Exceptions;
 
 import com.google.common.base.Function;
@@ -74,26 +75,26 @@ public class PolicyResource extends AbstractBrooklynRestResource implements
Poli
     }
 
     @Override
-    public String addPolicy( String application,String entityToken, String policyTypeName,
+    public PolicySummary addPolicy( String application,String entityToken, String policyTypeName,
             // TODO would like to make this optional but jersey complains if we do
             Map<String, String> config
     ) {
         EntityLocal entity = brooklyn().getEntity(application, entityToken);
-
+        Class<? extends Policy> policyType;
         try {
-            Class<?> policyType = Class.forName(policyTypeName);
-            Policy policy = (Policy) policyType.newInstance();
-            if (config != null && !config.isEmpty()) {
-                // TODO support this:
-                //policy.setConfig(config);
-                policyType.getMethod("setConfig", Map.class).invoke(policy, config);
-            }
-            log.debug("REST API adding policy " + policy + " to " + entity);
-            entity.addPolicy(policy);
-            return policy.getId();
+            policyType = (Class<? extends Policy>) Class.forName(policyTypeName);
+        } catch (ClassNotFoundException e) {
+            throw WebResourceUtils.badRequest("No policy with type %s found", policyTypeName);
+        } catch (ClassCastException e) {
+            throw WebResourceUtils.badRequest("No policy with type %s found", policyTypeName);
         } catch (Exception e) {
             throw Exceptions.propagate(e);
         }
+
+        Policy policy = entity.addPolicy(PolicySpec.create(policyType).configure(config));
+        log.debug("REST API added policy " + policy + " to " + entity);
+
+        return PolicyTransformer.policySummary(entity, policy);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/96449efd/usage/rest-server/src/test/java/brooklyn/rest/resources/ApplicationResourceTest.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/test/java/brooklyn/rest/resources/ApplicationResourceTest.java
b/usage/rest-server/src/test/java/brooklyn/rest/resources/ApplicationResourceTest.java
index 22454f5..a4eed27 100644
--- a/usage/rest-server/src/test/java/brooklyn/rest/resources/ApplicationResourceTest.java
+++ b/usage/rest-server/src/test/java/brooklyn/rest/resources/ApplicationResourceTest.java
@@ -20,6 +20,7 @@ package brooklyn.rest.resources;
 
 import static com.google.common.collect.Iterables.find;
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertTrue;
 
 import java.io.IOException;
@@ -489,7 +490,9 @@ public class ApplicationResourceTest extends BrooklynRestResourceTest
{
           queryParam("type", CapitalizePolicy.class.getCanonicalName()).
           post(ClientResponse.class, Maps.newHashMap());
       assertEquals(response.getStatus(), 200);
-      String newPolicyId = response.getEntity(String.class);
+      PolicySummary policy = response.getEntity(PolicySummary.class);
+      assertNotNull(policy.getId());
+      String newPolicyId = policy.getId();
       log.info("POLICY CREATED: "+newPolicyId);
       policies = client().resource(policiesEndpoint).get(new GenericType<Set<PolicySummary>>(){});
       assertEquals(policies.size(), 1);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/96449efd/usage/rest-server/src/test/java/brooklyn/rest/resources/ErrorResponseTest.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/test/java/brooklyn/rest/resources/ErrorResponseTest.java
b/usage/rest-server/src/test/java/brooklyn/rest/resources/ErrorResponseTest.java
index 8518d7c..9a06a07 100644
--- a/usage/rest-server/src/test/java/brooklyn/rest/resources/ErrorResponseTest.java
+++ b/usage/rest-server/src/test/java/brooklyn/rest/resources/ErrorResponseTest.java
@@ -19,6 +19,7 @@
 package brooklyn.rest.resources;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertTrue;
 
 import javax.ws.rs.core.MediaType;
@@ -30,6 +31,7 @@ import org.testng.annotations.Test;
 import brooklyn.rest.domain.ApiError;
 import brooklyn.rest.domain.ApplicationSpec;
 import brooklyn.rest.domain.EntitySpec;
+import brooklyn.rest.domain.PolicySummary;
 import brooklyn.rest.testing.BrooklynRestResourceTest;
 import brooklyn.rest.testing.mocks.RestMockSimpleEntity;
 import brooklyn.rest.testing.mocks.RestMockSimplePolicy;
@@ -58,7 +60,9 @@ public class ErrorResponseTest extends BrooklynRestResourceTest {
         ClientResponse pResponse = client().resource(policiesEndpoint)
                 .queryParam("type", RestMockSimplePolicy.class.getCanonicalName())
                 .post(ClientResponse.class, Maps.newHashMap());
-        policyId = pResponse.getEntity(String.class);
+        PolicySummary response = pResponse.getEntity(PolicySummary.class);
+        assertNotNull(response.getId());
+        policyId = response.getId();
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/96449efd/usage/rest-server/src/test/java/brooklyn/rest/resources/PolicyResourceTest.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/test/java/brooklyn/rest/resources/PolicyResourceTest.java
b/usage/rest-server/src/test/java/brooklyn/rest/resources/PolicyResourceTest.java
index 119458d..cb1323d 100644
--- a/usage/rest-server/src/test/java/brooklyn/rest/resources/PolicyResourceTest.java
+++ b/usage/rest-server/src/test/java/brooklyn/rest/resources/PolicyResourceTest.java
@@ -19,6 +19,7 @@
 package brooklyn.rest.resources;
 
 import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.fail;
 
 import java.util.Map;
@@ -34,6 +35,7 @@ import org.testng.annotations.Test;
 import brooklyn.rest.domain.ApplicationSpec;
 import brooklyn.rest.domain.EntitySpec;
 import brooklyn.rest.domain.PolicyConfigSummary;
+import brooklyn.rest.domain.PolicySummary;
 import brooklyn.rest.testing.BrooklynRestResourceTest;
 import brooklyn.rest.testing.mocks.RestMockSimpleEntity;
 import brooklyn.rest.testing.mocks.RestMockSimplePolicy;
@@ -51,6 +53,8 @@ public class PolicyResourceTest extends BrooklynRestResourceTest {
     @SuppressWarnings("unused")
     private static final Logger log = LoggerFactory.getLogger(PolicyResourceTest.class);
 
+    private static final String ENDPOINT = "/v1/applications/simple-app/entities/simple-ent/policies/";
+
     private final ApplicationSpec simpleSpec = ApplicationSpec.builder().name("simple-app").entities(
             ImmutableSet.of(new EntitySpec("simple-ent", RestMockSimpleEntity.class.getName()))).locations(
             ImmutableSet.of("localhost")).build();
@@ -65,18 +69,19 @@ public class PolicyResourceTest extends BrooklynRestResourceTest {
         ClientResponse aResponse = clientDeploy(simpleSpec);
         waitForApplicationToBeRunning(aResponse.getLocation());
 
-        String policiesEndpoint = "/v1/applications/simple-app/entities/simple-ent/policies";
-
-        ClientResponse pResponse = client().resource(policiesEndpoint)
+        ClientResponse pResponse = client().resource(ENDPOINT)
                 .queryParam("type", RestMockSimplePolicy.class.getCanonicalName())
                 .post(ClientResponse.class, Maps.newHashMap());
-        policyId = pResponse.getEntity(String.class);
+
+        PolicySummary response = pResponse.getEntity(PolicySummary.class);
+        assertNotNull(response.getId());
+        policyId = response.getId();
 
     }
 
     @Test
     public void testListConfig() throws Exception {
-        Set<PolicyConfigSummary> config = client().resource("/v1/applications/simple-app/entities/simple-ent/policies/"
+ policyId + "/config")
+        Set<PolicyConfigSummary> config = client().resource(ENDPOINT + policyId + "/config")
                 .get(new GenericType<Set<PolicyConfigSummary>>() {});
         
         Set<String> configNames = Sets.newLinkedHashSet();
@@ -93,7 +98,7 @@ public class PolicyResourceTest extends BrooklynRestResourceTest {
     public void testGetNonExistantConfigReturns404() throws Exception {
         String invalidConfigName = "doesnotexist";
         try {
-            PolicyConfigSummary summary = client().resource("/v1/applications/simple-app/entities/simple-ent/policies/"
+ policyId + "/config/" + invalidConfigName)
+            PolicyConfigSummary summary = client().resource(ENDPOINT + policyId + "/config/"
+ invalidConfigName)
                     .get(PolicyConfigSummary.class);
             fail("Should have thrown 404, but got "+summary);
         } catch (Exception e) {
@@ -106,7 +111,7 @@ public class PolicyResourceTest extends BrooklynRestResourceTest {
         String configName = RestMockSimplePolicy.SAMPLE_CONFIG.getName();
         String expectedVal = RestMockSimplePolicy.SAMPLE_CONFIG.getDefaultValue();
         
-        String configVal = client().resource("/v1/applications/simple-app/entities/simple-ent/policies/"
+ policyId + "/config/" + configName)
+        String configVal = client().resource(ENDPOINT + policyId + "/config/" + configName)
                 .get(String.class);
         assertEquals(configVal, expectedVal);
     }
@@ -115,8 +120,7 @@ public class PolicyResourceTest extends BrooklynRestResourceTest {
     public void testReconfigureConfig() throws Exception {
         String configName = RestMockSimplePolicy.SAMPLE_CONFIG.getName();
         
-        ClientResponse response = client().resource(
-                "/v1/applications/simple-app/entities/simple-ent/policies/" + policyId +
"/config/" + configName + "/set")
+        ClientResponse response = client().resource(ENDPOINT + policyId + "/config/" + configName
+ "/set")
                 .queryParam("value", "newval")
                 .post(ClientResponse.class);
 
@@ -128,11 +132,11 @@ public class PolicyResourceTest extends BrooklynRestResourceTest {
         String configName = RestMockSimplePolicy.SAMPLE_CONFIG.getName();
         String expectedVal = "newval";
         
-        Map<String, Object> allState = client().resource("/v1/applications/simple-app/entities/simple-ent/policies/"
+ policyId + "/config/current-state")
+        Map<String, Object> allState = client().resource(ENDPOINT + policyId + "/config/current-state")
                 .get(new GenericType<Map<String, Object>>() {});
         assertEquals(allState, ImmutableMap.of(configName, expectedVal));
         
-        String configVal = client().resource("/v1/applications/simple-app/entities/simple-ent/policies/"
+ policyId + "/config/" + configName)
+        String configVal = client().resource(ENDPOINT + policyId + "/config/" + configName)
                 .get(String.class);
         assertEquals(configVal, expectedVal);
     }


Mime
View raw message