Skip to content

Commit 6eadede

Browse files
author
yevhenii.nadtochii
committed
Refactor the test with commands in a banch
1 parent cfed5da commit 6eadede

File tree

3 files changed

+70
-45
lines changed

3 files changed

+70
-45
lines changed

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

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import io.spine.environment.Tests;
4141
import io.spine.server.BoundedContext;
4242
import io.spine.server.BoundedContextBuilder;
43-
import io.spine.server.ContextSpec;
4443
import io.spine.server.ServerEnvironment;
4544
import io.spine.server.aggregate.given.Given;
4645
import io.spine.server.aggregate.given.aggregate.AggregateWithMissingApplier;
@@ -54,20 +53,16 @@
5453
import io.spine.server.aggregate.given.salary.Employee;
5554
import io.spine.server.aggregate.given.salary.EmployeeAgg;
5655
import io.spine.server.aggregate.given.salary.PreparedInboxStorage;
56+
import io.spine.server.aggregate.given.salary.PreparedStorageFactory;
5757
import io.spine.server.aggregate.given.salary.event.NewEmployed;
5858
import io.spine.server.aggregate.given.thermometer.SafeThermometer;
5959
import io.spine.server.aggregate.given.thermometer.SafeThermometerRepo;
6060
import io.spine.server.aggregate.given.thermometer.Thermometer;
6161
import io.spine.server.aggregate.given.thermometer.ThermometerId;
6262
import io.spine.server.aggregate.given.thermometer.event.TemperatureChanged;
6363
import io.spine.server.delivery.DeliveryStrategy;
64-
import io.spine.server.delivery.InboxStorage;
6564
import io.spine.server.delivery.MessageEndpoint;
6665
import io.spine.server.model.ModelError;
67-
import io.spine.server.storage.RecordSpec;
68-
import io.spine.server.storage.RecordStorage;
69-
import io.spine.server.storage.StorageFactory;
70-
import io.spine.server.storage.memory.InMemoryStorageFactory;
7166
import io.spine.server.type.CommandClass;
7267
import io.spine.server.type.CommandEnvelope;
7368
import io.spine.server.type.EventClass;
@@ -112,7 +107,6 @@
112107
import static com.google.common.collect.Lists.newArrayList;
113108
import static com.google.common.truth.Truth.assertThat;
114109
import static com.google.common.truth.extensions.proto.ProtoTruth.assertThat;
115-
import static io.spine.base.Identifier.newUuid;
116110
import static io.spine.grpc.StreamObservers.noOpObserver;
117111
import static io.spine.protobuf.AnyPacker.unpack;
118112
import static io.spine.server.aggregate.given.Given.EventMessage.projectCreated;
@@ -464,8 +458,8 @@ void restoreSnapshot() {
464458
}
465459

466460
@Test
467-
@DisplayName("add events to `UncommittedHistory` only if they were successfully applied")
468-
void addEventsToUncommittedOnlyIfApplied3() {
461+
@DisplayName("store events only if they were successfully applied")
462+
void storeEventsOnlyIfApplied() {
469463
var jack = newEmployee();
470464
var shardIndex = DeliveryStrategy.newIndex(0, 1);
471465
var inboxStorage = PreparedInboxStorage.withCommands(
@@ -477,50 +471,26 @@ void addEventsToUncommittedOnlyIfApplied3() {
477471
command(increaseSalary(jack, 500))
478472
);
479473

480-
System.out.println("Setting storage factory ...");
481-
ServerEnvironment.instance().reset();
482-
ServerEnvironment.when(Tests.class)
483-
.use(new StorageFactory() {
484-
@Override
485-
public <I, R extends Message> RecordStorage<I, R> createRecordStorage(
486-
ContextSpec context, RecordSpec<I, R, ?> spec) {
487-
return InMemoryStorageFactory.newInstance().createRecordStorage(context, spec);
488-
}
489-
490-
@Override
491-
public InboxStorage createInboxStorage(boolean multitenant) {
492-
return inboxStorage;
493-
}
494-
495-
@Override
496-
public void close() {
497-
// NO OP
498-
}
499-
});
474+
var serverEnv = ServerEnvironment.instance();
475+
serverEnv.reset();
476+
ServerEnvironment.when(Tests.class).use(PreparedStorageFactory.with(inboxStorage));
500477

501478
var repository = new DefaultAggregateRepository<>(EmployeeAgg.class);
502479
BoundedContextBuilder.assumingTests()
503480
.add(repository)
504481
.build();
505482

506-
System.out.println(ServerEnvironment.instance().type());
507-
System.out.println(ServerEnvironment.instance().storageFactory().createInboxStorage(false));
508-
509-
var stats = ServerEnvironment
510-
.instance()
511-
.delivery()
512-
.deliverMessagesFrom(shardIndex)
513-
.orElseThrow();
514-
System.out.println(stats.deliveredCount());
515-
ServerEnvironment.instance().reset();
483+
serverEnv.delivery().deliverMessagesFrom(shardIndex);
484+
serverEnv.reset();
516485

517486
var storedEvents = repository.aggregateStorage()
518487
.read(jack)
519488
.orElseThrow()
520489
.getEventList();
490+
var singleEvent = storedEvents.get(0).enclosedMessage();
521491

522492
assertThat(storedEvents.size()).isEqualTo(1);
523-
assertThat(storedEvents.get(0).enclosedMessage().getClass()).isEqualTo(NewEmployed.class);
493+
assertThat(singleEvent.getClass()).isEqualTo(NewEmployed.class);
524494
}
525495

526496
@Nested

server/src/test/java/io/spine/server/aggregate/given/salary/PreparedInboxStorage.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,8 @@ private PreparedInboxStorage() {
4949
super(InMemoryStorageFactory.newInstance(), false);
5050
}
5151

52-
public static PreparedInboxStorage withCommands(
53-
ShardIndex shardIndex,
54-
TypeUrl target,
55-
Command... commands
56-
) {
52+
public static InboxStorage
53+
withCommands(ShardIndex shardIndex, TypeUrl target, Command... commands) {
5754

5855
var routing = CommandRouting.newInstance(EmployeeId.class);
5956
var storage = new PreparedInboxStorage();
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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.given.salary;
28+
29+
import com.google.protobuf.Message;
30+
import io.spine.server.ContextSpec;
31+
import io.spine.server.delivery.InboxStorage;
32+
import io.spine.server.storage.RecordSpec;
33+
import io.spine.server.storage.RecordStorage;
34+
import io.spine.server.storage.StorageFactory;
35+
import io.spine.server.storage.memory.InMemoryStorageFactory;
36+
37+
public class PreparedStorageFactory implements StorageFactory {
38+
39+
public static StorageFactory with(InboxStorage inboxStorage) {
40+
return new PreparedStorageFactory() {
41+
@Override
42+
public InboxStorage createInboxStorage(boolean multitenant) {
43+
return inboxStorage;
44+
}
45+
};
46+
}
47+
48+
@Override
49+
public <I, R extends Message> RecordStorage<I, R>
50+
createRecordStorage(ContextSpec context, RecordSpec<I, R, ?> spec) {
51+
return InMemoryStorageFactory.newInstance().createRecordStorage(context, spec);
52+
}
53+
54+
@Override
55+
public void close() throws Exception {
56+
// NO OP
57+
}
58+
}

0 commit comments

Comments
 (0)