generated from tktcorporation/.github
/
LogTest.kt
151 lines (135 loc) · 5.27 KB
/
LogTest.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.example.appsyncsandbox.appsync
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.amazonaws.amplify.generated.graphql.CreateLogMutation
import com.amazonaws.amplify.generated.graphql.GetLogQuery
import com.amazonaws.amplify.generated.graphql.ListLogsQuery
import com.amazonaws.mobile.config.AWSConfiguration
import com.amazonaws.mobileconnectors.appsync.AWSAppSyncClient
import com.amazonaws.mobileconnectors.appsync.fetcher.AppSyncResponseFetchers.NETWORK_FIRST
import com.apollographql.apollo.GraphQLCall
import com.apollographql.apollo.api.Response
import com.apollographql.apollo.exception.ApolloException
import kotlinx.coroutines.runBlocking
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.Assert.*
import type.CreateLogInput
import java.util.*
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class LogTest {
private val appContext = InstrumentationRegistry.getInstrumentation().targetContext
private val client = AWSAppSyncClient.builder()
.context(appContext)
.awsConfiguration(AWSConfiguration(appContext))
.build()
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.appsyncsandbox", appContext.packageName)
}
@Test
fun createTest() = runBlocking {
val log = create()
assertEquals(log.content(), "sample")
assertEquals(log.kind(), "kind")
}
@Test
fun listTest() = runBlocking {
val list = list()
assert(list.count() > 0)
val log = list.first()
assertNotNull(log)
assertEquals(log.content(), "sample")
assertEquals(log.kind(), "kind")
}
@Test
fun getTest() = runBlocking {
val list = list()
assert(list.count() > 0)
val log = list.first()
val result = get(log.kind(), log.timestampMs())
assertNotNull(result)
assertEquals(result!!.content(), "sample")
assertEquals(result.kind(), "kind")
}
private suspend fun create():
CreateLogMutation.CreateLog {
val input = CreateLogInput
.builder()
.content("sample")
.id(UUID.randomUUID().toString())
.kind("kind")
.timestampMs(System.currentTimeMillis().toDouble())
.build()
return suspendCoroutine { continuation ->
val mutation = CreateLogMutation.builder().input(input).build()
val caller = client.mutate(mutation)
caller.enqueue(object :
GraphQLCall.Callback<CreateLogMutation.Data>() {
override fun onFailure(e: ApolloException) {
throw e
}
override fun onResponse(response: Response<CreateLogMutation.Data>) {
if (response.errors().size > 0) {
continuation.resumeWithException(Exception(response.errors().toString()))
return
}
val result = response.data()!!.createLog()!!
continuation.resume(result)
}
})
}
}
private suspend fun list() : List<ListLogsQuery.Item> {
val filter = ListLogsQuery.builder().build()
return suspendCoroutine { continuation ->
client.query(filter).responseFetcher(NETWORK_FIRST).enqueue(object :
GraphQLCall.Callback<ListLogsQuery.Data>() {
override fun onFailure(e: ApolloException) {
throw e
}
override fun onResponse(response: Response<ListLogsQuery.Data>) {
if (response.errors().size > 0) {
continuation.resumeWithException(Exception(response.errors().toString()))
return
}
val result = response.data()!!.listLogs()!!.items()!!
continuation.resume(result)
}
})
}
}
private suspend fun get(kind: String, timestampMs: Double) : GetLogQuery.GetLog? {
val query = GetLogQuery
.builder()
.kind(kind)
.timestampMs(timestampMs)
.build()
return suspendCoroutine { continuation ->
client.query(query).responseFetcher(NETWORK_FIRST).enqueue(object :
GraphQLCall.Callback<GetLogQuery.Data>() {
override fun onFailure(e: ApolloException) {
throw e
}
override fun onResponse(response: Response<GetLogQuery.Data>) {
if (response.errors().size > 0) {
continuation.resumeWithException(Exception(response.errors().toString()))
return
}
val result = response.data()!!.log
continuation.resume(result)
}
})
}
}
}