Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Version 1.1.0 (?)

* [new] Add Bean Validation support to Morphia (at pre-persist)
* [new] Add `exists()` and `count()` in `BaseMorphiaRepository`
* [chg] Update for SeedStack 16.4
* [brk] remove `do*()` methods in `BaseMorphiaRepository`

# Version 1.0.1 (2016-02-09)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,21 @@ public class MorphiaRepositoryIT extends AbstractSeedIT {
@Test
public void repository_injection_test_no_client_for_aggregate() {
try {

injector.getInstance(
Key.get(TypeLiteral.get(Types.newParameterizedType(Repository.class, Dummy1.class, Long.class)),
Morphia.class));
injector.getInstance(getMorphiaRepositoryOf(Dummy1.class));
} catch (ProvisionException e) {
assertThat(e.getCause().getMessage())
.isEqualTo(SeedException.createNew(MorphiaErrorCodes.UNKNOW_DATASTORE_CLIENT).getMessage());
}
}

private Key<?> getMorphiaRepositoryOf(Class entity) {
return Key.get(TypeLiteral.get(Types.newParameterizedType(Repository.class, entity, Long.class)), Morphia.class);
}

@Test
public void repository_injection_test_no_dbName_for_aggregate() {
try {
injector.getInstance(
Key.get(TypeLiteral.get(Types.newParameterizedType(Repository.class, Dummy2.class, Long.class)),
Morphia.class));
injector.getInstance(getMorphiaRepositoryOf(Dummy2.class));
} catch (ProvisionException e) {
assertThat(e.getCause().getMessage())
.isEqualTo(SeedException.createNew(MorphiaErrorCodes.UNKNOW_DATASTORE_DATABASE).getMessage());
Expand All @@ -74,9 +73,7 @@ public void repository_injection_test_no_dbName_for_aggregate() {
@Test
public void repository_injection_test_no_mongoDb_client() {
try {
injector.getInstance(
Key.get(TypeLiteral.get(Types.newParameterizedType(Repository.class, Dummy3.class, Long.class)),
Morphia.class));
injector.getInstance(getMorphiaRepositoryOf(Dummy3.class));
} catch (ProvisionException e) {
assertThat(e.getCause().getMessage())
.isEqualTo(SeedException.createNew(MorphiaErrorCodes.UNKNOW_DATASTORE_CLIENT).getMessage());
Expand All @@ -86,9 +83,7 @@ public void repository_injection_test_no_mongoDb_client() {
@Test
public void repository_injection_test_no_mongoDb_database() {
try {
injector.getInstance(
Key.get(TypeLiteral.get(Types.newParameterizedType(Repository.class, Dummy4.class, Long.class)),
Morphia.class));
injector.getInstance(getMorphiaRepositoryOf(Dummy4.class));
} catch (ProvisionException e) {
assertThat(e.getCause().getMessage())
.isEqualTo(SeedException.createNew(MorphiaErrorCodes.UNKNOW_DATABASE_NAME).getMessage());
Expand All @@ -98,9 +93,7 @@ public void repository_injection_test_no_mongoDb_database() {
@Test
public void repository_injection_test_no_mongodb_for_aggregate() {
try {
injector.getInstance(
Key.get(TypeLiteral.get(Types.newParameterizedType(Repository.class, Dummy5.class, Long.class)),
Morphia.class));
injector.getInstance(getMorphiaRepositoryOf(Dummy5.class));
} catch (ProvisionException e) {
assertThat(e.getCause().getMessage())
.isEqualTo(SeedException.createNew(MorphiaErrorCodes.UNKNOW_DATASTORE_CONFIGURATION).getMessage());
Expand All @@ -110,9 +103,7 @@ public void repository_injection_test_no_mongodb_for_aggregate() {
@Test
public void repository_injection_async_client() {
try {
injector.getInstance(
Key.get(TypeLiteral.get(Types.newParameterizedType(Repository.class, Dummy6.class, Long.class)),
Morphia.class));
injector.getInstance(getMorphiaRepositoryOf(Dummy6.class));
} catch (ProvisionException e) {
assertThat(e.getCause().getMessage())
.isEqualTo(SeedException.createNew(MorphiaErrorCodes.ERROR_ASYNC_CLIENT).getMessage());
Expand Down Expand Up @@ -178,6 +169,20 @@ public void mongodb_repository_clear() {
assertThat(userRepository.load(401L)).isNull();
}

@Test
public void mongodb_repository_exists() {
userRepository.persist(getUser(300L, "Robert", "SMITH"));
assertThat(userRepository.exists(300L)).isTrue();
assertThat(userRepository.exists(3010L)).isFalse();
}

@Test
public void mongodb_repository_count() {
userRepository.persist(getUser(300L, "Robert", "SMITH"));
userRepository.persist(getUser(301L, "Roberta", "SMITH"));
assertThat(userRepository.count()).isEqualTo(2);
}

public User getUser(long id, String firstname, String lastName) {
return new User(id, firstname, lastName, new Address("France", "75001", "Paris", "Champ Elysee avenue", 1));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,39 +36,56 @@ protected Datastore getDatastore() {
return datastore;
}

public BaseMorphiaRepository() {
}

public BaseMorphiaRepository(Class<A> aggregateRootClass, Class<K> kClass) {
super(aggregateRootClass, kClass);
}

@Inject
private void initDatastore(Application application, Injector injector) {
datastore = injector.getInstance(Key.get(Datastore.class, MorphiaUtils.getMongoDatastore(application, aggregateRootClass)));
datastore = injector.getInstance(Key.get(Datastore.class, MorphiaUtils.getMongoDatastore(application, getAggregateRootClass())));
}

@Override
protected A doLoad(K id) {
return datastore.get(aggregateRootClass, id);
public A load(K id) {
return datastore.get(getAggregateRootClass(), id);
}

@Override
protected void doClear() {
datastore.delete(datastore.createQuery(aggregateRootClass));
public void clear() {
datastore.getCollection(getAggregateRootClass()).drop();
}

@Override
protected void doDelete(K id) {
datastore.delete(aggregateRootClass, id);
public void delete(K id) {
datastore.delete(getAggregateRootClass(), id);
}

@Override
protected void doDelete(A aggregate) {
public void delete(A aggregate) {
datastore.delete(aggregate);
}

@Override
protected void doPersist(A aggregate) {
public void persist(A aggregate) {
datastore.save(aggregate);
}

@Override
protected A doSave(A aggregate) {
public A save(A aggregate) {
datastore.merge(aggregate);
return aggregate;
}

@Override
public boolean exists(K id) {
return load(id) != null;
}

@Override
public long count() {
return datastore.getCount(getAggregateRootClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public class DefaultMorphiaRepository<AGGREGATE extends AggregateRoot<KEY>, KEY>
@SuppressWarnings("unchecked")
@Inject
public DefaultMorphiaRepository(@Assisted Object[] genericClasses) {
Object[] clonedClasses = genericClasses.clone();
SeedCheckUtils.checkIfNotNull(clonedClasses);
SeedCheckUtils.checkIf(clonedClasses.length == 2);
this.aggregateRootClass = (Class) clonedClasses[0];
this.keyClass = (Class) clonedClasses[1];
super((Class) genericClasses.clone()[0], (Class) genericClasses.clone()[0]);
}
}
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@
</licenseMerges>
</configuration>
</plugin>
<plugin>
<groupId>com.github.siom79.japicmp</groupId>
<artifactId>japicmp-maven-plugin</artifactId>
<configuration>
<parameter>
<excludes>
<exclude>*.internal.*</exclude>
<exclude>org.seedstack.mongodb.morphia.BaseMorphiaRepository</exclude>
</excludes>
</parameter>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down