Skip to content

Commit de2e552

Browse files
authored
Convert hibernate-procedure-call-4.3 tests from groovy to java (#9290)
1 parent 27c8d00 commit de2e552

File tree

4 files changed

+192
-176
lines changed

4 files changed

+192
-176
lines changed

instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/test/groovy/ProcedureCallTest.groovy

Lines changed: 0 additions & 175 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.hibernate.v4_3;
7+
8+
import static io.opentelemetry.api.common.AttributeKey.stringKey;
9+
import static io.opentelemetry.api.trace.SpanKind.CLIENT;
10+
import static io.opentelemetry.api.trace.SpanKind.INTERNAL;
11+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
12+
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
import static org.assertj.core.api.Assertions.catchThrowable;
15+
16+
import io.opentelemetry.api.common.Attributes;
17+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
18+
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
19+
import io.opentelemetry.sdk.trace.data.StatusData;
20+
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
21+
import java.sql.Connection;
22+
import java.sql.DriverManager;
23+
import java.sql.SQLException;
24+
import java.sql.Statement;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import javax.persistence.ParameterMode;
28+
import org.hibernate.Session;
29+
import org.hibernate.SessionFactory;
30+
import org.hibernate.cfg.Configuration;
31+
import org.hibernate.exception.SQLGrammarException;
32+
import org.hibernate.procedure.ParameterRegistration;
33+
import org.hibernate.procedure.ProcedureCall;
34+
import org.junit.jupiter.api.AfterAll;
35+
import org.junit.jupiter.api.BeforeAll;
36+
import org.junit.jupiter.api.Test;
37+
import org.junit.jupiter.api.extension.RegisterExtension;
38+
39+
class ProcedureCallTest {
40+
41+
@RegisterExtension
42+
protected static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
43+
44+
protected static SessionFactory sessionFactory;
45+
protected static List<Value> prepopulated;
46+
47+
@BeforeAll
48+
@SuppressWarnings("deprecation") // buildSessionFactory
49+
static void setUp() throws SQLException {
50+
sessionFactory = new Configuration().configure().buildSessionFactory();
51+
52+
// Pre-populate the DB, so delete/update can be tested.
53+
Session writer = sessionFactory.openSession();
54+
writer.beginTransaction();
55+
prepopulated = new ArrayList<>();
56+
for (int i = 0; i < 2; i++) {
57+
prepopulated.add(new Value("Hello :) " + i));
58+
writer.save(prepopulated.get(i));
59+
}
60+
writer.getTransaction().commit();
61+
writer.close();
62+
63+
// Create a stored procedure.
64+
Connection conn = DriverManager.getConnection("jdbc:hsqldb:mem:test", "sa", "1");
65+
Statement stmt = conn.createStatement();
66+
stmt.execute(
67+
"CREATE PROCEDURE TEST_PROC() MODIFIES SQL DATA BEGIN ATOMIC INSERT INTO Value VALUES (420, 'fred'); END");
68+
stmt.close();
69+
conn.close();
70+
}
71+
72+
@AfterAll
73+
static void cleanUp() {
74+
if (sessionFactory != null) {
75+
sessionFactory.close();
76+
}
77+
}
78+
79+
@Test
80+
void testProcedureCall() {
81+
82+
testing.runWithSpan(
83+
"parent",
84+
() -> {
85+
Session session = sessionFactory.openSession();
86+
session.beginTransaction();
87+
88+
ProcedureCall call = session.createStoredProcedureCall("TEST_PROC");
89+
call.getOutputs();
90+
91+
session.getTransaction().commit();
92+
session.close();
93+
});
94+
95+
testing.waitAndAssertTraces(
96+
trace ->
97+
trace.hasSpansSatisfyingExactly(
98+
span ->
99+
span.hasName("parent")
100+
.hasKind(INTERNAL)
101+
.hasNoParent()
102+
.hasAttributes(Attributes.empty()),
103+
span ->
104+
span.hasName("ProcedureCall.getOutputs TEST_PROC")
105+
.hasKind(INTERNAL)
106+
.hasParent(trace.getSpan(0))
107+
.hasAttributesSatisfyingExactly(
108+
satisfies(
109+
stringKey("hibernate.session_id"),
110+
val -> val.isInstanceOf(String.class))),
111+
span ->
112+
span.hasName("CALL test.TEST_PROC")
113+
.hasKind(CLIENT)
114+
.hasParent(trace.getSpan(1))
115+
.hasAttributesSatisfyingExactly(
116+
equalTo(SemanticAttributes.DB_SYSTEM, "hsqldb"),
117+
equalTo(SemanticAttributes.DB_NAME, "test"),
118+
equalTo(SemanticAttributes.DB_USER, "sa"),
119+
equalTo(SemanticAttributes.DB_STATEMENT, "{call TEST_PROC()}"),
120+
equalTo(SemanticAttributes.DB_CONNECTION_STRING, "hsqldb:mem:"),
121+
equalTo(SemanticAttributes.DB_OPERATION, "CALL")),
122+
span ->
123+
span.hasName("Transaction.commit")
124+
.hasKind(INTERNAL)
125+
.hasParent(trace.getSpan(0))
126+
.hasAttributesSatisfyingExactly(
127+
equalTo(
128+
stringKey("hibernate.session_id"),
129+
trace
130+
.getSpan(1)
131+
.getAttributes()
132+
.get(stringKey("hibernate.session_id"))))));
133+
}
134+
135+
@Test
136+
void testFailingProcedureCall() {
137+
138+
Throwable exception =
139+
testing.runWithSpan(
140+
"parent",
141+
() -> {
142+
Session session = sessionFactory.openSession();
143+
session.beginTransaction();
144+
145+
ProcedureCall call = session.createStoredProcedureCall("TEST_PROC");
146+
ParameterRegistration<Long> parameterRegistration =
147+
call.registerParameter("nonexistent", Long.class, ParameterMode.IN);
148+
parameterRegistration.bindValue(420L);
149+
150+
Throwable thrown = catchThrowable(call::getOutputs);
151+
152+
assertThat(thrown).isInstanceOf(SQLGrammarException.class);
153+
154+
session.getTransaction().commit();
155+
session.close();
156+
return thrown;
157+
});
158+
159+
testing.waitAndAssertTraces(
160+
trace ->
161+
trace.hasSpansSatisfyingExactly(
162+
span ->
163+
span.hasName("parent")
164+
.hasKind(INTERNAL)
165+
.hasNoParent()
166+
.hasAttributes(Attributes.empty()),
167+
span ->
168+
span.hasName("ProcedureCall.getOutputs TEST_PROC")
169+
.hasKind(INTERNAL)
170+
.hasParent(trace.getSpan(0))
171+
.hasException(exception)
172+
.hasStatus(StatusData.error())
173+
.hasAttributesSatisfyingExactly(
174+
satisfies(
175+
stringKey("hibernate.session_id"),
176+
val -> val.isInstanceOf(String.class))),
177+
span ->
178+
span.hasName("Transaction.commit")
179+
.hasKind(INTERNAL)
180+
.hasParent(trace.getSpan(0))
181+
.hasAttributesSatisfyingExactly(
182+
equalTo(
183+
stringKey("hibernate.session_id"),
184+
trace
185+
.getSpan(1)
186+
.getAttributes()
187+
.get(stringKey("hibernate.session_id"))))));
188+
}
189+
}

instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/test/java/Value.java renamed to instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_3/Value.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
package io.opentelemetry.javaagent.instrumentation.hibernate.v4_3;
7+
68
import javax.persistence.Entity;
79
import javax.persistence.GeneratedValue;
810
import javax.persistence.Id;

instrumentation/hibernate/hibernate-procedure-call-4.3/javaagent/src/test/resources/hibernate.cfg.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<property name="hibernate.hbm2ddl.auto">create</property>
1818

1919
<!-- Objects -->
20-
<mapping class="Value"/>
20+
<mapping class="io.opentelemetry.javaagent.instrumentation.hibernate.v4_3.Value"/>
2121

2222
</session-factory>
2323

0 commit comments

Comments
 (0)