|
| 1 | +package org.jooby.internal.mongodb; |
| 2 | + |
| 3 | +import static org.easymock.EasyMock.expect; |
| 4 | + |
| 5 | +import java.lang.reflect.Field; |
| 6 | + |
| 7 | +import org.jooby.mongodb.GeneratedValue; |
| 8 | +import org.jooby.mongodb.IdGen; |
| 9 | +import org.jooby.test.MockUnit; |
| 10 | +import org.junit.Test; |
| 11 | +import org.junit.runner.RunWith; |
| 12 | +import org.mongodb.morphia.Datastore; |
| 13 | +import org.mongodb.morphia.annotations.Id; |
| 14 | +import org.mongodb.morphia.mapping.MappedClass; |
| 15 | +import org.mongodb.morphia.mapping.Mapper; |
| 16 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 17 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 18 | + |
| 19 | +import com.mongodb.DBObject; |
| 20 | + |
| 21 | +@RunWith(PowerMockRunner.class) |
| 22 | +@PrepareForTest({AutoIncID.class, Field.class, StoredId.class }) |
| 23 | +public class Issue569 { |
| 24 | + |
| 25 | + public static class MonDoc { |
| 26 | + @Id |
| 27 | + public Long id; |
| 28 | + } |
| 29 | + @Test |
| 30 | + public void shouldSkipNullID() throws Exception { |
| 31 | + Object entity = new Object(); |
| 32 | + new MockUnit(Datastore.class, DBObject.class, Mapper.class, MappedClass.class, Field.class, |
| 33 | + GeneratedValue.class) |
| 34 | + .expect(unit -> { |
| 35 | + |
| 36 | + MappedClass mclass = unit.get(MappedClass.class); |
| 37 | + expect(mclass.getIdField()).andReturn(null); |
| 38 | + Mapper mapper = unit.get(Mapper.class); |
| 39 | + expect(mapper.getMappedClass(entity)).andReturn(mclass); |
| 40 | + }) |
| 41 | + .run(unit -> { |
| 42 | + new AutoIncID(unit.get(Datastore.class), IdGen.LOCAL) |
| 43 | + .prePersist(entity, unit.get(DBObject.class), unit.get(Mapper.class)); |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | +} |
0 commit comments