diff options
author | Bjørn Christian Seime <bjorncs@yahoo-inc.com> | 2017-03-30 21:04:42 +0200 |
---|---|---|
committer | Bjørn Christian Seime <bjorncs@yahoo-inc.com> | 2017-03-30 21:04:42 +0200 |
commit | 6f348dc6c11caee726d643e9768dbaf352940f21 (patch) | |
tree | f2dedc70b6bffabcda035e1a1bb7add63e61d178 | |
parent | e1abfd25bae0956b549eebe2be03a159565451b3 (diff) |
Convert Scala code to Java
4 files changed, 334 insertions, 351 deletions
diff --git a/config-model/src/test/java/com/yahoo/vespa/model/container/jersey/xml/MultipleRestApisTest.java b/config-model/src/test/java/com/yahoo/vespa/model/container/jersey/xml/MultipleRestApisTest.java new file mode 100644 index 00000000000..050f8cfe502 --- /dev/null +++ b/config-model/src/test/java/com/yahoo/vespa/model/container/jersey/xml/MultipleRestApisTest.java @@ -0,0 +1,121 @@ +// Copyright 2017 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.model.container.jersey.xml; + +import com.yahoo.component.ComponentId; +import com.yahoo.config.model.builder.xml.test.DomBuilderTest; +import com.yahoo.container.ComponentsConfig; +import com.yahoo.container.di.config.JerseyBundlesConfig; +import com.yahoo.container.jdisc.JdiscBindingsConfig; +import com.yahoo.vespa.model.container.jersey.JerseyHandler; +import com.yahoo.vespa.model.container.jersey.RestApi; +import com.yahoo.vespa.model.container.jersey.RestApiContext; +import com.yahoo.vespa.model.container.xml.ContainerModelBuilderTestBase; +import org.junit.Before; +import org.junit.Test; + +import java.util.Map; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.nullValue; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertTrue; + +/** + * @author bjorncs + */ +public class MultipleRestApisTest extends ContainerModelBuilderTestBase { + + private static final String CLUSTER_ID = "container"; + private static final String PATH_1 = "rest_1"; + private static final String PATH_2 = "rest_2"; + private static final String HTTP_BINDING_1 = "http://*/" + PATH_1 + "/*"; + private static final String HTTPS_BINDING_1 = "https://*/" + PATH_1 + "/*"; + private static final String HTTP_BINDING_2 = "http://*/" + PATH_2 + "/*"; + private static final String HTTPS_BINDING_2 = "https://*/" + PATH_2 + "/*"; + private static final String HANDLER_ID_1 = JerseyHandler.CLASS + "-" + PATH_1; + private static final String HANDLER_ID_2 = JerseyHandler.CLASS + "-" + PATH_2; + private static final String REST_API_CONTEXT_ID_1 = RestApiContext.CONTAINER_CLASS + "-" + PATH_1; + private static final String REST_API_CONTEXT_ID_2 = RestApiContext.CONTAINER_CLASS + "-" + PATH_2; + private static final String REST_API_XML = + "<container version=\"1.0\" id=\"" + CLUSTER_ID + "\">\n" + + " <rest-api path=\"" + PATH_1 + "\">\n" + + " <components bundle=\"bundle1\" />\n" + + " </rest-api>\n" + + " <rest-api path=\"" + PATH_2 + "\">\n" + + " <components bundle=\"bundle2\" />\n" + + " </rest-api>\n" + + "</container>"; + + + private JerseyHandler handler1; + private JerseyHandler handler2; + private Map<ComponentId, RestApi> restApis; + + @Before + public void setup() throws Exception { + createModel(root, DomBuilderTest.parse(REST_API_XML)); + handler1 = (JerseyHandler)getContainerComponentNested(CLUSTER_ID, HANDLER_ID_1); + handler2 = (JerseyHandler)getContainerComponentNested(CLUSTER_ID, HANDLER_ID_2); + restApis = getContainerCluster(CLUSTER_ID).getRestApiMap(); + } + + @Test + public void cluster_has_all_rest_apis() { + assertThat(restApis.size(), is(2)); + } + + @Test + public void rest_apis_have_path_as_component_id() { + assertTrue(restApis.get(ComponentId.fromString(PATH_1)) instanceof RestApi); + assertTrue(restApis.get(ComponentId.fromString(PATH_2)) instanceof RestApi); + } + + @Test + public void jersey_handler_has_correct_bindings() { + assertThat(handler1, not(nullValue())); + assertThat(handler1.getServerBindings(), hasItems(HTTP_BINDING_1, HTTPS_BINDING_1)); + + assertThat(handler2, not(nullValue())); + assertThat(handler2.getServerBindings(), hasItems(HTTP_BINDING_2, HTTPS_BINDING_2)); + } + + @Test + public void jersey_bindings_are_included_in_config() { + JdiscBindingsConfig config = root.getConfig(JdiscBindingsConfig.class, CLUSTER_ID); + assertThat(config.handlers(HANDLER_ID_1).serverBindings(), hasItems(HTTP_BINDING_1, HTTPS_BINDING_1)); + assertThat(config.handlers(HANDLER_ID_2).serverBindings(), hasItems(HTTP_BINDING_2, HTTPS_BINDING_2)); + } + + + @Test + public void jersey_handler_for_each_rest_api_is_included_in_components_config() { + ComponentsConfig config = root.getConfig(ComponentsConfig.class, CLUSTER_ID); + assertThat(config.toString(), containsString(".id \"" + HANDLER_ID_1 + "\"")); + assertThat(config.toString(), containsString(".id \"" + HANDLER_ID_2 + "\"")); + } + + @Test + public void jersey_bundles_component_for_each_rest_api_is_included_in_components_config() { + + ComponentsConfig config = root.getConfig(ComponentsConfig.class, CLUSTER_ID); + assertThat(config.toString(), containsString(".id \"" + REST_API_CONTEXT_ID_1 + "\"")); + assertThat(config.toString(), containsString(".id \"" + REST_API_CONTEXT_ID_2 + "\"")); + } + + @Test + public void each_rest_api_has_correct_bundle() { + RestApiContext restApiContext1 = restApis.get(ComponentId.fromString(PATH_1)).getContext(); + RestApiContext restApiContext2 = restApis.get(ComponentId.fromString(PATH_2)).getContext(); + + JerseyBundlesConfig bundlesConfig1 = root.getConfig(JerseyBundlesConfig.class, restApiContext1.getConfigId()); + assertThat(bundlesConfig1.toString(), containsString("bundle1")); + assertThat(bundlesConfig1.toString(), not(containsString("bundle2"))); + + JerseyBundlesConfig bundlesConfig2 = root.getConfig(JerseyBundlesConfig.class, restApiContext2.getConfigId()); + assertThat(bundlesConfig2.toString(), containsString("bundle2")); + assertThat(bundlesConfig2.toString(), not(containsString("bundle1"))); + } +} diff --git a/config-model/src/test/java/com/yahoo/vespa/model/container/jersey/xml/RestApiTest.java b/config-model/src/test/java/com/yahoo/vespa/model/container/jersey/xml/RestApiTest.java new file mode 100644 index 00000000000..7db459caaf7 --- /dev/null +++ b/config-model/src/test/java/com/yahoo/vespa/model/container/jersey/xml/RestApiTest.java @@ -0,0 +1,213 @@ +// Copyright 2017 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.model.container.jersey.xml; + +import com.yahoo.component.ComponentId; +import com.yahoo.config.model.builder.xml.test.DomBuilderTest; +import com.yahoo.container.ComponentsConfig; +import com.yahoo.container.config.jersey.JerseyInitConfig; +import com.yahoo.container.di.config.JerseyBundlesConfig; +import com.yahoo.container.di.config.JerseyInjectionConfig; +import com.yahoo.container.jdisc.JdiscBindingsConfig; +import com.yahoo.vespa.model.container.component.Component; +import com.yahoo.vespa.model.container.component.Handler; +import com.yahoo.vespa.model.container.jersey.JerseyHandler; +import com.yahoo.vespa.model.container.jersey.RestApi; +import com.yahoo.vespa.model.container.jersey.RestApiContext; +import com.yahoo.vespa.model.container.xml.ContainerModelBuilderTestBase; +import org.junit.Ignore; +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsInAnyOrder; + +/** + * @author bjorncs + */ +public class RestApiTest extends ContainerModelBuilderTestBase { + private static final String Path = "rest/api"; + private static final String HttpBinding = "http://*/" + Path + "/*"; + private static final String HttpsBinding = "https://*/" + Path + "/*"; + private static final String HandlerId = JerseyHandler.CLASS + "-" + RestApi.idFromPath(Path); + private static final String RestApiContextId = RestApiContext.CONTAINER_CLASS + "-" + RestApi.idFromPath(Path); + private static final String InjectedComponentId = "injectedHandler"; + + private static final String ClusterId = "container"; + + private static final String restApiXml = + "<container version=\"1.0\" id=\"" + ClusterId + "\" jetty=\"true\">\n" + + " <rest-api path=\"" + Path + "\">\n" + + " <components bundle=\"my-jersey-bundle:1.0\">\n" + + " <package>com.yahoo.foo</package>\n" + + " </components>\n" + + " </rest-api>\n" + + " <handler id=\"" + InjectedComponentId + "\" />\n" + + "</container>"; + + private RestApi restApi; + private JerseyHandler handler; + private RestApiContext context; + + public void setup() throws Exception { + createModel(root, DomBuilderTest.parse(restApiXml)); + root.validate(); + getContainerCluster(ClusterId).prepare(); + restApi = getContainerCluster(ClusterId).getRestApiMap().values().iterator().next(); + handler = (JerseyHandler) getContainerComponentNested(ClusterId, HandlerId); + context = restApi.getContext(); + } + + @Test + public void jersey_handler_has_correct_bindings() throws Exception { + setup(); + assertThat(handler, not(nullValue())); + assertThat(handler.getServerBindings(), hasItems(HttpBinding, HttpsBinding)); + } + + @Test + public void jersey_bindings_are_included_in_config() throws Exception { + setup(); + JdiscBindingsConfig config = root.getConfig(JdiscBindingsConfig.class, ClusterId); + assertThat(config.handlers(HandlerId).serverBindings(), hasItems(HttpBinding, HttpsBinding)); + } + + @Test + public void jersey_handler_has_correct_bundle_spec() throws Exception { + setup(); + assertThat(handler.model.bundleInstantiationSpec.bundle.stringValue(), is(JerseyHandler.BUNDLE)); + } + + @Test + public void config_has_correct_jersey_mapping() throws Exception { + setup(); + JerseyInitConfig config = root.getConfig(JerseyInitConfig.class, handler.getConfigId()); + assertThat(config.jerseyMapping(), is(Path)); + } + + @Test + public void resource_bundles_are_included_in_config() throws Exception { + setup(); + JerseyBundlesConfig config = root.getConfig(JerseyBundlesConfig.class, context.getConfigId()); + assertThat(config.bundles().size(), is(1)); + assertThat(config.bundles(0).spec(), is("my-jersey-bundle:1.0")); + } + + @Test + public void packages_to_scan_are_included_in_config() throws Exception { + setup(); + JerseyBundlesConfig config = root.getConfig(JerseyBundlesConfig.class, context.getConfigId()); + assertThat(config.bundles(0).packages(), contains("com.yahoo.foo")); + } + + @Test + public void jersey_handler_is_included_in_components_config() throws Exception { + setup(); + ComponentsConfig config = root.getConfig(ComponentsConfig.class, ClusterId); + assertThat(config.toString(), containsString(".id \"" + HandlerId + "\"")); + } + + @Test + public void restApiContext_is_included_in_components_config() throws Exception { + setup(); + ComponentsConfig config = root.getConfig(ComponentsConfig.class, ClusterId); + assertThat(config.toString(), containsString(".id \"" + RestApiContextId + "\"")); + } + + @Test + public void all_non_restApi_components_are_injected_to_RestApiContext() throws Exception { + setup(); + ComponentsConfig componentsConfig = root.getConfig(ComponentsConfig.class, ClusterId); + + Set<ComponentId> clusterChildrenComponentIds = getContainerCluster(ClusterId).getAllComponents().stream() + .map(Component::getComponentId) + .collect(Collectors.toSet()); + + Set<ComponentId> restApiChildrenComponentIds = restApi.getChildren().values().stream() + .map(child -> ((Component<?, ?>) child).getComponentId()) + .collect(Collectors.toSet()); + + //TODO: Review: replace with filtering against RestApiContext.isCycleGeneratingComponent + ComponentId cycleInducingComponents = ComponentId.fromString("com.yahoo.container.handler.observability.ApplicationStatusHandler"); + + Set<ComponentId> expectedInjectedConfigIds = new HashSet<>(clusterChildrenComponentIds); + expectedInjectedConfigIds.removeAll(restApiChildrenComponentIds); + expectedInjectedConfigIds.remove(cycleInducingComponents); + + Set<ComponentId> injectedConfigIds = restApiContextConfig(componentsConfig).inject().stream() + .map(inject -> ComponentId.fromString(inject.id())) + .collect(Collectors.toSet()); + + // Verify that the two sets are equal. Split in two asserts to get decent failure messages. + assertThat( + "Not all required components are injected", + injectedConfigIds, + containsInAnyOrder(expectedInjectedConfigIds.toArray())); + assertThat( + "We inject some components that should not be injected", + expectedInjectedConfigIds, + containsInAnyOrder(injectedConfigIds.toArray())); + } + + private static ComponentsConfig.Components restApiContextConfig(ComponentsConfig config) { + return config.components().stream() + .filter(component -> component.classId().equals(RestApiContext.CONTAINER_CLASS)) + .findFirst() + .get(); + } + + @Ignore // TODO: use for naming components instead + @Test + public void jdisc_components_can_be_injected() throws Exception { + setup(); + JerseyInjectionConfig config = root.getConfig(JerseyInjectionConfig.class, context.getConfigId()); + assertThat(config.inject(0).instance(), is("injectedHandler")); + assertThat(config.inject(0).forClass(), is("com.yahoo.handler.Handler")); + } + + @Ignore // TODO: use for naming a non-existent component instead + @Test(expected = IllegalArgumentException.class) + public void injecting_non_existent_component() throws Exception { + String restApiXml = + "<container version=\"1.0\" id=\"" + ClusterId + "\">\n" + + " <rest-api path=\"" + Path + "\">\n" + + " <components bundle=\"my-jersey-bundle:1.0\" />\n" + + " <inject jdisc-component=\"non-existent\" for-class=\"foo\" />\n" + + " </rest-api>\n" + + "</container>"; + createModel(root, DomBuilderTest.parse(restApiXml)); + root.validate(); + } + + @Test + public void legacy_syntax_should_produce_valid_model() throws Exception { + String legacyXml = + "<container version=\"1.0\" >\n" + + " <handler id=\"" + JerseyHandler.CLASS + "\" >\n" + + " <binding>" + HttpBinding + "</binding>\n" + + " <config name=\"jdisc.jersey.jersey-handler\">\n" + + " <jerseyMapping>jersey</jerseyMapping>\n" + + " </config>\n" + + " </handler>\n" + + "</container>"; + + createModel(root, DomBuilderTest.parse(legacyXml)); + + Handler<?> handler = (Handler<?>) getContainerComponent("container", JerseyHandler.CLASS); + assertThat(handler, not(nullValue())); + assertThat(handler.getServerBindings(), hasItem(HttpBinding)); + + JdiscBindingsConfig bindingsConfig = root.getConfig(JdiscBindingsConfig.class, ClusterId); + assertThat(bindingsConfig.handlers(JerseyHandler.CLASS).serverBindings(), hasItem(HttpBinding)); + } + +} diff --git a/config-model/src/test/scala/com/yahoo/vespa/model/container/jersey/xml/MultipleRestApisTest.scala b/config-model/src/test/scala/com/yahoo/vespa/model/container/jersey/xml/MultipleRestApisTest.scala deleted file mode 100644 index f932e2c15c0..00000000000 --- a/config-model/src/test/scala/com/yahoo/vespa/model/container/jersey/xml/MultipleRestApisTest.scala +++ /dev/null @@ -1,137 +0,0 @@ -// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.model.container.jersey.xml - -import scala.language.implicitConversions -import com.yahoo.vespa.model.container.xml.ContainerModelBuilderTestBase._ -import com.yahoo.vespa.model.container.jersey.{JerseyHandler => ModelJerseyHandler, RestApiContext, RestApi} -import com.yahoo.config.model.builder.xml.test.DomBuilderTest -import com.yahoo.vespa.model.container.xml.ContainerModelBuilderTestBase -import MultipleRestApisTest._ -import org.junit.Test -import scala.xml.Elem -import org.w3c.dom.Element -import com.yahoo.container.jdisc.JdiscBindingsConfig -import org.junit.Assert._ -import org.hamcrest.CoreMatchers._ -import com.yahoo.container.ComponentsConfig -import com.yahoo.component.ComponentId -import com.yahoo.container.di.config.JerseyBundlesConfig - -/** - * @author gjoranv - * @since 5.11 - */ - -class MultipleRestApisTest extends ContainerModelBuilderTestBase { - - trait TestApp { - createModel(root, restApiXml) - - val handler1 = getContainerComponentNested(ClusterId, HandlerId1).asInstanceOf[ModelJerseyHandler] - val handler2 = getContainerComponentNested(ClusterId, HandlerId2).asInstanceOf[ModelJerseyHandler] - val restApis = getContainerCluster(ClusterId).getRestApiMap - } - - @Test - def cluster_has_all_rest_apis() { - new TestApp { - assertThat(restApis.size(), is(2)) - } - } - - @Test - def rest_apis_have_path_as_component_id() { - new TestApp { - assertTrue(restApis.get(ComponentId.fromString(Path1)).isInstanceOf[RestApi]) - assertTrue(restApis.get(ComponentId.fromString(Path2)).isInstanceOf[RestApi]) - } - } - - @Test - def jersey_handler_has_correct_bindings() { - new TestApp { - assertThat(handler1, not(nullValue())) - assertThat(handler1.getServerBindings, hasItems(HttpBinding1, HttpsBinding1)) - - assertThat(handler2, not(nullValue())) - assertThat(handler2.getServerBindings, hasItems(HttpBinding2, HttpsBinding2)) - } - } - - @Test - def jersey_bindings_are_included_in_config() { - new TestApp { - val config = root.getConfig(classOf[JdiscBindingsConfig], ClusterId) - assertThat(config.handlers(HandlerId1).serverBindings(), hasItems(HttpBinding1, HttpsBinding1)) - assertThat(config.handlers(HandlerId2).serverBindings(), hasItems(HttpBinding2, HttpsBinding2)) - } - } - - - @Test - def jersey_handler_for_each_rest_api_is_included_in_components_config() { - new TestApp { - val config = root.getConfig(classOf[ComponentsConfig], ClusterId) - assertThat(config.toString, containsString(".id \"" + HandlerId1 + "\"")) - assertThat(config.toString, containsString(".id \"" + HandlerId2 + "\"")) - } - } - - @Test - def jersey_bundles_component_for_each_rest_api_is_included_in_components_config() { - new TestApp { - val config = root.getConfig(classOf[ComponentsConfig], ClusterId) - assertThat(config.toString, containsString(".id \"" + RestApiContextId1 + "\"")) - assertThat(config.toString, containsString(".id \"" + RestApiContextId2 + "\"")) - } - } - - @Test - def each_rest_api_has_correct_bundle() { - new TestApp { - val restApiContext1 = restApis.get(ComponentId.fromString(Path1)).getContext - val restApiContext2 = restApis.get(ComponentId.fromString(Path2)).getContext - - val bundlesConfig1 = root.getConfig(classOf[JerseyBundlesConfig], restApiContext1.getConfigId) - assertThat(bundlesConfig1.toString, containsString("bundle1")) - assertThat(bundlesConfig1.toString, not(containsString("bundle2"))) - - val bundlesConfig2 = root.getConfig(classOf[JerseyBundlesConfig], restApiContext2.getConfigId) - assertThat(bundlesConfig2.toString, containsString("bundle2")) - assertThat(bundlesConfig2.toString, not(containsString("bundle1"))) - } - } - -} - -object MultipleRestApisTest { - val ClusterId = "container" - - val Path1 = "rest_1" - val Path2 = "rest_2" - val HttpBinding1 = "http://*/" + Path1 + "/*" - val HttpsBinding1 = "https://*/" + Path1 + "/*" - val HttpBinding2 = "http://*/" + Path2 + "/*" - val HttpsBinding2 = "https://*/" + Path2 + "/*" - - val HandlerId1 = ModelJerseyHandler.CLASS + "-" + Path1 - val HandlerId2 = ModelJerseyHandler.CLASS + "-" + Path2 - val RestApiContextId1 = RestApiContext.CONTAINER_CLASS + "-" + Path1 - val RestApiContextId2 = RestApiContext.CONTAINER_CLASS + "-" + Path2 - - val restApiXml = - <container version="1.0" id={ClusterId}> - <rest-api path={Path1}> - <components bundle="bundle1" /> - </rest-api> - - <rest-api path={Path2}> - <components bundle="bundle2" /> - </rest-api> - </container> - - implicit def toDomElement(elem: Elem): Element = { - DomBuilderTest.parse(elem.toString()) - } - -} diff --git a/config-model/src/test/scala/com/yahoo/vespa/model/container/jersey/xml/RestApiTest.scala b/config-model/src/test/scala/com/yahoo/vespa/model/container/jersey/xml/RestApiTest.scala deleted file mode 100644 index 6eef04cb08f..00000000000 --- a/config-model/src/test/scala/com/yahoo/vespa/model/container/jersey/xml/RestApiTest.scala +++ /dev/null @@ -1,214 +0,0 @@ -// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.model.container.jersey.xml - -import scala.language.implicitConversions -import RestApiTest._ -import scala.xml.Elem -import scala.collection.JavaConverters._ -import com.yahoo.config.model.builder.xml.test.DomBuilderTest -import org.w3c.dom.Element -import org.junit.{Ignore, Test} -import org.junit.Assert._ -import com.yahoo.vespa.model.container.component.{Component, Handler} -import com.yahoo.vespa.model.container.jersey.{JerseyHandler => ModelJerseyHandler, RestApi, Jersey2Servlet, RestApiContext} -import com.yahoo.vespa.model.container.xml.ContainerModelBuilderTestBase -import com.yahoo.vespa.model.container.xml.ContainerModelBuilderTestBase._ -import com.yahoo.container.ComponentsConfig -import org.hamcrest.CoreMatchers.{is, nullValue, notNullValue, not, containsString, hasItem, hasItems} -import org.hamcrest.Matchers.{empty, contains} -import com.yahoo.container.jdisc.JdiscBindingsConfig -import com.yahoo.container.di.config.{JerseyInjectionConfig, JerseyBundlesConfig} -import com.yahoo.container.config.jersey.JerseyInitConfig -import com.yahoo.component.ComponentId - -/** - * @author gjoranv - * @since 5.5 - */ -class RestApiTest extends ContainerModelBuilderTestBase { - - trait TestApp { - createModel(root, restApiXml) - root.validate() - getContainerCluster(ClusterId).prepare() - - val restApi = getContainerCluster(ClusterId).getRestApiMap.values().iterator().next() - val handler = getContainerComponentNested(ClusterId, HandlerId).asInstanceOf[ModelJerseyHandler] - val context = restApi.getContext - } - - @Test - def jersey_handler_has_correct_bindings() { - new TestApp { - assertThat(handler, not(nullValue())) - assertThat(handler.getServerBindings, hasItems(HttpBinding, HttpsBinding)) - } - } - - @Test - def jersey_bindings_are_included_in_config() { - new TestApp { - val config = root.getConfig(classOf[JdiscBindingsConfig], ClusterId) - assertThat(config.handlers(HandlerId).serverBindings(), hasItems(HttpBinding, HttpsBinding)) - } - } - - @Test - def jersey_handler_has_correct_bundle_spec() { - new TestApp { - assertThat(handler.model.bundleInstantiationSpec.bundle.stringValue(), is(ModelJerseyHandler.BUNDLE)) - } - } - - @Test - def config_has_correct_jersey_mapping() { - new TestApp { - val config = root.getConfig(classOf[JerseyInitConfig], handler.getConfigId) - assertThat(config.jerseyMapping, is(Path)) - } - } - - @Test - def resource_bundles_are_included_in_config() { - new TestApp { - val config = root.getConfig(classOf[JerseyBundlesConfig], context.getConfigId) - - assertThat(config.bundles.size, is(1)) - assertThat(config.bundles(0).spec, is("my-jersey-bundle:1.0")) - } - } - - @Test - def packages_to_scan_are_included_in_config() { - new TestApp { - val config = root.getConfig(classOf[JerseyBundlesConfig], context.getConfigId) - assertThat(config.bundles(0).packages, contains("com.yahoo.foo")) - } - } - - @Test - def jersey_handler_is_included_in_components_config() { - new TestApp { - val config = root.getConfig(classOf[ComponentsConfig], ClusterId) - assertThat(config.toString, containsString(".id \"" + HandlerId + "\"")) - } - } - - @Test - def restApiContext_is_included_in_components_config() { - new TestApp { - val config = root.getConfig(classOf[ComponentsConfig], ClusterId) - assertThat(config.toString, containsString(".id \"" + RestApiContextId + "\"")) - } - } - - @Test - def all_non_restApi_components_are_injected_to_RestApiContext() { - new TestApp { - def restApiContextConfig(config: ComponentsConfig) = - (for { - component <- config.components.asScala - if component.classId == RestApiContext.CONTAINER_CLASS - } yield component).head - - val componentsConfig = root.getConfig(classOf[ComponentsConfig], ClusterId) - - val clusterChildrenComponentIds = getContainerCluster(ClusterId).getAllComponents.asScala.map( - child => child.getComponentId).toSet - - val restApiChildrenComponentIds = restApi.getChildren.values.asScala.map( - child => child.asInstanceOf[Component[_, _]].getComponentId).toSet - - //TODO: Review: replace with filtering against RestApiContext.isCycleGeneratingComponent - val cycleInducingComponents = Set("com.yahoo.container.handler.observability.ApplicationStatusHandler") map ComponentId.fromString - - val expectedInjectedConfigIds = clusterChildrenComponentIds -- restApiChildrenComponentIds -- cycleInducingComponents - - val injectedConfigIds = restApiContextConfig(componentsConfig).inject.asScala.map( - inject => ComponentId.fromString(inject.id())) - - // Verify that the two sets are equal. Split in two asserts to get decent failure messages. - assertThat("Not all required components are injected", - (expectedInjectedConfigIds -- injectedConfigIds).asJavaCollection, empty[Any]) - assertThat("We inject some components that should not be injected", - (injectedConfigIds -- expectedInjectedConfigIds).asJavaCollection, empty[Any]) - } - } - - @Ignore // TODO: use for naming components instead - @Test - def jdisc_components_can_be_injected() { - new TestApp { - val config = root.getConfig(classOf[JerseyInjectionConfig], context.getConfigId) - assertThat(config.inject(0).instance(), is("injectedHandler")) - assertThat(config.inject(0).forClass(), is("com.yahoo.handler.Handler")) - } - } - - @Ignore // TODO: use for naming a non-existent component instead - @Test(expected = classOf[IllegalArgumentException]) - def injecting_non_existent_component() { - val restApiXml = - <container version="1.0" id={ClusterId}> - <rest-api path={Path}> - <components bundle="my-jersey-bundle:1.0" /> - <inject jdisc-component="non-existent" for-class="foo" /> - </rest-api> - - </container> - - createModel(root, restApiXml) - root.validate() - - } - - @Test - def legacy_syntax_should_produce_valid_model() { - val legacyXml = - <container version="1.0" > - <handler id={ModelJerseyHandler.CLASS}> - <binding>{HttpBinding}</binding> - <config name="jdisc.jersey.jersey-handler"> - <jerseyMapping>jersey</jerseyMapping> - </config> - </handler> - </container> - - createModel(root, legacyXml) - - val handler = getContainerComponent("container", ModelJerseyHandler.CLASS).asInstanceOf[Handler[_]] - assertThat(handler, not(nullValue())) - assertThat(handler.getServerBindings, hasItem(HttpBinding)) - - val bindingsConfig = root.getConfig(classOf[JdiscBindingsConfig], ClusterId) - assertThat(bindingsConfig.handlers(ModelJerseyHandler.CLASS).serverBindings(), hasItem(HttpBinding)) - } - -} - -object RestApiTest { - val Path = "rest/api" - val HttpBinding = "http://*/" + Path + "/*" - val HttpsBinding = "https://*/" + Path + "/*" - val HandlerId = ModelJerseyHandler.CLASS + "-" + RestApi.idFromPath(Path) - val RestApiContextId = RestApiContext.CONTAINER_CLASS + "-" + RestApi.idFromPath(Path) - val InjectedComponentId = "injectedHandler" - - val ClusterId = "container" - - val restApiXml = - <container version="1.0" id={ClusterId} jetty="true"> - <rest-api path={Path}> - <components bundle="my-jersey-bundle:1.0"> - <package>com.yahoo.foo</package> - </components> - </rest-api> - - <handler id={InjectedComponentId} /> - </container> - - implicit def toDomElement(elem: Elem): Element = { - DomBuilderTest.parse(elem.toString()) - } - -} |