Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 7b476ea

Browse files
committed
Add snapshot test
1 parent 496189b commit 7b476ea

File tree

1 file changed

+57
-16
lines changed

1 file changed

+57
-16
lines changed

cloudstate-kotlin-support/src/test/kotlin/io/cloudstate/kotlinsupport/tests/KotlinAnnotationBasedEventSourcedTest.kt

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import io.cloudstate.javasupport.ServiceCallFactory
99
import io.cloudstate.javasupport.eventsourced.CommandContext
1010
import io.cloudstate.javasupport.eventsourced.EventContext
1111
import io.cloudstate.javasupport.eventsourced.EventSourcedContext
12+
import io.cloudstate.javasupport.eventsourced.SnapshotContext
1213
import io.cloudstate.javasupport.impl.AnySupport
1314
import io.cloudstate.kotlinsupport.annotations.EntityId
1415
import io.cloudstate.kotlinsupport.annotations.eventsourced.*
1516
import io.cloudstate.kotlinsupport.api.eventsourced.KotlinAnnotationBasedEventSourced
1617
import io.cloudstate.kotlinsupport.logger
1718
import org.junit.Test
1819
import java.util.*
20+
import java.util.stream.Collectors
1921
import kotlin.test.assertEquals
2022
import kotlin.test.assertNotNull
2123
import kotlin.test.assertTrue
@@ -42,11 +44,11 @@ class KotlinAnnotationBasedEventSourcedTest {
4244
val handler = annotationHandler.create(creationContext)
4345

4446
val expectResult = Optional.of(
45-
params.first.encodeJava(
46-
ShoppingCartProto.Cart.newBuilder()
47-
.addAllItems(mutableMapOf<String, ShoppingCartProto.LineItem?>().values)
48-
.build())
49-
)
47+
params.first.encodeJava(
48+
ShoppingCartProto.Cart.newBuilder()
49+
.addAllItems(mutableMapOf<String, ShoppingCartProto.LineItem?>().values)
50+
.build())
51+
)
5052

5153
val command = params.first.encodeJava(Empty.getDefaultInstance())
5254

@@ -99,12 +101,37 @@ class KotlinAnnotationBasedEventSourcedTest {
99101
log.info("Result of Call GetCart is: ${result.get()}")
100102
}
101103

104+
@Test
102105
fun `Validate SnapshotInvoker Calls`() {
103-
TODO()
106+
val annotationHandler = params.second
107+
val creationContext = createCreationContext()
108+
val snapshotContext: SnapshotContext = createSnapshotContext()
109+
val handler = annotationHandler.create(creationContext)
110+
111+
val expectResult = Optional.of(
112+
params.first.encodeJava(
113+
Domain.Cart.newBuilder()
114+
.addAllItems(mutableMapOf<String, Domain.LineItem?>().values)
115+
.build())
116+
)
117+
118+
val result = handler.snapshot(snapshotContext)
119+
assertTrue(result.isPresent)
120+
assertEquals(expectResult.get(), result.get())
121+
log.info("Result of Call Snapshot is: ${result.get()}")
104122
}
105123

106-
fun `Validate SnapshotHandlers Calls`() {
107-
TODO()
124+
private fun createSnapshotContext(): SnapshotContext = object: SnapshotContext{
125+
override fun entityId(): String = "test:1"
126+
127+
override fun serviceCallFactory(): ServiceCallFactory {
128+
TODO("Not yet implemented")
129+
}
130+
131+
override fun sequenceNumber(): Long {
132+
TODO("Not yet implemented")
133+
}
134+
108135
}
109136

110137
private fun createItemAddedEventContext(): EventContext? = object: EventContext{
@@ -181,7 +208,14 @@ class TestEntity(@EntityId private val entityId: String) {
181208
private val cart: MutableMap<String, ShoppingCartProto.LineItem?> = mutableMapOf<String, ShoppingCartProto.LineItem?>()
182209

183210
@Snapshot
184-
fun snapshot() {}
211+
fun snapshot(): Domain.Cart =
212+
Domain.Cart.newBuilder()
213+
.addAllItems(
214+
cart.values.stream()
215+
.map { item: ShoppingCartProto.LineItem? -> this.convert(item) }
216+
.collect(Collectors.toList())
217+
)
218+
.build()
185219

186220
@SnapshotHandler
187221
fun handleSnapshot(cart: Domain.Cart) {}
@@ -194,8 +228,8 @@ class TestEntity(@EntityId private val entityId: String) {
194228
convert(itemAdded.item)
195229
} else {
196230
item.toBuilder()
197-
.setQuantity(item.quantity + itemAdded.item.quantity)
198-
.build()
231+
.setQuantity(item.quantity + itemAdded.item.quantity)
232+
.build()
199233
}
200234
cart[item!!.productId] = item
201235
}
@@ -213,9 +247,16 @@ class TestEntity(@EntityId private val entityId: String) {
213247
fun removeItem() {}
214248

215249
private fun convert(item: Domain.LineItem): ShoppingCartProto.LineItem =
216-
ShoppingCartProto.LineItem.newBuilder()
217-
.setProductId(item.productId)
218-
.setName(item.name)
219-
.setQuantity(item.quantity)
220-
.build()
250+
ShoppingCartProto.LineItem.newBuilder()
251+
.setProductId(item.productId)
252+
.setName(item.name)
253+
.setQuantity(item.quantity)
254+
.build()
255+
256+
private fun convert(item: ShoppingCartProto.LineItem?): Domain.LineItem =
257+
Domain.LineItem.newBuilder()
258+
.setProductId(item!!.productId)
259+
.setName(item.name)
260+
.setQuantity(item.quantity)
261+
.build()
221262
}

0 commit comments

Comments
 (0)