|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.wicket; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import hello.HelloApplication; |
| 11 | +import io.opentelemetry.api.trace.SpanKind; |
| 12 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
| 13 | +import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpServerUsingTest; |
| 14 | +import io.opentelemetry.instrumentation.testing.junit.http.HttpServerInstrumentationExtension; |
| 15 | +import io.opentelemetry.sdk.trace.data.StatusData; |
| 16 | +import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse; |
| 17 | +import java.util.EnumSet; |
| 18 | +import javax.servlet.DispatcherType; |
| 19 | +import javax.servlet.FilterRegistration; |
| 20 | +import org.apache.wicket.protocol.http.WicketFilter; |
| 21 | +import org.eclipse.jetty.server.Server; |
| 22 | +import org.eclipse.jetty.servlet.DefaultServlet; |
| 23 | +import org.eclipse.jetty.servlet.ServletContextHandler; |
| 24 | +import org.eclipse.jetty.util.resource.FileResource; |
| 25 | +import org.eclipse.jetty.util.resource.Resource; |
| 26 | +import org.jsoup.Jsoup; |
| 27 | +import org.jsoup.nodes.Document; |
| 28 | +import org.junit.jupiter.api.BeforeAll; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 31 | + |
| 32 | +class WicketTest extends AbstractHttpServerUsingTest<Server> { |
| 33 | + |
| 34 | + @RegisterExtension |
| 35 | + public static final InstrumentationExtension testing = |
| 36 | + HttpServerInstrumentationExtension.forAgent(); |
| 37 | + |
| 38 | + @Override |
| 39 | + protected Server setupServer() throws Exception { |
| 40 | + Server server = new Server(port); |
| 41 | + |
| 42 | + ServletContextHandler context = new ServletContextHandler(0); |
| 43 | + context.setContextPath(getContextPath()); |
| 44 | + |
| 45 | + Resource resource = new FileResource(getClass().getResource("/")); |
| 46 | + context.setBaseResource(resource); |
| 47 | + server.setHandler(context); |
| 48 | + |
| 49 | + context.addServlet(DefaultServlet.class, "/"); |
| 50 | + FilterRegistration.Dynamic registration = |
| 51 | + context.getServletContext().addFilter("WicketApplication", WicketFilter.class); |
| 52 | + registration.setInitParameter("applicationClassName", HelloApplication.class.getName()); |
| 53 | + registration.setInitParameter("filterMappingUrlPattern", "/wicket-test/*"); |
| 54 | + registration.addMappingForUrlPatterns( |
| 55 | + EnumSet.of(DispatcherType.REQUEST), false, "/wicket-test/*"); |
| 56 | + |
| 57 | + server.start(); |
| 58 | + |
| 59 | + return server; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected void stopServer(Server server) throws Exception { |
| 64 | + server.stop(); |
| 65 | + server.destroy(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected String getContextPath() { |
| 70 | + return "/jetty-context"; |
| 71 | + } |
| 72 | + |
| 73 | + @BeforeAll |
| 74 | + void setup() { |
| 75 | + startServer(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void testHello() { |
| 80 | + AggregatedHttpResponse response = |
| 81 | + client.get(address.resolve("wicket-test/").toString()).aggregate().join(); |
| 82 | + Document doc = Jsoup.parse(response.contentUtf8()); |
| 83 | + |
| 84 | + assertThat(response.status().code()).isEqualTo(200); |
| 85 | + assertThat(doc.selectFirst("#message").text()).isEqualTo("Hello World!"); |
| 86 | + |
| 87 | + testing.waitAndAssertTraces( |
| 88 | + trace -> |
| 89 | + trace.hasSpansSatisfyingExactly( |
| 90 | + span -> |
| 91 | + span.hasName("GET " + getContextPath() + "/wicket-test/hello.HelloPage") |
| 92 | + .hasNoParent() |
| 93 | + .hasKind(SpanKind.SERVER))); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void testException() { |
| 98 | + AggregatedHttpResponse response = |
| 99 | + client.get(address.resolve("wicket-test/exception").toString()).aggregate().join(); |
| 100 | + |
| 101 | + assertThat(response.status().code()).isEqualTo(500); |
| 102 | + |
| 103 | + testing.waitAndAssertTraces( |
| 104 | + trace -> |
| 105 | + trace.hasSpansSatisfyingExactly( |
| 106 | + span -> |
| 107 | + span.hasName("GET " + getContextPath() + "/wicket-test/hello.ExceptionPage") |
| 108 | + .hasKind(SpanKind.SERVER) |
| 109 | + .hasNoParent() |
| 110 | + .hasStatus(StatusData.error()) |
| 111 | + .hasException(new Exception("test exception")))); |
| 112 | + } |
| 113 | +} |
0 commit comments