diff options
author | Olli Virtanen <olli.virtanen@oath.com> | 2018-05-16 15:47:24 +0200 |
---|---|---|
committer | Olli Virtanen <olli.virtanen@oath.com> | 2018-05-16 15:47:24 +0200 |
commit | 33329dab273f14e7073e1a40727ce59c833d6d3a (patch) | |
tree | d93f047e87cc9fa3990d140c3782e7560487f6b3 /application | |
parent | 863d8a7a616350b222da6239344a2a537d952f25 (diff) |
Sync style with intelliJ defaults
Diffstat (limited to 'application')
9 files changed, 473 insertions, 466 deletions
diff --git a/application/src/test/java/com/yahoo/application/ApplicationBuilderTest.java b/application/src/test/java/com/yahoo/application/ApplicationBuilderTest.java index 94c3e742923..4bce75b3b98 100644 --- a/application/src/test/java/com/yahoo/application/ApplicationBuilderTest.java +++ b/application/src/test/java/com/yahoo/application/ApplicationBuilderTest.java @@ -1,78 +1,78 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.application; +import com.yahoo.io.IOUtils; +import org.junit.Test; + +import java.nio.file.Files; + import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import java.nio.file.Files; - -import org.junit.Test; - -import com.yahoo.io.IOUtils; - /** * @author tonytv + * @author Olli Virtanen */ public class ApplicationBuilderTest { - @Test - public void query_profile_types_can_be_added() throws Exception { - withApplicationBuilder(builder -> { - builder.queryProfileType("MyProfileType", "<query-profile-type id=\"MyProfileType\">" + // - "<field name=\"age\" type=\"integer\" />" + // - "<field name=\"profession\" type=\"string\" />" + // - "<field name=\"user\" type=\"query-profile:MyUserProfile\" />" + // - "</query-profile-type>"); + @Test + public void query_profile_types_can_be_added() throws Exception { + withApplicationBuilder(builder -> { + builder.queryProfileType("MyProfileType", "<query-profile-type id=\"MyProfileType\">" + // + "<field name=\"age\" type=\"integer\" />" + // + "<field name=\"profession\" type=\"string\" />" + // + "<field name=\"user\" type=\"query-profile:MyUserProfile\" />" + // + "</query-profile-type>"); - assertTrue(Files.exists(builder.getPath().resolve("search/query-profiles/types/MyProfileType.xml"))); - }); - } + assertTrue(Files.exists(builder.getPath().resolve("search/query-profiles/types/MyProfileType.xml"))); + }); + } - @Test - public void query_profile_can_be_added() throws Exception { - withApplicationBuilder(builder -> { - builder.queryProfile("MyProfile", "<query-profile id=\"MyProfile\">" + // - "<field name=\"message\">Hello world!</field>" + // - "</query-profile>"); + @Test + public void query_profile_can_be_added() throws Exception { + withApplicationBuilder(builder -> { + builder.queryProfile("MyProfile", "<query-profile id=\"MyProfile\">" + // + "<field name=\"message\">Hello world!</field>" + // + "</query-profile>"); - assertTrue(Files.exists(builder.getPath().resolve("search/query-profiles/MyProfile.xml"))); - }); - } + assertTrue(Files.exists(builder.getPath().resolve("search/query-profiles/MyProfile.xml"))); + }); + } - @Test - public void rank_expression_can_be_added() throws Exception { - withApplicationBuilder(builder -> { - builder.rankExpression("myExpression", "content"); - assertTrue(Files.exists(builder.getPath().resolve("searchdefinitions/myExpression.expression"))); - }); - } + @Test + public void rank_expression_can_be_added() throws Exception { + withApplicationBuilder(builder -> { + builder.rankExpression("myExpression", "content"); + assertTrue(Files.exists(builder.getPath().resolve("searchdefinitions/myExpression.expression"))); + }); + } - @Test - public void builder_cannot_be_reused() throws Exception { - try { - ApplicationBuilder builder = new ApplicationBuilder(); - builder.servicesXml("<jdisc version=\"1.0\" />"); - builder.build(); + @Test + public void builder_cannot_be_reused() throws Exception { + try { + ApplicationBuilder builder = new ApplicationBuilder(); + builder.servicesXml("<jdisc version=\"1.0\" />"); + builder.build(); - builder.servicesXml(""); - fail("Expected exception."); - } catch (RuntimeException e) { - assertThat(e.getMessage(), containsString("build method")); - } + builder.servicesXml(""); + fail("Expected exception."); + } catch (RuntimeException e) { + assertThat(e.getMessage(), containsString("build method")); + } - } + } - private interface TestCase { - public void accept(ApplicationBuilder ab) throws Exception; - } + private interface TestCase { + public void accept(ApplicationBuilder ab) throws Exception; + } - public void withApplicationBuilder(TestCase f) throws Exception { - ApplicationBuilder builder = new ApplicationBuilder(); - try { - f.accept(builder); - } finally { - IOUtils.recursiveDeleteDir(builder.getPath().toFile()); - } - } + public void withApplicationBuilder(TestCase f) throws Exception { + ApplicationBuilder builder = new ApplicationBuilder(); + try { + f.accept(builder); + } finally { + IOUtils.recursiveDeleteDir(builder.getPath().toFile()); + } + } } diff --git a/application/src/test/java/com/yahoo/application/container/JDiscContainerSearchTest.java b/application/src/test/java/com/yahoo/application/container/JDiscContainerSearchTest.java index 439683bb335..ab67bb02433 100644 --- a/application/src/test/java/com/yahoo/application/container/JDiscContainerSearchTest.java +++ b/application/src/test/java/com/yahoo/application/container/JDiscContainerSearchTest.java @@ -1,62 +1,61 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.application.container; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -import org.junit.Test; - import com.yahoo.application.Networking; import com.yahoo.application.container.searchers.AddHitSearcher; import com.yahoo.component.ComponentSpecification; import com.yahoo.search.Query; import com.yahoo.search.Result; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; /** - * * @author gjoranv + * @author Olli Virtanen * @since 5.1.15 */ public class JDiscContainerSearchTest { - @Test - public void processing_and_rendering_works() throws Exception { - final String searcherId = AddHitSearcher.class.getName(); - - try (JDisc container = containerWithSearch(searcherId)) { - byte[] rendered = container.search().processAndRender(ComponentSpecification.fromString("mychain"), - ComponentSpecification.fromString("DefaultRenderer"), new Query("")); - String renderedAsString = new String(rendered, "utf-8"); - assertThat(renderedAsString, containsString(searcherId)); - } - } - - @Test - public void searching_works() throws Exception { - final String searcherId = AddHitSearcher.class.getName(); - - try (JDisc container = containerWithSearch(searcherId)) { - Search searching = container.search(); - Result result = searching.process(ComponentSpecification.fromString("mychain"), new Query("")); - String hitTitle = result.hits().get(0).getField("title").toString(); - assertThat(hitTitle, is(searcherId)); - } - } - - public JDisc containerWithSearch(String searcherId) { - return JDisc.fromServicesXml("<container version=\"1.0\">" + // - "<search>" + // - "<chain id=\"mychain\">" + // - "<searcher id=\"" + searcherId + "\"/>" + // - "</chain>" + // - "</search>" + // - "</container>", Networking.disable); - } - - @Test(expected = UnsupportedOperationException.class) - public void retrieving_search_from_container_without_search_is_illegal() throws Exception { - try (JDisc container = JDisc.fromServicesXml("<container version=\"1.0\" />", Networking.disable)) { - container.search(); // throws - } - } + @Test + public void processing_and_rendering_works() throws Exception { + final String searcherId = AddHitSearcher.class.getName(); + + try (JDisc container = containerWithSearch(searcherId)) { + byte[] rendered = container.search().processAndRender(ComponentSpecification.fromString("mychain"), + ComponentSpecification.fromString("DefaultRenderer"), new Query("")); + String renderedAsString = new String(rendered, "utf-8"); + assertThat(renderedAsString, containsString(searcherId)); + } + } + + @Test + public void searching_works() throws Exception { + final String searcherId = AddHitSearcher.class.getName(); + + try (JDisc container = containerWithSearch(searcherId)) { + Search searching = container.search(); + Result result = searching.process(ComponentSpecification.fromString("mychain"), new Query("")); + String hitTitle = result.hits().get(0).getField("title").toString(); + assertThat(hitTitle, is(searcherId)); + } + } + + public JDisc containerWithSearch(String searcherId) { + return JDisc.fromServicesXml("<container version=\"1.0\">" + // + "<search>" + // + "<chain id=\"mychain\">" + // + "<searcher id=\"" + searcherId + "\"/>" + // + "</chain>" + // + "</search>" + // + "</container>", Networking.disable); + } + + @Test(expected = UnsupportedOperationException.class) + public void retrieving_search_from_container_without_search_is_illegal() throws Exception { + try (JDisc container = JDisc.fromServicesXml("<container version=\"1.0\" />", Networking.disable)) { + container.search(); // throws + } + } } diff --git a/application/src/test/java/com/yahoo/application/container/JDiscTest.java b/application/src/test/java/com/yahoo/application/container/JDiscTest.java index 25849f1a95e..b54fc2196d2 100644 --- a/application/src/test/java/com/yahoo/application/container/JDiscTest.java +++ b/application/src/test/java/com/yahoo/application/container/JDiscTest.java @@ -1,19 +1,6 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.application.container; -import static com.yahoo.application.container.JDisc.fromServicesXml; -import static org.hamcrest.CoreMatchers.containsString; -import static org.hamcrest.CoreMatchers.hasItem; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; - -import java.nio.charset.CharacterCodingException; -import java.nio.file.FileSystems; - -import org.junit.Ignore; -import org.junit.Test; - import com.yahoo.application.Application; import com.yahoo.application.ApplicationBuilder; import com.yahoo.application.Networking; @@ -26,149 +13,162 @@ import com.yahoo.jdisc.http.server.jetty.JettyHttpServer; import com.yahoo.jdisc.service.ServerProvider; import com.yahoo.search.Query; import com.yahoo.search.Result; +import org.junit.Ignore; +import org.junit.Test; + +import java.nio.charset.CharacterCodingException; +import java.nio.file.FileSystems; + +import static com.yahoo.application.container.JDisc.fromServicesXml; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.hasItem; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; public class JDiscTest { - @Test - public void jdisc_can_be_used_as_top_level_element() throws Exception { - try (JDisc container = fromServicesXml("<jdisc version=\"1.0\">" + // - "<search />" + // - "</jdisc>", Networking.disable)) { - assertNotNull(container.search()); - } - } - - @Test - public void jdisc_id_can_be_set() throws Exception { - try (JDisc container = fromServicesXml("<jdisc version=\"1.0\" id=\"my-service-id\">" + // - "<search />" + // - "</jdisc>", Networking.disable)) { - assertNotNull(container.search()); - } - } - - @Test - public void jdisc_can_be_embedded_in_services_tag() throws Exception { - try (JDisc container = fromServicesXml("<services>" + // - "<jdisc version=\"1.0\" id=\"my-service-id\">" + // - "<search />" + // - "</jdisc>" + // - "</services>", Networking.disable)) { - assertNotNull(container.search()); - } - } - - @Test - @SuppressWarnings("try") // container is unused inside the try block - public void multiple_jdisc_elements_gives_exception() { - try (JDisc container = fromServicesXml("<services>" + // - "<jdisc version=\"1.0\" id=\"id1\" />" + // - "<jdisc version=\"1.0\" />" + // - "<container version=\"1.0\"/>" + // - "</services>", Networking.disable)) { - fail("expected exception"); - } catch (Exception e) { - assertThat(e.getMessage(), containsString("container id='', jdisc id='id1', jdisc id=''")); - } - } - - @Test - public void handleRequest_yields_response_from_correct_request_handler() throws Exception { - final String handlerClass = TestHandler.class.getName(); - try (JDisc container = fromServicesXml("<container version=\"1.0\">" + // - "<handler id=\"test-handler\" class=\"" + handlerClass + "\">" + // - "<binding>http://*/TestHandler</binding>" + // - "</handler>" + // - "</container>", Networking.disable)) { - Response response = container.handleRequest(new Request("http://foo/TestHandler")); - try { - assertThat(response.getBodyAsString(), is(TestHandler.RESPONSE)); - } catch (CharacterCodingException e) { - throw new RuntimeException(e); - } - } - } - - @Test - public void load_searcher_from_bundle() throws Exception { - try (JDisc container = JDisc.fromPath(FileSystems.getDefault().getPath("src/test/app-packages/searcher-app"), - Networking.disable)) { - Result result = container.search().process(ComponentSpecification.fromString("default"), - new Query("?query=ignored")); - assertThat(result.hits().get(0).getField("title").toString(), is("Heal the World!")); - } - } - - @Test - public void document_types_can_be_accessed() throws Exception { - try (Application application = new ApplicationBuilder().documentType("example", EXAMPLE_DOCUMENT) - .servicesXml(CONTAINER_WITH_DOCUMENT_PROCESSING).build()) { - JDisc container = application.getJDisc("jdisc"); - DocumentProcessing processing = container.documentProcessing(); - assertThat(processing.getDocumentTypes().keySet(), hasItem("example")); - } - } - - @Test - public void annotation_types_can_be_accessed() throws Exception { - try (Application application = new ApplicationBuilder().documentType("example", "search example {\n" + // - " " + EXAMPLE_DOCUMENT + "\n" + // - " annotation exampleAnnotation {}\n" + // - "}\n").// - servicesXml(CONTAINER_WITH_DOCUMENT_PROCESSING).build()) { - JDisc container = application.getJDisc("jdisc"); - DocumentProcessing processing = container.documentProcessing(); - assertThat(processing.getAnnotationTypes().keySet(), hasItem("exampleAnnotation")); - } - } - - @Ignore // Enable this when static state has been removed. - @Test - public void multiple_containers_can_be_run_in_parallel() throws Exception { - try (JDisc jdisc1 = jdiscWithHttp(); JDisc jdisc2 = jdiscWithHttp()) { - sendRequest(jdisc1); - sendRequest(jdisc2); - } - } - - private void sendRequest(JDisc jdisc) throws CharacterCodingException { - Response response = jdisc.handleRequest(new Request("http://foo/TestHandler")); - assertThat(response.getBodyAsString(), is(TestHandler.RESPONSE)); - } - - public static final String CONTAINER_WITH_DOCUMENT_PROCESSING = // - "<jdisc version=\"1.0\">" + // - "<http />" + // - "<document-processing />" + // - "</jdisc>"; - - public static final String EXAMPLE_DOCUMENT = // - "document example {\n" + // - "\n" + // - " field title type string {\n" + // - " indexing: summary | index # How this field should be indexed\n" + // - " weight: 75 # Ranking importancy of this field, used by the built in nativeRank feature\n" + // - " header\n" + // - " }\n" + // - "}\n"; - - protected JDisc jdiscWithHttp() { - final String handlerId = TestHandler.class.getName(); - final String xml = // - "<jdisc version=\"1.0\">" + // - "<handler id=" + handlerId + " />" + // - "<http>\n" + // - "<server id=\"main\" port=\"9999\" />\n" + // - "</http>\n" + // - "</jdisc>"; - return JDisc.fromServicesXml(xml, Networking.disable); - } - - public static int getListenPort() { - for (ServerProvider server : Container.get().getServerProviderRegistry().allComponents()) { - if (null != server && server instanceof JettyHttpServer) { - return ((JettyHttpServer) server).getListenPort(); - } - } - throw new RuntimeException("No http server found"); - } + @Test + public void jdisc_can_be_used_as_top_level_element() throws Exception { + try (JDisc container = fromServicesXml("<jdisc version=\"1.0\">" + // + "<search />" + // + "</jdisc>", Networking.disable)) { + assertNotNull(container.search()); + } + } + + @Test + public void jdisc_id_can_be_set() throws Exception { + try (JDisc container = fromServicesXml("<jdisc version=\"1.0\" id=\"my-service-id\">" + // + "<search />" + // + "</jdisc>", Networking.disable)) { + assertNotNull(container.search()); + } + } + + @Test + public void jdisc_can_be_embedded_in_services_tag() throws Exception { + try (JDisc container = fromServicesXml("<services>" + // + "<jdisc version=\"1.0\" id=\"my-service-id\">" + // + "<search />" + // + "</jdisc>" + // + "</services>", Networking.disable)) { + assertNotNull(container.search()); + } + } + + @Test + @SuppressWarnings("try") // container is unused inside the try block + public void multiple_jdisc_elements_gives_exception() { + try (JDisc container = fromServicesXml("<services>" + // + "<jdisc version=\"1.0\" id=\"id1\" />" + // + "<jdisc version=\"1.0\" />" + // + "<container version=\"1.0\"/>" + // + "</services>", Networking.disable)) { + fail("expected exception"); + } catch (Exception e) { + assertThat(e.getMessage(), containsString("container id='', jdisc id='id1', jdisc id=''")); + } + } + + @Test + public void handleRequest_yields_response_from_correct_request_handler() throws Exception { + final String handlerClass = TestHandler.class.getName(); + try (JDisc container = fromServicesXml("<container version=\"1.0\">" + // + "<handler id=\"test-handler\" class=\"" + handlerClass + "\">" + // + "<binding>http://*/TestHandler</binding>" + // + "</handler>" + // + "</container>", Networking.disable)) { + Response response = container.handleRequest(new Request("http://foo/TestHandler")); + try { + assertThat(response.getBodyAsString(), is(TestHandler.RESPONSE)); + } catch (CharacterCodingException e) { + throw new RuntimeException(e); + } + } + } + + @Test + public void load_searcher_from_bundle() throws Exception { + try (JDisc container = JDisc.fromPath(FileSystems.getDefault().getPath("src/test/app-packages/searcher-app"), + Networking.disable)) { + Result result = container.search().process(ComponentSpecification.fromString("default"), + new Query("?query=ignored")); + assertThat(result.hits().get(0).getField("title").toString(), is("Heal the World!")); + } + } + + @Test + public void document_types_can_be_accessed() throws Exception { + try (Application application = new ApplicationBuilder().documentType("example", EXAMPLE_DOCUMENT) + .servicesXml(CONTAINER_WITH_DOCUMENT_PROCESSING).build()) { + JDisc container = application.getJDisc("jdisc"); + DocumentProcessing processing = container.documentProcessing(); + assertThat(processing.getDocumentTypes().keySet(), hasItem("example")); + } + } + + @Test + public void annotation_types_can_be_accessed() throws Exception { + try (Application application = new ApplicationBuilder().documentType("example", "search example {\n" + // + " " + EXAMPLE_DOCUMENT + "\n" + // + " annotation exampleAnnotation {}\n" + // + "}\n").// + servicesXml(CONTAINER_WITH_DOCUMENT_PROCESSING).build()) { + JDisc container = application.getJDisc("jdisc"); + DocumentProcessing processing = container.documentProcessing(); + assertThat(processing.getAnnotationTypes().keySet(), hasItem("exampleAnnotation")); + } + } + + @Ignore // Enable this when static state has been removed. + @Test + public void multiple_containers_can_be_run_in_parallel() throws Exception { + try (JDisc jdisc1 = jdiscWithHttp(); JDisc jdisc2 = jdiscWithHttp()) { + sendRequest(jdisc1); + sendRequest(jdisc2); + } + } + + private void sendRequest(JDisc jdisc) throws CharacterCodingException { + Response response = jdisc.handleRequest(new Request("http://foo/TestHandler")); + assertThat(response.getBodyAsString(), is(TestHandler.RESPONSE)); + } + + public static final String CONTAINER_WITH_DOCUMENT_PROCESSING = // + "<jdisc version=\"1.0\">" + // + "<http />" + // + "<document-processing />" + // + "</jdisc>"; + + public static final String EXAMPLE_DOCUMENT = // + "document example {\n" + // + "\n" + // + " field title type string {\n" + // + " indexing: summary | index # How this field should be indexed\n" + // + " weight: 75 # Ranking importancy of this field, used by the built in nativeRank feature\n" + // + " header\n" + // + " }\n" + // + "}\n"; + + protected JDisc jdiscWithHttp() { + final String handlerId = TestHandler.class.getName(); + final String xml = // + "<jdisc version=\"1.0\">" + // + "<handler id=" + handlerId + " />" + // + "<http>\n" + // + "<server id=\"main\" port=\"9999\" />\n" + // + "</http>\n" + // + "</jdisc>"; + return JDisc.fromServicesXml(xml, Networking.disable); + } + + public static int getListenPort() { + for (ServerProvider server : Container.get().getServerProviderRegistry().allComponents()) { + if (null != server && server instanceof JettyHttpServer) { + return ((JettyHttpServer) server).getListenPort(); + } + } + throw new RuntimeException("No http server found"); + } } diff --git a/application/src/test/java/com/yahoo/application/container/handlers/TestHandler.java b/application/src/test/java/com/yahoo/application/container/handlers/TestHandler.java index f4d986232b3..4179d62b43f 100644 --- a/application/src/test/java/com/yahoo/application/container/handlers/TestHandler.java +++ b/application/src/test/java/com/yahoo/application/container/handlers/TestHandler.java @@ -1,16 +1,21 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.application.container.handlers; -import com.yahoo.jdisc.handler.*; +import com.yahoo.jdisc.handler.AbstractRequestHandler; +import com.yahoo.jdisc.handler.ContentChannel; +import com.yahoo.jdisc.handler.FastContentWriter; +import com.yahoo.jdisc.handler.ResponseDispatch; +import com.yahoo.jdisc.handler.ResponseHandler; public class TestHandler extends AbstractRequestHandler { - public static final String RESPONSE = "Hello, World!"; + public static final String RESPONSE = "Hello, World!"; - public ContentChannel handleRequest(com.yahoo.jdisc.Request request, ResponseHandler handler) { - FastContentWriter writer = ResponseDispatch.newInstance(com.yahoo.jdisc.Response.Status.OK) - .connectFastWriter(handler); - writer.write(RESPONSE); - writer.close(); - return null; - } + public ContentChannel handleRequest(com.yahoo.jdisc.Request request, ResponseHandler handler) { + FastContentWriter writer = ResponseDispatch.newInstance(com.yahoo.jdisc.Response.Status.OK) + .connectFastWriter(handler); + writer.write(RESPONSE); + writer.close(); + return null; + } } diff --git a/application/src/test/java/com/yahoo/application/container/jersey/JerseyTest.java b/application/src/test/java/com/yahoo/application/container/jersey/JerseyTest.java index 5727243554f..f4ed754ad2d 100644 --- a/application/src/test/java/com/yahoo/application/container/jersey/JerseyTest.java +++ b/application/src/test/java/com/yahoo/application/container/jersey/JerseyTest.java @@ -1,11 +1,24 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.application.container.jersey; -import static java.util.Collections.emptyList; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import com.yahoo.application.Networking; +import com.yahoo.application.container.JDisc; +import com.yahoo.application.container.JDiscTest; +import com.yahoo.application.container.jersey.resources.TestResource; +import com.yahoo.application.container.jersey.resources.nestedpackage1.NestedTestResource1; +import com.yahoo.application.container.jersey.resources.nestedpackage2.NestedTestResource2; +import com.yahoo.container.test.jars.jersey.resources.TestResourceBase; +import com.yahoo.osgi.maven.ProjectBundleClassPaths; +import com.yahoo.osgi.maven.ProjectBundleClassPaths.BundleClasspathMapping; +import org.apache.commons.io.IOUtils; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.util.EntityUtils; +import org.junit.Test; +import javax.ws.rs.core.UriBuilder; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -15,179 +28,166 @@ import java.util.HashSet; import java.util.List; import java.util.Set; -import javax.ws.rs.core.UriBuilder; - -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.util.EntityUtils; -import org.junit.Test; - -import com.yahoo.application.Networking; -import com.yahoo.application.container.JDisc; -import com.yahoo.application.container.JDiscTest; -import com.yahoo.application.container.jersey.resources.TestResource; -import com.yahoo.application.container.jersey.resources.nestedpackage1.NestedTestResource1; -import com.yahoo.application.container.jersey.resources.nestedpackage2.NestedTestResource2; -import com.yahoo.container.test.jars.jersey.resources.TestResourceBase; -import com.yahoo.osgi.maven.ProjectBundleClassPaths; -import com.yahoo.osgi.maven.ProjectBundleClassPaths.BundleClasspathMapping; +import static java.util.Collections.emptyList; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; /** * @author tonytv + * @author Olli Virtanen */ public class JerseyTest { - private final Path testJar = Paths.get("target/test-jars/jersey-resources.jar"); - private final String testClassesDirectory = "target/test-classes"; - private final String bundleSymbolicName = "myBundle"; - - private static final Set<Class<? extends TestResourceBase>> classPathResources; - private static final Set<Class<? extends TestResourceBase>> jarFileResources; - static { - classPathResources = new HashSet<>(); - classPathResources.add(TestResource.class); - classPathResources.add(NestedTestResource1.class); - classPathResources.add(NestedTestResource2.class); - - jarFileResources = new HashSet<>(); - jarFileResources.add(com.yahoo.container.test.jars.jersey.resources.TestResource.class); - jarFileResources.add(com.yahoo.container.test.jars.jersey.resources.nestedpackage1.NestedTestResource1.class); - jarFileResources.add(com.yahoo.container.test.jars.jersey.resources.nestedpackage2.NestedTestResource2.class); - } - - @Test - public void jersey_resources_on_classpath_can_be_invoked_from_application() throws Exception { - saveMainBundleClassPathMappings(testClassesDirectory); - - with_jersey_resources(emptyList(), httpGetter -> assertResourcesResponds(classPathResources, httpGetter)); - } - - @Test - public void jersey_resources_in_provided_dependencies_can_be_invoked_from_application() throws Exception { - BundleClasspathMapping providedDependency = new BundleClasspathMapping(bundleSymbolicName, - Arrays.asList(testClassesDirectory)); - - save(new ProjectBundleClassPaths(new BundleClasspathMapping("main", emptyList()), - Arrays.asList(providedDependency))); - with_jersey_resources(emptyList(), httpGetter -> assertResourcesResponds(classPathResources, httpGetter)); - } - - @Test - public void jersey_resource_on_classpath_can_be_filtered_using_packages() throws Exception { - saveMainBundleClassPathMappings(testClassesDirectory); - - with_jersey_resources(Arrays.asList("com.yahoo.application.container.jersey.resources", - "com.yahoo.application.container.jersey.resources.nestedpackage1"), httpGetter -> { - Class<NestedTestResource2> nestedResource2 = NestedTestResource2.class; - assertDoesNotRespond(nestedResource2, httpGetter); - assertResourcesResponds(copySetExcept(classPathResources, nestedResource2), httpGetter); - }); - } - - @Test - public void jersey_resource_in_jar_can_be_invoked_from_application() throws Exception { - saveMainBundleJarClassPathMappings(testJar); - - with_jersey_resources(emptyList(), httpGetter -> assertResourcesResponds(jarFileResources, httpGetter)); - } - - @Test - public void jersey_resource_in_jar_can_be_filtered_using_packages() throws Exception { - saveMainBundleJarClassPathMappings(testJar); - - with_jersey_resources(Arrays.asList("com.yahoo.container.test.jars.jersey.resources", - "com.yahoo.container.test.jars.jersey.resources.nestedpackage1"), httpGetter -> { - Class<com.yahoo.container.test.jars.jersey.resources.nestedpackage2.NestedTestResource2> nestedResource2 = com.yahoo.container.test.jars.jersey.resources.nestedpackage2.NestedTestResource2.class; - - assertDoesNotRespond(nestedResource2, httpGetter); - assertResourcesResponds(copySetExcept(jarFileResources, nestedResource2), httpGetter); - }); - } - - private static <T> Set<T> copySetExcept(Set<T> in, T except) { - Set<T> ret = new HashSet<>(in); - ret.remove(except); - return ret; - } - - private interface ThrowingConsumer<T> { - public void accept(T arg) throws Exception; - } - - private interface HttpGetter { - public HttpResponse get(String path) throws Exception; - } - - @SuppressWarnings("try") // jdisc unused inside try-with-resources - private void with_jersey_resources(List<String> packagesToScan, ThrowingConsumer<HttpGetter> f) throws Exception { - StringBuffer packageElements = new StringBuffer(); - for (String p : packagesToScan) { - packageElements.append("<package>"); - packageElements.append(p); - packageElements.append("</package>"); - } - - try (JDisc jdisc = JDisc.fromServicesXml(// - "<services>" + // - "<jdisc version=\"1.0\" id=\"default\" jetty=\"true\">" + // - "<rest-api path=\"rest-api\" jersey2=\"true\">" + // - "<components bundle=\"" + bundleSymbolicName + "\">" + // - packageElements + // - "</components>" + // - "</rest-api>" + // - "<http>" + // - "<server id=\"mainServer\" port=\"0\" />" + // - "</http>" + // - "</jdisc>" + // - "</services>", // - Networking.enable)) { - final int port = JDiscTest.getListenPort(); - f.accept(path -> { - String p = path.startsWith("/") ? path.substring(1) : path; - CloseableHttpClient client = HttpClientBuilder.create().build(); - return client.execute(new HttpGet("http://localhost:" + port + "/rest-api/" + p)); - }); - } - } - - public void assertResourcesResponds(Collection<Class<? extends TestResourceBase>> resourceClasses, - HttpGetter httpGetter) throws Exception { - for (Class<? extends TestResourceBase> resource : resourceClasses) { - HttpResponse response = httpGetter.get(path(resource)); - assertThat("Failed sending response to " + resource, response.getStatusLine().getStatusCode(), is(200)); - - String content = IOUtils.toString(response.getEntity().getContent()); - assertThat(content, is(TestResourceBase.content(resource))); - } - } - - public void assertDoesNotRespond(Class<? extends TestResourceBase> resourceClass, HttpGetter httpGetter) - throws Exception { - HttpResponse response = httpGetter.get(path(resourceClass)); - assertThat(response.getStatusLine().getStatusCode(), is(404)); - EntityUtils.consume(response.getEntity()); - } - - public void saveMainBundleJarClassPathMappings(Path jarFile) throws Exception { - assertTrue("Couldn't find file " + jarFile + ", please remember to run mvn process-test-resources first.", - Files.isRegularFile(jarFile)); - saveMainBundleClassPathMappings(jarFile.toAbsolutePath().toString()); - } - - public void saveMainBundleClassPathMappings(String classPathElement) throws Exception { - BundleClasspathMapping mainBundleClassPathMappings = new BundleClasspathMapping(bundleSymbolicName, - Arrays.asList(classPathElement)); - save(new ProjectBundleClassPaths(mainBundleClassPathMappings, emptyList())); - } - - public void save(ProjectBundleClassPaths projectBundleClassPaths) throws Exception { - Path path = Paths.get(testClassesDirectory).resolve(ProjectBundleClassPaths.CLASSPATH_MAPPINGS_FILENAME); - ProjectBundleClassPaths.save(path, projectBundleClassPaths); - } - - public String path(Class<?> resourceClass) { - return UriBuilder.fromResource(resourceClass).build().toString(); - } + private final Path testJar = Paths.get("target/test-jars/jersey-resources.jar"); + private final String testClassesDirectory = "target/test-classes"; + private final String bundleSymbolicName = "myBundle"; + + private static final Set<Class<? extends TestResourceBase>> classPathResources; + private static final Set<Class<? extends TestResourceBase>> jarFileResources; + + static { + classPathResources = new HashSet<>(); + classPathResources.add(TestResource.class); + classPathResources.add(NestedTestResource1.class); + classPathResources.add(NestedTestResource2.class); + + jarFileResources = new HashSet<>(); + jarFileResources.add(com.yahoo.container.test.jars.jersey.resources.TestResource.class); + jarFileResources.add(com.yahoo.container.test.jars.jersey.resources.nestedpackage1.NestedTestResource1.class); + jarFileResources.add(com.yahoo.container.test.jars.jersey.resources.nestedpackage2.NestedTestResource2.class); + } + + @Test + public void jersey_resources_on_classpath_can_be_invoked_from_application() throws Exception { + saveMainBundleClassPathMappings(testClassesDirectory); + + with_jersey_resources(emptyList(), httpGetter -> assertResourcesResponds(classPathResources, httpGetter)); + } + + @Test + public void jersey_resources_in_provided_dependencies_can_be_invoked_from_application() throws Exception { + BundleClasspathMapping providedDependency = new BundleClasspathMapping(bundleSymbolicName, + Arrays.asList(testClassesDirectory)); + + save(new ProjectBundleClassPaths(new BundleClasspathMapping("main", emptyList()), + Arrays.asList(providedDependency))); + with_jersey_resources(emptyList(), httpGetter -> assertResourcesResponds(classPathResources, httpGetter)); + } + + @Test + public void jersey_resource_on_classpath_can_be_filtered_using_packages() throws Exception { + saveMainBundleClassPathMappings(testClassesDirectory); + + with_jersey_resources(Arrays.asList("com.yahoo.application.container.jersey.resources", + "com.yahoo.application.container.jersey.resources.nestedpackage1"), httpGetter -> { + Class<NestedTestResource2> nestedResource2 = NestedTestResource2.class; + assertDoesNotRespond(nestedResource2, httpGetter); + assertResourcesResponds(copySetExcept(classPathResources, nestedResource2), httpGetter); + }); + } + + @Test + public void jersey_resource_in_jar_can_be_invoked_from_application() throws Exception { + saveMainBundleJarClassPathMappings(testJar); + + with_jersey_resources(emptyList(), httpGetter -> assertResourcesResponds(jarFileResources, httpGetter)); + } + + @Test + public void jersey_resource_in_jar_can_be_filtered_using_packages() throws Exception { + saveMainBundleJarClassPathMappings(testJar); + + with_jersey_resources(Arrays.asList("com.yahoo.container.test.jars.jersey.resources", + "com.yahoo.container.test.jars.jersey.resources.nestedpackage1"), httpGetter -> { + Class<com.yahoo.container.test.jars.jersey.resources.nestedpackage2.NestedTestResource2> nestedResource2 = com.yahoo.container.test.jars.jersey.resources.nestedpackage2.NestedTestResource2.class; + + assertDoesNotRespond(nestedResource2, httpGetter); + assertResourcesResponds(copySetExcept(jarFileResources, nestedResource2), httpGetter); + }); + } + + private static <T> Set<T> copySetExcept(Set<T> in, T except) { + Set<T> ret = new HashSet<>(in); + ret.remove(except); + return ret; + } + + private interface ThrowingConsumer<T> { + public void accept(T arg) throws Exception; + } + + private interface HttpGetter { + public HttpResponse get(String path) throws Exception; + } + + @SuppressWarnings("try") // jdisc unused inside try-with-resources + private void with_jersey_resources(List<String> packagesToScan, ThrowingConsumer<HttpGetter> f) throws Exception { + StringBuffer packageElements = new StringBuffer(); + for (String p : packagesToScan) { + packageElements.append("<package>"); + packageElements.append(p); + packageElements.append("</package>"); + } + + try (JDisc jdisc = JDisc.fromServicesXml( + "<services>" + // + "<jdisc version=\"1.0\" id=\"default\" jetty=\"true\">" + // + "<rest-api path=\"rest-api\" jersey2=\"true\">" + // + "<components bundle=\"" + bundleSymbolicName + "\">" + // + packageElements + // + "</components>" + // + "</rest-api>" + // + "<http>" + // + "<server id=\"mainServer\" port=\"0\" />" + // + "</http>" + // + "</jdisc>" + // + "</services>", // + Networking.enable)) { + final int port = JDiscTest.getListenPort(); + f.accept(path -> { + String p = path.startsWith("/") ? path.substring(1) : path; + CloseableHttpClient client = HttpClientBuilder.create().build(); + return client.execute(new HttpGet("http://localhost:" + port + "/rest-api/" + p)); + }); + } + } + + public void assertResourcesResponds(Collection<Class<? extends TestResourceBase>> resourceClasses, + HttpGetter httpGetter) throws Exception { + for (Class<? extends TestResourceBase> resource : resourceClasses) { + HttpResponse response = httpGetter.get(path(resource)); + assertThat("Failed sending response to " + resource, response.getStatusLine().getStatusCode(), is(200)); + + String content = IOUtils.toString(response.getEntity().getContent()); + assertThat(content, is(TestResourceBase.content(resource))); + } + } + + public void assertDoesNotRespond(Class<? extends TestResourceBase> resourceClass, HttpGetter httpGetter) + throws Exception { + HttpResponse response = httpGetter.get(path(resourceClass)); + assertThat(response.getStatusLine().getStatusCode(), is(404)); + EntityUtils.consume(response.getEntity()); + } + + public void saveMainBundleJarClassPathMappings(Path jarFile) throws Exception { + assertTrue("Couldn't find file " + jarFile + ", please remember to run mvn process-test-resources first.", + Files.isRegularFile(jarFile)); + saveMainBundleClassPathMappings(jarFile.toAbsolutePath().toString()); + } + + public void saveMainBundleClassPathMappings(String classPathElement) throws Exception { + BundleClasspathMapping mainBundleClassPathMappings = new BundleClasspathMapping(bundleSymbolicName, + Arrays.asList(classPathElement)); + save(new ProjectBundleClassPaths(mainBundleClassPathMappings, emptyList())); + } + + public void save(ProjectBundleClassPaths projectBundleClassPaths) throws Exception { + Path path = Paths.get(testClassesDirectory).resolve(ProjectBundleClassPaths.CLASSPATH_MAPPINGS_FILENAME); + ProjectBundleClassPaths.save(path, projectBundleClassPaths); + } + + public String path(Class<?> resourceClass) { + return UriBuilder.fromResource(resourceClass).build().toString(); + } } diff --git a/application/src/test/java/com/yahoo/application/container/jersey/resources/TestResource.java b/application/src/test/java/com/yahoo/application/container/jersey/resources/TestResource.java index 57f9f71170a..6cf85236fd5 100644 --- a/application/src/test/java/com/yahoo/application/container/jersey/resources/TestResource.java +++ b/application/src/test/java/com/yahoo/application/container/jersey/resources/TestResource.java @@ -1,11 +1,13 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.application.container.jersey.resources; -import javax.ws.rs.Path; import com.yahoo.container.test.jars.jersey.resources.TestResourceBase; +import javax.ws.rs.Path; + /** * @author tonytv + * @author Olli Virtanen */ @Path("/test-resource") public class TestResource extends TestResourceBase { diff --git a/application/src/test/java/com/yahoo/application/container/jersey/resources/nestedpackage1/NestedTestResource1.java b/application/src/test/java/com/yahoo/application/container/jersey/resources/nestedpackage1/NestedTestResource1.java index f5468ebc365..cc848321941 100644 --- a/application/src/test/java/com/yahoo/application/container/jersey/resources/nestedpackage1/NestedTestResource1.java +++ b/application/src/test/java/com/yahoo/application/container/jersey/resources/nestedpackage1/NestedTestResource1.java @@ -1,12 +1,13 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.application.container.jersey.resources.nestedpackage1; -import javax.ws.rs.Path; - import com.yahoo.container.test.jars.jersey.resources.TestResourceBase; +import javax.ws.rs.Path; + /** * @author tonytv + * @author Olli Virtanen */ @Path("/nested-test-resource1") public class NestedTestResource1 extends TestResourceBase { diff --git a/application/src/test/java/com/yahoo/application/container/jersey/resources/nestedpackage2/NestedTestResource2.java b/application/src/test/java/com/yahoo/application/container/jersey/resources/nestedpackage2/NestedTestResource2.java index 8acdd61710c..af78b3fbaa5 100644 --- a/application/src/test/java/com/yahoo/application/container/jersey/resources/nestedpackage2/NestedTestResource2.java +++ b/application/src/test/java/com/yahoo/application/container/jersey/resources/nestedpackage2/NestedTestResource2.java @@ -1,10 +1,10 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.application.container.jersey.resources.nestedpackage2; -import javax.ws.rs.Path; - import com.yahoo.container.test.jars.jersey.resources.TestResourceBase; +import javax.ws.rs.Path; + /** * @author tonytv */ diff --git a/application/src/test/java/com/yahoo/application/container/searchers/AddHitSearcher.java b/application/src/test/java/com/yahoo/application/container/searchers/AddHitSearcher.java index dc9e7bedc53..274c37bcfc1 100644 --- a/application/src/test/java/com/yahoo/application/container/searchers/AddHitSearcher.java +++ b/application/src/test/java/com/yahoo/application/container/searchers/AddHitSearcher.java @@ -1,4 +1,4 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.application.container.searchers; import com.yahoo.search.Query; @@ -8,17 +8,17 @@ import com.yahoo.search.result.Hit; import com.yahoo.search.searchchain.Execution; public class AddHitSearcher extends Searcher { - @Override - public Result search(Query query, Execution execution) { - Result result = execution.search(query); - result.hits().add(getDummyHit()); + @Override + public Result search(Query query, Execution execution) { + Result result = execution.search(query); + result.hits().add(getDummyHit()); - return result; - } + return result; + } - private Hit getDummyHit() { - Hit hit = new Hit("dummy"); - hit.setField("title", getId().getName()); - return hit; - } + private Hit getDummyHit() { + Hit hit = new Hit("dummy"); + hit.setField("title", getId().getName()); + return hit; + } } |