Skip to content

Commit

Permalink
Convert tapestry tests from groovy to java (#9684)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydeluca authored Oct 17, 2023
1 parent 2a554bd commit 60b6548
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 168 deletions.

This file was deleted.

158 changes: 158 additions & 0 deletions instrumentation/tapestry-5.4/javaagent/src/test/java/TapestryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

import static org.assertj.core.api.Assertions.assertThat;

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.client.WebClient;
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;
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 TapestryTest extends AbstractHttpServerUsingTest<Server> {

private static WebClient client;

@RegisterExtension
public static final InstrumentationExtension testing =
HttpServerInstrumentationExtension.forAgent();

@Override
protected Server setupServer() {
WebAppContext webAppContext = new WebAppContext();
webAppContext.setContextPath(getContextPath());
Server jettyServer = new Server(port);

// set up test application
try {
webAppContext.setBaseResource(Resource.newResource("src/test/webapp"));
for (Connector connector : jettyServer.getConnectors()) {
connector.setHost("localhost");
}

jettyServer.setHandler(webAppContext);
jettyServer.start();
} catch (Exception e) {
throw new RuntimeException(e);
}
return jettyServer;
}

@Override
protected void stopServer(Server server) {
try {
server.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
protected String getContextPath() {
return "/jetty-context";
}

@BeforeAll
void setup() {
startServer();
client = WebClient.builder(address).followRedirects().build();
}

@Test
void testIndexPage() {
AggregatedHttpResponse response = client.get("/").aggregate().join();
Document doc = Jsoup.parse(response.contentUtf8());

assertThat(response.status().code()).isEqualTo(200);
assertThat(doc.selectFirst("title").text()).isEqualTo("Index page");

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET " + getContextPath() + "/Index")
.hasNoParent()
.hasKind(SpanKind.SERVER),
span ->
span.hasName("activate/Index")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))));
}

@Test
void testStartAction() {
AggregatedHttpResponse response = client.get("/index.start").aggregate().join();
Document doc = Jsoup.parse(response.contentUtf8());

assertThat(response.status().code()).isEqualTo(200);
assertThat(doc.selectFirst("title").text()).isEqualTo("Other page");

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET " + getContextPath() + "/Index")
.hasNoParent()
.hasKind(SpanKind.SERVER),
span ->
span.hasName("activate/Index")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0)),
span ->
span.hasName("action/Index:start")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0)),
span ->
span.hasName("Response.sendRedirect")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(2))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET " + getContextPath() + "/Other")
.hasNoParent()
.hasKind(SpanKind.SERVER),
span ->
span.hasName("activate/Other")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))));
}

@Test
void testExceptionAction() {
AggregatedHttpResponse response = client.get("/index.exception").aggregate().join();

assertThat(response.status().code()).isEqualTo(500);

testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET " + getContextPath() + "/Index")
.hasStatus(StatusData.error())
.hasKind(SpanKind.SERVER),
span ->
span.hasName("activate/Index")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0)),
span ->
span.hasName("action/Index:exception")
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))
.hasStatus(StatusData.error())
.hasException(new IllegalStateException("expected"))));
}
}

0 comments on commit 60b6548

Please sign in to comment.