Clean up BrooklynJacksonSerializerIntegrationTest and mark test as Broken
Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/819f7252 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/819f7252 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/819f7252 Branch: refs/heads/master Commit: 819f7252ffbb394036da169fe48aac8205ae7fc5 Parents: 113f53b Author: Svetoslav Neykov <svetoslav.ney...@cloudsoftcorp.com> Authored: Mon Nov 14 13:54:29 2016 +0200 Committer: Svetoslav Neykov <svetoslav.ney...@cloudsoftcorp.com> Committed: Mon Nov 14 13:54:29 2016 +0200 ---------------------------------------------------------------------- ...rooklynJacksonSerializerIntegrationTest.java | 223 ++++++++++--------- 1 file changed, 120 insertions(+), 103 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/819f7252/rest/rest-server/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerIntegrationTest.java ---------------------------------------------------------------------- diff --git a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerIntegrationTest.java b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerIntegrationTest.java index 1cf79e8..2bb3203 100644 --- a/rest/rest-server/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerIntegrationTest.java +++ b/rest/rest-server/src/test/java/org/apache/brooklyn/rest/util/json/BrooklynJacksonSerializerIntegrationTest.java @@ -27,11 +27,14 @@ import javax.ws.rs.core.MediaType; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.mgmt.ManagementContext; import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.mgmt.internal.ManagementContextInternal; +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; 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.BrooklynRestApiLauncher; import org.apache.brooklyn.rest.util.json.BrooklynJacksonSerializerTest.SelfRefNonSerializableClass; +import org.apache.brooklyn.util.exceptions.Exceptions; import org.apache.brooklyn.util.http.HttpTool; import org.apache.http.client.HttpClient; import org.apache.http.client.utils.URIBuilder; @@ -40,15 +43,49 @@ import org.eclipse.jetty.server.Server; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.testng.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import com.google.common.collect.ImmutableMap; import com.google.gson.Gson; -public class BrooklynJacksonSerializerIntegrationTest { +public class BrooklynJacksonSerializerIntegrationTest extends BrooklynAppUnitTestSupport { private static final Logger log = LoggerFactory.getLogger(BrooklynJacksonSerializerIntegrationTest.class); + private Server server; + private HttpClient client; + URI entityUrl; + URI configUri; + + @BeforeMethod(alwaysRun = true) + public void setUp() throws Exception { + super.setUp(); + server = BrooklynRestApiLauncher.launcher().managementContext(mgmt).start(); + client = HttpTool.httpClientBuilder().build(); + + String serverAddress = "http://localhost:"+((NetworkConnector)server.getConnectors()[0]).getLocalPort(); + String appUrl = serverAddress + "/v1/applications/" + app.getId(); + entityUrl = URI.create(appUrl + "/entities/" + app.getId()); + configUri = new URIBuilder(entityUrl + "/config/" + TestEntity.CONF_OBJECT.getName()) + .addParameter("raw", "true") + .build(); + + } + + @AfterMethod(alwaysRun = true) + @Override + public void tearDown() throws Exception { + try { + if (server != null) server.stop(); + } catch (Exception e) { + Exceptions.propagateIfFatal(e); + log.warn("failed to stop server: "+e); + } + super.tearDown(); + } + // Ensure TEXT_PLAIN just returns toString for ManagementContext instance. // Strangely, testWithLauncherSerializingListsContainingEntitiesAndOtherComplexStuff ended up in the // EntityConfigResource.getPlain code, throwing a ClassCastException. @@ -57,117 +94,97 @@ public class BrooklynJacksonSerializerIntegrationTest { // testWithLauncherSerializingListsContainingEntitiesAndOtherComplexStuff was calling it. @Test(groups="Integration") //because of time public void testWithAcceptsPlainText() throws Exception { - ManagementContext mgmt = LocalManagementContextForTests.newInstance(); - Server server = null; - try { - server = BrooklynRestApiLauncher.launcher().managementContext(mgmt).start(); - HttpClient client = HttpTool.httpClientBuilder().build(); - - TestApplication app = TestApplication.Factory.newManagedInstanceForTests(mgmt); - - String serverAddress = "http://localhost:"+((NetworkConnector)server.getConnectors()[0]).getLocalPort(); - String appUrl = serverAddress + "/v1/applications/" + app.getId(); - String entityUrl = appUrl + "/entities/" + app.getId(); - URI configUri = new URIBuilder(entityUrl + "/config/" + TestEntity.CONF_OBJECT.getName()) - .addParameter("raw", "true") - .build(); - - // assert config here is just mgmt.toString() - app.config().set(TestEntity.CONF_OBJECT, mgmt); - String content = get(client, configUri, ImmutableMap.of("Accept", MediaType.TEXT_PLAIN)); - log.info("CONFIG MGMT is:\n"+content); - Assert.assertEquals(content, mgmt.toString(), "content="+content); - - } finally { - try { - if (server != null) server.stop(); - } catch (Exception e) { - log.warn("failed to stop server: "+e); - } - Entities.destroyAll(mgmt); - } + // assert config here is just mgmt.toString() + setConfig(mgmt); + String content = getConfigValueAsText(); + log.info("CONFIG MGMT is:\n"+content); + Assert.assertEquals(content, mgmt.toString(), "content="+content); } - + @Test(groups="Integration") //because of time - public void testWithLauncherSerializingListsContainingEntitiesAndOtherComplexStuff() throws Exception { - ManagementContext mgmt = LocalManagementContextForTests.newInstance(); - Server server = null; - try { - server = BrooklynRestApiLauncher.launcher().managementContext(mgmt).start(); - HttpClient client = HttpTool.httpClientBuilder().build(); - - TestApplication app = TestApplication.Factory.newManagedInstanceForTests(mgmt); - - String serverAddress = "http://localhost:"+((NetworkConnector)server.getConnectors()[0]).getLocalPort(); - String appUrl = serverAddress + "/v1/applications/" + app.getId(); - String entityUrl = appUrl + "/entities/" + app.getId(); - URI configUri = new URIBuilder(entityUrl + "/config/" + TestEntity.CONF_OBJECT.getName()) - .addParameter("raw", "true") - .build(); - - // assert config here is just mgmt - app.config().set(TestEntity.CONF_OBJECT, mgmt); - String content = get(client, configUri, ImmutableMap.of("Accept", MediaType.APPLICATION_JSON)); - log.info("CONFIG MGMT is:\n"+content); - @SuppressWarnings("rawtypes") - Map values = new Gson().fromJson(content, Map.class); - Assert.assertEquals(values, ImmutableMap.of("type", LocalManagementContextForTests.class.getCanonicalName()), "values="+values); - - // assert normal API returns the same, containing links - content = get(client, entityUrl, ImmutableMap.of("Accept", MediaType.APPLICATION_JSON)); - log.info("ENTITY is: \n"+content); - values = new Gson().fromJson(content, Map.class); - Assert.assertTrue(values.size()>=3, "Map is too small: "+values); - Assert.assertTrue(values.size()<=6, "Map is too big: "+values); - Assert.assertEquals(values.get("type"), TestApplication.class.getCanonicalName(), "values="+values); - Assert.assertNotNull(values.get("links"), "Map should have contained links: values="+values); - - // but config etc returns our nicely json serialized - app.config().set(TestEntity.CONF_OBJECT, app); - content = get(client, configUri, ImmutableMap.of("Accept", MediaType.APPLICATION_JSON)); - log.info("CONFIG ENTITY is:\n"+content); - values = new Gson().fromJson(content, Map.class); - Assert.assertEquals(values, ImmutableMap.of("type", Entity.class.getCanonicalName(), "id", app.getId()), "values="+values); - - // and self-ref gives error + toString - SelfRefNonSerializableClass angry = new SelfRefNonSerializableClass(); - app.config().set(TestEntity.CONF_OBJECT, angry); - content = get(client, configUri, ImmutableMap.of("Accept", MediaType.APPLICATION_JSON)); - log.info("CONFIG ANGRY is:\n"+content); - assertErrorObjectMatchingToString(content, angry); - - // as does Server - app.config().set(TestEntity.CONF_OBJECT, server); - content = get(client, configUri, ImmutableMap.of("Accept", MediaType.APPLICATION_JSON)); - // NOTE, if using the default visibility / object mapper, the getters of the object are invoked - // resulting in an object which is huge, 7+MB -- and it wreaks havoc w eclipse console regex parsing! - // (but with our custom VisibilityChecker server just gives us the nicer error!) - log.info("CONFIG SERVER is:\n"+content); - assertErrorObjectMatchingToString(content, server); - Assert.assertTrue(content.contains(NotSerializableException.class.getCanonicalName()), "server should have contained things which are not serializable"); - Assert.assertTrue(content.length() < 1024, "content should not have been very long; instead was: "+content.length()); - - } finally { - try { - if (server != null) server.stop(); - } catch (Exception e) { - log.warn("failed to stop server: "+e); - } - Entities.destroyAll(mgmt); - } + public void testWithMgmt() throws Exception { + setConfig(mgmt); + Map<?, ?> values = getConfigValueAsJson(); + Assert.assertEquals(values, ImmutableMap.of("type", LocalManagementContextForTests.class.getCanonicalName()), "values="+values); + + // assert normal API returns the same, containing links + values = getRestValueAsJson(entityUrl); + Assert.assertTrue(values.size()>=3, "Map is too small: "+values); + Assert.assertTrue(values.size()<=6, "Map is too big: "+values); + Assert.assertEquals(values.get("type"), TestApplication.class.getCanonicalName(), "values="+values); + Assert.assertNotNull(values.get("links"), "Map should have contained links: values="+values); + + } + + @Test(groups="Integration") //because of time + public void testWithApp() throws Exception { + // but config etc returns our nicely json serialized + setConfig(app); + Map<?, ?> values = getConfigValueAsJson(); + Assert.assertEquals(values, ImmutableMap.of("type", Entity.class.getCanonicalName(), "id", app.getId()), "values="+values); } - private void assertErrorObjectMatchingToString(String content, Object expected) { - Object value = new Gson().fromJson(content, Object.class); - Assert.assertTrue(value instanceof Map, "Expected map, got: "+value); - Assert.assertEquals(((Map<?,?>)value).get("toString"), expected.toString()); + @Test(groups="Integration") //because of time + public void testWithCyclic() throws Exception { + // and self-ref gives error + toString + SelfRefNonSerializableClass angry = new SelfRefNonSerializableClass(); + setConfig(angry); + Map<?, ?> values = getConfigValueAsJson(); + assertErrorObjectMatchingToString(values, angry); + } - private String get(HttpClient client, String uri, Map<String, String> headers) { - return get(client, URI.create(uri), headers); + // Broken. Serialization is failing because of "group" -> "threads" -> "group" cycle inside server (through the thread pool). + // It's doing best effort though - still serializing what it was able to before giving up. This results in a huge string + // which fails the assertions. + @Test(groups={"Integration", "Broken"}) //because of time + public void testWithServer() throws Exception { + // as does Server + setConfig(server); + Map<?, ?> values = getConfigValueAsJson(); + // NOTE, if using the default visibility / object mapper, the getters of the object are invoked + // resulting in an object which is huge, 7+MB -- and it wreaks havoc w eclipse console regex parsing! + // (but with our custom VisibilityChecker server just gives us the nicer error!) + String content = getRestValue(configUri, MediaType.APPLICATION_JSON); + log.info("CONFIG is:\n"+content); + values = new Gson().fromJson(content, Map.class); + assertErrorObjectMatchingToString(values, server); + Assert.assertTrue(content.contains(NotSerializableException.class.getCanonicalName()), "server should have contained things which are not serializable"); + Assert.assertTrue(content.length() < 1024, "content should not have been very long; instead was: "+content.length()); + } + + private void assertErrorObjectMatchingToString(Map<?, ?> content, Object expected) { + Assert.assertEquals(content.get("toString"), expected.toString()); } private String get(HttpClient client, URI uri, Map<String, String> headers) { return HttpTool.httpGet(client, uri, headers).getContentAsString(); } + + protected String getConfigValueAsText() { + return getRestValueAsText(configUri); + } + + protected String getRestValueAsText(URI url) { + return getRestValue(url, MediaType.TEXT_PLAIN); + } + + protected Map<?, ?> getConfigValueAsJson() { + return getRestValueAsJson(configUri); + } + + protected Map<?, ?> getRestValueAsJson(URI url) { + String content = getRestValue(url, MediaType.APPLICATION_JSON); + log.info("CONFIG is:\n"+content); + return new Gson().fromJson(content, Map.class); + } + + protected String getRestValue(URI url, String contentType) { + return get(client, url, ImmutableMap.of("Accept", contentType)); + } + + protected void setConfig(Object value) { + app.config().set(TestEntity.CONF_OBJECT, value); + } + }