From 60b65488cbad7d3c620e74c7a83fa044e77f310f Mon Sep 17 00:00:00 2001 From: Jay DeLuca Date: Tue, 17 Oct 2023 10:14:12 -0400 Subject: [PATCH] Convert tapestry tests from groovy to java (#9684) --- .../src/test/groovy/TapestryTest.groovy | 168 ------------------ .../javaagent/src/test/java/TapestryTest.java | 158 ++++++++++++++++ 2 files changed, 158 insertions(+), 168 deletions(-) delete mode 100644 instrumentation/tapestry-5.4/javaagent/src/test/groovy/TapestryTest.groovy create mode 100644 instrumentation/tapestry-5.4/javaagent/src/test/java/TapestryTest.java diff --git a/instrumentation/tapestry-5.4/javaagent/src/test/groovy/TapestryTest.groovy b/instrumentation/tapestry-5.4/javaagent/src/test/groovy/TapestryTest.groovy deleted file mode 100644 index 080cc8aee2e7..000000000000 --- a/instrumentation/tapestry-5.4/javaagent/src/test/groovy/TapestryTest.groovy +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -import io.opentelemetry.api.trace.SpanKind -import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification -import io.opentelemetry.instrumentation.test.asserts.TraceAssert -import io.opentelemetry.instrumentation.test.base.HttpServerTestTrait -import io.opentelemetry.testing.internal.armeria.client.WebClient -import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse -import org.eclipse.jetty.server.Server -import org.eclipse.jetty.util.resource.Resource -import org.eclipse.jetty.webapp.WebAppContext -import org.jsoup.Jsoup - -import static io.opentelemetry.api.trace.StatusCode.ERROR - -class TapestryTest extends AgentInstrumentationSpecification implements HttpServerTestTrait { - - def setupSpec() { - setupServer() - } - - def cleanupSpec() { - cleanupServer() - } - - @Override - Server startServer(int port) { - WebAppContext webAppContext = new WebAppContext() - webAppContext.setContextPath(getContextPath()) - // set up test application - webAppContext.setBaseResource(Resource.newResource("src/test/webapp")) - - def jettyServer = new Server(port) - jettyServer.connectors.each { - it.setHost('localhost') - } - - jettyServer.setHandler(webAppContext) - jettyServer.start() - - return jettyServer - } - - @Override - void stopServer(Server server) { - server.stop() - server.destroy() - } - - @Override - String getContextPath() { - return "/jetty-context" - } - - WebClient client - - def setup() { - client = WebClient.builder(address) - .followRedirects() - .build() - } - - static serverSpan(TraceAssert trace, int index, String spanName) { - trace.span(index) { - hasNoParent() - - name spanName - kind SpanKind.SERVER - } - } - - def "test index page"() { - setup: - AggregatedHttpResponse response = client.get("/").aggregate().join() - def doc = Jsoup.parse(response.contentUtf8()) - - expect: - response.status().code() == 200 - doc.selectFirst("title").text() == "Index page" - - assertTraces(1) { - trace(0, 2) { - serverSpan(it, 0, "GET " + getContextPath() + "/Index") - span(1) { - name "activate/Index" - kind SpanKind.INTERNAL - childOf span(0) - } - } - } - } - - def "test start action"() { - setup: - // index.start triggers an action named "start" on index page - AggregatedHttpResponse response = client.get("/index.start").aggregate().join() - def doc = Jsoup.parse(response.contentUtf8()) - - expect: - response.status().code() == 200 - doc.selectFirst("title").text() == "Other page" - - assertTraces(2) { - trace(0, 4) { - serverSpan(it, 0, "GET " + getContextPath() + "/Index") - span(1) { - name "activate/Index" - kind SpanKind.INTERNAL - childOf span(0) - } - span(2) { - name "action/Index:start" - kind SpanKind.INTERNAL - childOf span(0) - } - span(3) { - name "Response.sendRedirect" - kind SpanKind.INTERNAL - childOf span(2) - } - } - trace(1, 2) { - serverSpan(it, 0, "GET " + getContextPath() + "/Other") - span(1) { - name "activate/Other" - kind SpanKind.INTERNAL - childOf span(0) - } - } - } - } - - def "test exception action"() { - setup: - // index.exception triggers an action named "exception" on index page - AggregatedHttpResponse response = client.get("/index.exception").aggregate().join() - - expect: - response.status().code() == 500 - def ex = new IllegalStateException("expected") - - assertTraces(1) { - trace(0, 3) { - span(0) { - hasNoParent() - kind SpanKind.SERVER - name "GET " + getContextPath() + "/Index" - status ERROR - } - span(1) { - name "activate/Index" - kind SpanKind.INTERNAL - childOf span(0) - } - span(2) { - name "action/Index:exception" - kind SpanKind.INTERNAL - childOf span(0) - status ERROR - errorEvent(ex.class, ex.message) - } - } - } - } -} diff --git a/instrumentation/tapestry-5.4/javaagent/src/test/java/TapestryTest.java b/instrumentation/tapestry-5.4/javaagent/src/test/java/TapestryTest.java new file mode 100644 index 000000000000..bb9cb0aa62e9 --- /dev/null +++ b/instrumentation/tapestry-5.4/javaagent/src/test/java/TapestryTest.java @@ -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 { + + 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")))); + } +}