Skip to content

Commit 202f144

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

File tree

1 file changed

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

1 file changed

+343
-0
lines changed
Lines changed: 343 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,343 @@
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 ) )
164+
.getResultList()
165+
.thenAccept( list -> assertOrderByBookArray( list, book1, book2 ) )
166+
.thenCompose( v -> session
167+
.createSelectionQuery( "select isbn, title from Book", Object[].class )
168+
.setOrder( desc( 2 ) )
169+
.getResultList()
170+
.thenAccept( list -> assertOrderByBookArray( list, book2, book1 ) )
171+
)
172+
) );
173+
}
174+
175+
@Test
176+
public void testAscDescBySelectElementMutiny(VertxTestContext context) {
177+
test( context, getMutinySessionFactory().withSession( session -> session
178+
.createSelectionQuery( "select isbn, title from Book", Object[].class )
179+
.setOrder( asc( 2 ) )
180+
.getResultList()
181+
.invoke( list -> assertOrderByBookArray( list, book1, book2 ) )
182+
.chain( v -> session
183+
.createSelectionQuery( "select isbn, title from Book", Object[].class )
184+
.setOrder( desc( 2 ) )
185+
.getResultList()
186+
.invoke( list -> assertOrderByBookArray( list, book2, book1 ) )
187+
)
188+
) );
189+
}
190+
191+
@Test
192+
public void testOrderWithList(VertxTestContext context) {
193+
test( context, getSessionFactory().withSession( session -> session
194+
.createSelectionQuery( "from Book", Book.class )
195+
.setOrder( List.of( asc( isbn ), desc( title ) ) ).getResultList()
196+
.thenAccept( isbnAsc -> assertThat( isbnAsc ).containsExactly( book2, book1 ) )
197+
.thenCompose( v -> session
198+
.createSelectionQuery( "from Book", Book.class )
199+
.setOrder( List.of( desc( isbn ), desc( title ) ) ).getResultList()
200+
.thenAccept( isbnDesc -> assertThat( isbnDesc ).containsExactly( book1, book2 ) )
201+
)
202+
) );
203+
}
204+
205+
@Test
206+
public void testOrderWithListMutiny(VertxTestContext context) {
207+
test( context, getMutinySessionFactory().withSession( session -> session
208+
.createSelectionQuery( "from Book", Book.class )
209+
.setOrder( List.of( asc( isbn ), desc( title ) ) ).getResultList()
210+
.invoke( isbnAsc -> assertThat( isbnAsc ).containsExactly( book2, book1 ) )
211+
.chain( v -> session
212+
.createSelectionQuery( "from Book", Book.class )
213+
.setOrder( List.of( desc( isbn ), desc( title ) ) )
214+
.getResultList()
215+
.invoke( isbnDesc -> assertThat( isbnDesc ).containsExactly( book1, book2 ) )
216+
)
217+
) );
218+
}
219+
220+
@Test
221+
public void testAscDescWithNamedParam(VertxTestContext context) {
222+
test( context, getSessionFactory().withSession( session -> session
223+
.createSelectionQuery( "from Book where title like :title", Book.class )
224+
.setParameter( "title", "%Hibernate%" )
225+
.setOrder( asc( title ) )
226+
.getResultList()
227+
.thenAccept( list -> assertThat( list ).containsExactly( book1, book2 ) )
228+
.thenCompose( v -> session
229+
.createSelectionQuery( "from Book where title like :title", Book.class )
230+
.setParameter( "title", "%Hibernate%" )
231+
.setOrder( desc( title ) ).getResultList()
232+
.thenAccept( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) )
233+
)
234+
) );
235+
}
236+
237+
@Test
238+
public void testAscDescWithNamedParamMutiny(VertxTestContext context) {
239+
test( context, getMutinySessionFactory().withSession( session -> session
240+
.createSelectionQuery( "from Book where title like :title", Book.class )
241+
.setParameter( "title", "%Hibernate%" )
242+
.setOrder( asc( title ) )
243+
.getResultList()
244+
.invoke( list -> assertThat( list ).containsExactly( book1, book2 ) )
245+
.chain( v -> session
246+
.createSelectionQuery( "from Book where title like :title", Book.class )
247+
.setParameter( "title", "%Hibernate%" )
248+
.setOrder( desc( title ) )
249+
.getResultList()
250+
.invoke( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) )
251+
)
252+
) );
253+
}
254+
255+
@Test
256+
public void testAscDescWithPositionalParam(VertxTestContext context) {
257+
test( context, getSessionFactory().withSession( session -> session
258+
.createSelectionQuery( "from Book where title like :title", Book.class )
259+
.setParameter( "title", "%Hibernate%" )
260+
.setOrder( asc( title ) )
261+
.getResultList()
262+
.thenAccept( list -> assertThat( list ).containsExactly( book1, book2 ) )
263+
.thenCompose( v -> session
264+
.createSelectionQuery( "from Book where title like :title", Book.class )
265+
.setParameter( "title", "%Hibernate%" )
266+
.setOrder( desc( title ) )
267+
.getResultList()
268+
.thenAccept( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) )
269+
)
270+
) );
271+
}
272+
273+
@Test
274+
public void testAscDescWithPositionalParamMutiny(VertxTestContext context) {
275+
test( context, getMutinySessionFactory().withSession( session -> session
276+
.createSelectionQuery( "from Book where title like :title", Book.class )
277+
.setParameter( "title", "%Hibernate%" )
278+
.setOrder( asc( title ) )
279+
.getResultList()
280+
.invoke( list -> assertThat( list ).containsExactly( book1, book2 ) )
281+
.chain( v -> session
282+
.createSelectionQuery( "from Book where title like :title", Book.class )
283+
.setParameter( "title", "%Hibernate%" )
284+
.setOrder( desc( title ) )
285+
.getResultList()
286+
.invoke( isbnDesc -> assertThat( isbnDesc ).containsExactly( book2, book1 ) )
287+
)
288+
) );
289+
}
290+
291+
private void assertOrderByBookArray(List<Object[]> resultList, Book... expectedBooks) {
292+
List<Book> books = resultList.stream()
293+
.map( objects -> new Book( (String) objects[0], (String) objects[1] ) )
294+
.collect( toList() );
295+
assertThat( books ).containsExactly( expectedBooks );
296+
}
297+
298+
private SingularAttribute<? super Book, ?> attribute(String name) {
299+
MappingMetamodelImpl metamodel = (MappingMetamodelImpl) getSessionFactory().getMetamodel();
300+
EntityDomainType<Book> bookType = metamodel.getJpaMetamodel().findEntityType( Book.class );
301+
return bookType.findSingularAttribute( name );
302+
}
303+
304+
@Entity(name = "Book")
305+
static class Book {
306+
@Id
307+
String isbn;
308+
String title;
309+
310+
Book(String isbn, String title) {
311+
this.isbn = isbn;
312+
this.title = title;
313+
}
314+
315+
Book() {
316+
}
317+
318+
@Override
319+
public boolean equals(Object o) {
320+
if ( this == o ) {
321+
return true;
322+
}
323+
if ( o == null || getClass() != o.getClass() ) {
324+
return false;
325+
}
326+
Book book = (Book) o;
327+
return Objects.equals( isbn, book.isbn ) && Objects.equals(
328+
title,
329+
book.title
330+
);
331+
}
332+
333+
@Override
334+
public int hashCode() {
335+
return Objects.hash( isbn, title );
336+
}
337+
338+
@Override
339+
public String toString() {
340+
return isbn + ":" + title;
341+
}
342+
}
343+
}

0 commit comments

Comments
 (0)