Skip to content

Commit 4c7cba1

Browse files
blafondDavideD
authored andcommitted
[hibernate#1735] tests for setOrder()
1 parent 844376f commit 4c7cba1

File tree

1 file changed

+342
-0
lines changed
  • hibernate-reactive-core/src/test/java/org/hibernate/reactive

1 file changed

+342
-0
lines changed
Lines changed: 342 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
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.Collection;
9+
import java.util.List;
10+
import java.util.Objects;
11+
12+
import org.hibernate.metamodel.model.domain.EntityDomainType;
13+
import org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl;
14+
15+
import org.junit.jupiter.api.BeforeEach;
16+
import org.junit.jupiter.api.Test;
17+
18+
import io.vertx.junit5.Timeout;
19+
import io.vertx.junit5.VertxTestContext;
20+
import jakarta.persistence.Entity;
21+
import jakarta.persistence.Id;
22+
import jakarta.persistence.metamodel.SingularAttribute;
23+
24+
import static java.util.concurrent.TimeUnit.MINUTES;
25+
import static java.util.stream.Collectors.toList;
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.hibernate.query.Order.asc;
28+
import static org.hibernate.query.Order.desc;
29+
30+
@Timeout(value = 10, timeUnit = MINUTES)
31+
public class OrderTest extends BaseReactiveTest {
32+
final Book book1 = new Book( "9781932394153", "Hibernate in Action" );
33+
final Book book2 = new Book( "9781617290459", "Java Persistence with Hibernate" );
34+
35+
SingularAttribute<? super Book, ?> isbn;
36+
SingularAttribute<? super Book, ?> title;
37+
38+
@Override
39+
protected Collection<Class<?>> annotatedEntities() {
40+
return List.of( Book.class );
41+
}
42+
43+
@BeforeEach
44+
public void populateDB(VertxTestContext context) {
45+
isbn = attribute( "isbn" );
46+
title = attribute( "title" );
47+
test( context, getSessionFactory().withTransaction( session -> session.persist( book1, book2 ) ) );
48+
}
49+
50+
@Test
51+
public void descPositionalColumnWithStage(VertxTestContext context) {
52+
test( context, getSessionFactory().withSession( session -> session
53+
// Not sure if it's a bug, but setOrder doesn't work if we use String.class
54+
.createSelectionQuery( "select title from Book", Object[].class )
55+
.setOrder( desc( 1 ) )
56+
.getResultList()
57+
.thenAccept( results -> assertThat( results )
58+
// Keep the title
59+
.map( row -> row[0] )
60+
.containsExactly( book2.title, book1.title ) )
61+
) );
62+
}
63+
64+
@Test
65+
public void descPositionalColumnWithMutiny(VertxTestContext context) {
66+
test( context, getMutinySessionFactory().withSession( session -> session
67+
// Not sure if it's a bug, but setOrder doesn't work if we use String.class or Object.class
68+
.createSelectionQuery( "select title from Book", Object[].class )
69+
.setOrder( desc( 1 ) )
70+
.getResultList()
71+
.invoke( results -> assertThat( results )
72+
// Keep the title
73+
.map( row -> row[0] )
74+
.containsExactly( book2.title, book1.title )
75+
)
76+
) );
77+
}
78+
79+
@Test
80+
public void ascAttributeWithStage(VertxTestContext context) {
81+
test( context, getSessionFactory().withSession( session -> session
82+
.createSelectionQuery( "from Book", Book.class )
83+
.setOrder( asc( title ) )
84+
.getResultList()
85+
.thenAccept( books -> assertThat( books ).containsExactly( book1, book2 ) )
86+
) );
87+
}
88+
89+
@Test
90+
public void ascAttributeWithMutiny(VertxTestContext context) {
91+
test( context, getMutinySessionFactory().withSession( session -> session
92+
.createSelectionQuery( "from Book", Book.class )
93+
.setOrder( asc( title ) )
94+
.getResultList()
95+
.invoke( books -> assertThat( books ).containsExactly( book1, book2 ) )
96+
) );
97+
}
98+
99+
@Test
100+
public void descAttributeWithStage(VertxTestContext context) {
101+
test( context, getSessionFactory().withSession( session -> session
102+
.createSelectionQuery( "from Book", Book.class )
103+
.setOrder( desc( title ) )
104+
.getResultList()
105+
.thenAccept( books -> assertThat( books ).containsExactly( book2, book1 ) )
106+
) );
107+
}
108+
109+
@Test
110+
public void descAttributeWithMutiny(VertxTestContext context) {
111+
test( context, getMutinySessionFactory().withSession( session -> session
112+
.createSelectionQuery( "from Book", Book.class )
113+
.setOrder( desc( title ) )
114+
.getResultList()
115+
.invoke( books -> assertThat( books ).containsExactly( book2, book1 ) )
116+
) );
117+
}
118+
119+
@Test
120+
public void ascIdWithStage(VertxTestContext context) {
121+
test( context, getSessionFactory().withSession( session -> session
122+
.createSelectionQuery( "from Book", Book.class )
123+
.setOrder( asc( isbn ) )
124+
.getResultList()
125+
.thenAccept( books -> assertThat( books ).containsExactly( book2, book1 ) )
126+
) );
127+
}
128+
129+
@Test
130+
public void ascIdWithMutiny(VertxTestContext context) {
131+
test( context, getMutinySessionFactory().withSession( session -> session
132+
.createSelectionQuery( "from Book", Book.class )
133+
.setOrder( asc( isbn ) )
134+
.getResultList()
135+
.invoke( books -> assertThat( books ).containsExactly( book2, book1 ) )
136+
) );
137+
}
138+
139+
@Test
140+
public void descIdWithStage(VertxTestContext context) {
141+
test( context, getSessionFactory().withSession( session -> session
142+
.createSelectionQuery( "from Book", Book.class )
143+
.setOrder( desc( isbn ) )
144+
.getResultList()
145+
.thenAccept( books -> assertThat( books ).containsExactly( book1, book2 ) )
146+
) );
147+
}
148+
149+
@Test
150+
public void descIdWithMutiny(VertxTestContext context) {
151+
test( context, getMutinySessionFactory().withSession( session -> session
152+
.createSelectionQuery( "from Book", Book.class )
153+
.setOrder( desc( isbn ) )
154+
.getResultList()
155+
.invoke( books -> assertThat( books ).containsExactly( book1, book2 ) )
156+
) );
157+
}
158+
159+
@Test
160+
public void testAscDescBySelectElement(VertxTestContext context) {
161+
test( context, getSessionFactory().withSession( session -> session
162+
.createSelectionQuery( "select isbn, title from Book", Object[].class )
163+
.setOrder( asc( 2 ) ).getResultList()
164+
.thenAccept( list -> assertOrderByBookArray( list, book1, book2 ) )
165+
.thenCompose( v -> session
166+
.createSelectionQuery( "select isbn, title from Book", Object[].class )
167+
.setOrder( desc( 2 ) )
168+
.getResultList()
169+
.thenAccept( list -> assertOrderByBookArray( list, book2, book1 ) )
170+
)
171+
) );
172+
}
173+
174+
@Test
175+
public void testAscDescBySelectElementMutiny(VertxTestContext context) {
176+
test( context, getMutinySessionFactory().withSession( session -> session
177+
.createSelectionQuery( "select isbn, title from Book", Object[].class )
178+
.setOrder( asc( 2 ) )
179+
.getResultList()
180+
.invoke( list -> assertOrderByBookArray( list, book1, book2 ) )
181+
.chain( v -> session
182+
.createSelectionQuery( "select isbn, title from Book", Object[].class )
183+
.setOrder( desc( 2 ) )
184+
.getResultList()
185+
.invoke( list -> assertOrderByBookArray( list, book2, book1 ) )
186+
)
187+
) );
188+
}
189+
190+
@Test
191+
public void testOrderWithList(VertxTestContext context) {
192+
test( context, getSessionFactory().withSession( session -> session
193+
.createSelectionQuery( "from Book", Book.class )
194+
.setOrder( List.of( asc( isbn ), desc( title ) ) ).getResultList()
195+
.thenAccept( isbnAsc -> assertThat( isbnAsc ).containsExactly( book2, book1 ) )
196+
.thenCompose( v -> session
197+
.createSelectionQuery( "from Book", Book.class )
198+
.setOrder( List.of( desc( isbn ), desc( title ) ) ).getResultList()
199+
.thenAccept( isbnDesc -> assertThat( isbnDesc ).containsExactly( book1, book2 ) )
200+
)
201+
) );
202+
}
203+
204+
@Test
205+
public void testOrderWithListMutiny(VertxTestContext context) {
206+
test( context, getMutinySessionFactory().withSession( session -> session
207+
.createSelectionQuery( "from Book", Book.class )
208+
.setOrder( List.of( asc( isbn ), desc( title ) ) ).getResultList()
209+
.invoke( isbnAsc -> assertThat( isbnAsc ).containsExactly( book2, book1 ) )
210+
.chain( v -> session
211+
.createSelectionQuery( "from Book", Book.class )
212+
.setOrder( List.of( desc( isbn ), desc( title ) ) )
213+
.getResultList()
214+
.invoke( isbnDesc -> assertThat( isbnDesc ).containsExactly( book1, book2 ) )
215+
)
216+
) );
217+
}
218+
219+
@Test
220+
public void testAscDescWithNamedParam(VertxTestContext context) {
221+
test( context, getSessionFactory().withSession( session -> session
222+
.createSelectionQuery( "from Book where title like :title", Book.class )
223+
.setParameter( "title", "%Hibernate%" )
224+
.setOrder( asc( title ) )
225+
.getResultList()
226+
.thenAccept( list -> assertThat( list ).containsExactly( book1, book2 ) )
227+
.thenCompose( v -> session
228+
.createSelectionQuery( "from Book where title like :title", Book.class )
229+
.setParameter( "title", "%Hibernate%" )
230+
.setOrder( desc( title ) ).getResultList()
231+
.thenAccept( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) )
232+
)
233+
) );
234+
}
235+
236+
@Test
237+
public void testAscDescWithNamedParamMutiny(VertxTestContext context) {
238+
test( context, getMutinySessionFactory().withSession( session -> session
239+
.createSelectionQuery( "from Book where title like :title", Book.class )
240+
.setParameter( "title", "%Hibernate%" )
241+
.setOrder( asc( title ) )
242+
.getResultList()
243+
.invoke( list -> assertThat( list ).containsExactly( book1, book2 ) )
244+
.chain( v -> session
245+
.createSelectionQuery( "from Book where title like :title", Book.class )
246+
.setParameter( "title", "%Hibernate%" )
247+
.setOrder( desc( title ) )
248+
.getResultList()
249+
.invoke( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) )
250+
)
251+
) );
252+
}
253+
254+
@Test
255+
public void testAscDescWithPositionalParam(VertxTestContext context) {
256+
test( context, getSessionFactory().withSession( session -> session
257+
.createSelectionQuery( "from Book where title like :title", Book.class )
258+
.setParameter( "title", "%Hibernate%" )
259+
.setOrder( asc( title ) )
260+
.getResultList()
261+
.thenAccept( list -> assertThat( list ).containsExactly( book1, book2 ) )
262+
.thenCompose( v -> session
263+
.createSelectionQuery( "from Book where title like :title", Book.class )
264+
.setParameter( "title", "%Hibernate%" )
265+
.setOrder( desc( title ) )
266+
.getResultList()
267+
.thenAccept( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) )
268+
)
269+
) );
270+
}
271+
272+
@Test
273+
public void testAscDescWithPositionalParamMutiny(VertxTestContext context) {
274+
test( context, getMutinySessionFactory().withSession( session -> session
275+
.createSelectionQuery( "from Book where title like :title", Book.class )
276+
.setParameter( "title", "%Hibernate%" )
277+
.setOrder( asc( title ) )
278+
.getResultList()
279+
.invoke( list -> assertThat( list ).containsExactly( book1, book2 ) )
280+
.chain( v -> session
281+
.createSelectionQuery( "from Book where title like :title", Book.class )
282+
.setParameter( "title", "%Hibernate%" )
283+
.setOrder( desc( title ) )
284+
.getResultList()
285+
.invoke( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) )
286+
)
287+
) );
288+
}
289+
290+
private void assertOrderByBookArray(List<Object[]> resultList, Book... expectedBooks) {
291+
List<Book> books = resultList.stream()
292+
.map( objects -> new Book( (String) objects[0], (String) objects[1] ) )
293+
.collect( toList() );
294+
assertThat( books ).containsExactly( expectedBooks );
295+
}
296+
297+
private SingularAttribute<? super Book, ?> attribute(String name) {
298+
MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel();
299+
EntityDomainType<Book> bookType = metamodel.getJpaMetamodel().findEntityType( Book.class );
300+
return bookType.findSingularAttribute( name );
301+
}
302+
303+
@Entity(name = "Book")
304+
static class Book {
305+
@Id
306+
String isbn;
307+
String title;
308+
309+
Book(String isbn, String title) {
310+
this.isbn = isbn;
311+
this.title = title;
312+
}
313+
314+
Book() {
315+
}
316+
317+
@Override
318+
public boolean equals(Object o) {
319+
if ( this == o ) {
320+
return true;
321+
}
322+
if ( o == null || getClass() != o.getClass() ) {
323+
return false;
324+
}
325+
Book book = (Book) o;
326+
return Objects.equals( isbn, book.isbn ) && Objects.equals(
327+
title,
328+
book.title
329+
);
330+
}
331+
332+
@Override
333+
public int hashCode() {
334+
return Objects.hash( isbn, title );
335+
}
336+
337+
@Override
338+
public String toString() {
339+
return isbn + ":" + title;
340+
}
341+
}
342+
}

0 commit comments

Comments
 (0)