Skip to content

Commit 0b47f5e

Browse files
author
Alex Tymchenko
committed
Merge branch 'master' into v0.9.0-release
# Conflicts: # build.gradle
2 parents 0f5a006 + 93874f9 commit 0b47f5e

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed

server/src/main/java/org/spine3/server/entity/RecordBasedRepository.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.annotations.VisibleForTesting;
2424
import com.google.common.base.Function;
2525
import com.google.common.base.Optional;
26+
import com.google.common.base.Predicate;
2627
import com.google.common.collect.Collections2;
2728
import com.google.common.collect.FluentIterable;
2829
import com.google.common.collect.ImmutableCollection;
@@ -134,6 +135,15 @@ public Optional<E> find(I id) {
134135
return Optional.of(entity);
135136
}
136137

138+
@Override
139+
public Iterator<E> iterator(Predicate<E> filter) {
140+
final Iterable<E> allEntities = loadAll();
141+
final Iterator<E> result = FluentIterable.from(allEntities)
142+
.filter(filter)
143+
.iterator();
144+
return result;
145+
}
146+
137147
/**
138148
* Loads an entity by the passed ID or creates a new one, if the entity was not found.
139149
*/

server/src/test/java/org/spine3/server/aggregate/AggregateRepositoryShould.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
package org.spine3.server.aggregate;
2222

2323
import com.google.common.base.Optional;
24+
import com.google.common.base.Predicates;
25+
import com.google.common.collect.Lists;
2426
import com.google.protobuf.Message;
2527
import org.junit.After;
2628
import org.junit.Before;
@@ -31,6 +33,7 @@
3133
import org.spine3.server.BoundedContext;
3234
import org.spine3.server.command.Assign;
3335
import org.spine3.server.storage.StorageFactorySwitch;
36+
import org.spine3.server.tenant.TenantAwareOperation;
3437
import org.spine3.test.Given;
3538
import org.spine3.test.TestActorRequestFactory;
3639
import org.spine3.test.aggregate.Project;
@@ -44,6 +47,7 @@
4447
import org.spine3.testdata.Sample;
4548
import org.spine3.type.CommandClass;
4649

50+
import java.util.Iterator;
4751
import java.util.Set;
4852

4953
import static org.junit.Assert.assertEquals;
@@ -58,6 +62,7 @@
5862
import static org.mockito.Mockito.spy;
5963
import static org.mockito.Mockito.times;
6064
import static org.mockito.Mockito.verify;
65+
import static org.spine3.test.Tests.newTenantUuid;
6166
import static org.spine3.validate.Validate.isDefault;
6267
import static org.spine3.validate.Validate.isNotDefault;
6368

@@ -235,6 +240,26 @@ public void mark_aggregate_deleted() {
235240
.isPresent());
236241
}
237242

243+
@Test(expected = IllegalStateException.class)
244+
public void throw_ISE_if_unable_to_load_entity_by_id_from_storage_index() {
245+
createAndStoreAggregate();
246+
247+
// Store a troublesome entity, which cannot be loaded.
248+
final TenantAwareOperation op = new TenantAwareOperation(newTenantUuid()) {
249+
@Override
250+
public void run() {
251+
createAndStore(ProjectAggregateRepository.troublesome.getId());
252+
}
253+
};
254+
op.execute();
255+
256+
final Iterator<ProjectAggregate> iterator = repository.iterator(
257+
Predicates.<ProjectAggregate>alwaysTrue());
258+
259+
// This should iterate through all.
260+
Lists.newArrayList(iterator);
261+
}
262+
238263
/*
239264
* Utility methods.
240265
****************************/
@@ -279,6 +304,15 @@ private ProjectAggregate createAndStoreAggregate() {
279304
return aggregate;
280305
}
281306

307+
private void createAndStore(String id) {
308+
final ProjectId projectId = ProjectId.newBuilder()
309+
.setId(id)
310+
.build();
311+
final ProjectAggregate aggregate = givenAggregateWithUncommittedEvents(projectId);
312+
313+
repository.store(aggregate);
314+
}
315+
282316
private AggregateStorage<ProjectId> givenAggregateStorageMock() {
283317
@SuppressWarnings("unchecked")
284318
final AggregateStorage<ProjectId> storage = mock(AggregateStorage.class);
@@ -358,10 +392,23 @@ protected void setDeleted(boolean deleted) {
358392

359393
private static class ProjectAggregateRepository
360394
extends AggregateRepository<ProjectId, ProjectAggregate> {
395+
396+
private static final ProjectId troublesome = ProjectId.newBuilder()
397+
.setId("CANNOT_BE_LOADED")
398+
.build();
399+
361400
protected ProjectAggregateRepository(BoundedContext boundedContext) {
362401
super(boundedContext);
363402
initStorage(StorageFactorySwitch.getInstance(boundedContext.isMultitenant())
364403
.get());
365404
}
405+
406+
@Override
407+
public Optional<ProjectAggregate> find(ProjectId id) {
408+
if (id.equals(troublesome)) {
409+
return Optional.absent();
410+
}
411+
return super.find(id);
412+
}
366413
}
367414
}

server/src/test/java/org/spine3/server/entity/RepositoryShould.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -255,23 +255,4 @@ private void createAndStore(String entityId) {
255255
ProjectEntity entity = repository.create(createId(entityId));
256256
repository.store(entity);
257257
}
258-
259-
@Test(expected = IllegalStateException.class)
260-
public void throw_ISE_if_unable_to_load_entity_by_id_from_storage_index() {
261-
createAndStoreEntities();
262-
263-
// Store a troublesome entity, which cannot be loaded.
264-
final TenantAwareOperation op = new TenantAwareOperation(tenantId) {
265-
@Override
266-
public void run() {
267-
createAndStore(TestRepo.troublesome.getId());
268-
}
269-
};
270-
op.execute();
271-
272-
final Iterator<ProjectEntity> iterator = getIterator(tenantId);
273-
274-
// This should iterate through all.
275-
Lists.newArrayList(iterator);
276-
}
277258
}

0 commit comments

Comments
 (0)