From ae6a285e3e2c3f6ac3e7f61afea517a2412eb90c Mon Sep 17 00:00:00 2001 From: gjoranv Date: Mon, 12 Jul 2021 12:15:11 +0200 Subject: Remove JerseyTest --- .../application/container/jersey/JerseyTest.java | 195 --------------------- .../container/jersey/resources/TestResource.java | 14 -- .../nestedpackage1/NestedTestResource1.java | 14 -- .../nestedpackage2/NestedTestResource2.java | 14 -- 4 files changed, 237 deletions(-) delete mode 100644 application/src/test/java/com/yahoo/application/container/jersey/JerseyTest.java delete mode 100644 application/src/test/java/com/yahoo/application/container/jersey/resources/TestResource.java delete mode 100644 application/src/test/java/com/yahoo/application/container/jersey/resources/nestedpackage1/NestedTestResource1.java delete mode 100644 application/src/test/java/com/yahoo/application/container/jersey/resources/nestedpackage2/NestedTestResource2.java (limited to 'application/src') 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 deleted file mode 100644 index 18e206c7add..00000000000 --- a/application/src/test/java/com/yahoo/application/container/jersey/JerseyTest.java +++ /dev/null @@ -1,195 +0,0 @@ -// 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 com.yahoo.application.Networking; -import com.yahoo.application.container.JDisc; -import com.yahoo.application.container.ContainerTest; -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.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.Ignore; -import org.junit.Test; - -import javax.ws.rs.core.UriBuilder; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -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 Tony Vaagenes - * @author ollivir - */ -@Ignore // TODO: remove test -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 final Set> classPathResources; - private final Set> jarFileResources; - - public JerseyTest() { - 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, List.of(testClassesDirectory)); - - save(new ProjectBundleClassPaths(new BundleClasspathMapping("main", emptyList()), List.of(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 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 nestedResource2 = com.yahoo.container.test.jars.jersey.resources.nestedpackage2.NestedTestResource2.class; - - assertDoesNotRespond(nestedResource2, httpGetter); - assertResourcesResponds(copySetExcept(jarFileResources, nestedResource2), httpGetter); - }); - } - - private static Set copySetExcept(Set in, T except) { - Set ret = new HashSet<>(in); - ret.remove(except); - return ret; - } - - private interface ThrowingConsumer { - void accept(T arg) throws Exception; - } - - private interface HttpGetter { - HttpResponse get(String path) throws Exception; - } - - @SuppressWarnings("try") // jdisc unreferenced inside try - private void with_jersey_resources(List packagesToScan, ThrowingConsumer f) throws Exception { - StringBuilder packageElements = new StringBuilder(); - for (String p : packagesToScan) { - packageElements.append(""); - packageElements.append(p); - packageElements.append(""); - } - - try (JDisc jdisc = JDisc.fromServicesXml( - "" + // - "" + // - "" + // - "" + // - packageElements + // - "" + // - "" + // - "" + // - "" + // - "" + // - "" + - "" + // - "", // - Networking.enable)) { - final int port = ContainerTest.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> resourceClasses, - HttpGetter httpGetter) throws Exception { - for (Class resource : resourceClasses) { - HttpResponse response = httpGetter.get(path(resource)); - assertThat("Failed sending response to " + resource, response.getStatusLine().getStatusCode(), is(200)); - - String content = new String(response.getEntity().getContent().readAllBytes(), StandardCharsets.UTF_8); - assertThat(content, is(TestResourceBase.content(resource))); - } - } - - public void assertDoesNotRespond(Class 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, List.of(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 deleted file mode 100644 index 5b6f1fa9c35..00000000000 --- a/application/src/test/java/com/yahoo/application/container/jersey/resources/TestResource.java +++ /dev/null @@ -1,14 +0,0 @@ -// 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 com.yahoo.container.test.jars.jersey.resources.TestResourceBase; - -import javax.ws.rs.Path; - -/** - * @author Tony Vaagenes - * @author ollivir - */ -@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 deleted file mode 100644 index d4901995152..00000000000 --- a/application/src/test/java/com/yahoo/application/container/jersey/resources/nestedpackage1/NestedTestResource1.java +++ /dev/null @@ -1,14 +0,0 @@ -// 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 com.yahoo.container.test.jars.jersey.resources.TestResourceBase; - -import javax.ws.rs.Path; - -/** - * @author Tony Vaagenes - * @author ollivir - */ -@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 deleted file mode 100644 index 1763023a533..00000000000 --- a/application/src/test/java/com/yahoo/application/container/jersey/resources/nestedpackage2/NestedTestResource2.java +++ /dev/null @@ -1,14 +0,0 @@ -// 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 com.yahoo.container.test.jars.jersey.resources.TestResourceBase; - -import javax.ws.rs.Path; - -/** - * @author Tony Vaagenes - * @author ollivir - */ -@Path("/nested-test-resource2") -public class NestedTestResource2 extends TestResourceBase { -} -- cgit v1.2.3