Skip to content

Commit

Permalink
[release/v1.33.x] Convert Wicket groovy tests to java (#10818)
Browse files Browse the repository at this point in the history
Co-authored-by: H <[email protected]>
Co-authored-by: Lauri Tulmin <[email protected]>
  • Loading branch information
3 people authored Mar 12, 2024
1 parent 456ab3f commit 64dec1e
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 160 deletions.
9 changes: 9 additions & 0 deletions instrumentation/wicket-8.0/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
102 changes: 0 additions & 102 deletions instrumentation/wicket-8.0/javaagent/src/test/groovy/WicketTest.groovy

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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<? extends Page> getHomePage() {
return HelloPage.class;
}

@Override
protected void init() {
super.init();

mountPage("/exception", ExceptionPage.class);
}

@Override
public RuntimeConfigurationType getConfigurationType() {
return RuntimeConfigurationType.DEPLOYMENT;
}
}
Original file line number Diff line number Diff line change
@@ -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!"));
}
}
Original file line number Diff line number Diff line change
@@ -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<Server> {

@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"))));
}
}

0 comments on commit 64dec1e

Please sign in to comment.