Skip to content

Commit

Permalink
adds missing isTrue assert in GraphQLJavaErrorInstrumentationTest
Browse files Browse the repository at this point in the history
  • Loading branch information
mrvaruntandon authored and kilink committed Aug 8, 2024
1 parent 4bc8ba8 commit d66d78a
Showing 1 changed file with 76 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ package com.netflix.graphql.dgs
import com.netflix.graphql.dgs.internal.GraphQLJavaErrorInstrumentation
import graphql.GraphQL
import graphql.GraphQLContext
import graphql.execution.CoercedVariables
import graphql.execution.instrumentation.Instrumentation
import graphql.language.StringValue
import graphql.language.Value
import graphql.schema.Coercing
import graphql.schema.CoercingParseLiteralException
import graphql.schema.CoercingParseValueException
import graphql.schema.CoercingSerializeException
import graphql.schema.GraphQLScalarType
import graphql.schema.StaticDataFetcher
Expand All @@ -32,16 +37,18 @@ import org.assertj.core.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import java.util.Locale
import java.util.regex.Pattern

class GraphQLJavaErrorInstrumentationTest {

private lateinit var graphqlJavaErrorInstrumentation: Instrumentation

private val schema = """
scalar IPv4
scalar Domain
type Query{
hello(name: String!): String
ip: IPv4
ip(domain: Domain): IPv4
}
""".trimMargin()

Expand Down Expand Up @@ -79,7 +86,7 @@ class GraphQLJavaErrorInstrumentationTest {

Assertions.assertThat(result.isDataPresent).isFalse
Assertions.assertThat(result.errors.size).isEqualTo(1)
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("classification", "errorDetail", "errorType")))
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("classification", "errorDetail", "errorType"))).isTrue()
Assertions.assertThat(result.errors[0].extensions["classification"]).isEqualTo("ValidationError")
Assertions.assertThat(result.errors[0].extensions["errorType"]).isEqualTo("BAD_REQUEST")
Assertions.assertThat(result.errors[0].extensions["errorDetail"]).isEqualTo("FIELD_NOT_FOUND")
Expand All @@ -98,7 +105,7 @@ class GraphQLJavaErrorInstrumentationTest {

Assertions.assertThat(result.isDataPresent).isFalse
Assertions.assertThat(result.errors.size).isEqualTo(1)
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("classification", "errorType")))
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("classification", "errorType"))).isTrue()
Assertions.assertThat(result.errors[0].extensions["classification"]).isEqualTo("ValidationError")
Assertions.assertThat(result.errors[0].extensions["errorType"]).isEqualTo("BAD_REQUEST")
}
Expand All @@ -116,7 +123,7 @@ class GraphQLJavaErrorInstrumentationTest {

Assertions.assertThat(result.isDataPresent).isFalse
Assertions.assertThat(result.errors.size).isEqualTo(1)
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("classification", "errorDetail", "errorType")))
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("classification", "errorType"))).isTrue()
Assertions.assertThat(result.errors[0].extensions["classification"]).isEqualTo("InvalidSyntax")
Assertions.assertThat(result.errors[0].extensions["errorType"]).isEqualTo("BAD_REQUEST")
}
Expand All @@ -134,7 +141,7 @@ class GraphQLJavaErrorInstrumentationTest {

Assertions.assertThat(result.isDataPresent).isFalse
Assertions.assertThat(result.errors.size).isEqualTo(1)
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("classification", "errorDetail", "errorType")))
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("classification", "errorDetail", "errorType"))).isTrue()
Assertions.assertThat(result.errors[0].extensions["classification"]).isEqualTo("OperationNotSupported")
Assertions.assertThat(result.errors[0].extensions["errorType"]).isEqualTo("BAD_REQUEST")
Assertions.assertThat(result.errors[0].extensions["errorDetail"]).isEqualTo("INVALID_ARGUMENT")
Expand All @@ -154,7 +161,7 @@ class GraphQLJavaErrorInstrumentationTest {

Assertions.assertThat(result.isDataPresent).isFalse
Assertions.assertThat(result.errors.size).isEqualTo(2)
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("classification", "errorDetail", "errorType")))
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("classification", "errorDetail", "errorType"))).isTrue()
Assertions.assertThat(result.errors[0].extensions["classification"]).isEqualTo("ValidationError")
Assertions.assertThat(result.errors[0].extensions["errorType"]).isEqualTo("BAD_REQUEST")
Assertions.assertThat(result.errors[0].extensions["errorDetail"]).isEqualTo("FIELD_NOT_FOUND")
Expand All @@ -176,11 +183,28 @@ class GraphQLJavaErrorInstrumentationTest {
)

