|
| 1 | +/* |
| 2 | + * Copyright 2023 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.aggregation; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.assertj.core.api.InstanceOfAssertFactories; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.mockito.ArgumentCaptor; |
| 26 | +import org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation.InheritsFieldsAggregationOperation; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Christoph Strobl |
| 30 | + */ |
| 31 | +public class AggregationOperationRendererUnitTests { |
| 32 | + |
| 33 | + @Test // GH-4443 |
| 34 | + void nonFieldsExposingAggregationOperationContinuesWithSameContextForNextStage() { |
| 35 | + |
| 36 | + AggregationOperationContext rootContext = mock(AggregationOperationContext.class); |
| 37 | + AggregationOperation stage1 = mock(AggregationOperation.class); |
| 38 | + AggregationOperation stage2 = mock(AggregationOperation.class); |
| 39 | + |
| 40 | + AggregationOperationRenderer.toDocument(List.of(stage1, stage2), rootContext); |
| 41 | + |
| 42 | + verify(stage1).toPipelineStages(eq(rootContext)); |
| 43 | + verify(stage2).toPipelineStages(eq(rootContext)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test // GH-4443 |
| 47 | + void fieldsExposingAggregationOperationNotExposingFieldsForcesUseOfDefaultContextForNextStage() { |
| 48 | + |
| 49 | + AggregationOperationContext rootContext = mock(AggregationOperationContext.class); |
| 50 | + FieldsExposingAggregationOperation stage1 = mock(FieldsExposingAggregationOperation.class); |
| 51 | + ExposedFields stage1fields = mock(ExposedFields.class); |
| 52 | + AggregationOperation stage2 = mock(AggregationOperation.class); |
| 53 | + |
| 54 | + when(stage1.getFields()).thenReturn(stage1fields); |
| 55 | + when(stage1fields.exposesNoFields()).thenReturn(true); |
| 56 | + |
| 57 | + AggregationOperationRenderer.toDocument(List.of(stage1, stage2), rootContext); |
| 58 | + |
| 59 | + verify(stage1).toPipelineStages(eq(rootContext)); |
| 60 | + verify(stage2).toPipelineStages(eq(AggregationOperationRenderer.DEFAULT_CONTEXT)); |
| 61 | + } |
| 62 | + |
| 63 | + @Test // GH-4443 |
| 64 | + void fieldsExposingAggregationOperationForcesNewContextForNextStage() { |
| 65 | + |
| 66 | + AggregationOperationContext rootContext = mock(AggregationOperationContext.class); |
| 67 | + FieldsExposingAggregationOperation stage1 = mock(FieldsExposingAggregationOperation.class); |
| 68 | + ExposedFields stage1fields = mock(ExposedFields.class); |
| 69 | + AggregationOperation stage2 = mock(AggregationOperation.class); |
| 70 | + |
| 71 | + when(stage1.getFields()).thenReturn(stage1fields); |
| 72 | + when(stage1fields.exposesNoFields()).thenReturn(false); |
| 73 | + |
| 74 | + ArgumentCaptor<AggregationOperationContext> captor = ArgumentCaptor.forClass(AggregationOperationContext.class); |
| 75 | + |
| 76 | + AggregationOperationRenderer.toDocument(List.of(stage1, stage2), rootContext); |
| 77 | + |
| 78 | + verify(stage1).toPipelineStages(eq(rootContext)); |
| 79 | + verify(stage2).toPipelineStages(captor.capture()); |
| 80 | + |
| 81 | + assertThat(captor.getValue()).isInstanceOf(ExposedFieldsAggregationOperationContext.class) |
| 82 | + .isNotInstanceOf(InheritingExposedFieldsAggregationOperationContext.class); |
| 83 | + } |
| 84 | + |
| 85 | + @Test // GH-4443 |
| 86 | + void inheritingFieldsExposingAggregationOperationForcesNewContextForNextStageKeepingReferenceToPreviousContext() { |
| 87 | + |
| 88 | + AggregationOperationContext rootContext = mock(AggregationOperationContext.class); |
| 89 | + InheritsFieldsAggregationOperation stage1 = mock(InheritsFieldsAggregationOperation.class); |
| 90 | + InheritsFieldsAggregationOperation stage2 = mock(InheritsFieldsAggregationOperation.class); |
| 91 | + InheritsFieldsAggregationOperation stage3 = mock(InheritsFieldsAggregationOperation.class); |
| 92 | + |
| 93 | + ExposedFields exposedFields = mock(ExposedFields.class); |
| 94 | + when(exposedFields.exposesNoFields()).thenReturn(false); |
| 95 | + when(stage1.getFields()).thenReturn(exposedFields); |
| 96 | + when(stage2.getFields()).thenReturn(exposedFields); |
| 97 | + when(stage3.getFields()).thenReturn(exposedFields); |
| 98 | + |
| 99 | + ArgumentCaptor<AggregationOperationContext> captor = ArgumentCaptor.forClass(AggregationOperationContext.class); |
| 100 | + |
| 101 | + AggregationOperationRenderer.toDocument(List.of(stage1, stage2, stage3), rootContext); |
| 102 | + |
| 103 | + verify(stage1).toPipelineStages(captor.capture()); |
| 104 | + verify(stage2).toPipelineStages(captor.capture()); |
| 105 | + verify(stage3).toPipelineStages(captor.capture()); |
| 106 | + |
| 107 | + assertThat(captor.getAllValues().get(0)).isEqualTo(rootContext); |
| 108 | + |
| 109 | + assertThat(captor.getAllValues().get(1)) |
| 110 | + .asInstanceOf(InstanceOfAssertFactories.type(InheritingExposedFieldsAggregationOperationContext.class)) |
| 111 | + .extracting("previousContext").isSameAs(captor.getAllValues().get(0)); |
| 112 | + |
| 113 | + assertThat(captor.getAllValues().get(2)) |
| 114 | + .asInstanceOf(InstanceOfAssertFactories.type(InheritingExposedFieldsAggregationOperationContext.class)) |
| 115 | + .extracting("previousContext").isSameAs(captor.getAllValues().get(1)); |
| 116 | + } |
| 117 | + |
| 118 | +} |
0 commit comments