Skip to content

Commit

Permalink
Add missing RESTEasy (and Jersey) unit tests (#1058)
Browse files Browse the repository at this point in the history
* Add missing RESTEasy (and Jersey) unit tests

* Standard HTTP server test (extending HttpServerTest)
* AsyncResponse tests
* Error handling in JAX-RS HTTP client
* HTTP client JDK proxies (this one is RESTEasy only)
  • Loading branch information
Mateusz Rzeszutek authored Aug 21, 2020
1 parent 51a6e8b commit 5fbc316
Show file tree
Hide file tree
Showing 10 changed files with 535 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
* limitations under the License.
*/

import static io.opentelemetry.trace.Span.Kind.CLIENT

import io.opentelemetry.auto.test.base.HttpClientTest
import io.opentelemetry.trace.attributes.SemanticAttributes
import java.util.concurrent.TimeUnit
import javax.ws.rs.client.Client
import javax.ws.rs.client.ClientBuilder
Expand All @@ -29,6 +32,7 @@ import org.glassfish.jersey.client.ClientProperties
import org.glassfish.jersey.client.JerseyClientBuilder
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder
import spock.lang.Timeout
import spock.lang.Unroll

abstract class JaxRsClientTest extends HttpClientTest {

Expand All @@ -47,6 +51,44 @@ abstract class JaxRsClientTest extends HttpClientTest {
}

abstract ClientBuilder builder()

@Unroll
def "should properly convert HTTP status #statusCode to span error status"() {
given:
def method = "GET"
def uri = server.address.resolve(path)

when:
def actualStatusCode = doRequest(method, uri)

then:
assert actualStatusCode == statusCode

assertTraces(1) {
trace(0, 2) {
span(0) {
parent()
operationName expectedOperationName(method)
spanKind CLIENT
errored true
attributes {
"${SemanticAttributes.NET_PEER_NAME.key()}" uri.host
"${SemanticAttributes.NET_PEER_IP.key()}" { it == null || it == "127.0.0.1" }
"${SemanticAttributes.NET_PEER_PORT.key()}" uri.port > 0 ? uri.port : { it == null || it == 443 }
"${SemanticAttributes.HTTP_URL.key()}" "${uri}"
"${SemanticAttributes.HTTP_METHOD.key()}" method
"${SemanticAttributes.HTTP_STATUS_CODE.key()}" statusCode
}
}
serverSpan(it, 1, span(0))
}
}

where:
path | statusCode
"/client-error" | 400
"/error" | 500
}
}

@Timeout(5)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import io.opentelemetry.auto.test.base.HttpClientTest
import java.nio.charset.StandardCharsets
import javax.ws.rs.GET
import javax.ws.rs.HeaderParam
import javax.ws.rs.POST
import javax.ws.rs.PUT
import javax.ws.rs.Path
import javax.ws.rs.QueryParam
import javax.ws.rs.core.Response
import org.apache.http.client.utils.URLEncodedUtils
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder
import org.jboss.resteasy.specimpl.ResteasyUriBuilder

class ResteasyProxyClientTest extends HttpClientTest {
@Override
int doRequest(String method, URI uri, Map<String, String> headers, Closure callback) {
def proxyMethodName = "${method}_${uri.path}".toLowerCase()
.replace("/", "")
.replace('-', '_')

def param = URLEncodedUtils.parse(uri, StandardCharsets.UTF_8.name())
.stream().findFirst()
.map({ it.value })
.orElse(null)

def isTestServer = headers.get("is-test-server")

def proxy = new ResteasyClientBuilder()
.build()
.target(new ResteasyUriBuilder().uri(server.address))
.proxy(ResteasyProxyResource)

def response = proxy."$proxyMethodName"(param, isTestServer)

callback?.call()

return response.status
}

@Override
boolean testRedirects() {
false
}

@Override
boolean testConnectionFailure() {
false
}

@Override
boolean testRemoteConnection() {
false
}
}

@Path("")
interface ResteasyProxyResource {
@GET
@Path("success")
Response get_success(@QueryParam("with") String param,
@HeaderParam("is-test-server") String isTestServer)

@POST
@Path("success")
Response post_success(@QueryParam("with") String param,
@HeaderParam("is-test-server") String isTestServer)

@PUT
@Path("success")
Response put_success(@QueryParam("with") String param,
@HeaderParam("is-test-server") String isTestServer)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ muzzle {
// Cant assert fails because muzzle assumes all instrumentations will fail
// Instrumentations in jaxrs-2.0 will pass

// Resteasy changes a class's package in 3.1.0 then moves it back in 3.5.0
// Resteasy changes a class's package in 3.1.0 then moves it back in 3.5.0 and then moves it forward again in 4.0.0
// so the jaxrs-2.0-resteasy-3.0 module applies to [3.0, 3.1) and [3.5, 4.0)
// and the jaxrs-2.0-resteasy-3.1 module applies to [3.1, 3.5) and [4.0, )
pass {
group = "org.jboss.resteasy"
module = "resteasy-jaxrs"
Expand All @@ -14,7 +16,7 @@ muzzle {
pass {
group = "org.jboss.resteasy"
module = "resteasy-jaxrs"
versions = "[3.5.0.Final,)"
versions = "[3.5.0.Final,4)"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ muzzle {
// Cant assert fails because muzzle assumes all instrumentations will fail
// Instrumentations in jaxrs-2.0 will pass

// Resteasy changes a class's package in 3.1.0 then moves it back in 3.5.0
// Resteasy changes a class's package in 3.1.0 then moves it back in 3.5.0 and then moves it forward again in 4.0.0
// so the jaxrs-2.0-resteasy-3.0 module applies to [3.0, 3.1) and [3.5, 4.0)
// and the jaxrs-2.0-resteasy-3.1 module applies to [3.1, 3.5) and [4.0, )
pass {
group = "org.jboss.resteasy"
module = "resteasy-jaxrs"
versions = "[3.1.0.Final,3.5.0.Final)"
}

pass {
group = "org.jboss.resteasy"
module = "resteasy-core"
versions = "[4.0.0.Final,)"
}
}

dependencies {
Expand Down
17 changes: 17 additions & 0 deletions instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,25 @@ dependencies {

// Resteasy
testLibrary group: 'org.jboss.resteasy', name: 'resteasy-jaxrs', version: '3.0.0.Final'
testImplementation(group: 'org.jboss.resteasy', name: 'resteasy-undertow', version: '3.0.4.Final') {
exclude group: 'org.jboss.resteasy', module: 'resteasy-client'
}
testImplementation group: 'io.undertow', name: 'undertow-servlet', version: '1.0.0.Final'

resteasy31TestImplementation(group: 'org.jboss.resteasy', name: 'resteasy-jaxrs', version: '3.1.0.Final')
resteasy31TestImplementation(group: 'org.jboss.resteasy', name: 'resteasy-undertow', version: '3.1.0.Final') {
exclude group: 'org.jboss.resteasy', module: 'resteasy-client'
}

latestDepTestLibrary(group: 'org.jboss.resteasy', name: 'resteasy-undertow', version: '3.+') {
exclude group: 'org.jboss.resteasy', module: 'resteasy-client'
}

// TODO: resteasy 4.+ has changed artifact name to resteasy-core
// latestDepTestLibrary group: 'org.jboss.resteasy', name: 'resteasy-core', version: '+'
// latestDepTestLibrary(group: 'org.jboss.resteasy', name: 'resteasy-undertow', version: '+') {
// exclude group: 'org.jboss.resteasy', module: 'resteasy-client'
// }
}

test.dependsOn resteasy31Test
Loading

0 comments on commit 5fbc316

Please sign in to comment.