Update Spring Tests

This commit is contained in:
Tyler Benson 2018-08-07 16:04:28 +10:00
parent 9d8548406b
commit 05dc51633a
1 changed files with 170 additions and 173 deletions

View File

@ -10,6 +10,8 @@ import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.web.bind.MethodArgumentNotValidException import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.util.NestedServletException import org.springframework.web.util.NestedServletException
import static datadog.trace.agent.test.ListWriterAssert.assertTraces
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class SpringBootBasedTest extends AgentTestRunner { class SpringBootBasedTest extends AgentTestRunner {
@ -32,211 +34,206 @@ class SpringBootBasedTest extends AgentTestRunner {
def "generates spans"() { def "generates spans"() {
expect: expect:
restTemplate.getForObject("http://localhost:$port/param/asdf1234/", String) == "Hello asdf1234" restTemplate.getForObject("http://localhost:$port/param/asdf1234/", String) == "Hello asdf1234"
TEST_WRITER.waitForTraces(1)
TEST_WRITER.size() == 1
def trace = TEST_WRITER.firstTrace() assertTraces(TEST_WRITER, 1) {
trace.size() == 1 trace(0, 1) {
def span = trace[0] span(0) {
operationName "servlet.request"
span.context().operationName == "servlet.request" resourceName "GET /param/{parameter}/"
span.context().resourceName == "GET /param/{parameter}/" spanType DDSpanTypes.WEB_SERVLET
span.context().spanType == DDSpanTypes.WEB_SERVLET parent()
!span.context().getErrorFlag() errored false
span.context().parentId == "0" tags {
span.context().tags["http.url"] == "http://localhost:$port/param/asdf1234/" "http.url" "http://localhost:$port/param/asdf1234/"
span.context().tags["http.method"] == "GET" "http.method" "GET"
span.context().tags["span.kind"] == "server" "span.kind" "server"
span.context().tags["span.type"] == "web" "span.type" "web"
span.context().tags["component"] == "java-web-servlet" "component" "java-web-servlet"
span.context().tags["http.status_code"] == 200 "http.status_code" 200
span.context().tags["thread.name"] != null defaultTags()
span.context().tags["thread.id"] != null }
span.context().tags.size() == 8 }
}
}
} }
def "generates 404 spans"() { def "generates 404 spans"() {
setup:
def response = restTemplate.getForObject("http://localhost:$port/invalid", Map) def response = restTemplate.getForObject("http://localhost:$port/invalid", Map)
expect: expect:
response.get("status") == 404 response.get("status") == 404
response.get("error") == "Not Found" response.get("error") == "Not Found"
TEST_WRITER.waitForTraces(2)
TEST_WRITER.size() == 2
and: // trace 0 assertTraces(TEST_WRITER, 2) {
def trace0 = TEST_WRITER.get(0) trace(0, 1) {
trace0.size() == 1 span(0) {
def span0 = trace0[0] operationName "servlet.request"
resourceName "404"
span0.context().operationName == "servlet.request" spanType DDSpanTypes.WEB_SERVLET
span0.context().resourceName == "404" parent()
span0.context().spanType == DDSpanTypes.WEB_SERVLET errored false
!span0.context().getErrorFlag() tags {
span0.context().parentId == "0" "http.url" "http://localhost:$port/invalid"
span0.context().tags["http.url"] == "http://localhost:$port/invalid" "http.method" "GET"
span0.context().tags["http.method"] == "GET" "span.kind" "server"
span0.context().tags["span.kind"] == "server" "span.type" "web"
span0.context().tags["span.type"] == "web" "component" "java-web-servlet"
span0.context().tags["component"] == "java-web-servlet" "http.status_code" 404
span0.context().tags["http.status_code"] == 404 defaultTags()
span0.context().tags["thread.name"] != null }
span0.context().tags["thread.id"] != null }
span0.context().tags.size() == 8 }
trace(1, 1) {
and: // trace 1 span(0) {
def trace1 = TEST_WRITER.get(1) operationName "servlet.request"
trace1.size() == 1 resourceName "404"
def span1 = trace1[0] spanType DDSpanTypes.WEB_SERVLET
parent()
span1.context().operationName == "servlet.request" errored false
span1.context().resourceName == "404" tags {
span1.context().spanType == DDSpanTypes.WEB_SERVLET "http.url" "http://localhost:$port/error"
!span1.context().getErrorFlag() "http.method" "GET"
span1.context().parentId == "0" "span.kind" "server"
span1.context().tags["http.url"] == "http://localhost:$port/error" "span.type" "web"
span1.context().tags["http.method"] == "GET" "component" "java-web-servlet"
span1.context().tags["span.kind"] == "server" "http.status_code" 404
span1.context().tags["span.type"] == "web" defaultTags()
span1.context().tags["component"] == "java-web-servlet" }
span1.context().tags["http.status_code"] == 404 }
span1.context().tags["thread.name"] != null }
span1.context().tags["thread.id"] != null }
span1.context().tags.size() == 8
} }
def "generates error spans"() { def "generates error spans"() {
expect: setup:
def response = restTemplate.getForObject("http://localhost:$port/error/qwerty/", Map) def response = restTemplate.getForObject("http://localhost:$port/error/qwerty/", Map)
expect:
response.get("status") == 500 response.get("status") == 500
response.get("error") == "Internal Server Error" response.get("error") == "Internal Server Error"
response.get("exception") == "java.lang.RuntimeException" response.get("exception") == "java.lang.RuntimeException"
response.get("message") == "qwerty" response.get("message") == "qwerty"
TEST_WRITER.waitForTraces(2)
TEST_WRITER.size() == 2
and: // trace 0 assertTraces(TEST_WRITER, 2) {
def trace0 = TEST_WRITER.get(0) trace(0, 1) {
trace0.size() == 1 span(0) {
def span0 = trace0[0] operationName "servlet.request"
resourceName "GET /error/{parameter}/"
span0.context().operationName == "servlet.request" spanType DDSpanTypes.WEB_SERVLET
span0.context().resourceName == "GET /error/{parameter}/" parent()
span0.context().spanType == DDSpanTypes.WEB_SERVLET errored true
span0.context().getErrorFlag() tags {
span0.context().parentId == "0" "http.url" "http://localhost:$port/error/qwerty/"
span0.context().tags["http.url"] == "http://localhost:$port/error/qwerty/" "http.method" "GET"
span0.context().tags["http.method"] == "GET" "span.kind" "server"
span0.context().tags["span.kind"] == "server" "span.type" "web"
span0.context().tags["span.type"] == "web" "component" "java-web-servlet"
span0.context().tags["component"] == "java-web-servlet" "http.status_code" 500
span0.context().tags["http.status_code"] == 500 errorTags NestedServletException, "Request processing failed; nested exception is java.lang.RuntimeException: qwerty"
span0.context().tags["thread.name"] != null defaultTags()
span0.context().tags["thread.id"] != null }
span0.context().tags["error"] == true }
span0.context().tags["error.msg"] == "Request processing failed; nested exception is java.lang.RuntimeException: qwerty" }
span0.context().tags["error.type"] == NestedServletException.getName() trace(1, 1) {
span0.context().tags["error.stack"] != null span(0) {
span0.context().tags.size() == 12 operationName "servlet.request"
resourceName "GET /error"
and: // trace 1 spanType DDSpanTypes.WEB_SERVLET
def trace1 = TEST_WRITER.get(1) parent()
trace1.size() == 1 errored true
def span1 = trace1[0] tags {
"http.url" "http://localhost:$port/error"
span1.context().operationName == "servlet.request" "http.method" "GET"
span1.context().resourceName == "GET /error" "span.kind" "server"
span1.context().spanType == DDSpanTypes.WEB_SERVLET "span.type" "web"
span1.context().parentId == "0" "component" "java-web-servlet"
span1.context().tags["http.url"] == "http://localhost:$port/error" "http.status_code" 500
span1.context().tags["http.method"] == "GET" "error" true
span1.context().tags["span.kind"] == "server" defaultTags()
span1.context().tags["span.type"] == "web" }
span1.context().tags["component"] == "java-web-servlet" }
span1.context().tags["http.status_code"] == 500 }
span1.context().getErrorFlag() }
span1.context().tags["thread.name"] != null
span1.context().tags["thread.id"] != null
span1.context().tags.size() == 9
} }
def "validated form"() { def "validated form"() {
expect: expect:
restTemplate.postForObject("http://localhost:$port/validated", new TestForm("bob", 20), String) == "Hello bob Person(Name: bob, Age: 20)" restTemplate.postForObject("http://localhost:$port/validated", new TestForm("bob", 20), String) == "Hello bob Person(Name: bob, Age: 20)"
TEST_WRITER.waitForTraces(1)
TEST_WRITER.size() == 1
def trace = TEST_WRITER.firstTrace() assertTraces(TEST_WRITER, 1) {
trace.size() == 1 trace(0, 1) {
def span = trace[0] span(0) {
operationName "servlet.request"
span.context().operationName == "servlet.request" resourceName "POST /validated"
span.context().resourceName == "POST /validated" spanType DDSpanTypes.WEB_SERVLET
span.context().spanType == DDSpanTypes.WEB_SERVLET parent()
!span.context().getErrorFlag() errored false
span.context().parentId == "0" tags {
span.context().tags["http.url"] == "http://localhost:$port/validated" "http.url" "http://localhost:$port/validated"
span.context().tags["http.method"] == "POST" "http.method" "POST"
span.context().tags["span.kind"] == "server" "span.kind" "server"
span.context().tags["span.type"] == "web" "span.type" "web"
span.context().tags["component"] == "java-web-servlet" "component" "java-web-servlet"
span.context().tags["http.status_code"] == 200 "http.status_code" 200
span.context().tags["thread.name"] != null defaultTags()
span.context().tags["thread.id"] != null }
span.context().tags.size() == 8 }
}
}
} }
def "invalid form"() { def "invalid form"() {
expect: setup:
def response = restTemplate.postForObject("http://localhost:$port/validated", new TestForm("bill", 5), Map, Map) def response = restTemplate.postForObject("http://localhost:$port/validated", new TestForm("bill", 5), Map, Map)
expect:
response.get("status") == 400 response.get("status") == 400
response.get("error") == "Bad Request" response.get("error") == "Bad Request"
response.get("exception") == "org.springframework.web.bind.MethodArgumentNotValidException" response.get("exception") == "org.springframework.web.bind.MethodArgumentNotValidException"
response.get("message") == "Validation failed for object='testForm'. Error count: 1" response.get("message") == "Validation failed for object='testForm'. Error count: 1"
TEST_WRITER.waitForTraces(2)
TEST_WRITER.size() == 2
and: // trace 0 assertTraces(TEST_WRITER, 2) {
def trace0 = TEST_WRITER.get(0) trace(0, 1) {
trace0.size() == 1 span(0) {
def span0 = trace0[0] operationName "servlet.request"
resourceName "POST /validated"
span0.context().operationName == "servlet.request" spanType DDSpanTypes.WEB_SERVLET
span0.context().resourceName == "POST /validated" parent()
span0.context().spanType == DDSpanTypes.WEB_SERVLET errored false
!span0.context().getErrorFlag() // This should be an error once we have the http status code decorator working. tags {
span0.context().parentId == "0" "http.url" "http://localhost:$port/validated"
span0.context().tags["http.url"] == "http://localhost:$port/validated" "http.method" "POST"
span0.context().tags["http.method"] == "POST" "span.kind" "server"
span0.context().tags["span.kind"] == "server" "span.type" "web"
span0.context().tags["span.type"] == "web" "component" "java-web-servlet"
span0.context().tags["component"] == "java-web-servlet" "http.status_code" 400
span0.context().tags["http.status_code"] == 400 "error" false
span0.context().tags["thread.name"] != null "error.msg" String
span0.context().tags["thread.id"] != null "error.type" MethodArgumentNotValidException.name
// span0.context().tags["error"] == true // This should be an error once we have the http status code decorator working. "error.stack" String
span0.context().tags["error.msg"].toString().startsWith("Validation failed") defaultTags()
span0.context().tags["error.type"] == MethodArgumentNotValidException.getName() }
span0.context().tags["error.stack"] != null }
span0.context().tags.size() == 12 }
trace(1, 1) {
and: // trace 1 span(0) {
def trace1 = TEST_WRITER.get(1) operationName "servlet.request"
trace1.size() == 1 resourceName "POST /error"
def span1 = trace1[0] spanType DDSpanTypes.WEB_SERVLET
parent()
span1.context().operationName == "servlet.request" errored false
span1.context().resourceName == "POST /error" tags {
span1.context().spanType == DDSpanTypes.WEB_SERVLET "http.url" "http://localhost:$port/error"
!span1.context().getErrorFlag() "http.method" "POST"
span1.context().parentId == "0" "span.kind" "server"
span1.context().tags["http.url"] == "http://localhost:$port/error" "span.type" "web"
span1.context().tags["http.method"] == "POST" "component" "java-web-servlet"
span1.context().tags["span.kind"] == "server" "http.status_code" 400
span1.context().tags["span.type"] == "web" defaultTags()
span1.context().tags["component"] == "java-web-servlet" }
span1.context().tags["http.status_code"] == 400 }
span1.context().tags["thread.name"] != null }
span1.context().tags["thread.id"] != null }
span1.context().tags.size() == 8
} }
} }