Skip to content

Commit a05fbeb

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

File tree

1 file changed

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

1 file changed

+341
-0
lines changed
Lines changed: 341 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,341 @@
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+
.map( row -> row[0] )
73+
.containsExactly( book2.title, book1.title )
74+
)
75+
) );
76+
}
77+
78+
@Test
79+
public void ascAttributeWithStage(VertxTestContext context) {
80+
test( context, getSessionFactory().withSession( session -> session
81+
.createSelectionQuery( "from Book", Book.class )
82+
.setOrder( asc( title ) )
83+
.getResultList()
84+
.thenAccept( books -> assertThat( books ).containsExactly( book1, book2 ) )
85+
) );
86+
}
87+
88+
@Test
89+
public void ascAttributeWithMutiny(VertxTestContext context) {
90+
test( context, getMutinySessionFactory().withSession( session -> session
91+
.createSelectionQuery( "from Book", Book.class )
92+
.setOrder( asc( title ) )
93+
.getResultList()
94+
.invoke( books -> assertThat( books ).containsExactly( book1, book2 ) )
95+
) );
96+
}
97+
98+
@Test
99+
public void descAttributeWithStage(VertxTestContext context) {
100+
test( context, getSessionFactory().withSession( session -> session
101+
.createSelectionQuery( "from Book", Book.class )
102+
.setOrder( desc( title ) )
103+
.getResultList()
104+
.thenAccept( books -> assertThat( books ).containsExactly( book2, book1 ) )
105+
) );
106+
}
107+
108+
@Test
109+
public void descAttributeWithMutiny(VertxTestContext context) {
110+
test( context, getMutinySessionFactory().withSession( session -> session
111+
.createSelectionQuery( "from Book", Book.class )
112+
.setOrder( desc( title ) )
113+
.getResultList()
114+
.invoke( books -> assertThat( books ).containsExactly( book2, book1 ) )
115+
) );
116+
}
117+
118+
@Test
119+
public void ascIdWithStage(VertxTestContext context) {
120+
test( context, getSessionFactory().withSession( session -> session
121+
.createSelectionQuery( "from Book", Book.class )
122+
.setOrder( asc( isbn ) )
123+
.getResultList()
124+
.thenAccept( books -> assertThat( books ).containsExactly( book2, book1 ) )
125+
) );
126+
}
127+
128+
@Test
129+
public void ascIdWithMutiny(VertxTestContext context) {
130+
test( context, getMutinySessionFactory().withSession( session -> session
131+
.createSelectionQuery( "from Book", Book.class )
132+
.setOrder( asc( isbn ) )
133+
.getResultList()
134+
.invoke( books -> assertThat( books ).containsExactly( book2, book1 ) )
135+
) );
136+
}
137+
138+
@Test
139+
public void descIdWithStage(VertxTestContext context) {
140+
test( context, getSessionFactory().withSession( session -> session
141+
.createSelectionQuery( "from Book", Book.class )
142+
.setOrder( desc( isbn ) )
143+
.getResultList()
144+
.thenAccept( books -> assertThat( books ).containsExactly( book1, book2 ) )
145+
) );
146+
}
147+
148+
@Test
149+
public void descIdWithMutiny(VertxTestContext context) {
150+
test( context, getMutinySessionFactory().withSession( session -> session
151+
.createSelectionQuery( "from Book", Book.class )
152+
.setOrder( desc( isbn ) )
153+
.getResultList()
154+
.invoke( books -> assertThat( books ).containsExactly( book1, book2 ) )
155+
) );
156+
}
157+
158+
@Test
159+
public void testAscDescBySelectElement(VertxTestContext context) {
160+
test( context, getSessionFactory().withSession( session -> session
161+
.createSelectionQuery( "select isbn, title from Book", Object[].class )
162+
.setOrder( asc( 2 ) ).getResultList()
163+
.thenAccept( list -> assertOrderByBookArray( list, book1, book2 ) )
164+
.thenCompose( v -> session
165+
.createSelectionQuery( "select isbn, title from Book", Object[].class )
166+
.setOrder( desc( 2 ) )
167+
.getResultList()
168+
.thenAccept( list -> assertOrderByBookArray( list, book2, book1 ) )
169+
)
170+
) );
171+
}
172+
173+
@Test
174+
public void testAscDescBySelectElementMutiny(VertxTestContext context) {
175+
test( context, getMutinySessionFactory().withSession( session -> session
176+
.createSelectionQuery( "select isbn, title from Book", Object[].class )
177+
.setOrder( asc( 2 ) )
178+
.getResultList()
179+
.invoke( list -> assertOrderByBookArray( list, book1, book2 ) )
180+
.chain( v -> session
181+
.createSelectionQuery( "select isbn, title from Book", Object[].class )
182+
.setOrder( desc( 2 ) )
183+
.getResultList()
184+
.invoke( list -> assertOrderByBookArray( list, book2, book1 ) )
185+
)
186+
) );
187+
}
188+
189+
@Test
190+
public void testOrderWithList(VertxTestContext context) {
191+
test( context, getSessionFactory().withSession( session -> session
192+
.createSelectionQuery( "from Book", Book.class )
193+
.setOrder( List.of( asc( isbn ), desc( title ) ) ).getResultList()
194+
.thenAccept( isbnAsc -> assertThat( isbnAsc ).containsExactly( book2, book1 ) )
195+
.thenCompose( v -> session
196+
.createSelectionQuery( "from Book", Book.class )
197+
.setOrder( List.of( desc( isbn ), desc( title ) ) ).getResultList()
198+
.thenAccept( isbnDesc -> assertThat( isbnDesc ).containsExactly( book1, book2 ) )
199+
)
200+
) );
201+
}
202+
203+
@Test
204+
public void testOrderWithListMutiny(VertxTestContext context) {
205+
test( context, getMutinySessionFactory().withSession( session -> session
206+
.createSelectionQuery( "from Book", Book.class )
207+
.setOrder( List.of( asc( isbn ), desc( title ) ) ).getResultList()
208+
.invoke( isbnAsc -> assertThat( isbnAsc ).containsExactly( book2, book1 ) )
209+
.chain( v -> session
210+
.createSelectionQuery( "from Book", Book.class )
211+
.setOrder( List.of( desc( isbn ), desc( title ) ) )
212+
.getResultList()
213+
.invoke( isbnDesc -> assertThat( isbnDesc ).containsExactly( book1, book2 ) )
214+
)
215+
) );
216+
}
217+
218+
@Test
219+
public void testAscDescWithNamedParam(VertxTestContext context) {
220+
test( context, getSessionFactory().withSession( session -> session
221+
.createSelectionQuery( "from Book where title like :title", Book.class )
222+
.setParameter( "title", "%Hibernate%" )
223+
.setOrder( asc( title ) )
224+
.getResultList()
225+
.thenAccept( list -> assertThat( list ).containsExactly( book1, book2 ) )
226+
.thenCompose( v -> session
227+
.createSelectionQuery( "from Book where title like :title", Book.class )
228+
.setParameter( "title", "%Hibernate%" )
229+
.setOrder( desc( title ) ).getResultList()
230+
.thenAccept( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) )
231+
)
232+
) );
233+
}
234+
235+
@Test
236+
public void testAscDescWithNamedParamMutiny(VertxTestContext context) {
237+
test( context, getMutinySessionFactory().withSession( session -> session
238+
.createSelectionQuery( "from Book where title like :title", Book.class )
239+
.setParameter( "title", "%Hibernate%" )
240+
.setOrder( asc( title ) )
241+
.getResultList()
242+
.invoke( list -> assertThat( list ).containsExactly( book1, book2 ) )
243+
.chain( v -> session
244+
.createSelectionQuery( "from Book where title like :title", Book.class )
245+
.setParameter( "title", "%Hibernate%" )
246+
.setOrder( desc( title ) )
247+
.getResultList()
248+
.invoke( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) )
249+
)
250+
) );
251+
}
252+
253+
@Test
254+
public void testAscDescWithPositionalParam(VertxTestContext context) {
255+
test( context, getSessionFactory().withSession( session -> session
256+
.createSelectionQuery( "from Book where title like :title", Book.class )
257+
.setParameter( "title", "%Hibernate%" )
258+
.setOrder( asc( title ) )
259+
.getResultList()
260+
.thenAccept( list -> assertThat( list ).containsExactly( book1, book2 ) )
261+
.thenCompose( v -> session
262+
.createSelectionQuery( "from Book where title like :title", Book.class )
263+
.setParameter( "title", "%Hibernate%" )
264+
.setOrder( desc( title ) )
265+
.getResultList()
266+
.thenAccept( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) )
267+
)
268+
) );
269+
}
270+
271+
@Test
272+
public void testAscDescWithPositionalParamMutiny(VertxTestContext context) {
273+
test( context, getMutinySessionFactory().withSession( session -> session
274+
.createSelectionQuery( "from Book where title like :title", Book.class )
275+
.setParameter( "title", "%Hibernate%" )
276+
.setOrder( asc( title ) )
277+
.getResultList()
278+
.invoke( list -> assertThat( list ).containsExactly( book1, book2 ) )
279+
.chain( v -> session
280+
.createSelectionQuery( "from Book where title like :title", Book.class )
281+
.setParameter( "title", "%Hibernate%" )
282+
.setOrder( desc( title ) )
283+
.getResultList()
284+
.invoke( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) )
285+
)
286+
) );
287+
}
288+
289+
private void assertOrderByBookArray(List<Object[]> resultList, Book... expectedBooks) {
290+
List<Book> books = resultList.stream()
291+
.map( objects -> new Book( (String) objects[0], (String) objects[1] ) )
292+
.collect( toList() );
293+
assertThat( books ).containsExactly( expectedBooks );
294+
}
295+
296+
private SingularAttribute<? super Book, ?> attribute(String name) {
297+
MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel();
298+
EntityDomainType<Book> bookType = metamodel.getJpaMetamodel().findEntityType( Book.class );
299+
return bookType.findSingularAttribute( name );
300+
}
301+
302+
@Entity(name = "Book")
303+
static class Book {
304+
@Id
305+
String isbn;
306+
String title;
307+
308+
Book(String isbn, String title) {
309+
this.isbn = isbn;
310+
this.title = title;
311+
}
312+
313+
Book() {
314+
}
315+
316+
@Override
317+
public boolean equals(Object o) {
318+
if ( this == o ) {
319+
return true;
320+
}
321+
if ( o == null || getClass() != o.getClass() ) {
322+
return false;
323+
}
324+
Book book = (Book) o;
325+
return Objects.equals( isbn, book.isbn ) && Objects.equals(
326+
title,
327+
book.title
328+
);
329+
}
330+
331+
@Override
332+
public int hashCode() {
333+
return Objects.hash( isbn, title );
334+
}
335+
336+
@Override
337+
public String toString() {
338+
return isbn + ":" + title;
339+
}
340+
}
341+
}

0 commit comments

Comments
 (0)