Skip to content

Commit 714fe62

Browse files
Make sure that the introspection endpoint property value is checked at execution time (#6657)
* add tests * makes ure the endpoint input value presence is verified at execution time
1 parent cb41b1f commit 714fe62

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

libraries/apollo-gradle-plugin/src/main/kotlin/com/apollographql/apollo/gradle/internal/DefaultService.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,6 @@ abstract class DefaultService @Inject constructor(val project: Project, override
6565

6666
configure.execute(introspection)
6767

68-
if (!introspection.endpointUrl.isPresent) {
69-
throw IllegalArgumentException("introspection must have a url")
70-
}
71-
7268
this.introspection = introspection
7369
}
7470

libraries/apollo-gradle-plugin/src/test/kotlin/test/DownloadSchemaTests.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ package test
22

33
import com.apollographql.execution.ExecutableSchema
44
import com.apollographql.execution.http4k.apolloHandler
5+
import com.google.common.truth.Truth
56
import okhttp3.mockwebserver.MockResponse
67
import okhttp3.mockwebserver.MockWebServer
78
import okhttp3.tls.HandshakeCertificates
89
import okhttp3.tls.HeldCertificate
910
import org.gradle.testkit.runner.TaskOutcome
11+
import org.gradle.testkit.runner.UnexpectedBuildFailure
1012
import org.http4k.server.Jetty
1113
import org.http4k.server.asServer
1214
import org.junit.Assert
1315
import org.junit.Test
1416
import util.TestUtils
1517
import java.io.File
18+
import kotlin.test.assertContains
19+
import kotlin.test.assertEquals
1620

1721
class DownloadSchemaTests {
1822
private val mockServer = MockWebServer()
@@ -152,6 +156,17 @@ class DownloadSchemaTests {
152156
}
153157
""".trimIndent()
154158

159+
private val apolloConfigurationWithGradleProperty = """
160+
apollo {
161+
service("mock") {
162+
introspection {
163+
schemaFile = file("src/main/graphql/com/example/schema.json")
164+
endpointUrl = providers.gradleProperty("schema-endpoint")
165+
}
166+
}
167+
}
168+
""".trimIndent()
169+
155170
@Test
156171
fun `schema is downloaded correctly`() {
157172
TestUtils.withSimpleProject(apolloConfiguration = apolloConfiguration) { dir ->
@@ -240,4 +255,37 @@ class DownloadSchemaTests {
240255

241256
server.stop()
242257
}
258+
259+
@Test
260+
fun `when endpoint url property value is missing and the download task isn't executed, build succeeds`() {
261+
TestUtils.withSimpleProject(apolloConfiguration = apolloConfigurationWithGradleProperty) { dir ->
262+
Assert.assertEquals(TaskOutcome.SUCCESS, TestUtils.executeTask("help", dir).task(":help")?.outcome)
263+
}
264+
}
265+
266+
@Test
267+
fun `when endpoint url property value is missing and the download task is executed, build fails`() {
268+
TestUtils.withSimpleProject(apolloConfiguration = apolloConfigurationWithGradleProperty) { dir ->
269+
try {
270+
TestUtils.executeTask("downloadMockApolloSchemaFromIntrospection", dir)
271+
} catch (ex: UnexpectedBuildFailure) {
272+
Assert.assertEquals(TaskOutcome.FAILED, ex.buildResult.task(":downloadMockApolloSchemaFromIntrospection")?.outcome)
273+
Truth.assertThat(ex.buildResult.output).contains("Apollo: either endpoint (for introspection) or key (for registry) is required")
274+
}
275+
}
276+
}
277+
278+
@Test
279+
fun `when endpoint url property value is present and the download task is executed, build succeeds`() {
280+
TestUtils.withSimpleProject(apolloConfiguration = apolloConfigurationWithGradleProperty) { dir ->
281+
mockServer.enqueue(MockResponse().setBody(preIntrospectionResponse))
282+
mockServer.enqueue(MockResponse().setBody(schemaString1))
283+
284+
val buildResult =
285+
TestUtils.executeTask("downloadMockApolloSchemaFromIntrospection", dir, "-Pschema-endpoint=${mockServer.url("/").toUrl()}")
286+
287+
Assert.assertEquals(TaskOutcome.SUCCESS, buildResult.task(":downloadMockApolloSchemaFromIntrospection")?.outcome)
288+
Assert.assertEquals(schemaString1, File(dir, "src/main/graphql/com/example/schema.json").readText())
289+
}
290+
}
243291
}

0 commit comments

Comments
 (0)