Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class Aggregation {
private final MongoCollection<?> collection;

private final List<AggregationStage> stages = new ArrayList<>();
private Map<String, Object> variables = Collections.emptyMap();
private Document variables = new Document();

private Aggregation(MongoCollection<?> collection) {
this.collection = collection;
Expand Down Expand Up @@ -198,6 +198,7 @@ public List<Document> runStages(Stream<Document> stream) {
}

public Stream<Document> runStagesAsStream(Stream<Document> stream) {
stream = stream.map(Document::cloneDeeply);
if (hasVariables()) {
stream = stream.map(this::addAllVariables);
}
Expand All @@ -215,9 +216,8 @@ private boolean hasVariables() {
}

private Document addAllVariables(Document document) {
Document clone = document.clone();
clone.putAll(variables);
return clone;
variables.cloneInto(document);
return document;
}

private Document removeAllVariables(Document document) {
Expand Down Expand Up @@ -245,7 +245,7 @@ public List<Document> computeResult() {
}

public void setVariables(Map<String, Object> variables) {
this.variables = Collections.unmodifiableMap(variables);
this.variables = new Document(variables);
}

public void validate(Document query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private List<Document> findLinkedDocuments(Object value) {
}
Object newValue = Utils.getSubdocumentValueCollectionAware(newDocument, connectFromField);
enqueueValues(pending, newValue, current.depth() + 1);
Document result = newDocument.clone();
Document result = newDocument.cloneDeeply();
if (depthField != null) {
result.put(depthField, current.depth());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ private List<Document> lookupValue(Object value) {
value = null;
}
Document query = new Document(foreignField, value);
return collection.handleQueryAsStream(query).collect(toList());
return collection.handleQueryAsStream(query)
.map(Document::cloneDeeply)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import de.bwaldvogel.mongo.MongoDatabase;

@ExtendWith(MockitoExtension.class)
public abstract class AbstractLookupStageTest {
abstract class AbstractLookupStageTest {

@Mock
MongoDatabase database;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import de.bwaldvogel.mongo.bson.Document;
import de.bwaldvogel.mongo.exception.MongoServerError;

public class AddFieldsStageTest {
class AddFieldsStageTest {

@Test
void testAddFields() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ void setUp() {

@Test
void testGraphLookupObjectThatHasLinkedDocuments() {
GraphLookupStage graphLookupStage = buildGraphLookupStage("from: 'employees', 'startWith': '$manager', 'connectFromField': 'manager', 'connectToField': 'name', 'as': 'managers', 'depthField': 'order'");
GraphLookupStage graphLookupStage = buildGraphLookupStage("""
from: 'employees',
'startWith': '$manager',
'connectFromField': 'manager',
'connectToField': 'name',
'as': 'managers',
'depthField': 'order'
""");
configureEmployeesCollection("name: 'Dave'", "_id: 2, name: 'Dave', manager: 'Mike'");
configureEmployeesCollection("name: 'Mike'", "_id: 1, name: 'Mike'");
Document document = json("name: 'Bob', manager: 'Dave'");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,80 @@
import de.bwaldvogel.mongo.bson.Document;
import de.bwaldvogel.mongo.exception.MongoServerException;

public class LookupStageTest extends AbstractLookupStageTest {
class LookupStageTest extends AbstractLookupStageTest {

@Mock
private MongoCollection<?> authorsCollection;

@Test
void testMissingFromField() {
assertThatThrownBy(() -> buildLookupStage("localField: 'authorId', foreignField: '_id', as: 'author'"))
assertThatThrownBy(() -> buildLookupStage("""
localField: 'authorId',
foreignField: '_id',
as: 'author'
"""))
.isInstanceOf(MongoServerException.class)
.hasMessage("[Error 9] missing 'from' option to $lookup stage specification: " +
"{\"localField\" : \"authorId\", \"foreignField\" : \"_id\", \"as\" : \"author\"}");
}

@Test
void testMissingLocalFieldField() {
assertThatThrownBy(() -> buildLookupStage("from: 'authors', foreignField: '_id', as: 'author'"))
assertThatThrownBy(() -> buildLookupStage("""
from: 'authors',
foreignField: '_id',
as: 'author'
"""))
.isInstanceOf(MongoServerException.class)
.hasMessage("[Error 9] missing 'localField' option to $lookup stage specification: " +
"{\"from\" : \"authors\", \"foreignField\" : \"_id\", \"as\" : \"author\"}");
}

@Test
void testMissingForeignFieldField() {
assertThatThrownBy(() -> buildLookupStage("from: 'authors', localField: 'authorId', as: 'author'"))
assertThatThrownBy(() -> buildLookupStage("""
from: 'authors',
localField: 'authorId',
as: 'author'
"""))
.isInstanceOf(MongoServerException.class)
.hasMessage("[Error 9] missing 'foreignField' option to $lookup stage specification: " +
"{\"from\" : \"authors\", \"localField\" : \"authorId\", \"as\" : \"author\"}");
}

@Test
void testMissingAsField() {
assertThatThrownBy(() -> buildLookupStage("from: 'authors', localField: 'authorId', foreignField: '_id'"))
assertThatThrownBy(() -> buildLookupStage("""
from: 'authors',
localField: 'authorId',
foreignField: '_id'
"""))
.isInstanceOf(MongoServerException.class)
.hasMessage("[Error 9] missing 'as' option to $lookup stage specification: " +
"{\"from\" : \"authors\", \"localField\" : \"authorId\", \"foreignField\" : \"_id\"}");
}

@Test
void testFromFieldWithWrongType() {
assertThatThrownBy(() -> buildLookupStage("from: 1, localField: 'authorId', foreignField: '_id', as: 'author'"))
assertThatThrownBy(() -> buildLookupStage("""
from: 1,
localField: 'authorId',
foreignField: '_id',
as: 'author'
"""))
.isInstanceOf(MongoServerException.class)
.hasMessage("[Error 9] 'from' option to $lookup must be a string, but was type int");
}

@Test
void testUnexpectedConfigurationField() {
assertThatThrownBy(() -> buildLookupStage("from: 'authors', localField: 'authorId', foreignField: '_id', as: 'author', unknown: 'hello'"))
assertThatThrownBy(() -> buildLookupStage("""
from: 'authors',
localField: 'authorId',
foreignField: '_id',
as: 'author',
unknown: 'hello'
"""))
.isInstanceOf(MongoServerException.class)
.hasMessage("[Error 9] unknown argument to $lookup: unknown");
}
Expand All @@ -70,7 +97,12 @@ void testUnexpectedConfigurationField() {
void testLookupObjectThatExists() {
prepareAuthorsCollectionMock();

LookupStage lookupStage = buildLookupStage("from: 'authors', 'localField': 'authorId', foreignField: '_id', as: 'author'");
LookupStage lookupStage = buildLookupStage("""
from: 'authors',
'localField': 'authorId',
foreignField: '_id',
as: 'author'
""");
configureAuthorsCollection("_id: 3", "_id: 3, name: 'Uncle Bob'");
Document document = json("title: 'Clean Code', authorId: 3");

Expand All @@ -85,7 +117,12 @@ void testLookupObjectThatExists() {
void testLookupObjectThatExistsMultipleTimes() {
prepareAuthorsCollectionMock();

LookupStage lookupStage = buildLookupStage("from: 'authors', 'localField': 'job', foreignField: 'job', as: 'seeAuthors'");
LookupStage lookupStage = buildLookupStage("""
from: 'authors',
'localField': 'job',
foreignField: 'job',
as: 'seeAuthors'
""");
configureAuthorsCollection("job: 'Developer'",
"_id: 3, name: 'Uncle Bob', job: 'Developer'",
"_id: 5, name: 'Alice', job: 'Developer'");
Expand All @@ -94,16 +131,28 @@ void testLookupObjectThatExistsMultipleTimes() {
Stream<Document> result = lookupStage.apply(Stream.of(document));

assertThat(result).containsExactly(
json("_id: 1, title: 'Developing for dummies', job: 'Developer', " +
"seeAuthors: [{_id: 3, name: 'Uncle Bob', job: 'Developer'}, {_id: 5, name: 'Alice', job: 'Developer'}]")
json("""
_id: 1,
title: 'Developing for dummies',
job: 'Developer',
seeAuthors: [
{_id: 3, name: 'Uncle Bob', job: 'Developer'},
{_id: 5, name: 'Alice', job: 'Developer'}
]
""")
);
}

@Test
void testLookupObjectThatDoesNotExist() {
prepareAuthorsCollectionMock();

LookupStage lookupStage = buildLookupStage("from: 'authors', 'localField': 'job', foreignField: 'job', as: 'seeAuthors'");
LookupStage lookupStage = buildLookupStage("""
from: 'authors',
'localField': 'job',
foreignField: 'job',
as: 'seeAuthors'
""");
configureAuthorsCollection("job: 'Developer'");
Document document = json("_id: 1, title: 'Developing for dummies', job: 'Developer'");

Expand All @@ -118,7 +167,12 @@ void testLookupObjectThatDoesNotExist() {
void testLookupObjectBasedOnAnArrayKey() {
prepareAuthorsCollectionMock();

LookupStage lookupStage = buildLookupStage("from: 'authors', 'localField': 'authorsIds', foreignField: '_id', as: 'authors'");
LookupStage lookupStage = buildLookupStage("""
from: 'authors',
'localField': 'authorsIds',
foreignField: '_id',
as: 'authors'
""");
configureAuthorsCollection("_id: {$in: [3, 20, 22]}",
"_id: 3, name: 'Uncle Bob'", "_id: 20, name: 'Martin Fowler'");
configureAuthorsCollection("_id: {$in: [3]}", "_id: 3, name: 'Uncle Bob'");
Expand All @@ -128,16 +182,28 @@ void testLookupObjectBasedOnAnArrayKey() {
Stream<Document> result = lookupStage.apply(Stream.of(document1, document2));

assertThat(result).containsExactly(
json("title: 'Agile Manifesto', authorsIds: [3, 20, 22], authors: [{_id: 3, name: 'Uncle Bob'}, {_id: 20, name: 'Martin Fowler'}]"),
json("""
title: 'Agile Manifesto',
authorsIds: [3, 20, 22],
authors: [
{_id: 3, name: 'Uncle Bob'},
{_id: 20, name: 'Martin Fowler'}
]
"""),
json("title: 'Clean Code', authorsIds: [3], authors: [{_id: 3, name: 'Uncle Bob'}]")
);
}

@Test
public void testLookupWithNestedField() {
void testLookupWithNestedField() {
prepareAuthorsCollectionMock();

LookupStage lookupStage = buildLookupStage("from: 'authors', 'localField': 'author.id', foreignField: '_id', as: 'author'");
LookupStage lookupStage = buildLookupStage("""
from: 'authors',
'localField': 'author.id',
foreignField: '_id',
as: 'author'
""");
configureAuthorsCollection("_id: 3", "_id: 3, name: 'Uncle Bob'");
Document document = json("title: 'Clean Code', author: { id: 3 }");

Expand All @@ -149,10 +215,15 @@ public void testLookupWithNestedField() {
}

@Test
public void testLookupWithNestedFieldArray() {
void testLookupWithNestedFieldArray() {
prepareAuthorsCollectionMock();

LookupStage lookupStage = buildLookupStage("from: 'authors', 'localField': 'author.id', foreignField: '_id', as: 'author'");
LookupStage lookupStage = buildLookupStage("""
from: 'authors',
'localField': 'author.id',
foreignField: '_id',
as: 'author'
""");
configureAuthorsCollection("_id: {$in: [3, 4]}",
"_id: 3, name: 'Uncle Bob'", "_id: 4, name: 'Aunt Bobby'");
Document document = json("title: 'Clean Code', author: [{ id: 3 }, {id: 4}]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
import de.bwaldvogel.mongo.exception.MongoServerException;
import de.bwaldvogel.mongo.oplog.NoopOplog;

public class LookupWithPipelineStageTest extends AbstractLookupStageTest {
class LookupWithPipelineStageTest extends AbstractLookupStageTest {

@Test
void testUnexpectedConfigurationField() {
assertThatThrownBy(() -> buildLookupStage("from: 'authors', pipeline: [], as: 'author', unknown: 'hello'"))
assertThatThrownBy(() -> buildLookupStage("""
from: 'authors',
pipeline: [],
as: 'author',
unknown: 'hello'
"""))
.isInstanceOf(MongoServerException.class)
.hasMessage("[Error 9] unknown argument to $lookup: unknown");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import de.bwaldvogel.mongo.bson.Document;
import de.bwaldvogel.mongo.exception.MongoServerError;

public class ReplaceRootStageTest {
class ReplaceRootStageTest {

@Test
void testReplaceRoot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import de.bwaldvogel.mongo.bson.Document;
import de.bwaldvogel.mongo.exception.MongoServerError;

public class UnwindStageTest {
class UnwindStageTest {

@Test
void testUnwind() {
Expand Down
Loading
Loading