Skip to content

Commit

Permalink
Add a test that downloads introspection against a real server (#5478)
Browse files Browse the repository at this point in the history
* Add a test that downloads introspection against a real server

* add Tests to the Contributing guide
  • Loading branch information
martinbonnin authored Dec 15, 2023
1 parent aba989a commit c631558
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 3 deletions.
16 changes: 16 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,22 @@ java -cp build/libs/r8_with_deps.jar com.android.tools.r8.retrace.Retrace apollo
[copy paste your stacktrace and press Crtl-D to launch the retracing]
```

## Tests

You can run tests with `./gradlew build`.

Because the Apollo Kotlin compiler runs from a Gradle Plugin, a lot of integration tests are in the `tests` composite build.

You can run integration tests with `./gradlew -p tests build`

Gradle tests are slow and not always easy to debug in the IDE. Most of the times,
an integration test from the `tests` composite build is faster. Keep Gradle tests for the cases
where:

* We need to test a specific version of Gradle and/or KGP, AGP or another buildscript dependency.
* We need to tweak the Gradle environment, for an example, a server needs to run in the background.
* We need to test up-to-date checks, build cache or other things that require instrumenting the build outcome.

## Overview of the CI

The project uses [GitHub Actions](https://docs.github.com/en/actions) to automate the build process.
Expand Down
10 changes: 10 additions & 0 deletions libraries/apollo-gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ dependencies {
testImplementation(libs.assertj)
testImplementation(libs.okhttp.mockwebserver)
testImplementation(libs.okhttp.tls)

testImplementation(project(":apollo-execution-incubating"))

testImplementation(platform(libs.http4k.bom.get()))
testImplementation(libs.http4k.core)
testImplementation(libs.http4k.server.jetty)
testImplementation(libs.slf4j.get().toString()) {
because("jetty uses SL4F")
}

}

if (relocateJar) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
package com.apollographql.apollo3.gradle.test


import com.apollographql.apollo3.api.ExecutionContext
import com.apollographql.apollo3.execution.ExecutableSchema
import com.apollographql.apollo3.execution.GraphQLRequest
import com.apollographql.apollo3.execution.GraphQLRequestError
import com.apollographql.apollo3.execution.parsePostGraphQLRequest
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.tls.HandshakeCertificates
import okhttp3.tls.HeldCertificate
import okio.Buffer
import okio.buffer
import okio.source
import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.TaskOutcome
import org.http4k.core.HttpHandler
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status
import org.http4k.routing.bind
import org.http4k.routing.routes
import org.http4k.server.Jetty
import org.http4k.server.asServer
import org.junit.Assert.assertEquals
import org.junit.Test
import util.TestUtils
import util.TestUtils.withSimpleProject
import util.TestUtils.withTestProject
import java.io.File

class DownloadSchemaTests {
Expand Down Expand Up @@ -267,4 +286,47 @@ class DownloadSchemaTests {
}
}

@Test
fun `download a schema from a real server is working`() {
val executableSchema = ExecutableSchema.Builder()
.schema("type Query {foo: Int}")
.build()

val server = routes("/graphql" bind Method.POST to GraphQLHttpHandler(executableSchema, ExecutionContext.Empty))
.asServer(Jetty(8001))
.start()

val buildResult = withTestProject("downloadIntrospection") {dir ->
TestUtils.executeGradle(dir, "downloadServiceApolloSchemaFromIntrospection")
}

assertEquals(TaskOutcome.SUCCESS, buildResult.task(":downloadServiceApolloSchemaFromIntrospection")?.outcome)

server.stop()
}

class GraphQLHttpHandler(val executableSchema: ExecutableSchema, val executionContext: ExecutionContext) : HttpHandler {
override fun invoke(request: Request): Response {

val graphQLRequestResult = when (request.method) {
org.http4k.core.Method.POST -> request.body.stream.source().buffer().use { it.parsePostGraphQLRequest() }
else -> error("")
}

if (graphQLRequestResult is GraphQLRequestError) {
return Response(Status.BAD_REQUEST).body(graphQLRequestResult.message)
}
graphQLRequestResult as GraphQLRequest

val response = executableSchema.execute(graphQLRequestResult, executionContext)

val buffer = Buffer()
response.serialize(buffer)
val responseText = buffer.readUtf8()

return Response(Status.OK)
.header("content-type", "application/json")
.body(responseText)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ object TestUtils {
val kotlinAndroidPlugin = Plugin(id = "org.jetbrains.kotlin.android", artifact = "libs.plugins.kotlin.android")
val apolloPlugin = Plugin(id = "com.apollographql.apollo3", artifact = "libs.plugins.apollo")

fun withDirectory(testDir: String? = null, block: (File) -> Unit) {
fun <T> withDirectory(testDir: String? = null, block: (File) -> T): T {
val dest = if (testDir == null) {
File.createTempFile("testProject", "", File(System.getProperty("user.dir")).resolve("build"))
} else {
Expand All @@ -34,7 +34,7 @@ object TestUtils {
|
""".trimMargin())

try {
return try {
block(dest)
} finally {
// Comment this line if you want to keep the directory around during development
Expand Down Expand Up @@ -139,7 +139,7 @@ object TestUtils {
block = block
)

fun withTestProject(name: String, testDir: String? = null, block: (File) -> Unit) = withDirectory(testDir) { dir ->
fun <T> withTestProject(name: String, testDir: String? = null, block: (File) -> T): T = withDirectory(testDir) { dir ->
File(System.getProperty("user.dir"), "testProjects/$name").copyRecursively(dir, overwrite = true)

block(dir)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.apollo)
}

apollo {
service("service") {
packageName.set("com.example")
introspection {
endpointUrl.set("http://localhost:8001/graphql")
schemaFile.set(file("schema.graphqls"))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apply(from = "../../../../gradle/test.settings.gradle.kts")

0 comments on commit c631558

Please sign in to comment.