You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by sv...@apache.org on 2016/12/01 12:44:40 UTC

[2/3] brooklyn-server git commit: Add API test for format of retrieved multimap sensor

Add API test for format of retrieved multimap sensor

- unit test for deserializing multimap object


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

Branch: refs/heads/master
Commit: 6d710b7a77220f5ecba82fd32d7d04480ffa5855
Parents: cbc1b2c
Author: Valentin Aitken <bo...@gmail.com>
Authored: Thu Dec 1 01:45:11 2016 +0200
Committer: Valentin Aitken <bo...@gmail.com>
Committed: Thu Dec 1 13:50:31 2016 +0200

----------------------------------------------------------------------
 .../util/core/json/MultimapSerializer.java      |  2 +-
 .../json/BrooklynJacksonSerializerTest.java     |  9 +++
 .../BrooklynRestApiLauncherTestFixture.java     | 42 ++++++++++++-
 .../AbstractRestApiEntitlementsTest.java        | 25 --------
 .../brooklyn/rest/entity/SensorsApiTest.java    | 64 ++++++++++++++++++++
 5 files changed, 115 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/6d710b7a/core/src/main/java/org/apache/brooklyn/util/core/json/MultimapSerializer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/json/MultimapSerializer.java b/core/src/main/java/org/apache/brooklyn/util/core/json/MultimapSerializer.java
index d825e3e..91287dd 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/json/MultimapSerializer.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/json/MultimapSerializer.java
@@ -61,4 +61,4 @@ public class MultimapSerializer extends StdSerializer<Multimap<?, ?>> {
             provider.defaultSerializeValue(Lists.newArrayList(entry.getValue()), jgen);
         }
     }
-}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/6d710b7a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerTest.java
----------------------------------------------------------------------
diff --git a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerTest.java b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerTest.java
index eb91f0a..4f5a6ca 100644
--- a/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerTest.java
+++ b/rest/rest-resources/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerTest.java
@@ -32,6 +32,7 @@ import org.apache.brooklyn.util.collections.MutableList;
 import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.core.json.BidiSerialization;
 import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.net.UserAndHostAndPort;
 import org.apache.brooklyn.util.stream.Streams;
 import org.apache.brooklyn.util.text.Strings;
 import org.slf4j.Logger;
@@ -203,6 +204,14 @@ public class BrooklynJacksonSerializerTest {
     }
 
     @Test
