|
18 | 18 | import static org.assertj.core.api.Assertions.*;
|
19 | 19 | import static org.mockito.Mockito.*;
|
20 | 20 |
|
| 21 | +import org.bson.Document; |
| 22 | +import org.bson.types.ObjectId; |
21 | 23 | import org.junit.jupiter.api.BeforeEach;
|
22 | 24 | import org.junit.jupiter.api.Test;
|
23 | 25 | import org.junit.jupiter.api.extension.ExtendWith;
|
|
32 | 34 | import org.springframework.data.mongodb.core.aggregation.TypeBasedAggregationOperationContext;
|
33 | 35 | import org.springframework.data.mongodb.core.convert.QueryMapper;
|
34 | 36 | import org.springframework.data.mongodb.core.convert.UpdateMapper;
|
| 37 | +import org.springframework.data.mongodb.core.mapping.MongoId; |
35 | 38 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
| 39 | +import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity; |
| 40 | +import org.springframework.data.mongodb.core.mapping.MongoPersistentProperty; |
36 | 41 |
|
37 | 42 | /**
|
38 | 43 | * Unit tests for {@link QueryOperations}.
|
@@ -109,7 +114,99 @@ void createAggregationContextUsesStrictlyTypedContextForTypedAggregationsWhenReq
|
109 | 114 | assertThat(ctx.getAggregationOperationContext()).isInstanceOf(TypeBasedAggregationOperationContext.class);
|
110 | 115 | }
|
111 | 116 |
|
| 117 | + @Test // GH-4026 |
| 118 | + void insertContextDoesNotAddIdIfNoPersistentEntityCanBeFound() { |
| 119 | + |
| 120 | + assertThat(queryOperations.createInsertContext(new Document("value", "one")).prepareId(Person.class).getDocument())// |
| 121 | + .satisfies(result -> { |
| 122 | + assertThat(result).isEqualTo(new Document("value", "one")); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + @Test // GH-4026 |
| 127 | + void insertContextDoesNotAddIdIfNoIdPropertyCanBeFound() { |
| 128 | + |
| 129 | + MongoPersistentEntity<Person> entity = mock(MongoPersistentEntity.class); |
| 130 | + when(entity.getIdProperty()).thenReturn(null); |
| 131 | + when(mappingContext.getPersistentEntity(eq(Person.class))).thenReturn((MongoPersistentEntity) entity); |
| 132 | + |
| 133 | + assertThat(queryOperations.createInsertContext(new Document("value", "one")).prepareId(Person.class).getDocument())// |
| 134 | + .satisfies(result -> { |
| 135 | + assertThat(result).isEqualTo(new Document("value", "one")); |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + @Test // GH-4026 |
| 140 | + void insertContextDoesNotAddConvertedIdForNonExplicitFieldTypes() { |
| 141 | + |
| 142 | + MongoPersistentEntity<Person> entity = mock(MongoPersistentEntity.class); |
| 143 | + MongoPersistentProperty property = mock(MongoPersistentProperty.class); |
| 144 | + when(entity.getIdProperty()).thenReturn(property); |
| 145 | + when(property.hasExplicitWriteTarget()).thenReturn(false); |
| 146 | + doReturn(entity).when(mappingContext).getPersistentEntity(eq(Person.class)); |
| 147 | + |
| 148 | + assertThat(queryOperations.createInsertContext(new Document("value", "one")).prepareId(Person.class).getDocument())// |
| 149 | + .satisfies(result -> { |
| 150 | + assertThat(result).isEqualTo(new Document("value", "one")); |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + @Test // GH-4026 |
| 155 | + void insertContextAddsConvertedIdForExplicitFieldTypes() { |
| 156 | + |
| 157 | + MongoPersistentEntity<Person> entity = mock(MongoPersistentEntity.class); |
| 158 | + MongoPersistentProperty property = mock(MongoPersistentProperty.class); |
| 159 | + when(entity.getIdProperty()).thenReturn(property); |
| 160 | + when(property.hasExplicitWriteTarget()).thenReturn(true); |
| 161 | + doReturn(String.class).when(property).getFieldType(); |
| 162 | + doReturn(entity).when(mappingContext).getPersistentEntity(eq(Person.class)); |
| 163 | + |
| 164 | + when(queryMapper.convertId(any(), eq(String.class))).thenReturn("☮"); |
| 165 | + |
| 166 | + assertThat(queryOperations.createInsertContext(new Document("value", "one")).prepareId(Person.class).getDocument())// |
| 167 | + .satisfies(result -> { |
| 168 | + assertThat(result).isEqualTo(new Document("value", "one").append("_id", "☮")); |
| 169 | + }); |
| 170 | + } |
| 171 | + |
| 172 | + @Test // GH-4026 |
| 173 | + void insertContextAddsConvertedIdForMongoIdTypes() { |
| 174 | + |
| 175 | + MongoPersistentEntity<Person> entity = mock(MongoPersistentEntity.class); |
| 176 | + MongoPersistentProperty property = mock(MongoPersistentProperty.class); |
| 177 | + when(entity.getIdProperty()).thenReturn(property); |
| 178 | + when(property.hasExplicitWriteTarget()).thenReturn(false); |
| 179 | + when(property.isAnnotationPresent(eq(MongoId.class))).thenReturn(true); |
| 180 | + doReturn(String.class).when(property).getFieldType(); |
| 181 | + doReturn(entity).when(mappingContext).getPersistentEntity(eq(Person.class)); |
| 182 | + |
| 183 | + when(queryMapper.convertId(any(), eq(String.class))).thenReturn("☮"); |
| 184 | + |
| 185 | + assertThat(queryOperations.createInsertContext(new Document("value", "one")).prepareId(Person.class).getDocument())// |
| 186 | + .satisfies(result -> { |
| 187 | + assertThat(result).isEqualTo(new Document("value", "one").append("_id", "☮")); |
| 188 | + }); |
| 189 | + } |
| 190 | + |
| 191 | + @Test // GH-4026 |
| 192 | + void insertContextDoesNotAddConvertedIdForMongoIdTypesTargetingObjectId() { |
| 193 | + |
| 194 | + MongoPersistentEntity<Person> entity = mock(MongoPersistentEntity.class); |
| 195 | + MongoPersistentProperty property = mock(MongoPersistentProperty.class); |
| 196 | + when(entity.getIdProperty()).thenReturn(property); |
| 197 | + when(property.hasExplicitWriteTarget()).thenReturn(false); |
| 198 | + when(property.isAnnotationPresent(eq(MongoId.class))).thenReturn(true); |
| 199 | + doReturn(ObjectId.class).when(property).getFieldType(); |
| 200 | + doReturn(entity).when(mappingContext).getPersistentEntity(eq(Person.class)); |
| 201 | + |
| 202 | + assertThat(queryOperations.createInsertContext(new Document("value", "one")).prepareId(Person.class).getDocument())// |
| 203 | + .satisfies(result -> { |
| 204 | + assertThat(result).isEqualTo(new Document("value", "one")); |
| 205 | + }); |
| 206 | + } |
| 207 | + |
112 | 208 | static class Person {
|
113 | 209 |
|
114 | 210 | }
|
| 211 | + |
115 | 212 | }
|
0 commit comments