Skip to content

Increase code coverage #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import graphql.util.TraversalControl
import graphql.util.TraverserContext
import io.mockk.mockk
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotEquals

class KotlinDirectiveWiringFactoryTest {
Expand Down Expand Up @@ -253,8 +253,8 @@ class KotlinDirectiveWiringFactoryTest {
.withDirective(graphQLLowercaseDirective)
.build()

assertThrows<InvalidSchemaDirectiveWiringException> {
SimpleWiringFactory().onWire(graphQLType = myTestField)
assertFailsWith(InvalidSchemaDirectiveWiringException::class) {
SimpleWiringFactory().onWire(graphQLType = myTestField, coordinates = null, codeRegistry = null)
}
}

Expand All @@ -266,8 +266,22 @@ class KotlinDirectiveWiringFactoryTest {
.withDirective(graphQLOverrideDescriptionDirective)
.build()

assertThrows<InvalidSchemaDirectiveWiringException> {
assertFailsWith(InvalidSchemaDirectiveWiringException::class) {
SimpleWiringFactory(overrides = mapOf("overrideDescription" to UpdateDescriptionWiringKotlinSchema("should fail"))).onWire(myTestObject)
}
}

@Test
fun `verify exception is thrown if invalid code registry`() {
val myTestObject = GraphQLObjectType.newObject()
.name("MyObject")
.description("My Object Description")
.withDirective(graphQLOverrideDescriptionDirective)
.build()

assertFailsWith(InvalidSchemaDirectiveWiringException::class) {
SimpleWiringFactory(overrides = mapOf("overrideDescription" to UpdateDescriptionWiringKotlinSchema("should fail")))
.onWire(graphQLType = myTestObject, coordinates = mockk())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ class GraphQLSchemaExtensionsTest {
assertEquals(expected, sdl)
}

@Test
fun `verify print result of a simple schema with extended scalars`() {
val schema: GraphQLSchema = toSchema(queries = listOf(TopLevelObject(SimpleQuery())), config = testSchemaConfig)

val sdl = schema.print(includeDirectives = false, includeScalarTypes = false, includeExtendedScalarTypes = true).trim()
val expected = """
schema {
query: Query
}

type Query {
basic(msg: String!): String!
nullable(msg: String): String
}
""".trimIndent()
assertEquals(expected, sdl)
}

@Test
fun `verify print result of a simple schema with no scalars`() {
val schema: GraphQLSchema = toSchema(queries = listOf(TopLevelObject(SimpleQuery())), config = testSchemaConfig)

val sdl = schema.print(includeDirectives = false, includeScalarTypes = false, includeExtendedScalarTypes = false).trim()
val expected = """
schema {
query: Query
}

type Query {
basic(msg: String!): String!
nullable(msg: String): String
}
""".trimIndent()
assertEquals(expected, sdl)
}

class RenamedQuery {
@GraphQLName("renamedFunction")
fun original(id: Int) = OriginalType(id)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2019 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.spring.exception

import graphql.execution.DataFetcherExceptionHandlerParameters
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class KotlinDataFetcherExceptionHandlerTest {

@Test
fun `test exception handler`() {

val parameters: DataFetcherExceptionHandlerParameters = mockk {
every { exception } returns Throwable()
every { sourceLocation } returns mockk()
every { path } returns mockk {
every { toList() } returns listOf("foo")
}
}

val handler = KotlinDataFetcherExceptionHandler()
val result = handler.onException(parameters)

assertNotNull(result.errors)
assertEquals(expected = 1, actual = result.errors.size)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2019 Expedia, Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.expediagroup.graphql.spring.exception

import graphql.execution.AbortExecutionException
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

internal class SimpleKotlinGraphQLErrorTest {

@Test
fun `Message defaults to value if it is not set in the exception`() {
val error = SimpleKotlinGraphQLError(Throwable())
assertNotNull(error.message)
}

@Test
fun `Message comes from the exception if set`() {
val error = SimpleKotlinGraphQLError(Throwable("foo"))
assertEquals(expected = "foo", actual = error.message)
}

@Test
fun `extensions default to an empty map if the exception is not a GraphQLError`() {
val error = SimpleKotlinGraphQLError(Throwable())
assertTrue(error.extensions.isEmpty())
}

@Test
fun `extensions are empty if exception is a GraphQLError but extensions are null`() {
val graphQLError: AbortExecutionException = mockk {
every { extensions } returns null
}
val error = SimpleKotlinGraphQLError(graphQLError)
assertTrue(error.extensions.isEmpty())
}

@Test
fun `extensions are populated if exception is a GraphQLError and extensions are set`() {
val graphQLError: AbortExecutionException = mockk {
every { extensions } returns mapOf("foo" to "bar")
}
val error = SimpleKotlinGraphQLError(graphQLError)
assertEquals(expected = "bar", actual = error.extensions["foo"])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ import io.mockk.verify
import org.junit.jupiter.api.Test
import org.springframework.web.reactive.socket.WebSocketSession
import reactor.core.publisher.Flux
import reactor.test.StepVerifier
import java.time.Duration
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

class ApolloSubscriptionProtocolHandlerTest {
Expand Down Expand Up @@ -151,7 +151,30 @@ class ApolloSubscriptionProtocolHandlerTest {
val handler = ApolloSubscriptionProtocolHandler(config, subscriptionHandler, objectMapper)
val flux = handler.handle(objectMapper.writeValueAsString(operationMessage), session)

assertNull(flux.blockFirst(Duration.ofSeconds(2)))
StepVerifier.create(flux)
.verifyComplete()

verify(exactly = 1) { session.close() }
}

@Test
fun `Close session when sending GQL_CONNECTION_TERMINATE with id`() {
val config: GraphQLConfigurationProperties = mockk()
val operationMessage = SubscriptionOperationMessage(GQL_CONNECTION_TERMINATE.type, id = "123")
val session: WebSocketSession = mockk {
every { close() } returns mockk()
every { id } returns "abc"
}
val subscriptionHandler: SubscriptionHandler = mockk()

val handler = ApolloSubscriptionProtocolHandler(config, subscriptionHandler, objectMapper)
val flux = handler.handle(objectMapper.writeValueAsString(operationMessage), session)

StepVerifier.create(flux)
.expectNextCount(0)
.thenAwait()
.verifyComplete()

verify(exactly = 1) { session.close() }
}

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

assertNull(flux.blockFirst(Duration.ofSeconds(2)))
StepVerifier.create(flux)
.expectNextCount(0)
.thenCancel()
.verify()

verify(exactly = 0) { session.close() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.expediagroup.graphql.spring.execution

import graphql.GraphQLContext
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.Test
Expand All @@ -25,6 +26,7 @@ import org.springframework.web.server.WebFilterChain
import reactor.core.publisher.Mono
import reactor.test.StepVerifier
import reactor.util.context.Context
import kotlin.test.assertEquals
import kotlin.test.assertNotNull

class ContextWebFilterTest {
Expand All @@ -44,12 +46,25 @@ class ContextWebFilterTest {
}
}

val contextFilter = ContextWebFilter(EmptyContextFactory)
val simpleFactory: GraphQLContextFactory<Any> = mockk {
coEvery { generateContext(any(), any()) } returns GraphQLContext.newContext().build()
}

val contextFilter = ContextWebFilter(simpleFactory)
StepVerifier.create(contextFilter.filter(exchange, chain))
.verifyComplete()

assertNotNull(generatedContext)
val graphQLContext = generatedContext?.getOrDefault<GraphQLContext>(GRAPHQL_CONTEXT_KEY, null)
assertNotNull(graphQLContext)
}

@Test
fun `verify web filter order`() {
val factory: GraphQLContextFactory<Any> = mockk {
coEvery { generateContext(any(), any()) } returns mockk()
}
val contextFilter = ContextWebFilter(factory)
assertEquals(expected = 0, actual = contextFilter.order)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,17 @@
* limitations under the License.
*/

package com.expediagroup.graphql.spring.exception
package com.expediagroup.graphql.spring.execution

import com.expediagroup.graphql.spring.model.SubscriptionOperationMessage
import io.mockk.mockk
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

class UknownSubscriptionOperationType(operationMessage: SubscriptionOperationMessage) :
IllegalArgumentException("Unknown subscription operation $operationMessage")
internal class SubscriptionWebSocketHandlerTest {

@Test
fun getSubProtocols() {
val handler = SubscriptionWebSocketHandler(mockk(), mockk())
assertEquals(expected = listOf("graphql-ws"), actual = handler.subProtocols)
}
}
Loading