+    public void testUserHostAndPortSerialization() throws Exception {
+        String result = checkSerializesAs(UserAndHostAndPort.fromParts("testHostUser", "1.2.3.4", 22), null);
+        log.info("UserHostAndPort serialized as: " + result);
+        Assert.assertFalse(result.contains("error"), "Shouldn't have had an error, instead got: "+result);
+        Assert.assertEquals(Strings.collapseWhitespace(result, ""), "{\"user\":\"testHostUser\",\"hostAndPort\":{\"host\":\"1.2.3.4\",\"port\":22,\"hasBracketlessColons\":false}}");
+    }
+
+    @Test
     public void testSupplierSerialization() throws Exception {
         String result = checkSerializesAs(Strings.toStringSupplier(Streams.byteArrayOfString("x")), null);
         log.info("SUPPLIER json is: "+result);

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/6d710b7a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTestFixture.java
----------------------------------------------------------------------
diff --git a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTestFixture.java b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTestFixture.java
index 53068c0..5a102bc 100644
--- a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTestFixture.java
+++ b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/BrooklynRestApiLauncherTestFixture.java
@@ -18,6 +18,7 @@
  */
 package org.apache.brooklyn.rest;
 
+import com.google.common.collect.ImmutableMap;
 import org.apache.brooklyn.api.mgmt.ManagementContext;
 import org.apache.brooklyn.core.entity.Entities;
 import org.apache.brooklyn.core.internal.BrooklynProperties;
@@ -27,6 +28,11 @@ import org.apache.brooklyn.core.server.BrooklynServiceAttributes;
 import org.apache.brooklyn.rest.security.provider.AnyoneSecurityProvider;
 import org.apache.brooklyn.util.core.osgi.Compat;
 import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.http.HttpAsserts;
+import org.apache.brooklyn.util.http.HttpTool;
+import org.apache.brooklyn.util.http.HttpToolResponse;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.client.HttpClient;
 import org.eclipse.jetty.server.NetworkConnector;
 import org.eclipse.jetty.server.Server;
 import org.eclipse.jetty.server.handler.ContextHandler;
@@ -34,6 +40,11 @@ import org.reflections.util.ClasspathHelper;
 import org.testng.Assert;
 import org.testng.annotations.AfterMethod;
 
+import java.net.URI;
+
+import static org.apache.brooklyn.util.http.HttpTool.httpClientBuilder;
+import static org.testng.Assert.assertTrue;
+
 public abstract class BrooklynRestApiLauncherTestFixture {
 
     Server server = null;
@@ -47,7 +58,7 @@ public abstract class BrooklynRestApiLauncherTestFixture {
             server = null;
         }
     }
-    
+
     protected Server newServer() {
         try {
             Server server = baseLauncher()
@@ -72,6 +83,35 @@ public abstract class BrooklynRestApiLauncherTestFixture {
         return BrooklynRestApiLauncher.launcher()
                 .securityProvider(AnyoneSecurityProvider.class);
     }
+
+    protected HttpClient newClient(String user) throws Exception {
+        HttpTool.HttpClientBuilder builder = httpClientBuilder()
+                .uri(getBaseUriRest());
+        if (user != null) {
+            builder.credentials(new UsernamePasswordCredentials(user, "ignoredPassword"));
+        }
+        return builder.build();
+    }
+
+    protected String httpGet(String path) throws Exception {
+        return httpGet(null, path);
+    }
+
+    protected String httpGet(String user, String path) throws Exception {
+        HttpToolResponse response = HttpTool.httpGet(newClient(user), URI.create(getBaseUriRest()).resolve(path), ImmutableMap.<String, String>of());
+        assertHealthyStatusCode(response);
+        return response.getContentAsString();
+    }
+
+    protected HttpToolResponse httpPost(String user, String path, byte[] body) throws Exception {
+        final ImmutableMap<String, String> headers = ImmutableMap.of();
+        final URI uri = URI.create(getBaseUriRest()).resolve(path);
+        return HttpTool.httpPost(newClient(user), uri, headers, body);
+    }
+
+    protected void assertHealthyStatusCode(HttpToolResponse response) {
+        assertTrue(HttpAsserts.isHealthyStatusCode(response.getResponseCode()), "code="+response.getResponseCode()+"; reason="+response.getReasonPhrase());
+    }
     
     /** @deprecated since 0.9.0 use {@link #getBaseUriHostAndPost(Server)} or {@link #getBaseUriRest(Server)} */
     public static String getBaseUri(Server server) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/6d710b7a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/entitlement/AbstractRestApiEntitlementsTest.java
----------------------------------------------------------------------
diff --git a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/entitlement/AbstractRestApiEntitlementsTest.java b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/entitlement/AbstractRestApiEntitlementsTest.java
index 1181e94..ad16226 100644
--- a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/entitlement/AbstractRestApiEntitlementsTest.java
+++ b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/entitlement/AbstractRestApiEntitlementsTest.java
@@ -85,27 +85,6 @@ public abstract class AbstractRestApiEntitlementsTest extends BrooklynRestApiLau
         if (mgmt != null) Entities.destroyAll(mgmt);
     }
     
-    protected HttpClient newClient(String user) throws Exception {
-        HttpClientBuilder builder = httpClientBuilder()
-                .uri(getBaseUriRest());
-        if (user != null) {
-            builder.credentials(new UsernamePasswordCredentials(user, "ignoredPassword"));
-        }
-        return builder.build();
-    }
-
-    protected String httpGet(String user, String path) throws Exception {
-        HttpToolResponse response = HttpTool.httpGet(newClient(user), URI.create(getBaseUriRest()).resolve(path), ImmutableMap.<String, String>of());
-        assertHealthyStatusCode(response);
-        return response.getContentAsString();
-    }
-
-    protected HttpToolResponse httpPost(String user, String path, byte[] body) throws Exception {
-        final ImmutableMap<String, String> headers = ImmutableMap.of();
-        final URI uri = URI.create(getBaseUriRest()).resolve(path);
-        return HttpTool.httpPost(newClient(user), uri, headers, body);
-    }
-
     protected String assertPermitted(String user, String path) throws Exception {
         return httpGet(user, path);
     }
@@ -115,10 +94,6 @@ public abstract class AbstractRestApiEntitlementsTest extends BrooklynRestApiLau
         assertHealthyStatusCode(response);
     }
 
-    protected void assertHealthyStatusCode(HttpToolResponse response) {
-        assertTrue(HttpAsserts.isHealthyStatusCode(response.getResponseCode()), "code="+response.getResponseCode()+"; reason="+response.getReasonPhrase());
-    }
-
     protected void assertForbidden(String user, String path) throws Exception {
         HttpToolResponse response = HttpTool.httpGet(newClient(user), URI.create(getBaseUriRest()).resolve(path), ImmutableMap.<String, String>of());
         assertStatusCodeEquals(response, 403);

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/6d710b7a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/entity/SensorsApiTest.java
----------------------------------------------------------------------
diff --git a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/entity/SensorsApiTest.java b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/entity/SensorsApiTest.java
new file mode 100644
index 0000000..c2fc701
--- /dev/null
+++ b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/entity/SensorsApiTest.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.brooklyn.rest.entity;
+
+import com.google.common.collect.Iterables;
+import org.apache.brooklyn.api.entity.EntitySpec;
+import org.apache.brooklyn.api.mgmt.ManagementContext;
+import org.apache.brooklyn.core.entity.Attributes;
+import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
+import org.apache.brooklyn.core.test.entity.TestApplication;
+import org.apache.brooklyn.core.test.entity.TestEntity;
+import org.apache.brooklyn.rest.BrooklynRestApiLauncherTestFixture;
+import org.apache.brooklyn.util.net.UserAndHostAndPort;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+
+public class SensorsApiTest extends BrooklynRestApiLauncherTestFixture {
+    protected ManagementContext mgmt;
+    protected TestApplication app;
+    protected TestEntity entity;
+
+    @BeforeMethod(alwaysRun=true)
+    public void setUp() throws Exception {
+        mgmt = LocalManagementContextForTests.builder(false).build();
+        app = mgmt.getEntityManager().createEntity(EntitySpec.create(TestApplication.class)
+                .child(EntitySpec.create(TestEntity.class)));
+        entity = (TestEntity) Iterables.getOnlyElement(app.getChildren());
+
+        useServerForTest(baseLauncher()
+                .managementContext(mgmt)
+                .forceUseOfDefaultCatalogWithJavaClassPath(true)
+                .start());
+    }
+
+    // TODO turn into unit tests, not spinning up real http server.
+    @Test(groups = "Integration")
+    public void testGetHostAndPortSensor() throws Exception {
+        entity.sensors().set(Attributes.SSH_ADDRESS, UserAndHostAndPort.fromParts("testHostUser", "1.2.3.4", 22));
+
+        String sensorGetPath = "/v1/applications/"+app.getId()+"/entities/"+entity.getId()+"/sensors/" + Attributes.SSH_ADDRESS.getName();
+        assertEquals(httpGet(sensorGetPath), "\"testHostUser@1.2.3.4:22\"");
+
+        String descendantSensorPath = "/v1/applications/"+app.getId()+"/descendants/sensor/" + Attributes.SSH_ADDRESS.getName();
+        assertEquals(httpGet(descendantSensorPath), "{\"" + entity.getId() + "\":{\"user\":\"testHostUser\",\"hostAndPort\":{\"host\":\"1.2.3.4\",\"port\":22,\"hasBracketlessColons\":false}}}");
+    }
+}