Skip to content

Commit e509b87

Browse files
blafondDavideD
authored andcommitted
[#1627] SQLSelect test
1 parent 5f6a74c commit e509b87

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive;
7+
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.Collection;
11+
import java.util.List;
12+
13+
14+
import org.hibernate.annotations.SQLSelect;
15+
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
16+
import org.hibernate.cfg.Configuration;
17+
import org.hibernate.reactive.testing.DBSelectionExtension;
18+
import org.hibernate.reactive.testing.SqlStatementTracker;
19+
20+
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.RegisterExtension;
23+
24+
import io.vertx.junit5.VertxTestContext;
25+
import jakarta.persistence.ElementCollection;
26+
import jakarta.persistence.Entity;
27+
import jakarta.persistence.FetchType;
28+
import jakarta.persistence.Id;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.POSTGRESQL;
32+
import static org.hibernate.reactive.testing.DBSelectionExtension.runOnlyFor;
33+
import static org.junit.jupiter.api.Assertions.assertNotNull;
34+
35+
public class SQLSelectTest extends BaseReactiveTest {
36+
@RegisterExtension // We use native queries, which may be different for other DBs
37+
public DBSelectionExtension dbSelection = runOnlyFor( POSTGRESQL );
38+
39+
private SqlStatementTracker sqlTracker;
40+
private Person thePerson;
41+
public static String SQL = "xxxxx";
42+
43+
@Override
44+
protected Collection<Class<?>> annotatedEntities() {
45+
return List.of( Person.class );
46+
}
47+
48+
@Override
49+
protected Configuration constructConfiguration() {
50+
Configuration configuration = super.constructConfiguration();
51+
sqlTracker = new SqlStatementTracker( SQLSelectTest::doCheckQuery, configuration.getProperties() );
52+
return configuration;
53+
}
54+
55+
private static boolean doCheckQuery(String s) {
56+
return s.toLowerCase().startsWith( "select" );
57+
}
58+
59+
@Override
60+
protected void addServices(StandardServiceRegistryBuilder builder) {
61+
sqlTracker.registerService( builder );
62+
}
63+
64+
@BeforeEach
65+
public void populateDb(VertxTestContext context) {
66+
List<String> phones = Arrays.asList( "999-999-9999", "111-111-1111", "123-456-7890" );
67+
thePerson = new Person();
68+
thePerson.id = 724200;
69+
thePerson.name = "Claude";
70+
thePerson.phones = phones;
71+
72+
test( context, getMutinySessionFactory().withTransaction( (s, t) -> s.persist( thePerson ) ) );
73+
}
74+
75+
@Test
76+
public void findEntity(VertxTestContext context) {
77+
test( context, openSession()
78+
.thenCompose( session -> session.find( Person.class, thePerson.getId() ) )
79+
.thenAccept( found -> {
80+
assertPhones( found, "999-999-9999", "111-111-1111", "123-456-7890" );
81+
assertThat( sqlTracker.getLoggedQueries() ).contains( Person.SELECT_QUERY.replace( "?", "$1" ) );
82+
} )
83+
);
84+
}
85+
86+
private static void assertPhones(Person person, String... expectedPhones) {
87+
assertNotNull( person );
88+
assertThat( person.getPhones() ).containsExactlyInAnyOrder( expectedPhones );
89+
}
90+
91+
@Entity(name = "Person")
92+
@SQLSelect(sql = Person.SELECT_QUERY)
93+
static class Person {
94+
// Query containing simple where check to distinguish from a possible generated query
95+
public static final String SELECT_QUERY = "SELECT id, name FROM person WHERE id = ? and 'hreact' = 'hreact'";
96+
97+
@Id
98+
private int id;
99+
100+
private String name;
101+
102+
@ElementCollection(fetch = FetchType.EAGER)
103+
private List<String> phones = new ArrayList<>();
104+
105+
public int getId() {
106+
return id;
107+
}
108+
109+
public void setId(int id) {
110+
this.id = id;
111+
}
112+
113+
public String getName() {
114+
return name;
115+
}
116+
117+
public void setName(String name) {
118+
this.name = name;
119+
}
120+
121+
public List<String> getPhones() {
122+
return phones;
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)