Skip to content

Commit

Permalink
Refactor the test with commands in a banch
Browse files Browse the repository at this point in the history
  • Loading branch information
yevhenii-nadtochii committed Jan 6, 2022
1 parent cfed5da commit 6eadede
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 45 deletions.
50 changes: 10 additions & 40 deletions server/src/test/java/io/spine/server/aggregate/AggregateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import io.spine.environment.Tests;
import io.spine.server.BoundedContext;
import io.spine.server.BoundedContextBuilder;
import io.spine.server.ContextSpec;
import io.spine.server.ServerEnvironment;
import io.spine.server.aggregate.given.Given;
import io.spine.server.aggregate.given.aggregate.AggregateWithMissingApplier;
Expand All @@ -54,20 +53,16 @@
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.aggregate.given.salary.event.NewEmployed;
import io.spine.server.aggregate.given.thermometer.SafeThermometer;
import io.spine.server.aggregate.given.thermometer.SafeThermometerRepo;
import io.spine.server.aggregate.given.thermometer.Thermometer;
import io.spine.server.aggregate.given.thermometer.ThermometerId;
import io.spine.server.aggregate.given.thermometer.event.TemperatureChanged;
import io.spine.server.delivery.DeliveryStrategy;
import io.spine.server.delivery.InboxStorage;
import io.spine.server.delivery.MessageEndpoint;
import io.spine.server.model.ModelError;
import io.spine.server.storage.RecordSpec;
import io.spine.server.storage.RecordStorage;
import io.spine.server.storage.StorageFactory;
import io.spine.server.storage.memory.InMemoryStorageFactory;
import io.spine.server.type.CommandClass;
import io.spine.server.type.CommandEnvelope;
import io.spine.server.type.EventClass;
Expand Down Expand Up @@ -112,7 +107,6 @@
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;
import static io.spine.base.Identifier.newUuid;
import static io.spine.grpc.StreamObservers.noOpObserver;
import static io.spine.protobuf.AnyPacker.unpack;
import static io.spine.server.aggregate.given.Given.EventMessage.projectCreated;
Expand Down Expand Up @@ -464,8 +458,8 @@ void restoreSnapshot() {
}

@Test
@DisplayName("add events to `UncommittedHistory` only if they were successfully applied")
void addEventsToUncommittedOnlyIfApplied3() {
@DisplayName("store events only if they were successfully applied")
void storeEventsOnlyIfApplied() {
var jack = newEmployee();
var shardIndex = DeliveryStrategy.newIndex(0, 1);
var inboxStorage = PreparedInboxStorage.withCommands(
Expand All @@ -477,50 +471,26 @@ void addEventsToUncommittedOnlyIfApplied3() {
command(increaseSalary(jack, 500))
);

System.out.println("Setting storage factory ...");
ServerEnvironment.instance().reset();
ServerEnvironment.when(Tests.class)
.use(new StorageFactory() {
@Override
public <I, R extends Message> RecordStorage<I, R> createRecordStorage(
ContextSpec context, RecordSpec<I, R, ?> spec) {
return InMemoryStorageFactory.newInstance().createRecordStorage(context, spec);
}

@Override
public InboxStorage createInboxStorage(boolean multitenant) {
return inboxStorage;
}

@Override
public void close() {
// NO OP
}
});
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();

System.out.println(ServerEnvironment.instance().type());
System.out.println(ServerEnvironment.instance().storageFactory().createInboxStorage(false));

var stats = ServerEnvironment
.instance()
.delivery()
.deliverMessagesFrom(shardIndex)
.orElseThrow();
System.out.println(stats.deliveredCount());
ServerEnvironment.instance().reset();
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(storedEvents.get(0).enclosedMessage().getClass()).isEqualTo(NewEmployed.class);
assertThat(singleEvent.getClass()).isEqualTo(NewEmployed.class);
}

@Nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ private PreparedInboxStorage() {
super(InMemoryStorageFactory.newInstance(), false);
}

public static PreparedInboxStorage withCommands(
ShardIndex shardIndex,
TypeUrl target,
Command... commands
) {
public static InboxStorage
withCommands(ShardIndex shardIndex, TypeUrl target, Command... commands) {

var routing = CommandRouting.newInstance(EmployeeId.class);
var storage = new PreparedInboxStorage();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.given.salary;

import com.google.protobuf.Message;
import io.spine.server.ContextSpec;
import io.spine.server.delivery.InboxStorage;
import io.spine.server.storage.RecordSpec;
import io.spine.server.storage.RecordStorage;
import io.spine.server.storage.StorageFactory;
import io.spine.server.storage.memory.InMemoryStorageFactory;

public class PreparedStorageFactory implements StorageFactory {

public static StorageFactory with(InboxStorage inboxStorage) {
return new PreparedStorageFactory() {
@Override
public InboxStorage createInboxStorage(boolean multitenant) {
return inboxStorage;
}
};
}

@Override
public <I, R extends Message> RecordStorage<I, R>
createRecordStorage(ContextSpec context, RecordSpec<I, R, ?> spec) {
return InMemoryStorageFactory.newInstance().createRecordStorage(context, spec);
}

@Override
public void close() throws Exception {
// NO OP
}
}

0 comments on commit 6eadede

Please sign in to comment.