Skip to content

Commit 6445940

Browse files
committed
chore: clean codes
1 parent 0a04c19 commit 6445940

File tree

5 files changed

+89
-93
lines changed

5 files changed

+89
-93
lines changed

spring-graphql-rsocket-kotlin-co/src/main/kotlin/com/example/demo/ValidationConfig.kt

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
11
package com.example.demo.gql.scalars
22

3+
import graphql.GraphQLContext
4+
import graphql.execution.CoercedVariables
35
import graphql.language.StringValue
6+
import graphql.language.Value
47
import graphql.schema.Coercing
58
import graphql.schema.CoercingParseLiteralException
6-
import graphql.schema.CoercingParseValueException
79
import graphql.schema.CoercingSerializeException
810
import java.time.LocalDateTime
911
import java.time.format.DateTimeFormatter
12+
import java.util.*
1013

1114
class LocalDateTimeScalar : Coercing<LocalDateTime, String> {
1215
@Throws(CoercingSerializeException::class)
13-
override fun serialize(dataFetcherResult: Any): String? {
16+
override fun serialize(dataFetcherResult: Any, graphQLContext: GraphQLContext, locale: Locale): String? {
1417
return when (dataFetcherResult) {
1518
is LocalDateTime -> dataFetcherResult.format(DateTimeFormatter.ISO_DATE_TIME)
1619
else -> throw CoercingSerializeException("Not a valid DateTime")
1720
}
1821
}
1922

20-
@Throws(CoercingParseValueException::class)
21-
override fun parseValue(input: Any): LocalDateTime {
23+
override fun parseValue(input: Any, graphQLContext: GraphQLContext, locale: Locale): LocalDateTime {
2224
return LocalDateTime.parse(input.toString(), DateTimeFormatter.ISO_DATE_TIME)
2325
}
2426

25-
@Throws(CoercingParseLiteralException::class)
26-
override fun parseLiteral(input: Any): LocalDateTime {
27+
override fun parseLiteral(
28+
input: Value<*>,
29+
variables: CoercedVariables,
30+
graphQLContext: GraphQLContext,
31+
locale: Locale
32+
): LocalDateTime {
2733
when (input) {
2834
is StringValue -> return LocalDateTime.parse(input.value, DateTimeFormatter.ISO_DATE_TIME)
2935
else -> throw CoercingParseLiteralException("Value is not a valid ISO date time")
3036
}
3137
}
3238

39+
override fun valueToLiteral(input: Any, graphQLContext: GraphQLContext, locale: Locale): Value<*> {
40+
return StringValue(input.toString())
41+
}
42+
3343
}

spring-graphql-rsocket-kotlin-co/src/test/kotlin/com/example/demo/IntegrationTests.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.example.demo
33
import com.example.demo.gql.types.Comment
44
import com.example.demo.gql.types.Post
55
import com.fasterxml.jackson.databind.ObjectMapper
6-
import io.kotest.assertions.timing.continually
6+
import io.kotest.assertions.nondeterministic.continually
77
import io.kotest.matchers.shouldBe
88
import io.kotest.matchers.shouldNotBe
99
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -25,7 +25,6 @@ import java.util.*
2525
import java.util.concurrent.CopyOnWriteArrayList
2626
import kotlin.time.Duration.Companion.seconds
2727

28-
@OptIn(ExperimentalCoroutinesApi::class)
2928
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
3029
internal class IntegrationTests {
3130
companion object {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.example.demo
2+
3+
import com.example.demo.gql.datafetchers.PostController
4+
import com.example.demo.gql.types.Post
5+
import com.ninjasquad.springmockk.MockkBean
6+
import io.mockk.coEvery
7+
import io.mockk.coVerify
8+
import kotlinx.coroutines.test.runTest
9+
import org.junit.jupiter.api.Test
10+
import org.slf4j.LoggerFactory
11+
import org.springframework.beans.factory.annotation.Autowired
12+
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest
13+
import org.springframework.graphql.test.tester.GraphQlTester
14+
import java.time.LocalDateTime
15+
import java.util.*
16+
17+
@GraphQlTest(controllers = [PostController::class])
18+
internal class MutationTests {
19+
companion object {
20+
private val log = LoggerFactory.getLogger(MutationTests::class.java)
21+
}
22+
23+
@Autowired
24+
lateinit var graphQlTester: GraphQlTester
25+
26+
@MockkBean
27+
lateinit var postService: PostService
28+
29+
@MockkBean
30+
lateinit var authorService: AuthorService
31+
32+
@Test
33+
fun createPosts() = runTest {
34+
coEvery { postService.createPost(any()) } returns
35+
Post(
36+
id = UUID.randomUUID(),
37+
title = "test title",
38+
content = "test content",
39+
status = PostStatus.DRAFT,
40+
createdAt = LocalDateTime.now()
41+
)
42+
43+
val inputHolder = "\$input"
44+
val query = """
45+
mutation createPost($inputHolder: CreatePostInput!){
46+
createPost(createPostInput:$inputHolder){
47+
id,
48+
title,
49+
content
50+
}
51+
}
52+
""".trimIndent()
53+
graphQlTester.document(query)
54+
.variable(
55+
"input",
56+
mapOf(
57+
"title" to "test title",
58+
"content" to "test content"
59+
)
60+
)
61+
.execute()
62+
.path("data.createPost.title")
63+
.entity(String::class.java)
64+
.isEqualTo("TEST TITLE")
65+
66+
coVerify(exactly = 1) { postService.createPost(any()) }
67+
}
68+
}

spring-graphql-rsocket-kotlin-co/src/test/kotlin/com/example/demo/QueryTests.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,23 @@ package com.example.demo
33
import com.example.demo.gql.datafetchers.PostController
44
import com.example.demo.gql.types.Post
55
import com.ninjasquad.springmockk.MockkBean
6-
import io.kotest.matchers.collections.shouldContainAll
76
import io.kotest.matchers.ints.shouldBeGreaterThan
87
import io.kotest.matchers.shouldBe
98
import io.mockk.coEvery
109
import io.mockk.coVerify
11-
import kotlinx.coroutines.ExperimentalCoroutinesApi
1210
import kotlinx.coroutines.flow.flowOf
1311
import kotlinx.coroutines.test.runTest
1412
import org.junit.jupiter.api.Test
1513
import org.slf4j.LoggerFactory
1614
import org.springframework.beans.factory.annotation.Autowired
1715
import org.springframework.boot.test.autoconfigure.graphql.GraphQlTest
18-
import org.springframework.context.annotation.Import
1916
import org.springframework.graphql.ResponseError
2017
import org.springframework.graphql.test.tester.GraphQlTester
2118
import java.time.LocalDateTime
2219
import java.util.*
2320

24-
@OptIn(ExperimentalCoroutinesApi::class)
2521
@GraphQlTest(controllers = [PostController::class])
26-
@Import(ValidationConfig::class)
27-
internal class QueryTests {
22+
class QueryTests {
2823
companion object {
2924
private val log = LoggerFactory.getLogger(QueryTests::class.java)
3025
}
@@ -61,9 +56,9 @@ internal class QueryTests {
6156
// graphQlTester.document(query).execute()
6257
// .errors().satisfy { it.forEach { error -> log.debug("error message: ${error.message}") } }
6358
graphQlTester.document(query)
64-
.execute()
65-
.path("data.allPosts[*].title")
66-
.entityList(String::class.java).hasSize(2).contains("POST 1", "POST 2")
59+
.execute()
60+
.path("data.allPosts[*].title")
61+
.entityList(String::class.java).hasSize(2).contains("POST 1", "POST 2")
6762

6863
coVerify(exactly = 1) { postService.allPosts() }
6964
}

0 commit comments

Comments
 (0)