Skip to content

Commit

Permalink
Extract the test into a separate test class
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhenii-nadtochii committed Jan 6, 2022
1 parent 6eadede commit 1efbae3
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ final BatchDispatchOutcome apply(List<Event> events, int snapshotTrigger) {
System.out.println("Batch size: " + events.size());
uncommittedHistory.startTracking(snapshotTrigger);
System.out.println("Uncommitted size before: " + uncommittedHistory.events().list().size());
var result = play(versionedEvents);
var result = play((List<Event>)versionedEvents);
System.out.println("Batch successful ? " + result.getSuccessful());
uncommittedHistory.stopTracking();
System.out.println("Uncommitted size after: " + uncommittedHistory.events().list().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*
* @param <I> the type of aggregate IDs
* @param <S> the type of aggregate state
* @param <B> the type of a {@code ValidatingBuilder} for the aggregate state
* @param <B> the type of {@code ValidatingBuilder} for the aggregate state
*/
@Internal
public class AggregateTransaction<I,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2022, TeamDev. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.server.aggregate;

import io.spine.environment.Tests;
import io.spine.server.BoundedContextBuilder;
import io.spine.server.ServerEnvironment;
import io.spine.server.aggregate.given.salary.Employee;
import io.spine.server.aggregate.given.salary.EmployeeAgg;
import io.spine.server.aggregate.given.salary.PreparedInboxStorage;
import io.spine.server.aggregate.given.salary.PreparedStorageFactory;
import io.spine.server.delivery.DeliveryStrategy;
import io.spine.type.TypeUrl;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static com.google.common.truth.Truth.assertThat;
import static io.spine.server.aggregate.given.aggregate.AggregateTestEnv.command;
import static io.spine.server.aggregate.given.salary.Employees.decreaseSalary;
import static io.spine.server.aggregate.given.salary.Employees.employ;
import static io.spine.server.aggregate.given.salary.Employees.increaseSalary;
import static io.spine.server.aggregate.given.salary.Employees.newEmployee;

@DisplayName("Cached `Aggregate` should")
class AggregateCachingTest {

@Test
@DisplayName("store only successfully applied events")
void storeEventsOnlyIfApplied() {
var jack = newEmployee();
var shardIndex = DeliveryStrategy.newIndex(0, 1);
var inboxStorage = PreparedInboxStorage.withCommands(
shardIndex,
TypeUrl.of(Employee.class),
command(employ(jack, 250)),
command(decreaseSalary(jack, 15)),

// this one will fail the aggregate's state
// as no employee can be paid less than 200.
command(decreaseSalary(jack, 500)),

command(increaseSalary(jack, 500))
);

var serverEnv = ServerEnvironment.instance();
serverEnv.reset();
ServerEnvironment.when(Tests.class).use(PreparedStorageFactory.with(inboxStorage));

var repository = new DefaultAggregateRepository<>(EmployeeAgg.class);
BoundedContextBuilder.assumingTests()
.add(repository)
.build();

serverEnv.delivery().deliverMessagesFrom(shardIndex);
serverEnv.reset();

var storedEvents = repository.aggregateStorage()
.read(jack)
.orElseThrow()
.getEventList();
assertThat(storedEvents.size()).isEqualTo(3);
}
}
36 changes: 0 additions & 36 deletions server/src/test/java/io/spine/server/aggregate/AggregateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,42 +457,6 @@ void restoreSnapshot() {
assertEquals(aggregate, anotherAggregate);
}

@Test
@DisplayName("store events only if they were successfully applied")
void storeEventsOnlyIfApplied() {
var jack = newEmployee();
var shardIndex = DeliveryStrategy.newIndex(0, 1);
var inboxStorage = PreparedInboxStorage.withCommands(
shardIndex,
TypeUrl.of(Employee.class),
command(employ(jack, 250)),
command(decreaseSalary(jack, 15)),
command(decreaseSalary(jack, 500)),
command(increaseSalary(jack, 500))
);

var serverEnv = ServerEnvironment.instance();
serverEnv.reset();
ServerEnvironment.when(Tests.class).use(PreparedStorageFactory.with(inboxStorage));

var repository = new DefaultAggregateRepository<>(EmployeeAgg.class);
BoundedContextBuilder.assumingTests()
.add(repository)
.build();

serverEnv.delivery().deliverMessagesFrom(shardIndex);
serverEnv.reset();

var storedEvents = repository.aggregateStorage()
.read(jack)
.orElseThrow()
.getEventList();
var singleEvent = storedEvents.get(0).enclosedMessage();

assertThat(storedEvents.size()).isEqualTo(1);
assertThat(singleEvent.getClass()).isEqualTo(NewEmployed.class);
}

@Nested
@DisplayName("after dispatch, return event records")
class ReturnEventRecords {
Expand Down

0 comments on commit 1efbae3

Please sign in to comment.