forked from rodolfodpk/myeslib2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventoryItemTest.groovy
114 lines (91 loc) · 4.2 KB
/
InventoryItemTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package org.myeslib.sampledomain
import com.google.common.eventbus.EventBus
import com.google.inject.AbstractModule
import com.google.inject.Guice
import com.google.inject.Inject
import com.google.inject.TypeLiteral
import com.google.inject.util.Modules
import org.mockito.Mockito
import org.myeslib.data.CommandId
import org.myeslib.data.Event
import org.myeslib.data.EventMessage
import org.myeslib.infra.WriteModelDao
import org.myeslib.stack1.infra.dao.Stack1JdbiDao
import org.myeslib.stack1.infra.helpers.DatabaseHelper
import sampledomain.aggregates.inventoryitem.InventoryItemModule
import sampledomain.aggregates.inventoryitem.commands.CreateInventoryItem
import sampledomain.aggregates.inventoryitem.commands.DecreaseInventory
import sampledomain.aggregates.inventoryitem.commands.IncreaseInventory
import sampledomain.aggregates.inventoryitem.events.InventoryDecreased
import sampledomain.aggregates.inventoryitem.events.InventoryIncreased
import sampledomain.aggregates.inventoryitem.events.InventoryItemCreated
import sampledomain.services.SampleDomainService
import java.util.function.Consumer
import static org.mockito.Mockito.any
import static org.mockito.Mockito.when
public class InventoryItemTest extends Stack1BaseSpec<UUID> {
@Inject
EventBus commandBus
@Inject
WriteModelDao<UUID> unitOfWorkDao;
def setup() {
def Consumer<EventMessage> eventsConsumer = Mockito.mock(Consumer.class)
def sampleDomainService = Mockito.mock(SampleDomainService.class)
when(sampleDomainService.generateItemDescription(any(UUID.class))).thenReturn(itemDescription)
def injector = Guice.createInjector(Modules.override(new InventoryItemModule()).with(new MockedDomainServicesModule(sampleDomainService: sampleDomainService, eventsConsumer: eventsConsumer)))
injector.injectMembers(this);
injector.getInstance(DatabaseHelper.class).initDb();
}
// fixture
final UUID itemId = UUID.randomUUID();
final String itemDescription = "hammer"
def "creating an inventory item"() {
when:
command(CreateInventoryItem.create(CommandId.create(), itemId))
then:
lastCmdEvents(itemId) == [InventoryItemCreated.create(itemId, itemDescription)]
}
def "increase"() {
given:
command(CreateInventoryItem.create(CommandId.create(), itemId))
when:
command(IncreaseInventory.create(CommandId.create(), itemId, 10))
then:
lastCmdEvents(itemId) == [InventoryIncreased.create(10)]
}
def "decrease"() {
given:
command(CreateInventoryItem.create(CommandId.create(), itemId))
and:
command(IncreaseInventory.create(CommandId.create(), itemId, 10))
when:
command(DecreaseInventory.create(CommandId.create(), itemId, 7))
then:
lastCmdEvents(itemId) == [InventoryDecreased.create(7)] as List<Event>
and: "can check all events too"
allEvents(itemId) == [InventoryItemCreated.create(itemId, itemDescription), InventoryIncreased.create(10), InventoryDecreased.create(7)] as List<Event>
}
def "decrease an unavailable item *will log an error*"() {
given:
command(CreateInventoryItem.create(CommandId.create(), itemId))
and:
command(IncreaseInventory.create(CommandId.create(), itemId, 10))
when:
command(DecreaseInventory.create(CommandId.create(), itemId, 11))
then:
lastCmdEvents(itemId) == [InventoryIncreased.create(10)]
}
static class MockedDomainServicesModule extends AbstractModule {
def SampleDomainService sampleDomainService
def Consumer<EventMessage> eventsConsumer
@Override
protected void configure() {
bind(new TypeLiteral<List<Consumer<EventMessage>>>() {})
.toInstance([eventsConsumer])
bind(SampleDomainService.class).toInstance(sampleDomainService);
// If you want to persist to database, you may use Stack1JdbiDao instead of Stack1MemDao
// bind(new TypeLiteral<WriteModelDao<UUID>>() {})
// .to(new TypeLiteral<Stack1JdbiDao<UUID>>() {}).asEagerSingleton();
}
}
}