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