2121import jakarta .nosql .document .DocumentQuery ;
2222import org .eclipse .jnosql .diana .document .Documents ;
2323import org .elasticsearch .index .query .TermQueryBuilder ;
24+ import org .junit .jupiter .api .Assertions ;
2425import org .junit .jupiter .api .BeforeEach ;
2526import org .junit .jupiter .api .Test ;
27+ import org .testcontainers .shaded .org .apache .commons .lang .text .StrBuilder ;
2628
2729import java .time .Duration ;
2830import java .util .ArrayList ;
2931import java .util .Arrays ;
3032import java .util .List ;
33+ import java .util .Optional ;
34+ import java .util .concurrent .TimeUnit ;
3135import java .util .stream .Collectors ;
3236
3337import static jakarta .nosql .document .DocumentDeleteQuery .delete ;
3842import static org .hamcrest .MatcherAssert .assertThat ;
3943import static org .hamcrest .Matchers .contains ;
4044import static org .hamcrest .Matchers .containsInAnyOrder ;
45+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
4146import static org .junit .jupiter .api .Assertions .assertEquals ;
4247import static org .junit .jupiter .api .Assertions .assertFalse ;
4348import static org .junit .jupiter .api .Assertions .assertNotNull ;
@@ -54,6 +59,10 @@ public void setUp() {
5459 ElasticsearchDocumentCollectionManagerFactory managerFactory = ElasticsearchDocumentCollectionManagerFactorySupplier .INSTANCE .get ();
5560 entityManager = managerFactory .get (DocumentEntityGerator .INDEX );
5661
62+ DocumentDeleteQuery deleteQuery = DocumentDeleteQuery .delete ().from ("person" ).build ();
63+
64+ entityManager .delete (deleteQuery );
65+
5766 }
5867
5968 @ Test
@@ -76,10 +85,11 @@ public void shouldInsertTTL() {
7685 }
7786
7887 @ Test
79- public void shouldReturnAll () {
88+ public void shouldReturnAll () throws InterruptedException {
8089 DocumentEntity entity = DocumentEntityGerator .getEntity ();
8190 entityManager .insert (entity );
8291 DocumentQuery query = select ().from (DocumentEntityGerator .COLLECTION_NAME ).build ();
92+ SECONDS .sleep (1L );
8393 List <DocumentEntity > result = entityManager .select (query ).collect (Collectors .toList ());
8494 assertFalse (result .isEmpty ());
8595 }
@@ -161,6 +171,45 @@ public void shouldFindDocumentById() {
161171 assertThat (entities , contains (entity ));
162172 }
163173
174+ @ Test
175+ public void shouldFindOrderByName () throws InterruptedException {
176+ final DocumentEntity poliana = DocumentEntityGerator .getEntity ();
177+ final DocumentEntity otavio = DocumentEntityGerator .getEntity ();
178+ poliana .add ("name" , "poliana" );
179+ otavio .add ("name" , "otavio" );
180+ otavio .add ("_id" , "id2" );
181+ entityManager .insert (Arrays .asList (poliana , otavio ));
182+ SECONDS .sleep (1L );
183+ DocumentQuery query = DocumentQuery .select ().from ("person" ).orderBy ("name" ).asc ().build ();
184+ String [] names = entityManager .select (query ).map (d -> d .find ("name" ))
185+ .filter (Optional ::isPresent )
186+ .map (Optional ::get )
187+ .map (d -> d .get (String .class ))
188+ .toArray (String []::new );
189+
190+ assertArrayEquals (names , new String []{"otavio" , "poliana" });
191+ }
192+
193+ @ Test
194+ public void shouldFindOrderByNameDesc () throws InterruptedException {
195+ final DocumentEntity poliana = DocumentEntityGerator .getEntity ();
196+ final DocumentEntity otavio = DocumentEntityGerator .getEntity ();
197+ poliana .add ("name" , "poliana" );
198+ otavio .add ("name" , "otavio" );
199+ otavio .add ("_id" , "id2" );
200+ entityManager .insert (Arrays .asList (poliana , otavio ));
201+ SECONDS .sleep (1L );
202+ DocumentQuery query = DocumentQuery .select ().from ("person" ).orderBy ("name" ).desc ().build ();
203+ String [] names = entityManager .select (query ).map (d -> d .find ("name" ))
204+ .filter (Optional ::isPresent )
205+ .map (Optional ::get )
206+ .map (d -> d .get (String .class ))
207+ .toArray (String []::new );
208+
209+ assertArrayEquals (names , new String []{"poliana" , "otavio" });
210+ }
211+
212+
164213 @ Test
165214 public void shouldFindAll () throws InterruptedException {
166215 entityManager .insert (DocumentEntityGerator .getEntity ());
0 commit comments