|
| 1 | +/* |
| 2 | + * Copyright 2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.mongodb.core.convert; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collections; |
| 23 | + |
| 24 | +import org.assertj.core.api.Assertions; |
| 25 | +import org.bson.Document; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 31 | +import org.springframework.data.mapping.context.MappingContext; |
| 32 | +import org.springframework.data.mapping.model.SpELContext; |
| 33 | +import org.springframework.data.mongodb.core.convert.ReferenceLoader.DocumentReferenceQuery; |
| 34 | +import org.springframework.data.mongodb.core.convert.ReferenceLookupDelegate.LookupFunction; |
| 35 | +import org.springframework.data.mongodb.core.convert.ReferenceResolver.MongoEntityReader; |
| 36 | +import org.springframework.data.mongodb.core.convert.ReferenceResolver.ReferenceCollection; |
| 37 | +import org.springframework.data.mongodb.core.mapping.DocumentReference; |
| 38 | +import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; |
| 39 | +import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; |
| 40 | +import org.springframework.expression.EvaluationContext; |
| 41 | +import org.springframework.expression.spel.standard.SpelExpressionParser; |
| 42 | + |
| 43 | +/** |
| 44 | + * @author Christoph Strobl |
| 45 | + */ |
| 46 | +@ExtendWith(MockitoExtension.class) |
| 47 | +class ReferenceLookupDelegateUnitTests { |
| 48 | + |
| 49 | + @Mock MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext; |
| 50 | + @Mock SpELContext spELContext; |
| 51 | + @Mock EvaluationContext evaluationContext; |
| 52 | + @Mock MongoEntityReader entityReader; |
| 53 | + |
| 54 | + private ReferenceLookupDelegate lookupDelegate; |
| 55 | + |
| 56 | + @BeforeEach |
| 57 | + void beforeEach() { |
| 58 | + |
| 59 | + lookupDelegate = new ReferenceLookupDelegate(mappingContext, spELContext); |
| 60 | + when(spELContext.getParser()).thenReturn(new SpelExpressionParser()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test // GH-3842 |
| 64 | + void shouldComputePlainStringTargetCollection() { |
| 65 | + |
| 66 | + DocumentReference documentReference = mock(DocumentReference.class); |
| 67 | + MongoPersistentEntity entity = mock(MongoPersistentEntity.class); |
| 68 | + MongoPersistentProperty property = mock(MongoPersistentProperty.class); |
| 69 | + |
| 70 | + doReturn(entity).when(mappingContext).getRequiredPersistentEntity((Class) any()); |
| 71 | + |
| 72 | + when(property.isDocumentReference()).thenReturn(true); |
| 73 | + when(property.getDocumentReference()).thenReturn(documentReference); |
| 74 | + when(documentReference.collection()).thenReturn("collection1"); |
| 75 | + |
| 76 | + lookupDelegate.readReference(property, Arrays.asList("one"), new LookupFunction() { |
| 77 | + @Override |
| 78 | + public Iterable<Document> apply(DocumentReferenceQuery referenceQuery, ReferenceCollection referenceCollection) { |
| 79 | + |
| 80 | + assertThat(referenceCollection.getCollection()).isEqualTo("collection1"); |
| 81 | + return Collections.emptyList(); |
| 82 | + } |
| 83 | + }, entityReader); |
| 84 | + } |
| 85 | +} |
0 commit comments