|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2020 Netflix, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package com.netflix.graphql.dgs |
| 20 | + |
| 21 | +import org.assertj.core.api.Assertions.assertThat |
| 22 | +import org.gradle.testkit.runner.GradleRunner |
| 23 | +import org.junit.jupiter.api.Test |
| 24 | +import org.junit.jupiter.api.io.TempDir |
| 25 | +import java.io.File |
| 26 | +import java.net.URI |
| 27 | +import java.nio.file.* |
| 28 | +import kotlin.io.path.createDirectories |
| 29 | +import kotlin.io.path.writeText |
| 30 | + |
| 31 | +class CodegenGradlePluginConfigurationCacheTest { |
| 32 | + |
| 33 | + @TempDir |
| 34 | + lateinit var projectDir: File |
| 35 | + |
| 36 | + @Test |
| 37 | + fun `Test if configuration cache can be reused successfully`() { |
| 38 | + prepareBuildGraphQLSchema( |
| 39 | + """ |
| 40 | + type Query { |
| 41 | + test: String |
| 42 | + } |
| 43 | + """.trimMargin() |
| 44 | + ) |
| 45 | + |
| 46 | + prepareBuildGradleFile( |
| 47 | + """ |
| 48 | + plugins { |
| 49 | + id 'java' |
| 50 | + id 'com.netflix.dgs.codegen' |
| 51 | + } |
| 52 | + |
| 53 | + repositories { |
| 54 | + mavenCentral() |
| 55 | + } |
| 56 | + sourceCompatibility = 1.8 |
| 57 | + targetCompatibility = 1.8 |
| 58 | + generateJava { |
| 59 | + packageName = 'com.netflix.testproject.graphql' |
| 60 | + generateClient = true |
| 61 | + } |
| 62 | + // Need to disable the core conventions since the artifacts are not yet visible. |
| 63 | + codegen.clientCoreConventionsEnabled = false |
| 64 | + """.trimMargin() |
| 65 | + ) |
| 66 | + |
| 67 | + val runner = GradleRunner.create() |
| 68 | + .withProjectDir(projectDir) |
| 69 | + .withPluginClasspath() |
| 70 | + .withDebug(true) |
| 71 | + .withArguments( |
| 72 | + "--stacktrace", |
| 73 | + "--info", |
| 74 | + "--configuration-cache", |
| 75 | + "generateJava", |
| 76 | + "build" |
| 77 | + ) |
| 78 | + |
| 79 | + runner.build() // First build, warm up cache |
| 80 | + val result = runner.build() // Second build, should use cache |
| 81 | + |
| 82 | + assertThat(result.output).contains("Reusing configuration cache.") |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + fun `Test if configuration cache can be reused successfully with external schemas`() { |
| 87 | + prepareSchemaJar( |
| 88 | + """ |
| 89 | + type Query { |
| 90 | + test: String |
| 91 | + } |
| 92 | + """.trimMargin() |
| 93 | + ) |
| 94 | + |
| 95 | + prepareBuildGradleFile( |
| 96 | + """ |
| 97 | + plugins { |
| 98 | + id 'java' |
| 99 | + id 'com.netflix.dgs.codegen' |
| 100 | + } |
| 101 | + |
| 102 | + repositories { |
| 103 | + mavenCentral() |
| 104 | + } |
| 105 | + |
| 106 | + dependencies { |
| 107 | + // other dependencies |
| 108 | + dgsCodegen files("$projectDir/schema.jar") |
| 109 | + } |
| 110 | + |
| 111 | + sourceCompatibility = 1.8 |
| 112 | + targetCompatibility = 1.8 |
| 113 | + generateJava { |
| 114 | + packageName = 'com.netflix.testproject.graphql' |
| 115 | + generateClient = true |
| 116 | + } |
| 117 | + // Need to disable the core conventions since the artifacts are not yet visible. |
| 118 | + codegen.clientCoreConventionsEnabled = false |
| 119 | + """.trimMargin() |
| 120 | + ) |
| 121 | + |
| 122 | + val runner = GradleRunner.create() |
| 123 | + .withProjectDir(projectDir) |
| 124 | + .withPluginClasspath() |
| 125 | + .withDebug(true) |
| 126 | + .withArguments( |
| 127 | + "--stacktrace", |
| 128 | + "--info", |
| 129 | + "--configuration-cache", |
| 130 | + "generateJava", |
| 131 | + "build" |
| 132 | + ) |
| 133 | + |
| 134 | + runner.build() // First build, warm up cache |
| 135 | + val result = runner.build() // Second build, should use cache |
| 136 | + |
| 137 | + assertThat(result.output).contains("Reusing configuration cache.") |
| 138 | + } |
| 139 | + |
| 140 | + private fun prepareSchemaJar(content: String) { |
| 141 | + val env = mapOf("create" to "true") |
| 142 | + val uri: URI = URI.create("jar:file:$projectDir/schema.jar") |
| 143 | + FileSystems.newFileSystem(uri, env).use { zipfs -> |
| 144 | + val pathInZipfile: Path = zipfs.getPath("/schema/schema.graphql") |
| 145 | + pathInZipfile.parent.createDirectories() |
| 146 | + pathInZipfile.writeText(content) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private fun prepareBuildGradleFile(content: String) { |
| 151 | + writeProjectFile("build.gradle", content) |
| 152 | + } |
| 153 | + |
| 154 | + private fun prepareBuildGraphQLSchema(content: String) { |
| 155 | + writeProjectFile("src/main/resources/schema/schema.graphql", content) |
| 156 | + } |
| 157 | + |
| 158 | + private fun writeProjectFile(relativePath: String, content: String) { |
| 159 | + val file = File(projectDir, relativePath) |
| 160 | + file.parentFile.mkdirs() |
| 161 | + file.writeText(content) |
| 162 | + } |
| 163 | +} |
0 commit comments