-
Notifications
You must be signed in to change notification settings - Fork 28
/
MockServiceResponseQueueTest.kt
111 lines (91 loc) · 3.73 KB
/
MockServiceResponseQueueTest.kt
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 krotoplus.example
import com.github.marcoferrer.krotoplus.test.addServices
import io.grpc.Metadata
import io.grpc.Status
import io.grpc.StatusRuntimeException
import io.grpc.testing.GrpcServerRule
import jojo.bizarre.adventure.MockJojoServices
import jojo.bizarre.adventure.stand.*
import org.junit.Rule
import org.junit.Test
import kotlin.test.BeforeTest
import kotlin.test.assertEquals
import kotlin.test.fail
class MockServiceResponseQueueTest {
@[Rule JvmField]
var grpcServerRule = GrpcServerRule().directExecutor()
@BeforeTest
fun bindMockServices() {
grpcServerRule?.serviceRegistry?.addServices(MockJojoServices)
}
val metadataTestKey = Metadata.Key.of("test_header", Metadata.ASCII_STRING_MARSHALLER)
@Test
fun `Test Unary Response Queue`() {
MockStandService.getStandByNameResponseQueue.apply {
//Queue up a valid response message
addMessage {
name = "Star Platinum"
powerLevel = 500
speed = 550
addAttacks {
name = "ORA ORA ORA"
damage = 100
range = StandProto.Attack.Range.CLOSE
}
}
//Queue up an error
addError(Status.INVALID_ARGUMENT)
//Queue up a valid response message
addMessage {
name = "The World"
powerLevel = 490
speed = 550
addAttacks {
name = "ZA WARUDO"
damage = 0
range = StandProto.Attack.Range.MEDIUM
}
}
//Queue up another error but this time with metadata
addError(Status.PERMISSION_DENIED, Metadata().apply {
put(metadataTestKey, "some_metadata_value")
})
}
val standStub = StandServiceGrpc.newBlockingStub(grpcServerRule.channel)
standStub.getStandByName { name = "Star Platinum" }.let { response ->
assertEquals("Star Platinum", response.name)
assertEquals(500, response.powerLevel)
assertEquals(550, response.speed)
response.attacksList.first().let { attack ->
assertEquals("ORA ORA ORA", attack.name)
assertEquals(100, attack.damage)
assertEquals(StandProto.Attack.Range.CLOSE, attack.range)
}
}
try {
standStub.getStandByName { name = "The World" }
fail("Exception was expected with status code: ${Status.INVALID_ARGUMENT.code}")
} catch (e: StatusRuntimeException) {
assertEquals(Status.INVALID_ARGUMENT.code, e.status.code)
}
standStub.getStandByName { name = "The World" }.let { response ->
assertEquals("The World", response.name)
assertEquals(490, response.powerLevel)
assertEquals(550, response.speed)
response.attacksList.first().let { attack ->
assertEquals("ZA WARUDO", attack.name)
assertEquals(0, attack.damage)
assertEquals(StandProto.Attack.Range.MEDIUM, attack.range)
}
}
try {
standStub.getStandByName { name = "The World" }
fail("Exception was expected with status code: ${Status.INVALID_ARGUMENT.code}")
} catch (e: StatusRuntimeException) {
assertEquals(Status.PERMISSION_DENIED.code, e.status.code)
assertEquals("some_metadata_value", e.trailers?.get(metadataTestKey))
}
//Test default instance fall back when the response queue is empty
assertEquals(StandProto.Stand.getDefaultInstance(), standStub.getStandByName { name = "Anything" })
}
}