Skip to content

Commit 6e2dc63

Browse files
smyrickdariuszkuc
authored andcommitted
Increase code coverage (ExpediaGroup#376)
* Increase code coverage * Update jacoco config to exclude examples
1 parent 9c6114b commit 6e2dc63

File tree

9 files changed

+320
-12
lines changed

9 files changed

+320
-12
lines changed

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/directives/KotlinDirectiveWiringFactoryTest.kt

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import graphql.util.TraversalControl
3333
import graphql.util.TraverserContext
3434
import io.mockk.mockk
3535
import org.junit.jupiter.api.Test
36-
import org.junit.jupiter.api.assertThrows
3736
import kotlin.test.assertEquals
37+
import kotlin.test.assertFailsWith
3838
import kotlin.test.assertNotEquals
3939

4040
class KotlinDirectiveWiringFactoryTest {
@@ -253,8 +253,8 @@ class KotlinDirectiveWiringFactoryTest {
253253
.withDirective(graphQLLowercaseDirective)
254254
.build()
255255

256-
assertThrows<InvalidSchemaDirectiveWiringException> {
257-
SimpleWiringFactory().onWire(graphQLType = myTestField)
256+
assertFailsWith(InvalidSchemaDirectiveWiringException::class) {
257+
SimpleWiringFactory().onWire(graphQLType = myTestField, coordinates = null, codeRegistry = null)
258258
}
259259
}
260260

@@ -266,8 +266,22 @@ class KotlinDirectiveWiringFactoryTest {
266266
.withDirective(graphQLOverrideDescriptionDirective)
267267
.build()
268268

269-
assertThrows<InvalidSchemaDirectiveWiringException> {
269+
assertFailsWith(InvalidSchemaDirectiveWiringException::class) {
270270
SimpleWiringFactory(overrides = mapOf("overrideDescription" to UpdateDescriptionWiringKotlinSchema("should fail"))).onWire(myTestObject)
271271
}
272272
}
273+
274+
@Test
275+
fun `verify exception is thrown if invalid code registry`() {
276+
val myTestObject = GraphQLObjectType.newObject()
277+
.name("MyObject")
278+
.description("My Object Description")
279+
.withDirective(graphQLOverrideDescriptionDirective)
280+
.build()
281+
282+
assertFailsWith(InvalidSchemaDirectiveWiringException::class) {
283+
SimpleWiringFactory(overrides = mapOf("overrideDescription" to UpdateDescriptionWiringKotlinSchema("should fail")))
284+
.onWire(graphQLType = myTestObject, coordinates = mockk())
285+
}
286+
}
273287
}

graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/extensions/GraphQLSchemaExtensionsTest.kt

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,42 @@ class GraphQLSchemaExtensionsTest {
5858
assertEquals(expected, sdl)
5959
}
6060

61+
@Test
62+
fun `verify print result of a simple schema with extended scalars`() {
63+
val schema: GraphQLSchema = toSchema(queries = listOf(TopLevelObject(SimpleQuery())), config = testSchemaConfig)
64+
65+
val sdl = schema.print(includeDirectives = false, includeScalarTypes = false, includeExtendedScalarTypes = true).trim()
66+
val expected = """
67+
schema {
68+
query: Query
69+
}
70+
71+
type Query {
72+
basic(msg: String!): String!
73+
nullable(msg: String): String
74+
}
75+
""".trimIndent()
76+
assertEquals(expected, sdl)
77+
}
78+
79+
@Test
80+
fun `verify print result of a simple schema with no scalars`() {
81+
val schema: GraphQLSchema = toSchema(queries = listOf(TopLevelObject(SimpleQuery())), config = testSchemaConfig)
82+
83+
val sdl = schema.print(includeDirectives = false, includeScalarTypes = false, includeExtendedScalarTypes = false).trim()
84+
val expected = """
85+
schema {
86+
query: Query
87+
}
88+
89+
type Query {
90+
basic(msg: String!): String!
91+
nullable(msg: String): String
92+
}
93+
""".trimIndent()
94+
assertEquals(expected, sdl)
95+
}
96+
6197
class RenamedQuery {
6298
@GraphQLName("renamedFunction")
6399
fun original(id: Int) = OriginalType(id)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2019 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.spring.exception
18+
19+
import graphql.execution.DataFetcherExceptionHandlerParameters
20+
import io.mockk.every
21+
import io.mockk.mockk
22+
import org.junit.jupiter.api.Test
23+
import kotlin.test.assertEquals
24+
import kotlin.test.assertNotNull
25+
26+
class KotlinDataFetcherExceptionHandlerTest {
27+
28+
@Test
29+
fun `test exception handler`() {
30+
31+
val parameters: DataFetcherExceptionHandlerParameters = mockk {
32+
every { exception } returns Throwable()
33+
every { sourceLocation } returns mockk()
34+
every { path } returns mockk {
35+
every { toList() } returns listOf("foo")
36+
}
37+
}
38+
39+
val handler = KotlinDataFetcherExceptionHandler()
40+
val result = handler.onException(parameters)
41+
42+
assertNotNull(result.errors)
43+
assertEquals(expected = 1, actual = result.errors.size)
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2019 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.spring.exception
18+
19+
import graphql.execution.AbortExecutionException
20+
import io.mockk.every
21+
import io.mockk.mockk
22+
import org.junit.jupiter.api.Test
23+
import kotlin.test.assertEquals
24+
import kotlin.test.assertNotNull
25+
import kotlin.test.assertTrue
26+
27+
internal class SimpleKotlinGraphQLErrorTest {
28+
29+
@Test
30+
fun `Message defaults to value if it is not set in the exception`() {
31+
val error = SimpleKotlinGraphQLError(Throwable())
32+
assertNotNull(error.message)
33+
}
34+
35+
@Test
36+
fun `Message comes from the exception if set`() {
37+
val error = SimpleKotlinGraphQLError(Throwable("foo"))
38+
assertEquals(expected = "foo", actual = error.message)
39+
}
40+
41+
@Test
42+
fun `extensions default to an empty map if the exception is not a GraphQLError`() {
43+
val error = SimpleKotlinGraphQLError(Throwable())
44+
assertTrue(error.extensions.isEmpty())
45+
}
46+
47+
@Test
48+
fun `extensions are empty if exception is a GraphQLError but extensions are null`() {
49+
val graphQLError: AbortExecutionException = mockk {
50+
every { extensions } returns null
51+
}
52+
val error = SimpleKotlinGraphQLError(graphQLError)
53+
assertTrue(error.extensions.isEmpty())
54+
}
55+
56+
@Test
57+
fun `extensions are populated if exception is a GraphQLError and extensions are set`() {
58+
val graphQLError: AbortExecutionException = mockk {
59+
every { extensions } returns mapOf("foo" to "bar")
60+
}
61+
val error = SimpleKotlinGraphQLError(graphQLError)
62+
assertEquals(expected = "bar", actual = error.extensions["foo"])
63+
}
64+
}

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/execution/ApolloSubscriptionProtocolHandlerTest.kt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ import io.mockk.verify
3838
import org.junit.jupiter.api.Test
3939
import org.springframework.web.reactive.socket.WebSocketSession
4040
import reactor.core.publisher.Flux
41+
import reactor.test.StepVerifier
4142
import java.time.Duration
4243
import kotlin.test.assertEquals
4344
import kotlin.test.assertNotNull
44-
import kotlin.test.assertNull
4545
import kotlin.test.assertTrue
4646

4747
class ApolloSubscriptionProtocolHandlerTest {
@@ -151,7 +151,30 @@ class ApolloSubscriptionProtocolHandlerTest {
151151
val handler = ApolloSubscriptionProtocolHandler(config, subscriptionHandler, objectMapper)
152152
val flux = handler.handle(objectMapper.writeValueAsString(operationMessage), session)
153153

154-
assertNull(flux.blockFirst(Duration.ofSeconds(2)))
154+
StepVerifier.create(flux)
155+
.verifyComplete()
156+
157+
verify(exactly = 1) { session.close() }
158+
}
159+
160+
@Test
161+
fun `Close session when sending GQL_CONNECTION_TERMINATE with id`() {
162+
val config: GraphQLConfigurationProperties = mockk()
163+
val operationMessage = SubscriptionOperationMessage(GQL_CONNECTION_TERMINATE.type, id = "123")
164+
val session: WebSocketSession = mockk {
165+
every { close() } returns mockk()
166+
every { id } returns "abc"
167+
}
168+
val subscriptionHandler: SubscriptionHandler = mockk()
169+
170+
val handler = ApolloSubscriptionProtocolHandler(config, subscriptionHandler, objectMapper)
171+
val flux = handler.handle(objectMapper.writeValueAsString(operationMessage), session)
172+
173+
StepVerifier.create(flux)
174+
.expectNextCount(0)
175+
.thenAwait()
176+
.verifyComplete()
177+
155178
verify(exactly = 1) { session.close() }
156179
}
157180

@@ -167,7 +190,11 @@ class ApolloSubscriptionProtocolHandlerTest {
167190
val handler = ApolloSubscriptionProtocolHandler(config, subscriptionHandler, objectMapper)
168191
val flux = handler.handle(objectMapper.writeValueAsString(operationMessage), session)
169192

170-
assertNull(flux.blockFirst(Duration.ofSeconds(2)))
193+
StepVerifier.create(flux)
194+
.expectNextCount(0)
195+
.thenCancel()
196+
.verify()
197+
171198
verify(exactly = 0) { session.close() }
172199
}
173200

graphql-kotlin-spring-server/src/test/kotlin/com/expediagroup/graphql/spring/execution/ContextWebFilterTest.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.expediagroup.graphql.spring.execution
1818

1919
import graphql.GraphQLContext
20+
import io.mockk.coEvery
2021
import io.mockk.every
2122
import io.mockk.mockk
2223
import org.junit.jupiter.api.Test
@@ -25,6 +26,7 @@ import org.springframework.web.server.WebFilterChain
2526
import reactor.core.publisher.Mono
2627
import reactor.test.StepVerifier
2728
import reactor.util.context.Context
29+
import kotlin.test.assertEquals
2830
import kotlin.test.assertNotNull
2931

3032
class ContextWebFilterTest {
@@ -44,12 +46,25 @@ class ContextWebFilterTest {
4446
}
4547
}
4648

47-
val contextFilter = ContextWebFilter(EmptyContextFactory)
49+
val simpleFactory: GraphQLContextFactory<Any> = mockk {
50+
coEvery { generateContext(any(), any()) } returns GraphQLContext.newContext().build()
51+
}
52+
53+
val contextFilter = ContextWebFilter(simpleFactory)
4854
StepVerifier.create(contextFilter.filter(exchange, chain))
4955
.verifyComplete()
5056

5157
assertNotNull(generatedContext)
5258
val graphQLContext = generatedContext?.getOrDefault<GraphQLContext>(GRAPHQL_CONTEXT_KEY, null)
5359
assertNotNull(graphQLContext)
5460
}
61+
62+
@Test
63+
fun `verify web filter order`() {
64+
val factory: GraphQLContextFactory<Any> = mockk {
65+
coEvery { generateContext(any(), any()) } returns mockk()
66+
}
67+
val contextFilter = ContextWebFilter(factory)
68+
assertEquals(expected = 0, actual = contextFilter.order)
69+
}
5570
}
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.expediagroup.graphql.spring.exception
17+
package com.expediagroup.graphql.spring.execution
1818

19-
import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage
19+
import io.mockk.mockk
20+
import org.junit.jupiter.api.Test
21+
import kotlin.test.assertEquals
2022

21-
class UknownSubscriptionOperationType(operationMessage: SubscriptionOperationMessage) :
22-
IllegalArgumentException("Unknown subscription operation $operationMessage")
23+
internal class SubscriptionWebSocketHandlerTest {
24+
25+
@Test
26+
fun getSubProtocols() {
27+
val handler = SubscriptionWebSocketHandler(mockk(), mockk())
28+
assertEquals(expected = listOf("graphql-ws"), actual = handler.subProtocols)
29+
}
30+
}

0 commit comments

Comments
 (0)