Skip to content

Commit 6c54938

Browse files
authored
build: gradle plugin generated SDL integration tests (ExpediaGroup#1657)
### 📝 Description Migrate generate SDL task integration tests from using GradleRunner to a composite test build. ### 🔗 Related Issues
1 parent 0459612 commit 6c54938

File tree

36 files changed

+680
-530
lines changed

36 files changed

+680
-530
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import org.gradle.kotlin.dsl.`kotlin-dsl`
2+
3+
plugins {
4+
`kotlin-dsl`
5+
}
6+
7+
repositories {
8+
mavenCentral()
9+
}
10+
11+
dependencies {
12+
implementation(libs.kotlin.gradle.api)
13+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
dependencyResolutionManagement {
2+
versionCatalogs {
3+
create("libs") {
4+
from(files("../../../gradle/libs.versions.toml"))
5+
}
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.expediagroup.it
2+
3+
import org.gradle.api.DefaultTask
4+
import org.gradle.api.file.RegularFileProperty
5+
import org.gradle.api.tasks.InputFile
6+
import org.gradle.api.tasks.TaskAction
7+
8+
abstract class VerifyFileExistsTask : DefaultTask() {
9+
10+
@get:InputFile
11+
abstract val actualSchema: RegularFileProperty
12+
13+
@get:InputFile
14+
abstract val expectedSchema: RegularFileProperty
15+
16+
@TaskAction
17+
fun test() {
18+
if (!actualSchema.get().asFile.exists()) {
19+
throw RuntimeException("schema file was not generated")
20+
}
21+
22+
val expected = expectedSchema.get().asFile.readText()
23+
val actual = actualSchema.get().asFile.readText()
24+
if (expected != actual) {
25+
throw RuntimeException("generated schema file was different.\n---expected---\n$expected\n\n---actual---\n$actual")
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.expediagroup.it
2+
3+
import org.gradle.api.DefaultTask
4+
import org.gradle.api.file.RegularFileProperty
5+
import org.gradle.api.tasks.InputFile
6+
import org.gradle.api.tasks.TaskAction
7+
8+
abstract class VerifyGenerateSDLTask : DefaultTask() {
9+
10+
@get:InputFile
11+
abstract val actualSchema: RegularFileProperty
12+
13+
@get:InputFile
14+
abstract val expectedSchema: RegularFileProperty
15+
16+
@TaskAction
17+
fun test() {
18+
if (!actualSchema.get().asFile.exists()) {
19+
throw RuntimeException("schema file was not generated")
20+
}
21+
22+
val expected = expectedSchema.get().asFile.readText()
23+
val actual = actualSchema.get().asFile.readText()
24+
if (expected != actual) {
25+
throw RuntimeException("generated schema file was different.\n---expected---\n$expected\n\n---actual---\n$actual")
26+
}
27+
}
28+
}

integration/gradle-plugin-integration-tests/client-generator/custom-scalars-jackson/src/test/kotlin/com/expediagroup/scalars/CustomScalarApplicationTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import kotlin.test.assertNull
2323
import kotlinx.coroutines.runBlocking
2424
import org.junit.jupiter.api.Test
2525
import org.springframework.boot.test.context.SpringBootTest
26-
import org.springframework.boot.web.server.LocalServerPort
26+
import org.springframework.boot.test.web.server.LocalServerPort
2727

2828
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
2929
class CustomScalarApplicationTests(@LocalServerPort private val port: Int) {

integration/gradle-plugin-integration-tests/client-generator/polymorphic-types-jackson/src/test/kotlin/com/expediagroup/polymorphic/PolymorphicApplicationTests.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import com.expediagroup.polymorphic.generated.partialpolymorphicquery.DefaultBas
1010
import kotlinx.coroutines.runBlocking
1111
import org.junit.jupiter.api.Test
1212
import org.springframework.boot.test.context.SpringBootTest
13-
import org.springframework.boot.web.server.LocalServerPort
13+
import org.springframework.boot.test.web.server.LocalServerPort
1414
import kotlin.test.assertNull
1515
import kotlin.test.assertTrue
1616

integration/gradle-plugin-integration-tests/client-generator/polymorphic-types-kotlinx/src/test/kotlin/com/expediagroup/polymorphic/PolymorphicKotlinxTests.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import io.ktor.server.cio.CIO
1515
import io.ktor.server.engine.embeddedServer
1616
import java.net.URL
1717
import kotlinx.coroutines.runBlocking
18+
import kotlinx.serialization.ExperimentalSerializationApi
1819
import kotlinx.serialization.modules.SerializersModule
1920
import kotlinx.serialization.modules.polymorphic
2021
import org.junit.jupiter.api.Test
@@ -25,11 +26,12 @@ class PolymorphicKotlinxTests {
2526

2627
@Test
2728
fun `verify polymorphic queries are correctly serialized and deserialized`() {
28-
val engine = embeddedServer(CIO, port = 8080, module = Application::graphQLModule)
29+
val engine = embeddedServer(CIO, port = 0, module = Application::graphQLModule)
2930
try {
3031
engine.start()
3132
runBlocking {
32-
val client = GraphQLKtorClient(url = URL("http://localhost:8080/graphql"))
33+
val port = engine.resolvedConnectors().first().port
34+
val client = GraphQLKtorClient(url = URL("http://localhost:$port/graphql"))
3335

3436
val query = CompletePolymorphicQuery(variables = CompletePolymorphicQuery.Variables(input = "foo"))
3537
val response = client.execute(query)
@@ -48,9 +50,10 @@ class PolymorphicKotlinxTests {
4850
}
4951
}
5052

53+
@OptIn(ExperimentalSerializationApi::class)
5154
@Test
5255
fun `verify polymorphic queries fallbacks are correctly serialized and deserialized`() {
53-
val engine = embeddedServer(CIO, port = 8080, module = Application::graphQLModule)
56+
val engine = embeddedServer(CIO, port = 0, module = Application::graphQLModule)
5457
try {
5558
engine.start()
5659
runBlocking {
@@ -65,7 +68,8 @@ class PolymorphicKotlinxTests {
6568
}
6669
}
6770
})
68-
val client = GraphQLKtorClient(url = URL("http://localhost:8080/graphql"), serializer = serializerWithFallback)
71+
val port = engine.resolvedConnectors().first().port
72+
val client = GraphQLKtorClient(url = URL("http://localhost:$port/graphql"), serializer = serializerWithFallback)
6973

7074
val fallbackQuery = PartialPolymorphicQuery(variables = PartialPolymorphicQuery.Variables(input = "bar"))
7175
val fallbackResponse = client.execute(fallbackQuery)

integration/gradle-plugin-integration-tests/client-generator/skip-include/src/test/kotlin/com/expediagroup/directives/ApplicationTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import com.expediagroup.graphql.client.spring.GraphQLWebClient
55
import kotlinx.coroutines.runBlocking
66
import org.junit.jupiter.api.Test
77
import org.springframework.boot.test.context.SpringBootTest
8-
import org.springframework.boot.web.server.LocalServerPort
8+
import org.springframework.boot.test.web.server.LocalServerPort
99
import java.util.UUID
1010
import kotlin.test.assertNotNull
1111
import kotlin.test.assertNull
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import com.expediagroup.graphql.plugin.gradle.graphql
2+
import com.expediagroup.it.VerifyGenerateSDLTask
3+
4+
@Suppress("DSL_SCOPE_VIOLATION") // TODO: remove once KTIJ-19369 / Gradle#22797 is fixed
5+
plugins {
6+
id("com.expediagroup.graphql")
7+
alias(libs.plugins.kotlin.jvm)
8+
alias(libs.plugins.kotlin.spring)
9+
alias(libs.plugins.spring.boot)
10+
}
11+
12+
dependencies {
13+
implementation("com.expediagroup:graphql-kotlin-spring-server")
14+
implementation("com.expediagroup:graphql-kotlin-hooks-provider")
15+
implementation(libs.kotlin.stdlib)
16+
}
17+
18+
graphql {
19+
schema {
20+
packages = listOf("com.example")
21+
}
22+
}
23+
24+
// integration test
25+
tasks.register<VerifyGenerateSDLTask>("integrationTest") {
26+
dependsOn("graphqlGenerateSDL")
27+
28+
actualSchema.set(File(project.buildDir, "schema.graphql"))
29+
expectedSchema.set(File(rootProject.projectDir, "src/integration/resources/sdl/custom.graphql"))
30+
}
31+
tasks.named("build") {
32+
dependsOn("integrationTest")
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.example
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.runApplication
5+
import com.example.CustomFederatedHooks
6+
import com.expediagroup.graphql.generator.federation.FederatedSchemaGeneratorHooks
7+
import org.springframework.context.annotation.Bean
8+
9+
@SpringBootApplication
10+
class Application
11+
{
12+
@Bean
13+
fun federatedSchemaGeneratorHooks(): FederatedSchemaGeneratorHooks = CustomFederatedHooks()
14+
}
15+
16+
fun main(args: Array<String>) {
17+
runApplication<Application>(*args)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.example
2+
3+
import com.expediagroup.graphql.generator.federation.FederatedSchemaGeneratorHooks
4+
import graphql.language.StringValue
5+
import graphql.schema.Coercing
6+
import graphql.schema.CoercingParseLiteralException
7+
import graphql.schema.GraphQLScalarType
8+
import graphql.schema.GraphQLType
9+
import java.util.UUID
10+
import kotlin.reflect.KType
11+
12+
private val graphqlUUIDType = GraphQLScalarType.newScalar()
13+
.name("UUID")
14+
.description("Custom scalar representing UUID")
15+
.coercing(object : Coercing<UUID, String> {
16+
override fun parseValue(input: Any): UUID = UUID.fromString(
17+
serialize(input)
18+
)
19+
20+
override fun parseLiteral(input: Any): UUID {
21+
val uuidString = (input as? StringValue)?.value
22+
return if (uuidString != null) {
23+
UUID.fromString(uuidString)
24+
} else {
25+
throw CoercingParseLiteralException("Unable to convert $input to Any scalar")
26+
}
27+
}
28+
29+
override fun serialize(dataFetcherResult: Any): String = dataFetcherResult.toString()
30+
})
31+
.build()
32+
33+
class CustomFederatedHooks : FederatedSchemaGeneratorHooks(emptyList()) {
34+
35+
override fun willGenerateGraphQLType(type: KType): GraphQLType? = when (type.classifier) {
36+
UUID::class -> graphqlUUIDType
37+
else -> super.willGenerateGraphQLType(type)
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example
2+
3+
import com.expediagroup.graphql.generator.hooks.SchemaGeneratorHooks
4+
import com.expediagroup.graphql.plugin.schema.hooks.SchemaGeneratorHooksProvider
5+
6+
class CustomHooksProvider : SchemaGeneratorHooksProvider {
7+
8+
override fun hooks(): SchemaGeneratorHooks = CustomFederatedHooks()
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.example
2+
3+
import com.expediagroup.graphql.server.operations.Query
4+
import org.springframework.stereotype.Component
5+
import java.util.UUID
6+
7+
@Component
8+
class HelloWorldQuery : Query {
9+
10+
fun helloWorld(name: String? = null) = if (name != null) {
11+
"Hello, $name!!!"
12+
} else {
13+
"Hello, World!!!"
14+
}
15+
16+
fun randomUUID(): UUID = UUID.randomUUID()
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.example.CustomHooksProvider
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import com.expediagroup.it.VerifyGenerateSDLTask
2+
3+
plugins {
4+
id 'com.expediagroup.graphql'
5+
alias(libs.plugins.kotlin.jvm)
6+
alias(libs.plugins.kotlin.spring)
7+
alias(libs.plugins.spring.boot)
8+
}
9+
10+
dependencies {
11+
implementation "com.expediagroup:graphql-kotlin-spring-server"
12+
implementation(libs.kotlin.stdlib)
13+
}
14+
15+
graphql {
16+
schema {
17+
packages = ["com.example"]
18+
}
19+
}
20+
21+
// integration test
22+
tasks.register("integrationTest", VerifyGenerateSDLTask) {
23+
dependsOn("graphqlGenerateSDL")
24+
25+
actualSchema = new File(project.buildDir, "schema.graphql")
26+
expectedSchema = new File(rootProject.projectDir, "src/integration/resources/sdl/schema.graphql")
27+
}
28+
tasks.named("build") {
29+
dependsOn("integrationTest")
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.runApplication
5+
6+
@SpringBootApplication
7+
class Application
8+
9+
fun main(args: Array<String>) {
10+
runApplication<Application>(*args)
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example
2+
3+
import com.expediagroup.graphql.server.operations.Query
4+
import org.springframework.stereotype.Component
5+
6+
@Component
7+
class HelloWorldQuery : Query {
8+
9+
fun helloWorld(name: String? = null) = if (name != null) {
10+
"Hello, $name!!!"
11+
} else {
12+
"Hello, World!!!"
13+
}
14+
15+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import com.expediagroup.graphql.plugin.gradle.graphql
2+
import com.expediagroup.it.VerifyGenerateSDLTask
3+
4+
@Suppress("DSL_SCOPE_VIOLATION") // TODO: remove once KTIJ-19369 / Gradle#22797 is fixed
5+
plugins {
6+
id("com.expediagroup.graphql")
7+
alias(libs.plugins.kotlin.jvm)
8+
alias(libs.plugins.kotlin.spring)
9+
alias(libs.plugins.spring.boot)
10+
}
11+
12+
dependencies {
13+
implementation("com.expediagroup", "graphql-kotlin-spring-server")
14+
implementation(libs.kotlin.stdlib)
15+
}
16+
17+
graphql {
18+
schema {
19+
packages = listOf("com.example")
20+
}
21+
}
22+
23+
// integration test
24+
tasks.register<VerifyGenerateSDLTask>("integrationTest") {
25+
dependsOn("graphqlGenerateSDL")
26+
27+
actualSchema.set(File(project.buildDir, "schema.graphql"))
28+
expectedSchema.set(File(rootProject.projectDir, "src/integration/resources/sdl/schema.graphql"))
29+
}
30+
tasks.named("build") {
31+
dependsOn("integrationTest")
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.example
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication
4+
import org.springframework.boot.runApplication
5+
6+
@SpringBootApplication
7+
class Application
8+
9+
fun main(args: Array<String>) {
10+
runApplication<Application>(*args)
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example
2+
3+
import com.expediagroup.graphql.server.operations.Query
4+
import org.springframework.stereotype.Component
5+
6+
@Component
7+
class HelloWorldQuery : Query {
8+
9+
fun helloWorld(name: String? = null) = if (name != null) {
10+
"Hello, $name!!!"
11+
} else {
12+
"Hello, World!!!"
13+
}
14+
15+
}

0 commit comments

Comments
 (0)