Skip to content

Commit 1efbae3

Browse files
author
yevhenii.nadtochii
committed
Extract the test into a separate test class
1 parent 6eadede commit 1efbae3

File tree

4 files changed

+89
-38
lines changed

4 files changed

+89
-38
lines changed

server/src/main/java/io/spine/server/aggregate/Aggregate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ final BatchDispatchOutcome apply(List<Event> events, int snapshotTrigger) {
349349
System.out.println("Batch size: " + events.size());
350350
uncommittedHistory.startTracking(snapshotTrigger);
351351
System.out.println("Uncommitted size before: " + uncommittedHistory.events().list().size());
352-
var result = play(versionedEvents);
352+
var result = play((List<Event>)versionedEvents);
353353
System.out.println("Batch successful ? " + result.getSuccessful());
354354
uncommittedHistory.stopTracking();
355355
System.out.println("Uncommitted size after: " + uncommittedHistory.events().list().size());

server/src/main/java/io/spine/server/aggregate/AggregateTransaction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
*
4242
* @param <I> the type of aggregate IDs
4343
* @param <S> the type of aggregate state
44-
* @param <B> the type of a {@code ValidatingBuilder} for the aggregate state
44+
* @param <B> the type of {@code ValidatingBuilder} for the aggregate state
4545
*/
4646
@Internal
4747
public class AggregateTransaction<I,
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

server/src/test/java/io/spine/server/aggregate/AggregateTest.java

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -457,42 +457,6 @@ void restoreSnapshot() {
457457
assertEquals(aggregate, anotherAggregate);
458458
}
459459

460-
@Test
461-
@DisplayName("store events only if they were successfully applied")
462-
void storeEventsOnlyIfApplied() {
463-
var jack = newEmployee();
464-
var shardIndex = DeliveryStrategy.newIndex(0, 1);
465-
var inboxStorage = PreparedInboxStorage.withCommands(
466-
shardIndex,
467-
TypeUrl.of(Employee.class),
468-
command(employ(jack, 250)),
469-
command(decreaseSalary(jack, 15)),
470-
command(decreaseSalary(jack, 500)),
471-
command(increaseSalary(jack, 500))
472-
);
473-
474-
var serverEnv = ServerEnvironment.instance();
475-
serverEnv.reset();
476-
ServerEnvironment.when(Tests.class).use(PreparedStorageFactory.with(inboxStorage));
477-
478-
var repository = new DefaultAggregateRepository<>(EmployeeAgg.class);
479-
BoundedContextBuilder.assumingTests()
480-
.add(repository)
481-
.build();
482-
483-
serverEnv.delivery().deliverMessagesFrom(shardIndex);
484-
serverEnv.reset();
485-
486-
var storedEvents = repository.aggregateStorage()
487-
.read(jack)
488-
.orElseThrow()
489-
.getEventList();
490-
var singleEvent = storedEvents.get(0).enclosedMessage();
491-
492-
assertThat(storedEvents.size()).isEqualTo(1);
493-
assertThat(singleEvent.getClass()).isEqualTo(NewEmployed.class);
494-
}
495-
496460
@Nested
497461
@DisplayName("after dispatch, return event records")
498462
class ReturnEventRecords {

0 commit comments

Comments
 (0)