Tests for Session methods
MODIFY
dd-java-agent/instrumentation/hibernate/src/test/groovy/SessionTest.groovy
dd-java-agent/instrumentation/hibernate/src/test/groovy/SessionTest.groovy
@@ -2,10 +2,7 @@ import datadog.trace.agent.test.AgentTestRunner
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.DDTags
import io.opentracing.tag.Tags
-import org.hibernate.MappingException
-import org.hibernate.ReplicationMode
-import org.hibernate.Session
-import org.hibernate.SessionFactory
+import org.hibernate.*
import org.hibernate.boot.MetadataSources
import org.hibernate.boot.registry.StandardServiceRegistry
import org.hibernate.boot.registry.StandardServiceRegistryBuilder
@@ -34,91 +31,7 @@ class SessionTest extends AgentTestRunner {
}
}
- def "test hibernate session save"() {
- setup:
- Session session = sessionFactory.openSession()
- session.beginTransaction()
- session.save(new Value("A Hibernate value to be serialized"))
- session.save(new Value("Another value"))
- try {
- session.save(new Long(123) /* Not a valid Entity, should throw */)
- } catch (Exception e) {
- // Should set error tag.
- }
- session.getTransaction().commit()
- session.close()
-
- expect:
- assertTraces(1) {
- trace(0, 5) {
- span(0) {
- serviceName "hibernate"
- resourceName "hibernate.session"
- operationName "hibernate.session"
- spanType DDSpanTypes.HIBERNATE
- parent()
- tags {
- "$Tags.COMPONENT.key" "hibernate-java"
- "$Tags.SPAN_KIND.key" Tags.SPAN_KIND_CLIENT
- "$DDTags.SPAN_TYPE" DDSpanTypes.HIBERNATE
- defaultTags()
- }
- }
- span(1) {
- serviceName "hibernate"
- resourceName "hibernate.transaction.commit"
- operationName "hibernate.transaction.commit"
- spanType DDSpanTypes.HIBERNATE
- childOf span(0)
- tags {
- "$Tags.COMPONENT.key" "hibernate-java"
- "$DDTags.SPAN_TYPE" DDSpanTypes.HIBERNATE
- defaultTags()
- }
- }
- span(2) {
- serviceName "hibernate"
- operationName "hibernate.save"
- resourceName "unknown object"
- spanType DDSpanTypes.HIBERNATE
- childOf span(0)
- errored true
- tags {
- errorTags(MappingException, "Unknown entity: java.lang.Long")
- "$Tags.COMPONENT.key" "hibernate-java"
- "$DDTags.SPAN_TYPE" DDSpanTypes.HIBERNATE
- defaultTags()
- }
- }
- span(3) {
- serviceName "hibernate"
- operationName "hibernate.save"
- resourceName "Value"
- spanType DDSpanTypes.HIBERNATE
- childOf span(0)
- tags {
- "$Tags.COMPONENT.key" "hibernate-java"
- "$DDTags.SPAN_TYPE" DDSpanTypes.HIBERNATE
- defaultTags()
- }
- }
- span(4) {
- serviceName "hibernate"
- operationName "hibernate.save"
- resourceName "Value"
- spanType DDSpanTypes.HIBERNATE
- childOf span(0)
- tags {
- "$Tags.COMPONENT.key" "hibernate-java"
- "$DDTags.SPAN_TYPE" DDSpanTypes.HIBERNATE
- defaultTags()
- }
- }
- }
- }
- }
-
- def "test hibernate session replicate"() {
+ def "test hibernate #testName"() {
setup:
Session session = sessionFactory.openSession()
session.beginTransaction()
@@ -192,22 +105,56 @@ class SessionTest extends AgentTestRunner {
}
where:
- methodName | resource | isError | sessionMethodTest
- "replicate" | "Value" | false | { sesh, val ->
+ testName | methodName | resource | isError | sessionMethodTest
+ "replicate" | "replicate" | "Value" | false | { sesh, val ->
Value replicated = new Value(val.getName() + " replicated")
replicated.setId(val.getId())
sesh.replicate(replicated, ReplicationMode.OVERWRITE)
}
- "replicate" | "Value" | false | { sesh, val ->
+ "replicate by entityName" | "replicate" | "Value" | false | { sesh, val ->
Value replicated = new Value(val.getName() + " replicated")
replicated.setId(val.getId())
sesh.replicate("Value", replicated, ReplicationMode.OVERWRITE)
}
- "replicate" | "unknown object" | true | { sesh, val ->
+ "failed replicate" | "replicate" | "unknown object" | true | { sesh, val ->
sesh.replicate(new Long(123) /* Not a valid entity */, ReplicationMode.OVERWRITE)
}
- "save" | "Value" | false | { sesh, val ->
+ "save" | "save" | "Value" | false | { sesh, val ->
sesh.save(new Value("Another value"))
}
+ "saveOrUpdate save" | "saveOrUpdate" | "Value" | false | { sesh, val ->
+ sesh.saveOrUpdate(new Value("Value"))
+ }
+ "saveOrUpdate update" | "saveOrUpdate" | "Value" | false | { sesh, val ->
+ val.setName("New name")
+ sesh.saveOrUpdate(val)
}
+ "update" | "update" | "Value" | false | { sesh, val ->
+ val.setName("New name")
+ sesh.update(val)
}
+ "update by entityName" | "update" | "Value" | false | { sesh, val ->
+ val.setName("New name")
+ sesh.update("Value", val)
+ }
+ "merge" | "merge" | "Value" | false | { sesh, val ->
+ sesh.merge(new Value("merge me in"))
+ }
+ "persist" | "persist" | "Value" | false | { sesh, val ->
+ sesh.persist(new Value("merge me in"))
+ }
+ "lock" | "lock" | "Value" | false | { sesh, val ->
+ sesh.lock(val, LockMode.READ)
+ }
+ "refresh" | "refresh" | "Value" | false | { sesh, val ->
+ sesh.refresh(val)
+ }
+ "delete" | "delete" | "Value" | false | { sesh, val ->
+ sesh.delete(val)
+ }
+ "get" | "get" | "Value" | false | { sesh, val ->
+ sesh.get("Value", val.getId())
+ }
+ }
+}
+

Tests for Session methods