Repository: brooklyn-server Updated Branches: refs/heads/master 23cb3a080 -> 05a86be47
Replace http(s)://httpbin.org with a local server Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/a682daa6 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/a682daa6 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/a682daa6 Branch: refs/heads/master Commit: a682daa65333587ac365dfa1d4a988d77bfbf5e4 Parents: 23cb3a0 Author: Svetoslav Neykov <svetoslav.ney...@cloudsoftcorp.com> Authored: Fri Mar 24 12:15:47 2017 +0200 Committer: Svetoslav Neykov <svetoslav.ney...@cloudsoftcorp.com> Committed: Fri Mar 24 12:56:13 2017 +0200 ---------------------------------------------------------------------- .../CompositeEffectorYamlRebindTest.java | 29 ++- .../brooklyn/CompositeEffectorYamlTest.java | 27 ++- .../HttpCommandEffectorYamlRebindTest.java | 29 ++- .../brooklyn/HttpCommandEffectorYamlTest.java | 29 ++- .../http/HttpCommandEffectorHttpBinTest.java | 192 +++++++++++++++++++ .../HttpCommandEffectorIntegrationTest.java | 125 ------------ 6 files changed, 295 insertions(+), 136 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a682daa6/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/CompositeEffectorYamlRebindTest.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/CompositeEffectorYamlRebindTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/CompositeEffectorYamlRebindTest.java index 530abf6..1f33c78 100644 --- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/CompositeEffectorYamlRebindTest.java +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/CompositeEffectorYamlRebindTest.java @@ -27,10 +27,14 @@ import org.apache.brooklyn.api.effector.Effector; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.core.effector.CompositeEffector; import org.apache.brooklyn.core.effector.http.HttpCommandEffector; +import org.apache.brooklyn.core.effector.http.HttpCommandEffectorHttpBinTest; import org.apache.brooklyn.core.entity.EntityPredicates; import org.apache.brooklyn.core.entity.StartableApplication; import org.apache.brooklyn.core.test.entity.TestEntity; +import org.apache.brooklyn.test.http.TestHttpServer; import org.apache.brooklyn.util.guava.Maybe; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.google.common.base.Joiner; @@ -57,7 +61,7 @@ public class CompositeEffectorYamlRebindTest extends AbstractYamlRebindTest { " brooklyn.config:", " name: myEffector", " description: myDescription", - " uri: http://httpbin.org/get?id=myId", + " uri: ${serverUrl}/get?id=myId", " httpVerb: GET", " jsonPath: $.args.id", " publishSensor: results", @@ -69,9 +73,28 @@ public class CompositeEffectorYamlRebindTest extends AbstractYamlRebindTest { " - myEffector" ); - @Test(groups="Integration") + private TestHttpServer server; + private String serverUrl; + + @Override + @BeforeMethod(alwaysRun = true) + public void setUp() throws Exception { + super.setUp(); + server = HttpCommandEffectorHttpBinTest.createHttpBinServer(); + serverUrl = server.getUrl(); + } + + @Override + @AfterMethod(alwaysRun = true) + public void tearDown() throws Exception { + super.tearDown(); + server.stop(); + } + + + @Test public void testRebindWhenHealthy() throws Exception { - runRebindWhenIsUp(catalogYamlSimple, appVersionedId); + runRebindWhenIsUp(catalogYamlSimple.replace("${serverUrl}", serverUrl), appVersionedId); } protected void runRebindWhenIsUp(String catalogYaml, String appId) throws Exception { http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a682daa6/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/CompositeEffectorYamlTest.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/CompositeEffectorYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/CompositeEffectorYamlTest.java index 8d1ff0b..8833507 100644 --- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/CompositeEffectorYamlTest.java +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/CompositeEffectorYamlTest.java @@ -26,9 +26,13 @@ import org.apache.brooklyn.api.effector.Effector; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.core.effector.CompositeEffector; import org.apache.brooklyn.core.effector.http.HttpCommandEffector; +import org.apache.brooklyn.core.effector.http.HttpCommandEffectorHttpBinTest; import org.apache.brooklyn.entity.software.base.EmptySoftwareProcess; +import org.apache.brooklyn.test.http.TestHttpServer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.google.common.collect.ImmutableMap; @@ -37,7 +41,26 @@ import com.google.common.collect.Iterables; public class CompositeEffectorYamlTest extends AbstractYamlTest { private static final Logger log = LoggerFactory.getLogger(CompositeEffectorYamlTest.class); - @Test(groups="Integration") + private TestHttpServer server; + private String serverUrl; + + @Override + @BeforeMethod(alwaysRun = true) + public void setUp() throws Exception { + super.setUp(); + server = HttpCommandEffectorHttpBinTest.createHttpBinServer(); + serverUrl = server.getUrl(); + } + + @Override + @AfterMethod(alwaysRun = true) + public void tearDown() throws Exception { + super.tearDown(); + server.stop(); + } + + + @Test public void testCompositeEffector() throws Exception { Entity app = createAndStartApplication( "location: localhost", @@ -51,7 +74,7 @@ public class CompositeEffectorYamlTest extends AbstractYamlTest { " brooklyn.config:", " name: myEffector", " description: myDescription", - " uri: http://httpbin.org/get?id=myId", + " uri: " + serverUrl + "/get?id=myId", " httpVerb: GET", " jsonPath: $.args.id", " publishSensor: results", http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a682daa6/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/HttpCommandEffectorYamlRebindTest.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/HttpCommandEffectorYamlRebindTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/HttpCommandEffectorYamlRebindTest.java index 96db7d9..7565379 100644 --- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/HttpCommandEffectorYamlRebindTest.java +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/HttpCommandEffectorYamlRebindTest.java @@ -24,10 +24,14 @@ import static org.testng.Assert.assertEquals; import org.apache.brooklyn.api.effector.Effector; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.core.effector.http.HttpCommandEffector; +import org.apache.brooklyn.core.effector.http.HttpCommandEffectorHttpBinTest; import org.apache.brooklyn.core.entity.EntityPredicates; import org.apache.brooklyn.core.entity.StartableApplication; import org.apache.brooklyn.core.test.entity.TestEntity; +import org.apache.brooklyn.test.http.TestHttpServer; import org.apache.brooklyn.util.guava.Maybe; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.google.common.base.Joiner; @@ -54,14 +58,33 @@ public class HttpCommandEffectorYamlRebindTest extends AbstractYamlRebindTest { " brooklyn.config:", " name: myEffector", " description: myDescription", - " uri: http://httpbin.org/get?id=myId", + " uri: ${serverUrl}/get?id=myId", " httpVerb: GET", " jsonPath: $.args.id", " publishSensor: results"); - @Test(groups="Integration") + private TestHttpServer server; + private String serverUrl; + + @Override + @BeforeMethod(alwaysRun = true) + public void setUp() throws Exception { + super.setUp(); + server = HttpCommandEffectorHttpBinTest.createHttpBinServer(); + serverUrl = server.getUrl(); + } + + @Override + @AfterMethod(alwaysRun = true) + public void tearDown() throws Exception { + super.tearDown(); + server.stop(); + } + + + @Test public void testRebindWhenHealthy() throws Exception { - runRebindWhenIsUp(catalogYamlSimple, appVersionedId); + runRebindWhenIsUp(catalogYamlSimple.replace("${serverUrl}", serverUrl), appVersionedId); } protected void runRebindWhenIsUp(String catalogYaml, String appId) throws Exception { http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a682daa6/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/HttpCommandEffectorYamlTest.java ---------------------------------------------------------------------- diff --git a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/HttpCommandEffectorYamlTest.java b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/HttpCommandEffectorYamlTest.java index c1a1963..00e7cac 100644 --- a/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/HttpCommandEffectorYamlTest.java +++ b/camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/HttpCommandEffectorYamlTest.java @@ -23,9 +23,13 @@ import static org.testng.Assert.assertEquals; import org.apache.brooklyn.api.effector.Effector; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.core.effector.http.HttpCommandEffector; +import org.apache.brooklyn.core.effector.http.HttpCommandEffectorHttpBinTest; import org.apache.brooklyn.entity.software.base.EmptySoftwareProcess; +import org.apache.brooklyn.test.http.TestHttpServer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.google.common.collect.ImmutableMap; @@ -34,7 +38,26 @@ import com.google.common.collect.Iterables; public class HttpCommandEffectorYamlTest extends AbstractYamlTest { private static final Logger log = LoggerFactory.getLogger(HttpCommandEffectorYamlTest.class); - @Test(groups="Integration") + private TestHttpServer server; + private String serverUrl; + + @Override + @BeforeMethod(alwaysRun = true) + public void setUp() throws Exception { + super.setUp(); + server = HttpCommandEffectorHttpBinTest.createHttpBinServer(); + serverUrl = server.getUrl(); + } + + @Override + @AfterMethod(alwaysRun = true) + public void tearDown() throws Exception { + super.tearDown(); + server.stop(); + } + + + @Test public void testHttpCommandEffectorWithParameters() throws Exception { Entity app = createAndStartApplication( "location: localhost", @@ -48,7 +71,7 @@ public class HttpCommandEffectorYamlTest extends AbstractYamlTest { " brooklyn.config:", " name: myEffector", " description: myDescription", - " uri: http://httpbin.org/get?id=myId", + " uri: " + serverUrl + "/get?id=myId", " httpVerb: GET", " jsonPath: $.args.id", " publishSensor: results" @@ -60,7 +83,7 @@ public class HttpCommandEffectorYamlTest extends AbstractYamlTest { // Invoke with parameters { - Object result = entity.invoke(effector, ImmutableMap.of("uri", "http://httpbin.org/get?pwd=passwd", "jsonPath", "$.args.pwd")).get(); + Object result = entity.invoke(effector, ImmutableMap.of("uri", serverUrl + "/get?pwd=passwd", "jsonPath", "$.args.pwd")).get(); assertEquals(((String)result).trim(), "passwd"); } http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a682daa6/core/src/test/java/org/apache/brooklyn/core/effector/http/HttpCommandEffectorHttpBinTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/effector/http/HttpCommandEffectorHttpBinTest.java b/core/src/test/java/org/apache/brooklyn/core/effector/http/HttpCommandEffectorHttpBinTest.java new file mode 100644 index 0000000..e9eb491 --- /dev/null +++ b/core/src/test/java/org/apache/brooklyn/core/effector/http/HttpCommandEffectorHttpBinTest.java @@ -0,0 +1,192 @@ +/* + * 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.core.effector.http; + +import java.io.IOException; +import java.net.URI; +import java.util.List; + +import org.apache.brooklyn.api.effector.Effector; +import org.apache.brooklyn.api.entity.EntityLocal; +import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.location.Location; +import org.apache.brooklyn.core.effector.Effectors; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.sensor.Sensors; +import org.apache.brooklyn.core.test.entity.TestApplication; +import org.apache.brooklyn.core.test.entity.TestEntity; +import org.apache.brooklyn.test.http.TestHttpServer; +import org.apache.brooklyn.util.collections.Jsonya; +import org.apache.brooklyn.util.collections.Jsonya.Navigator; +import org.apache.brooklyn.util.collections.MutableMap; +import org.apache.brooklyn.util.core.config.ConfigBag; +import org.apache.http.HttpException; +import org.apache.http.HttpRequest; +import org.apache.http.HttpResponse; +import org.apache.http.NameValuePair; +import org.apache.http.client.utils.URLEncodedUtils; +import org.apache.http.entity.StringEntity; +import org.apache.http.message.BasicHttpRequest; +import org.apache.http.protocol.HttpContext; +import org.apache.http.protocol.HttpRequestHandler; +import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.jayway.jsonpath.JsonPath; + +public class HttpCommandEffectorHttpBinTest { + + final static Effector<String> EFFECTOR_HTTPBIN = Effectors.effector(String.class, "Httpbin").buildAbstract(); + + private TestHttpServer server; + private String serverUrl; + private TestApplication app; + private EntityLocal entity; + + public static final class HttpBinRequestHandler implements HttpRequestHandler { + private String serverUrl; + @Override + public void handle(HttpRequest request, HttpResponse response, HttpContext context) + throws HttpException, IOException { + Navigator<MutableMap<Object, Object>> j = Jsonya.newInstance().map(); + BasicHttpRequest req = (BasicHttpRequest)request; + String url = req.getRequestLine().getUri(); + URI uri = URI.create(url); + String method = req.getRequestLine().getMethod(); + boolean expectsPost = uri.getPath().equals("/post"); + if (expectsPost && !method.equals("POST") || + !expectsPost && !method.equals("GET")) { + throw new IllegalStateException("Method " + method + " not allowed on " + url); + } + List<NameValuePair> params = URLEncodedUtils.parse(uri, "UTF-8"); + if (!params.isEmpty()) { + j.push().at("args"); + for (NameValuePair param : params) { + j.put(param.getName(), param.getValue()); + } + j.pop(); + } + j.put("origin", "127.0.0.1"); + j.put("url", serverUrl + url); + + response.setHeader("Content-Type", "application/json"); + response.setStatusCode(200); + response.setEntity(new StringEntity(j.toString())); + } + public void setServerUrl(String serverUrl) { + this.serverUrl = serverUrl; + } + } + + @BeforeMethod(alwaysRun=true) + public void setUp() throws Exception { + app = TestApplication.Factory.newManagedInstanceForTests(); + entity = app.createAndManageChild(EntitySpec.create(TestEntity.class).location(TestApplication.LOCALHOST_MACHINE_SPEC)); + app.start(ImmutableList.<Location>of()); + server = createHttpBinServer(); + serverUrl = server.getUrl(); + } + + public static TestHttpServer createHttpBinServer() { + HttpBinRequestHandler handler = new HttpBinRequestHandler(); + TestHttpServer server = new TestHttpServer() + .handler("/get", handler) + .handler("/post", handler) + .handler("/ip", handler) + .start(); + handler.setServerUrl(server.getUrl()); + return server; + } + + @AfterMethod(alwaysRun=true) + public void tearDown() throws Exception { + if (app != null) Entities.destroyAll(app.getManagementContext()); + server.stop(); + } + + @Test + public void testHttpEffector() throws Exception { + new HttpCommandEffector(ConfigBag.newInstance() + .configure(HttpCommandEffector.EFFECTOR_NAME, "Httpbin") + .configure(HttpCommandEffector.EFFECTOR_URI, serverUrl + "/get?login=myLogin") + .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "GET") + ).apply(entity); + + String val = entity.invoke(EFFECTOR_HTTPBIN, MutableMap.<String,String>of()).get(); + Assert.assertEquals(JsonPath.parse(val).read("$.args.login", String.class), "myLogin"); + } + + @Test + public void testHttpEffectorWithPayload() throws Exception { + new HttpCommandEffector(ConfigBag.newInstance() + .configure(HttpCommandEffector.EFFECTOR_NAME, "HttpbinPost") + .configure(HttpCommandEffector.EFFECTOR_URI, serverUrl + "/post") + .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "POST") + .configure(HttpCommandEffector.EFFECTOR_HTTP_PAYLOAD, ImmutableMap.<String, Object>of( + "description", "Created via API", + "public", "false", + "files", ImmutableMap.of("demo.txt", ImmutableMap.of("content","Demo")))) + .configure(HttpCommandEffector.EFFECTOR_HTTP_HEADERS, ImmutableMap.of("Content-Type", "application/json")) + .configure(HttpCommandEffector.JSON_PATH, "$.url") + .configure(HttpCommandEffector.PUBLISH_SENSOR, "result") + ).apply(entity); + + String url = entity.invoke(Effectors.effector(String.class, "HttpbinPost").buildAbstract(), MutableMap.<String,String>of()).get(); + Assert.assertNotNull(url, "url"); + } + + @Test + public void testHttpEffectorWithJsonPath() throws Exception { + new HttpCommandEffector(ConfigBag.newInstance() + .configure(HttpCommandEffector.EFFECTOR_NAME, "Httpbin") + .configure(HttpCommandEffector.EFFECTOR_URI, serverUrl + "/get?id=myId") + .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "GET") + .configure(HttpCommandEffector.JSON_PATH, "$.args.id") + .configure(HttpCommandEffector.PUBLISH_SENSOR, "result") + ).apply(entity); + + String val = entity.invoke(EFFECTOR_HTTPBIN, MutableMap.<String,String>of()).get(); + Assert.assertEquals(val, "myId"); + Assert.assertEquals(entity.sensors().get(Sensors.newStringSensor("result")), "myId"); + } + + @Test + public void testHttpEffectorWithParameters() throws Exception { + new HttpCommandEffector(ConfigBag.newInstance() + .configure(HttpCommandEffector.EFFECTOR_NAME, "Httpbin") + .configure(HttpCommandEffector.EFFECTOR_URI, serverUrl + "/get") + .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "GET") + .configure(HttpCommandEffector.EFFECTOR_PARAMETER_DEFS, + MutableMap.<String,Object>of("uri", MutableMap.of("defaultValue", serverUrl + "/get")))) + .apply(entity); + + String val; + // explicit value + val = entity.invoke(EFFECTOR_HTTPBIN, MutableMap.of("uri", serverUrl + "/ip")).get(); + Assert.assertNotNull(JsonPath.parse(val).read("$.origin", String.class)); + + // default value + val = entity.invoke(EFFECTOR_HTTPBIN, MutableMap.<String,String>of()).get(); + Assert.assertEquals(JsonPath.parse(val).read("$.url", String.class), serverUrl + "/get"); + } +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a682daa6/core/src/test/java/org/apache/brooklyn/core/effector/http/HttpCommandEffectorIntegrationTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/brooklyn/core/effector/http/HttpCommandEffectorIntegrationTest.java b/core/src/test/java/org/apache/brooklyn/core/effector/http/HttpCommandEffectorIntegrationTest.java deleted file mode 100644 index c99c9c3..0000000 --- a/core/src/test/java/org/apache/brooklyn/core/effector/http/HttpCommandEffectorIntegrationTest.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * 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.core.effector.http; - -import org.apache.brooklyn.api.effector.Effector; -import org.apache.brooklyn.api.entity.EntityLocal; -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.api.location.Location; -import org.apache.brooklyn.core.effector.Effectors; -import org.apache.brooklyn.core.entity.Entities; -import org.apache.brooklyn.core.sensor.Sensors; -import org.apache.brooklyn.core.test.entity.TestApplication; -import org.apache.brooklyn.core.test.entity.TestEntity; -import org.apache.brooklyn.util.collections.MutableMap; -import org.apache.brooklyn.util.core.config.ConfigBag; -import org.testng.Assert; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.jayway.jsonpath.JsonPath; - -public class HttpCommandEffectorIntegrationTest { - - final static Effector<String> EFFECTOR_HTTPBIN = Effectors.effector(String.class, "Httpbin").buildAbstract(); - - private TestApplication app; - private EntityLocal entity; - - @BeforeMethod(alwaysRun=true) - public void setUp() throws Exception { - app = TestApplication.Factory.newManagedInstanceForTests(); - entity = app.createAndManageChild(EntitySpec.create(TestEntity.class).location(TestApplication.LOCALHOST_MACHINE_SPEC)); - app.start(ImmutableList.<Location>of()); - } - - @AfterMethod(alwaysRun=true) - public void tearDown() throws Exception { - if (app != null) Entities.destroyAll(app.getManagementContext()); - } - - @Test(groups="Integration") - public void testHttpEffector() throws Exception { - new HttpCommandEffector(ConfigBag.newInstance() - .configure(HttpCommandEffector.EFFECTOR_NAME, "Httpbin") - .configure(HttpCommandEffector.EFFECTOR_URI, "http://httpbin.org/get?login=myLogin") - .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "GET") - ).apply(entity); - - String val = entity.invoke(EFFECTOR_HTTPBIN, MutableMap.<String,String>of()).get(); - Assert.assertEquals(JsonPath.parse(val).read("$.args.login", String.class), "myLogin"); - } - - @Test(groups="Integration") - public void testHttpEffectorWithPayload() throws Exception { - new HttpCommandEffector(ConfigBag.newInstance() - .configure(HttpCommandEffector.EFFECTOR_NAME, "HttpbinPost") - .configure(HttpCommandEffector.EFFECTOR_URI, "http://httpbin.org/post") - .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "POST") - .configure(HttpCommandEffector.EFFECTOR_HTTP_PAYLOAD, ImmutableMap.<String, Object>of( - "description", "Created via API", - "public", "false", - "files", ImmutableMap.of("demo.txt", ImmutableMap.of("content","Demo")))) - .configure(HttpCommandEffector.EFFECTOR_HTTP_HEADERS, ImmutableMap.of("Content-Type", "application/json")) - .configure(HttpCommandEffector.JSON_PATH, "$.url") - .configure(HttpCommandEffector.PUBLISH_SENSOR, "result") - ).apply(entity); - - String url = entity.invoke(Effectors.effector(String.class, "HttpbinPost").buildAbstract(), MutableMap.<String,String>of()).get(); - Assert.assertNotNull(url, "url"); - } - - @Test(groups="Integration") - public void testHttpEffectorWithJsonPath() throws Exception { - new HttpCommandEffector(ConfigBag.newInstance() - .configure(HttpCommandEffector.EFFECTOR_NAME, "Httpbin") - .configure(HttpCommandEffector.EFFECTOR_URI, "http://httpbin.org/get?id=myId") - .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "GET") - .configure(HttpCommandEffector.JSON_PATH, "$.args.id") - .configure(HttpCommandEffector.PUBLISH_SENSOR, "result") - ).apply(entity); - - String val = entity.invoke(EFFECTOR_HTTPBIN, MutableMap.<String,String>of()).get(); - Assert.assertEquals(val, "myId"); - Assert.assertEquals(entity.sensors().get(Sensors.newStringSensor("result")), "myId"); - } - - @Test(groups="Integration") - public void testHttpEffectorWithParameters() throws Exception { - new HttpCommandEffector(ConfigBag.newInstance() - .configure(HttpCommandEffector.EFFECTOR_NAME, "Httpbin") - .configure(HttpCommandEffector.EFFECTOR_URI, "http://httpbin.org/get") - .configure(HttpCommandEffector.EFFECTOR_HTTP_VERB, "GET") - .configure(HttpCommandEffector.EFFECTOR_PARAMETER_DEFS, - MutableMap.<String,Object>of("uri", MutableMap.of("defaultValue", "http://httpbin.org/get")))) - .apply(entity); - - String val; - // explicit value - val = entity.invoke(EFFECTOR_HTTPBIN, MutableMap.of("uri", "http://httpbin.org/ip")).get(); - Assert.assertNotNull(JsonPath.parse(val).read("$.origin", String.class)); - - // default value - val = entity.invoke(EFFECTOR_HTTPBIN, MutableMap.<String,String>of()).get(); - Assert.assertEquals(JsonPath.parse(val).read("$.url", String.class), "http://httpbin.org/get"); - } -}