Skip to content

Commit 5a4edf8

Browse files
DATAMONGO-1590 - Add test checking Persistable.isNew() is considered correctly.
Related to: DATCMNS-976
1 parent f1d0f76 commit 5a4edf8

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MappingMongoEntityInformationUnitTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.junit.runner.RunWith;
2525
import org.mockito.Mock;
2626
import org.mockito.runners.MockitoJUnitRunner;
27+
import org.springframework.data.domain.Persistable;
2728
import org.springframework.data.mongodb.core.mapping.MongoPersistentEntity;
2829
import org.springframework.data.mongodb.repository.Person;
2930
import org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation;
@@ -32,17 +33,20 @@
3233
* Unit tests for {@link MappingMongoEntityInformation}.
3334
*
3435
* @author Oliver Gierke
36+
* @author Christoph Strobl
3537
*/
3638
@RunWith(MockitoJUnitRunner.class)
3739
public class MappingMongoEntityInformationUnitTests {
3840

3941
@Mock MongoPersistentEntity<Person> info;
42+
@Mock MongoPersistentEntity<TypeImplementingPersistable> persistableImplementingEntityTypeInfo;
4043

4144
@Before
4245
public void setUp() {
4346

4447
when(info.getType()).thenReturn(Person.class);
4548
when(info.getCollection()).thenReturn("Person");
49+
when(persistableImplementingEntityTypeInfo.getType()).thenReturn(TypeImplementingPersistable.class);
4650
}
4751

4852
@Test // DATAMONGO-248
@@ -58,4 +62,34 @@ public void usesCustomCollectionIfGiven() {
5862
MongoEntityInformation<Person, Long> information = new MappingMongoEntityInformation<Person, Long>(info, "foobar");
5963
assertThat(information.getCollectionName(), is("foobar"));
6064
}
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+
}
6195
}

0 commit comments

Comments
 (0)