Skip to content

Commit

Permalink
Add tests for Restlet Servlet extension (open-telemetry#4428)
Browse files Browse the repository at this point in the history
* test servlet

* delete unused web.xml
  • Loading branch information
anosek-an authored and RashmiRam committed May 23, 2022
1 parent 2f48d05 commit 2d63c86
Show file tree
Hide file tree
Showing 11 changed files with 306 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ dependencies {

implementation(project(":instrumentation:restlet:restlet-1.0:library"))
testImplementation(project(":instrumentation:restlet:restlet-1.0:testing"))
testInstrumentation(project(":instrumentation:jetty:jetty-8.0:javaagent"))
testInstrumentation(project(":instrumentation:servlet:servlet-3.0:javaagent"))
testInstrumentation(project(":instrumentation:servlet:servlet-javax-common:javaagent"))

latestDepTestLibrary("org.restlet:org.restlet:1.1.+")
latestDepTestLibrary("com.noelios.restlet:com.noelios.restlet:1.1.+")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ package io.opentelemetry.javaagent.instrumentation.restlet.v1_0

import io.opentelemetry.instrumentation.restlet.v1_0.AbstractRestletServerTest
import io.opentelemetry.instrumentation.test.AgentTestTrait
import org.restlet.Restlet

class RestletServerTest extends AbstractRestletServerTest implements AgentTestTrait {

@Override
Restlet wrapRestlet(Restlet restlet, String path){
return restlet
}

}
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 io.opentelemetry.javaagent.instrumentation.restlet.v1_0


import io.opentelemetry.instrumentation.restlet.v1_0.AbstractServletServerTest
import io.opentelemetry.instrumentation.test.AgentTestTrait

class ServletServerTest extends AbstractServletServerTest implements AgentTestTrait {

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.restlet.Restlet
class RestletServerTest extends AbstractRestletServerTest implements LibraryTestTrait {

@Override
Restlet wrapRestlet(Restlet restlet, String path){
Restlet wrapRestlet(Restlet restlet, String path) {
RestletTracing tracing = RestletTracing.newBuilder(openTelemetry)
.captureHttpHeaders(capturedHttpHeadersForTesting())
.build()
Expand Down
6 changes: 6 additions & 0 deletions instrumentation/restlet/restlet-1.0/testing/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ dependencies {

implementation("org.restlet:org.restlet:1.1.5")
implementation("com.noelios.restlet:com.noelios.restlet:1.1.5")
implementation("com.noelios.restlet:com.noelios.restlet.ext.servlet:1.1.5")
implementation("org.restlet:org.restlet.ext.spring:1.1.5")
implementation("org.springframework:spring:2.5.6")

implementation("org.codehaus.groovy:groovy-all")
implementation("io.opentelemetry:opentelemetry-api")
implementation("org.spockframework:spock-core")

val jettyVersion = "8.1.8.v20121106"
api("org.eclipse.jetty:jetty-annotations:$jettyVersion")
implementation("org.eclipse.jetty:jetty-server:$jettyVersion")
implementation("org.eclipse.jetty:jetty-servlet:$jettyVersion")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.restlet.v1_0

import io.opentelemetry.api.common.AttributeKey
import io.opentelemetry.instrumentation.test.base.HttpServerTest
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.util.resource.Resource
import org.eclipse.jetty.webapp.WebAppContext
import org.restlet.Application
import org.restlet.Restlet
import org.restlet.Router

import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.CAPTURE_HEADERS
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.ERROR
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.EXCEPTION
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.INDEXED_CHILD
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.NOT_FOUND
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.PATH_PARAM
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.REDIRECT
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.SUCCESS

abstract class AbstractServletServerTest extends HttpServerTest<Server> {

@Override
Server startServer(int port) {

def webAppContext = new WebAppContext()
webAppContext.setContextPath(getContextPath())

webAppContext.setBaseResource(Resource.newSystemResource("servlet-ext-app"))

def jettyServer = new Server(port)
jettyServer.connectors.each {
it.setHost('localhost')
}

jettyServer.setHandler(webAppContext)
jettyServer.start()

return jettyServer
}

@Override
List<AttributeKey<?>> extraAttributes() {
[
SemanticAttributes.NET_TRANSPORT,
SemanticAttributes.HTTP_SERVER_NAME
]
}

@Override
void stopServer(Server server) {
server.stop()
server.destroy()
}

@Override
boolean testException() {
false
}

@Override
boolean testPathParam() {
true
}

@Override
boolean testConcurrency() {
true
}

@Override
String expectedServerSpanName(ServerEndpoint endpoint) {
switch (endpoint) {
case PATH_PARAM:
return getContextPath() + "/path/{id}/param"
case NOT_FOUND:
return getContextPath() + "/*"
default:
return endpoint.resolvePath(address).path
}
}


static class TestApp extends Application {

@Override
Restlet createRoot() {
def router = new Router(getContext())

router.attach(SUCCESS.path, RestletAppTestBase.SuccessResource)
router.attach(REDIRECT.path, RestletAppTestBase.RedirectResource)
router.attach(ERROR.path, RestletAppTestBase.ErrorResource)
router.attach(EXCEPTION.path, RestletAppTestBase.ExceptionResource)
router.attach("/path/{id}/param", RestletAppTestBase.PathParamResource)
router.attach(QUERY_PARAM.path, RestletAppTestBase.QueryParamResource)
router.attach(CAPTURE_HEADERS.path, RestletAppTestBase.CaptureHeadersResource)
router.attach(INDEXED_CHILD.path, RestletAppTestBase.IndexedChildResource)

return router
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.restlet.v1_0

import org.restlet.Context
import org.restlet.data.Form
import org.restlet.data.MediaType
import org.restlet.data.Reference
import org.restlet.data.Request
import org.restlet.data.Response
import org.restlet.data.Status
import org.restlet.resource.Representation
import org.restlet.resource.Resource
import org.restlet.resource.StringRepresentation
import org.restlet.resource.Variant

import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.CAPTURE_HEADERS
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.ERROR
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.EXCEPTION
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.INDEXED_CHILD
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.PATH_PARAM
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.REDIRECT
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.SUCCESS
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.controller

class RestletAppTestBase {

static abstract class BaseResource extends Resource {

@Override
void init(Context context, Request request, Response response) {
super.init(context, request, response)
getVariants().add(new Variant(MediaType.TEXT_PLAIN))
}
}

static class SuccessResource extends BaseResource {

@Override
Representation represent(Variant variant) {
controller(SUCCESS) {
return new StringRepresentation(SUCCESS.body)
}
}
}

static class ErrorResource extends BaseResource {

@Override
Representation represent(Variant variant) {
controller(ERROR) {
getResponse().setStatus(Status.valueOf(ERROR.getStatus()), ERROR.getBody())
return new StringRepresentation(ERROR.body)
}
}
}

static class ExceptionResource extends BaseResource {

@Override
Representation represent(Variant variant) {
controller(EXCEPTION) {
throw new Exception(EXCEPTION.body)
}
}
}

static class QueryParamResource extends BaseResource {

@Override
Representation represent(Variant variant) {
controller(QUERY_PARAM) {
return new StringRepresentation(QUERY_PARAM.getBody())
}
}
}

static class PathParamResource extends BaseResource {

@Override
Representation represent(Variant variant) {
controller(PATH_PARAM) {
return new StringRepresentation(PATH_PARAM.getBody())
}
}
}

static class RedirectResource extends BaseResource {

@Override
Representation represent(Variant variant) {
controller(REDIRECT) {
response.setLocationRef(new Reference(REDIRECT.getBody()))
response.setStatus(Status.valueOf(REDIRECT.status))
return new StringRepresentation(REDIRECT.getBody())
}
}
}

static class CaptureHeadersResource extends BaseResource {

@Override
Representation represent(Variant variant) {
controller(CAPTURE_HEADERS) {
Form requestHeaders = request.getAttributes().get("org.restlet.http.headers")
Form responseHeaders = response.getAttributes().computeIfAbsent("org.restlet.http.headers", { new Form() })
responseHeaders.add("X-Test-Response", requestHeaders.getValues("X-Test-Request"))
return new StringRepresentation(CAPTURE_HEADERS.getBody())
}
}
}


static class IndexedChildResource extends BaseResource {

@Override
Representation represent(Variant variant) {
controller(INDEXED_CHILD) {
INDEXED_CHILD.collectSpanAttributes { request.getOriginalRef().getQueryAsForm().getFirst(it).getValue() }
return new StringRepresentation(INDEXED_CHILD.getBody())
}
}
}

}
Loading

0 comments on commit 2d63c86

Please sign in to comment.