|
8 | 8 | package org.seedstack.mongodb.morphia; |
9 | 9 |
|
10 | 10 | import com.google.inject.Inject; |
| 11 | +import com.google.inject.Injector; |
| 12 | +import com.google.inject.ProvisionException; |
| 13 | +import com.google.inject.TypeLiteral; |
| 14 | +import com.google.inject.util.Types; |
11 | 15 | import org.assertj.core.api.Assertions; |
12 | 16 | import org.junit.Test; |
13 | 17 | import org.mongodb.morphia.Datastore; |
14 | 18 | import org.mongodb.morphia.Key; |
| 19 | +import org.seedstack.business.domain.Repository; |
| 20 | +import org.seedstack.mongodb.morphia.fixtures.dummyobject.Dummy1; |
| 21 | +import org.seedstack.mongodb.morphia.fixtures.dummyobject.Dummy2; |
| 22 | +import org.seedstack.mongodb.morphia.fixtures.dummyobject.Dummy3; |
| 23 | +import org.seedstack.mongodb.morphia.fixtures.dummyobject.Dummy4; |
| 24 | +import org.seedstack.mongodb.morphia.fixtures.dummyobject.Dummy5; |
| 25 | +import org.seedstack.mongodb.morphia.fixtures.dummyobject.Dummy6; |
15 | 26 | import org.seedstack.mongodb.morphia.fixtures.user.Address; |
16 | 27 | import org.seedstack.mongodb.morphia.fixtures.user.User; |
| 28 | +import org.seedstack.mongodb.morphia.internal.MorphiaErrorCode; |
| 29 | +import org.seedstack.seed.SeedException; |
17 | 30 | import org.seedstack.seed.it.AbstractSeedIT; |
18 | 31 |
|
19 | 32 | import javax.validation.ConstraintViolationException; |
20 | 33 |
|
| 34 | +import static org.assertj.core.api.Assertions.assertThat; |
21 | 35 | import static org.assertj.core.api.Fail.fail; |
22 | 36 |
|
23 | 37 | public class MorphiaIT extends AbstractSeedIT { |
24 | 38 | @Inject |
25 | 39 | @MorphiaDatastore(clientName = "client1", dbName = "db") |
26 | 40 | private Datastore datastore; |
| 41 | + @Inject |
| 42 | + private Injector injector; |
27 | 43 |
|
28 | 44 | @Test |
29 | | - public void datastore_test() { |
| 45 | + public void datastoreAccess() { |
30 | 46 | User user = new User(1L, "Gerard", "menvuça", new Address("France", "78300", "Poissy", "avenue de l'europe", 1)); |
31 | 47 | Key<User> keyUser = datastore.save(user); |
32 | 48 | Assertions.assertThat(keyUser).isNotNull(); |
33 | 49 | } |
34 | 50 |
|
35 | 51 | @Test(expected = ConstraintViolationException.class) |
36 | | - public void validation_is_working() { |
| 52 | + public void validationIsWorking() { |
37 | 53 | User user = new User(1L, null, "menvuça", new Address("France", "78300", "Poissy", "avenue de l'europe", 1)); |
38 | 54 | datastore.save(user); |
39 | 55 | fail("should not have saved"); |
40 | 56 | } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void repositoryInjectionTestNoClientForAggregate() { |
| 60 | + try { |
| 61 | + injector.getInstance(getMorphiaRepositoryOf(Dummy1.class)); |
| 62 | + } catch (ProvisionException e) { |
| 63 | + assertThat(e.getCause().getMessage()) |
| 64 | + .isEqualTo(SeedException.createNew(MorphiaErrorCode.CLIENT_NAME_NOT_CONFIGURED).getMessage()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private com.google.inject.Key<?> getMorphiaRepositoryOf(Class entity) { |
| 69 | + return com.google.inject.Key.get(TypeLiteral.get(Types.newParameterizedType(Repository.class, entity, Long.class)), Morphia.class); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void repositoryInjectionTestNoDbNameForAggregate() { |
| 74 | + try { |
| 75 | + injector.getInstance(getMorphiaRepositoryOf(Dummy2.class)); |
| 76 | + } catch (ProvisionException e) { |
| 77 | + assertThat(e.getCause().getMessage()) |
| 78 | + .isEqualTo(SeedException.createNew(MorphiaErrorCode.UNKNOWN_DATABASE).getMessage()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void repositoryInjectionTestNoMongoDbClient() { |
| 84 | + try { |
| 85 | + injector.getInstance(getMorphiaRepositoryOf(Dummy3.class)); |
| 86 | + } catch (ProvisionException e) { |
| 87 | + assertThat(e.getCause().getMessage()) |
| 88 | + .isEqualTo(SeedException.createNew(MorphiaErrorCode.UNKNOWN_CLIENT).getMessage()); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void repositoryInjectionTestNoMongoDbDatabase() { |
| 94 | + try { |
| 95 | + injector.getInstance(getMorphiaRepositoryOf(Dummy4.class)); |
| 96 | + } catch (ProvisionException e) { |
| 97 | + assertThat(e.getCause().getMessage()) |
| 98 | + .isEqualTo(SeedException.createNew(MorphiaErrorCode.UNKNOWN_DATABASE).getMessage()); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void repositoryInjectionTestNoMongodbForAggregate() { |
| 104 | + try { |
| 105 | + injector.getInstance(getMorphiaRepositoryOf(Dummy5.class)); |
| 106 | + } catch (ProvisionException e) { |
| 107 | + assertThat(e.getCause().getMessage()) |
| 108 | + .isEqualTo(SeedException.createNew(MorphiaErrorCode.PERSISTED_CLASS_NOT_CONFIGURED).getMessage()); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void repositoryInjectionAsyncClient() { |
| 114 | + try { |
| 115 | + injector.getInstance(getMorphiaRepositoryOf(Dummy6.class)); |
| 116 | + } catch (ProvisionException e) { |
| 117 | + assertThat(e.getCause().getMessage()) |
| 118 | + .isEqualTo(SeedException.createNew(MorphiaErrorCode.ASYNC_CLIENT_NOT_SUPPORTED).getMessage()); |
| 119 | + } |
| 120 | + } |
41 | 121 | } |
0 commit comments