BAEL-5358 - Case Insensitive Sorting in MongoDB (#11815)
* [BAEL-5358] - MongoDB Case Insensitive Ordering. * [BAEL-5358] - removed comment, indent * [BAEL-5358] - using junit5 and testcontainers * [BAEL-5358] - fixed issues for PR * [BAEL-5358] - removed method-> inline
This commit is contained in:
parent
538c845739
commit
612dc0be7c
|
@ -30,6 +30,18 @@
|
|||
<artifactId>core</artifactId>
|
||||
<version>${morphia.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>mongodb</artifactId>
|
||||
<version>1.16.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>1.16.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -0,0 +1,108 @@
|
|||
package com.baeldung.ordering.caseinsensitive;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.client.*;
|
||||
import com.mongodb.client.model.Collation;
|
||||
import com.mongodb.client.model.Projections;
|
||||
import com.mongodb.client.model.Sorts;
|
||||
import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.testcontainers.containers.MongoDBContainer;
|
||||
import org.testcontainers.junit.jupiter.Container;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static com.mongodb.client.model.Aggregates.project;
|
||||
import static com.mongodb.client.model.Aggregates.sort;
|
||||
import static com.mongodb.client.model.Sorts.ascending;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@Testcontainers
|
||||
class CaseInsensitiveOrderingLiveTest {
|
||||
|
||||
private static MongoCollection<Document> userCollections;
|
||||
|
||||
@Container
|
||||
private static final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10"));
|
||||
|
||||
@BeforeAll
|
||||
private static void setup() {
|
||||
|
||||
MongoClient mongoClient = new MongoClient(mongoDBContainer.getContainerIpAddress(), mongoDBContainer.getMappedPort(27017));
|
||||
MongoDatabase database = mongoClient.getDatabase("test");
|
||||
userCollections = database.getCollection("users");
|
||||
|
||||
List<Document> list = new ArrayList<>();
|
||||
list.add(Document.parse("{'name': 'ben', surname: 'ThisField' }"));
|
||||
list.add(Document.parse("{'name': 'aen', surname: 'Does' }"));
|
||||
list.add(Document.parse("{'name': 'Ben', surname: 'Not' }"));
|
||||
list.add(Document.parse("{'name': 'cen', surname: 'Matter' }"));
|
||||
list.add(Document.parse("{'name': 'Aen', surname: 'Really' }"));
|
||||
list.add(Document.parse("{'name': 'Cen', surname: 'TrustMe' }"));
|
||||
|
||||
userCollections.insertMany(list);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMongoCollection_whenUsingFindWithSort_caseIsConsideredByDefault() {
|
||||
FindIterable<Document> nameDoc = userCollections.find().sort(ascending("name"));
|
||||
MongoCursor<Document> cursor = nameDoc.cursor();
|
||||
|
||||
List<String> expectedNamesOrdering = Arrays.asList("Aen", "Ben", "Cen", "aen", "ben", "cen");
|
||||
List<String> actualNamesOrdering = new ArrayList<>();
|
||||
while (cursor.hasNext()) {
|
||||
Document document = cursor.next();
|
||||
actualNamesOrdering.add(document.get("name").toString());
|
||||
}
|
||||
|
||||
assertEquals(expectedNamesOrdering, actualNamesOrdering);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMongoCollection_whenUsingFindWithSortAndCollation_caseIsNotConsidered() {
|
||||
FindIterable<Document> nameDoc = userCollections.find().sort(ascending("name"))
|
||||
.collation(Collation.builder().locale("en").build());
|
||||
MongoCursor<Document> cursor = nameDoc.cursor();
|
||||
List<String> expectedNamesOrdering = Arrays.asList("aen", "Aen", "ben", "Ben", "cen", "Cen");
|
||||
List<String> actualNamesOrdering = new ArrayList<>();
|
||||
while (cursor.hasNext()) {
|
||||
Document document = cursor.next();
|
||||
actualNamesOrdering.add(document.get("name").toString());
|
||||
}
|
||||
|
||||
assertEquals(expectedNamesOrdering, actualNamesOrdering);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMongoCollection_whenUsingFindWithSortAndAggregate_caseIsNotConsidered() {
|
||||
|
||||
Bson projectBson = project(
|
||||
Projections.fields(
|
||||
Projections.include("name", "surname"),
|
||||
Projections.computed("lowerName", Projections.computed("$toLower", "$name"))));
|
||||
|
||||
AggregateIterable<Document> nameDoc = userCollections.aggregate(
|
||||
Arrays.asList(projectBson,
|
||||
sort(Sorts.ascending("lowerName"))));
|
||||
|
||||
MongoCursor<Document> cursor = nameDoc.cursor();
|
||||
|
||||
List<String> expectedNamesOrdering = Arrays.asList("aen", "Aen", "ben", "Ben", "cen", "Cen");
|
||||
List<String> actualNamesOrdering = new ArrayList<>();
|
||||
while (cursor.hasNext()) {
|
||||
Document document = cursor.next();
|
||||
actualNamesOrdering.add(document.get("name").toString());
|
||||
}
|
||||
|
||||
assertEquals(expectedNamesOrdering, actualNamesOrdering);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue