|
| 1 | +/* |
| 2 | + * Copyright 2022, TeamDev. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Redistribution and use in source and/or binary forms, with or without |
| 11 | + * modification, must retain the above copyright notice and the following |
| 12 | + * disclaimer. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 15 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 16 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 17 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 18 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 19 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + */ |
| 26 | + |
| 27 | +package io.spine.server.aggregate; |
| 28 | + |
| 29 | +import io.spine.environment.Tests; |
| 30 | +import io.spine.server.BoundedContextBuilder; |
| 31 | +import io.spine.server.ServerEnvironment; |
| 32 | +import io.spine.server.aggregate.given.salary.Employee; |
| 33 | +import io.spine.server.aggregate.given.salary.EmployeeAgg; |
| 34 | +import io.spine.server.aggregate.given.salary.PreparedInboxStorage; |
| 35 | +import io.spine.server.aggregate.given.salary.PreparedStorageFactory; |
| 36 | +import io.spine.server.delivery.DeliveryStrategy; |
| 37 | +import io.spine.type.TypeUrl; |
| 38 | +import org.junit.jupiter.api.DisplayName; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | + |
| 41 | +import static com.google.common.truth.Truth.assertThat; |
| 42 | +import static io.spine.server.aggregate.given.aggregate.AggregateTestEnv.command; |
| 43 | +import static io.spine.server.aggregate.given.salary.Employees.decreaseSalary; |
| 44 | +import static io.spine.server.aggregate.given.salary.Employees.employ; |
| 45 | +import static io.spine.server.aggregate.given.salary.Employees.increaseSalary; |
| 46 | +import static io.spine.server.aggregate.given.salary.Employees.newEmployee; |
| 47 | + |
| 48 | +@DisplayName("Cached `Aggregate` should") |
| 49 | +class AggregateCachingTest { |
| 50 | + |
| 51 | + @Test |
| 52 | + @DisplayName("store only successfully applied events") |
| 53 | + void storeEventsOnlyIfApplied() { |
| 54 | + var jack = newEmployee(); |
| 55 | + var shardIndex = DeliveryStrategy.newIndex(0, 1); |
| 56 | + var inboxStorage = PreparedInboxStorage.withCommands( |
| 57 | + shardIndex, |
| 58 | + TypeUrl.of(Employee.class), |
| 59 | + command(employ(jack, 250)), |
| 60 | + command(decreaseSalary(jack, 15)), |
| 61 | + |
| 62 | + // this one will fail the aggregate's state |
| 63 | + // as no employee can be paid less than 200. |
| 64 | + command(decreaseSalary(jack, 500)), |
| 65 | + |
| 66 | + command(increaseSalary(jack, 500)) |
| 67 | + ); |
| 68 | + |
| 69 | + var serverEnv = ServerEnvironment.instance(); |
| 70 | + serverEnv.reset(); |
| 71 | + ServerEnvironment.when(Tests.class).use(PreparedStorageFactory.with(inboxStorage)); |
| 72 | + |
| 73 | + var repository = new DefaultAggregateRepository<>(EmployeeAgg.class); |
| 74 | + BoundedContextBuilder.assumingTests() |
| 75 | + .add(repository) |
| 76 | + .build(); |
| 77 | + |
| 78 | + serverEnv.delivery().deliverMessagesFrom(shardIndex); |
| 79 | + serverEnv.reset(); |
| 80 | + |
| 81 | + var storedEvents = repository.aggregateStorage() |
| 82 | + .read(jack) |
| 83 | + .orElseThrow() |
| 84 | + .getEventList(); |
| 85 | + assertThat(storedEvents.size()).isEqualTo(3); |
| 86 | + } |
| 87 | +} |
0 commit comments