|
21 | 21 | package org.spine3.server.aggregate; |
22 | 22 |
|
23 | 23 | import com.google.common.base.Optional; |
| 24 | +import com.google.common.base.Predicates; |
| 25 | +import com.google.common.collect.Lists; |
24 | 26 | import com.google.protobuf.Message; |
25 | 27 | import org.junit.After; |
26 | 28 | import org.junit.Before; |
|
31 | 33 | import org.spine3.server.BoundedContext; |
32 | 34 | import org.spine3.server.command.Assign; |
33 | 35 | import org.spine3.server.storage.StorageFactorySwitch; |
| 36 | +import org.spine3.server.tenant.TenantAwareOperation; |
34 | 37 | import org.spine3.test.Given; |
35 | 38 | import org.spine3.test.TestActorRequestFactory; |
36 | 39 | import org.spine3.test.aggregate.Project; |
|
44 | 47 | import org.spine3.testdata.Sample; |
45 | 48 | import org.spine3.type.CommandClass; |
46 | 49 |
|
| 50 | +import java.util.Iterator; |
47 | 51 | import java.util.Set; |
48 | 52 |
|
49 | 53 | import static org.junit.Assert.assertEquals; |
|
58 | 62 | import static org.mockito.Mockito.spy; |
59 | 63 | import static org.mockito.Mockito.times; |
60 | 64 | import static org.mockito.Mockito.verify; |
| 65 | +import static org.spine3.test.Tests.newTenantUuid; |
61 | 66 | import static org.spine3.validate.Validate.isDefault; |
62 | 67 | import static org.spine3.validate.Validate.isNotDefault; |
63 | 68 |
|
@@ -235,6 +240,26 @@ public void mark_aggregate_deleted() { |
235 | 240 | .isPresent()); |
236 | 241 | } |
237 | 242 |
|
| 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 | + |
238 | 263 | /* |
239 | 264 | * Utility methods. |
240 | 265 | ****************************/ |
@@ -279,6 +304,15 @@ private ProjectAggregate createAndStoreAggregate() { |
279 | 304 | return aggregate; |
280 | 305 | } |
281 | 306 |
|
| 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 | + |
282 | 316 | private AggregateStorage<ProjectId> givenAggregateStorageMock() { |
283 | 317 | @SuppressWarnings("unchecked") |
284 | 318 | final AggregateStorage<ProjectId> storage = mock(AggregateStorage.class); |
@@ -358,10 +392,23 @@ protected void setDeleted(boolean deleted) { |
358 | 392 |
|
359 | 393 | private static class ProjectAggregateRepository |
360 | 394 | extends AggregateRepository<ProjectId, ProjectAggregate> { |
| 395 | + |
| 396 | + private static final ProjectId troublesome = ProjectId.newBuilder() |
| 397 | + .setId("CANNOT_BE_LOADED") |
| 398 | + .build(); |
| 399 | + |
361 | 400 | protected ProjectAggregateRepository(BoundedContext boundedContext) { |
362 | 401 | super(boundedContext); |
363 | 402 | initStorage(StorageFactorySwitch.getInstance(boundedContext.isMultitenant()) |
364 | 403 | .get()); |
365 | 404 | } |
| 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 | + } |
366 | 413 | } |
367 | 414 | } |
0 commit comments