Assertions.assertThat(result.errors.size).isEqualTo(1)
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("errorDetail", "errorType")))
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("errorDetail", "errorType"))).isTrue()
Assertions.assertThat(result.errors[0].extensions["errorType"]).isEqualTo("INTERNAL")
Assertions.assertThat(result.errors[0].extensions["errorDetail"]).isEqualTo("SERIALIZATION_ERROR")
}

@Test
fun `Error contains errorDetail and errorType in the extensions for input arguments that cannot be parsed`() {
val graphQL: GraphQL = buildGraphQL(schema)
val result = graphQL.execute(
"""
{
ip(domain: "Wrong Domain Value")
}
""".trimIndent()
)

Assertions.assertThat(result.errors.size).isEqualTo(1)
Assertions.assertThat(result.errors[0].extensions.keys.containsAll(listOf("errorDetail", "errorType")))
Assertions.assertThat(result.errors[0].extensions["errorType"]).isEqualTo("BAD_REQUEST")
Assertions.assertThat(result.errors[0].extensions["errorDetail"]).isEqualTo("INVALID_ARGUMENT")
}

private fun buildGraphQL(schema: String): GraphQL {
val schemaParser = SchemaParser()
val typeDefinitionRegistry = schemaParser.parse(schema)
Expand All @@ -203,6 +227,51 @@ class GraphQLJavaErrorInstrumentationTest {
}
})
.build()
).scalar(
GraphQLScalarType.newScalar().name("Domain").description("A custom scalar that handles Domain names")
.coercing(object :
Coercing<String, String> {

override fun serialize(dataFetcherResult: Any, graphQLContext: GraphQLContext, locale: Locale): String {
if (dataFetcherResult is String) {
val domainName = dataFetcherResult
if (Pattern.compile(
"^(https?://)?(www\\.)?([a-zA-Z0-9-]{1,63}\\.)+[a-zA-Z]{2,}$|^(https?://)?(www\\.)?xn--([a-zA-Z0-9-]{1,59}\\.)+[a-zA-Z]{2,}$"
).matcher(domainName).matches()
) {
return domainName
}
}
throw CoercingSerializeException("Invalid domain name")
}

override fun parseLiteral(input: Value<*>, variables: CoercedVariables, graphQLContext: GraphQLContext, locale: Locale): String? {
if (input is StringValue) {
val domainName = input.value
if (Pattern.compile(
"^(https?://)?(www\\.)?([a-zA-Z0-9-]{1,63}\\.)+[a-zA-Z]{2,}$|^(https?://)?(www\\.)?xn--([a-zA-Z0-9-]{1,59}\\.)+[a-zA-Z]{2,}$"
).matcher(domainName).matches()
) {
return domainName
}
}
throw CoercingParseLiteralException("Invalid domain name")
}

override fun parseValue(input: Any, graphQLContext: GraphQLContext, locale: Locale): String? {
if (input is String) {
val domainName = input
if (Pattern.compile(
"^(https?://)?(www\\.)?([a-zA-Z0-9-]{1,63}\\.)+[a-zA-Z]{2,}$|^(https?://)?(www\\.)?xn--([a-zA-Z0-9-]{1,59}\\.)+[a-zA-Z]{2,}$"
).matcher(domainName).matches()
) {
return domainName
}
}
throw CoercingParseValueException("Invalid domain name")
}
})
.build()
)
.type("Query") { builder: TypeRuntimeWiring.Builder ->
builder.dataFetcher("hello", StaticDataFetcher("hello there!"))
Expand Down

0 comments on commit d66d78a

Please sign in to comment.