From 64dec1e21214584e5ba3fb93e9dc96038add8022 Mon Sep 17 00:00:00 2001 From: OpenTelemetry Bot <107717825+opentelemetrybot@users.noreply.github.com> Date: Tue, 12 Mar 2024 14:55:56 +0100 Subject: [PATCH] [release/v1.33.x] Convert Wicket groovy tests to java (#10818) Co-authored-by: H <149687215+happyuser23@users.noreply.github.com> Co-authored-by: Lauri Tulmin --- .../wicket-8.0/javaagent/build.gradle.kts | 9 ++ .../src/test/groovy/WicketTest.groovy | 102 ---------------- .../test/groovy/hello/ExceptionPage.groovy | 14 --- .../test/groovy/hello/HelloApplication.groovy | 29 ----- .../src/test/groovy/hello/HelloPage.groovy | 15 --- .../src/test/java/hello/ExceptionPage.java | 14 +++ .../src/test/java/hello/HelloApplication.java | 29 +++++ .../src/test/java/hello/HelloPage.java | 15 +++ .../instrumentation/wicket/WicketTest.java | 113 ++++++++++++++++++ 9 files changed, 180 insertions(+), 160 deletions(-) delete mode 100644 instrumentation/wicket-8.0/javaagent/src/test/groovy/WicketTest.groovy delete mode 100644 instrumentation/wicket-8.0/javaagent/src/test/groovy/hello/ExceptionPage.groovy delete mode 100644 instrumentation/wicket-8.0/javaagent/src/test/groovy/hello/HelloApplication.groovy delete mode 100644 instrumentation/wicket-8.0/javaagent/src/test/groovy/hello/HelloPage.groovy create mode 100644 instrumentation/wicket-8.0/javaagent/src/test/java/hello/ExceptionPage.java create mode 100644 instrumentation/wicket-8.0/javaagent/src/test/java/hello/HelloApplication.java create mode 100644 instrumentation/wicket-8.0/javaagent/src/test/java/hello/HelloPage.java create mode 100644 instrumentation/wicket-8.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/wicket/WicketTest.java diff --git a/instrumentation/wicket-8.0/javaagent/build.gradle.kts b/instrumentation/wicket-8.0/javaagent/build.gradle.kts index a87ef285f780..2e9504d415e3 100644 --- a/instrumentation/wicket-8.0/javaagent/build.gradle.kts +++ b/instrumentation/wicket-8.0/javaagent/build.gradle.kts @@ -24,3 +24,12 @@ dependencies { testInstrumentation(project(":instrumentation:servlet:servlet-3.0:javaagent")) testInstrumentation(project(":instrumentation:servlet:servlet-javax-common:javaagent")) } + +val latestDepTest = findProperty("testLatestDeps") as Boolean + +// Wicket 9 requires Java 11 +if (latestDepTest) { + otelJava { + minJavaVersionSupported.set(JavaVersion.VERSION_11) + } +} diff --git a/instrumentation/wicket-8.0/javaagent/src/test/groovy/WicketTest.groovy b/instrumentation/wicket-8.0/javaagent/src/test/groovy/WicketTest.groovy deleted file mode 100644 index 109576a3ba40..000000000000 --- a/instrumentation/wicket-8.0/javaagent/src/test/groovy/WicketTest.groovy +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -import hello.HelloApplication -import io.opentelemetry.api.trace.SpanKind -import io.opentelemetry.api.trace.StatusCode -import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification -import io.opentelemetry.instrumentation.test.base.HttpServerTestTrait -import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse -import org.apache.wicket.protocol.http.WicketFilter -import org.eclipse.jetty.server.Server -import org.eclipse.jetty.servlet.DefaultServlet -import org.eclipse.jetty.servlet.ServletContextHandler -import org.eclipse.jetty.util.resource.FileResource -import org.jsoup.Jsoup - -import javax.servlet.DispatcherType - -class WicketTest extends AgentInstrumentationSpecification implements HttpServerTestTrait { - - def setupSpec() { - setupServer() - } - - def cleanupSpec() { - cleanupServer() - } - - @Override - Server startServer(int port) { - def server = new Server(port) - ServletContextHandler context = new ServletContextHandler(0) - context.setContextPath(getContextPath()) - def resource = new FileResource(getClass().getResource("/")) - context.setBaseResource(resource) - server.setHandler(context) - - context.addServlet(DefaultServlet, "/") - def registration = context.getServletContext().addFilter("WicketApplication", WicketFilter) - registration.setInitParameter("applicationClassName", HelloApplication.getName()) - registration.setInitParameter("filterMappingUrlPattern", "/wicket-test/*") - registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "/wicket-test/*") - - server.start() - - return server - } - - @Override - void stopServer(Server server) { - server.stop() - server.destroy() - } - - @Override - String getContextPath() { - return "/jetty-context" - } - - def "test hello"() { - setup: - AggregatedHttpResponse response = client.get(address.resolve("wicket-test/").toString()).aggregate().join() - def doc = Jsoup.parse(response.contentUtf8()) - - expect: - response.status().code() == 200 - doc.selectFirst("#message").text() == "Hello World!" - - assertTraces(1) { - trace(0, 1) { - span(0) { - name "GET " + getContextPath() + "/wicket-test/hello.HelloPage" - kind SpanKind.SERVER - hasNoParent() - } - } - } - } - - def "test exception"() { - setup: - AggregatedHttpResponse response = client.get(address.resolve("wicket-test/exception").toString()).aggregate().join() - - expect: - response.status().code() == 500 - def ex = new Exception("test exception") - - assertTraces(1) { - trace(0, 1) { - span(0) { - name "GET " + getContextPath() + "/wicket-test/hello.ExceptionPage" - kind SpanKind.SERVER - hasNoParent() - status StatusCode.ERROR - errorEvent(ex.class, ex.message) - } - } - } - } -} diff --git a/instrumentation/wicket-8.0/javaagent/src/test/groovy/hello/ExceptionPage.groovy b/instrumentation/wicket-8.0/javaagent/src/test/groovy/hello/ExceptionPage.groovy deleted file mode 100644 index 2314d980489a..000000000000 --- a/instrumentation/wicket-8.0/javaagent/src/test/groovy/hello/ExceptionPage.groovy +++ /dev/null @@ -1,14 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package hello - -import org.apache.wicket.markup.html.WebPage - -class ExceptionPage extends WebPage { - ExceptionPage() { - throw new Exception("test exception") - } -} diff --git a/instrumentation/wicket-8.0/javaagent/src/test/groovy/hello/HelloApplication.groovy b/instrumentation/wicket-8.0/javaagent/src/test/groovy/hello/HelloApplication.groovy deleted file mode 100644 index 6f7295027696..000000000000 --- a/instrumentation/wicket-8.0/javaagent/src/test/groovy/hello/HelloApplication.groovy +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package hello - -import org.apache.wicket.Page -import org.apache.wicket.RuntimeConfigurationType -import org.apache.wicket.protocol.http.WebApplication - -class HelloApplication extends WebApplication { - @Override - Class getHomePage() { - HelloPage - } - - @Override - protected void init() { - super.init() - - mountPage("/exception", ExceptionPage) - } - - @Override - RuntimeConfigurationType getConfigurationType() { - return RuntimeConfigurationType.DEPLOYMENT - } -} diff --git a/instrumentation/wicket-8.0/javaagent/src/test/groovy/hello/HelloPage.groovy b/instrumentation/wicket-8.0/javaagent/src/test/groovy/hello/HelloPage.groovy deleted file mode 100644 index 80e0df316e52..000000000000 --- a/instrumentation/wicket-8.0/javaagent/src/test/groovy/hello/HelloPage.groovy +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package hello - -import org.apache.wicket.markup.html.WebPage -import org.apache.wicket.markup.html.basic.Label - -class HelloPage extends WebPage { - HelloPage() { - add(new Label("message", "Hello World!")) - } -} diff --git a/instrumentation/wicket-8.0/javaagent/src/test/java/hello/ExceptionPage.java b/instrumentation/wicket-8.0/javaagent/src/test/java/hello/ExceptionPage.java new file mode 100644 index 000000000000..a5867e017129 --- /dev/null +++ b/instrumentation/wicket-8.0/javaagent/src/test/java/hello/ExceptionPage.java @@ -0,0 +1,14 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package hello; + +import org.apache.wicket.markup.html.WebPage; + +public class ExceptionPage extends WebPage { + public ExceptionPage() throws Exception { + throw new Exception("test exception"); + } +} diff --git a/instrumentation/wicket-8.0/javaagent/src/test/java/hello/HelloApplication.java b/instrumentation/wicket-8.0/javaagent/src/test/java/hello/HelloApplication.java new file mode 100644 index 000000000000..8b0e55e98d3e --- /dev/null +++ b/instrumentation/wicket-8.0/javaagent/src/test/java/hello/HelloApplication.java @@ -0,0 +1,29 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package hello; + +import org.apache.wicket.Page; +import org.apache.wicket.RuntimeConfigurationType; +import org.apache.wicket.protocol.http.WebApplication; + +public class HelloApplication extends WebApplication { + @Override + public Class getHomePage() { + return HelloPage.class; + } + + @Override + protected void init() { + super.init(); + + mountPage("/exception", ExceptionPage.class); + } + + @Override + public RuntimeConfigurationType getConfigurationType() { + return RuntimeConfigurationType.DEPLOYMENT; + } +} diff --git a/instrumentation/wicket-8.0/javaagent/src/test/java/hello/HelloPage.java b/instrumentation/wicket-8.0/javaagent/src/test/java/hello/HelloPage.java new file mode 100644 index 000000000000..09ae0d4a2bb3 --- /dev/null +++ b/instrumentation/wicket-8.0/javaagent/src/test/java/hello/HelloPage.java @@ -0,0 +1,15 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package hello; + +import org.apache.wicket.markup.html.WebPage; +import org.apache.wicket.markup.html.basic.Label; + +public class HelloPage extends WebPage { + public HelloPage() { + add(new Label("message", "Hello World!")); + } +} diff --git a/instrumentation/wicket-8.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/wicket/WicketTest.java b/instrumentation/wicket-8.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/wicket/WicketTest.java new file mode 100644 index 000000000000..955013dfe659 --- /dev/null +++ b/instrumentation/wicket-8.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/wicket/WicketTest.java @@ -0,0 +1,113 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.wicket; + +import static org.assertj.core.api.Assertions.assertThat; + +import hello.HelloApplication; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerUsingTest; +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension; +import io.opentelemetry.sdk.trace.data.StatusData; +import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse; +import java.util.EnumSet; +import javax.servlet.DispatcherType; +import javax.servlet.FilterRegistration; +import org.apache.wicket.protocol.http.WicketFilter; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.DefaultServlet; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.util.resource.FileResource; +import org.eclipse.jetty.util.resource.Resource; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +class WicketTest extends AbstractHttpServerUsingTest { + + @RegisterExtension + public static final InstrumentationExtension testing = + HttpServerInstrumentationExtension.forAgent(); + + @Override + protected Server setupServer() throws Exception { + Server server = new Server(port); + + ServletContextHandler context = new ServletContextHandler(0); + context.setContextPath(getContextPath()); + + Resource resource = new FileResource(getClass().getResource("/")); + context.setBaseResource(resource); + server.setHandler(context); + + context.addServlet(DefaultServlet.class, "/"); + FilterRegistration.Dynamic registration = + context.getServletContext().addFilter("WicketApplication", WicketFilter.class); + registration.setInitParameter("applicationClassName", HelloApplication.class.getName()); + registration.setInitParameter("filterMappingUrlPattern", "/wicket-test/*"); + registration.addMappingForUrlPatterns( + EnumSet.of(DispatcherType.REQUEST), false, "/wicket-test/*"); + + server.start(); + + return server; + } + + @Override + protected void stopServer(Server server) throws Exception { + server.stop(); + server.destroy(); + } + + @Override + protected String getContextPath() { + return "/jetty-context"; + } + + @BeforeAll + void setup() { + startServer(); + } + + @Test + void testHello() { + AggregatedHttpResponse response = + client.get(address.resolve("wicket-test/").toString()).aggregate().join(); + Document doc = Jsoup.parse(response.contentUtf8()); + + assertThat(response.status().code()).isEqualTo(200); + assertThat(doc.selectFirst("#message").text()).isEqualTo("Hello World!"); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("GET " + getContextPath() + "/wicket-test/hello.HelloPage") + .hasNoParent() + .hasKind(SpanKind.SERVER))); + } + + @Test + void testException() { + AggregatedHttpResponse response = + client.get(address.resolve("wicket-test/exception").toString()).aggregate().join(); + + assertThat(response.status().code()).isEqualTo(500); + + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("GET " + getContextPath() + "/wicket-test/hello.ExceptionPage") + .hasKind(SpanKind.SERVER) + .hasNoParent() + .hasStatus(StatusData.error()) + .hasException(new Exception("test exception")))); + } +}