24
24
import org .junit .runner .RunWith ;
25
25
import org .mockito .Mock ;
26
26
import org .mockito .runners .MockitoJUnitRunner ;
27
+ import org .springframework .data .domain .Persistable ;
27
28
import org .springframework .data .mongodb .core .mapping .MongoPersistentEntity ;
28
29
import org .springframework .data .mongodb .repository .Person ;
29
30
import org .springframework .data .mongodb .repository .support .MappingMongoEntityInformation ;
32
33
* Unit tests for {@link MappingMongoEntityInformation}.
33
34
*
34
35
* @author Oliver Gierke
36
+ * @author Christoph Strobl
35
37
*/
36
38
@ RunWith (MockitoJUnitRunner .class )
37
39
public class MappingMongoEntityInformationUnitTests {
38
40
39
41
@ Mock MongoPersistentEntity <Person > info ;
42
+ @ Mock MongoPersistentEntity <TypeImplementingPersistable > persistableImplementingEntityTypeInfo ;
40
43
41
44
@ Before
42
45
public void setUp () {
43
46
44
47
when (info .getType ()).thenReturn (Person .class );
45
48
when (info .getCollection ()).thenReturn ("Person" );
49
+ when (persistableImplementingEntityTypeInfo .getType ()).thenReturn (TypeImplementingPersistable .class );
46
50
}
47
51
48
52
@ Test // DATAMONGO-248
@@ -58,4 +62,34 @@ public void usesCustomCollectionIfGiven() {
58
62
MongoEntityInformation <Person , Long > information = new MappingMongoEntityInformation <Person , Long >(info , "foobar" );
59
63
assertThat (information .getCollectionName (), is ("foobar" ));
60
64
}
65
+
66
+ @ Test // DATAMONGO-1590
67
+ public void considersPersistableIsNew () {
68
+
69
+ MongoEntityInformation <TypeImplementingPersistable , Long > information = new MappingMongoEntityInformation <TypeImplementingPersistable , Long >(
70
+ persistableImplementingEntityTypeInfo );
71
+ assertThat (information .isNew (new TypeImplementingPersistable (100L , true )), is (true ));
72
+ assertThat (information .isNew (new TypeImplementingPersistable (100L , false )), is (false ));
73
+ }
74
+
75
+ static class TypeImplementingPersistable implements Persistable <Long > {
76
+
77
+ final Long id ;
78
+ final boolean isNew ;
79
+
80
+ public TypeImplementingPersistable (Long id , boolean isNew ) {
81
+ this .id = id ;
82
+ this .isNew = isNew ;
83
+ }
84
+
85
+ @ Override
86
+ public Long getId () {
87
+ return id ;
88
+ }
89
+
90
+ @ Override
91
+ public boolean isNew () {
92
+ return isNew ;
93
+ }
94
+ }
61
95
}
0 commit comments