Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce java 11 due to openapi generator compatibility #74

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ $ ./gradlew install
This builds and installs the program into `build/install/gradle-enterprise-api-samples`.
You can use the `build/install/gradle-enterprise-api-samples/bin/gradle-enterprise-api-samples` script to run the sample.

### Note on JDK11

Due to a change in the [openapi generator project](https://github.com/gradle/gradle-enterprise-api-samples/pull/74), JDK 11 is required to build the samples. The clients still work using java 8
## How to run

A Gradle Enterprise access key with the “Export build data via the API” permission is required.
Expand Down Expand Up @@ -55,7 +58,6 @@ The sample code can be found [here](https://github.com/gradle/gradle-enterprise-

The Gradle Enterprise API manual and reference documentation for each version of the API can be found [here](https://docs.gradle.com/enterprise/api-manual).


## License

The Gradle Enterprise API Samples project is open-source software released under the [Apache 2.0 License][apache-license].
Expand Down
31 changes: 26 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.distsDirectory

group = "com.gradle.enterprise.api"
description = "Gradle Enterprise API sample"

Expand All @@ -18,7 +20,7 @@ application {

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
languageVersion.set(JavaLanguageVersion.of(11))
}
}

Expand All @@ -32,18 +34,25 @@ dependencies {
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.15.2")
implementation("com.google.code.findbugs:jsr305:3.0.2")
implementation("org.apache.httpcomponents.client5:httpclient5:5.2.1")

testImplementation("org.mock-server:mockserver-netty:5.15.0")
testImplementation("org.junit.jupiter:junit-jupiter:5.7.1")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")

}

val gradleEnterpriseVersion = "2023.2" // Must be later than 2022.1
val baseApiUrl = providers.gradleProperty("apiManualUrl").orElse("https://docs.gradle.com/enterprise/api-manual/ref/")

val apiSpecificationFileGradleProperty = providers.gradleProperty("apiSpecificationFile")
val apiSpecificationURL = baseApiUrl.map { "${it}gradle-enterprise-${gradleEnterpriseVersion}-api.yaml" }
val apiSpecificationFile = apiSpecificationFileGradleProperty
.map { s -> file(s) }
.orElse(objects.property(File::class)
.convention(provider {
resources.text.fromUri("${baseApiUrl.get()}gradle-enterprise-${gradleEnterpriseVersion}-api.yaml").asFile()
})
.orElse(
objects.property(File::class)
.convention(provider {
resources.text.fromUri(apiSpecificationURL).asFile()
})
).map { file -> file.absolutePath }

val basePackageName = "com.gradle.enterprise.api"
Expand Down Expand Up @@ -73,6 +82,18 @@ openApiGenerate {
))
}

tasks.test {
useJUnitPlatform()

apiSpecificationURL.orNull.let { systemProperties["ge.api.url"] = it }

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(8))
}
}
}

sourceSets {
main {
java {
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/com/gradle/enterprise/api/SimpleApiClientTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.gradle.enterprise.api;

import com.gradle.enterprise.api.client.ApiClient;
import com.gradle.enterprise.api.client.ApiException;
import com.gradle.enterprise.api.client.ServerConfiguration;
import com.gradle.enterprise.api.model.Build;
import com.gradle.enterprise.api.model.BuildsQuery;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockserver.configuration.Configuration;
import org.mockserver.integration.ClientAndServer;
import org.mockserver.logging.MockServerLogger;
import org.mockserver.mock.Expectation;
import org.mockserver.openapi.OpenAPIConverter;

import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertFalse;

public class SimpleApiClientTest {

private static final String gradleEnterpriseAPIYamlUrl = System.getProperty("ge.api.url");

private ClientAndServer mockServer;

@BeforeEach
public void setup() {
Configuration configuration = Configuration.configuration();
List<Expectation> openApiExpectations = new OpenAPIConverter(new MockServerLogger()).buildExpectations(gradleEnterpriseAPIYamlUrl, null);
mockServer = ClientAndServer.startClientAndServer(configuration, Collections.singletonList(19234));
mockServer.upsert(openApiExpectations.toArray(new Expectation[0]));
}

@Test
public void testSimpleAPICall() throws ApiException {
InetSocketAddress remoteAddress = mockServer.remoteAddress();

ApiClient apiClient = new ApiClient();
apiClient.setServers(Collections.singletonList(new ServerConfiguration(
"http://" + remoteAddress.getHostName() + ":" + remoteAddress.getPort(), "mockServer", Collections.emptyMap()
)));
BuildsApi buildsApi = new BuildsApi(apiClient);
apiClient.addDefaultHeader("Authorization", "Bearer XYZ");

List<Build> builds = buildsApi.getBuilds(new BuildsQuery());

assertFalse(builds.isEmpty());
}
}