Skip to content

Commit dce4b2e

Browse files
committed
add springjpa test
1 parent 71838a6 commit dce4b2e

File tree

1 file changed

+131
-0
lines changed
  • instrumentation/hibernate/hibernate-6.0/spring-testing/src/test/java/spring/jpa

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package spring.jpa;
2+
3+
import io.opentelemetry.api.common.AttributeKey;
4+
import io.opentelemetry.api.common.Attributes;
5+
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification;
6+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
7+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
8+
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
9+
import java.util.concurrent.atomic.AtomicReference;
10+
import org.hibernate.Version;
11+
import org.junit.jupiter.api.AfterAll;
12+
import org.junit.jupiter.api.BeforeAll;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.RegisterExtension;
15+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
16+
import spring.jpa.Customer;
17+
import spring.jpa.CustomerRepository;
18+
import spring.jpa.PersistenceConfig;
19+
20+
import static io.opentelemetry.api.trace.SpanKind.CLIENT;
21+
import static io.opentelemetry.api.trace.SpanKind.INTERNAL;
22+
import static io.opentelemetry.instrumentation.api.internal.SemconvStability.emitStableDatabaseSemconv;
23+
import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable;
24+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
25+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertFalse;
28+
import static org.junit.jupiter.api.Assertions.assertNotNull;
29+
import static org.junit.jupiter.api.Assertions.assertNull;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
32+
class SpringJpaTest {
33+
@RegisterExtension
34+
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
35+
36+
private static AnnotationConfigApplicationContext context;
37+
private static CustomerRepository repo;
38+
39+
@BeforeAll
40+
static void setUp() {
41+
context = new AnnotationConfigApplicationContext(PersistenceConfig.class);
42+
repo = context.getBean(CustomerRepository.class);
43+
}
44+
45+
@AfterAll
46+
static void tearDown() {
47+
if (context != null) {
48+
context.close();
49+
}
50+
}
51+
52+
@Test
53+
public void testCRUD() {
54+
boolean isHibernate4 = Version.getVersionString().startsWith("4.");
55+
final Customer customer = new Customer("Bob", "Anonymous");
56+
57+
assertNull(customer.getId());
58+
assertFalse(testing.runWithSpan("parent", () -> repo.findAll().iterator().hasNext()));
59+
60+
AtomicReference<String> sessionId = new AtomicReference<>();
61+
testing.waitAndAssertTraces(
62+
trace -> trace.hasSpansSatisfyingExactly(
63+
span ->
64+
span.hasName("parent")
65+
.hasKind(INTERNAL)
66+
.hasNoParent()
67+
.hasAttributes(Attributes.empty()),
68+
span ->
69+
span.hasName("SELECT spring.jpa.Customer")
70+
.hasKind(INTERNAL)
71+
.hasParent(trace.getSpan(0))
72+
.hasAttributesSatisfyingExactly(
73+
satisfies(AttributeKey.stringKey("hibernate.session_id"),
74+
val -> {
75+
sessionId.set(String.valueOf(val));
76+
val.isInstanceOf(String.class);
77+
}
78+
)),
79+
span ->
80+
span.hasName("SELECT test.Customer")
81+
.hasKind(CLIENT)
82+
.hasParent(trace.getSpan(1))
83+
.hasAttributesSatisfyingExactly(
84+
equalTo(DbIncubatingAttributes.DB_SYSTEM,"hsqldb"),
85+
equalTo(maybeStable(DbIncubatingAttributes.DB_NAME),"test"),
86+
equalTo(DbIncubatingAttributes.DB_USER, emitStableDatabaseSemconv() ? null : "sa"),
87+
equalTo(DbIncubatingAttributes.DB_CONNECTION_STRING, emitStableDatabaseSemconv() ? null : "hsqldb:mem:"),
88+
equalTo(maybeStable(DbIncubatingAttributes.DB_STATEMENT), "select ... from Customer ..."),
89+
equalTo(maybeStable(DbIncubatingAttributes.DB_OPERATION), "SELECT"),
90+
equalTo(maybeStable(DbIncubatingAttributes.DB_SQL_TABLE), "Customer")
91+
),
92+
span -> span.hasName("Transaction.commit")
93+
.hasKind(INTERNAL)
94+
.hasParent(trace.getSpan(0))
95+
.hasAttributesSatisfyingExactly(
96+
equalTo(AttributeKey.stringKey("hibernate.session_id"),sessionId.get())
97+
)
98+
));
99+
100+
testing.clearData();
101+
102+
testing.runWithSpan("parent", () -> repo.save(customer));
103+
Long savedId = customer.getId();
104+
105+
assertNotNull(customer.getId());
106+
String sessionId2 = null;
107+
// todo assert
108+
testing.clearData();
109+
110+
111+
customer.setFirstName("Bill");
112+
testing.runWithSpan("parent", () -> repo.save(customer));
113+
114+
assertEquals(customer.getId(), savedId);
115+
String sessionId3 = null;
116+
// todo assert
117+
testing.clearData();
118+
Customer customer1 = testing.runWithSpan("parent",
119+
() -> repo.findByLastName("Anonymous").get(0));
120+
121+
122+
assertEquals(savedId, customer1.getId() );
123+
assertEquals("Bill", customer1.getFirstName());
124+
// todo assert
125+
testing.clearData();
126+
127+
testing.runWithSpan("parent", () -> repo.delete(customer1));
128+
129+
//todo assert
130+
}
131+
}

0 commit comments

Comments
 (0)