|
19 | 19 | import static org.junit.Assert.*; |
20 | 20 |
|
21 | 21 | import java.util.Collections; |
| 22 | +import java.util.Date; |
22 | 23 | import java.util.HashMap; |
23 | 24 | import java.util.Map; |
24 | 25 |
|
|
27 | 28 | import org.junit.runner.RunWith; |
28 | 29 | import org.mockito.Mock; |
29 | 30 | import org.mockito.runners.MockitoJUnitRunner; |
| 31 | +import org.springframework.data.annotation.CreatedDate; |
30 | 32 | import org.springframework.data.annotation.Id; |
| 33 | +import org.springframework.data.annotation.ReadOnlyProperty; |
31 | 34 | import org.springframework.data.annotation.Version; |
32 | 35 | import org.springframework.data.mapping.context.PersistentEntities; |
33 | 36 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext; |
@@ -61,6 +64,7 @@ public void setUp() { |
61 | 64 | mappingContext.getPersistentEntity(Person.class); |
62 | 65 | mappingContext.getPersistentEntity(TypeWithGenericMap.class); |
63 | 66 | mappingContext.getPersistentEntity(VersionedType.class); |
| 67 | + mappingContext.getPersistentEntity(SampleWithCreatedDate.class); |
64 | 68 | mappingContext.afterPropertiesSet(); |
65 | 69 |
|
66 | 70 | PersistentEntities entities = new PersistentEntities(Collections.singleton(mappingContext)); |
@@ -169,6 +173,23 @@ public void doesNotWipeIdAndVersionPropertyForPut() throws Exception { |
169 | 173 | assertThat(result.version, is(1L)); |
170 | 174 | } |
171 | 175 |
|
| 176 | + /** |
| 177 | + * @see DATAREST-873 |
| 178 | + */ |
| 179 | + @Test |
| 180 | + public void doesNotApplyInputToReadOnlyFields() throws Exception { |
| 181 | + |
| 182 | + ObjectMapper mapper = new ObjectMapper(); |
| 183 | + ObjectNode node = (ObjectNode) mapper.readTree("{}"); |
| 184 | + |
| 185 | + Date reference = new Date(); |
| 186 | + |
| 187 | + SampleWithCreatedDate sample = new SampleWithCreatedDate(); |
| 188 | + sample.createdDate = reference; |
| 189 | + |
| 190 | + assertThat(reader.readPut(node, sample, mapper).createdDate, is(reference)); |
| 191 | + } |
| 192 | + |
172 | 193 | @JsonAutoDetect(fieldVisibility = Visibility.ANY) |
173 | 194 | static class SampleUser { |
174 | 195 |
|
@@ -210,4 +231,12 @@ static class VersionedType { |
210 | 231 |
|
211 | 232 | String firstname, lastname; |
212 | 233 | } |
| 234 | + |
| 235 | + @JsonAutoDetect(fieldVisibility = Visibility.ANY) |
| 236 | + static class SampleWithCreatedDate { |
| 237 | + |
| 238 | + @CreatedDate // |
| 239 | + @ReadOnlyProperty // |
| 240 | + Date createdDate; |
| 241 | + } |
213 | 242 | } |
0 commit comments