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

[WIP] http-client-java, e2e test, use clientcore #5437

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ packages/http-client-csharp/generator/TestProjects/**/tspCodeModel.json

# auto generated api view properties files
packages/http-client-java/generator/http-client-generator-test/src/main/**/*.json
packages/http-client-java/generator/http-client-generator-standard-test/src/main/**/*.json
1 change: 1 addition & 0 deletions cspell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ ignorePaths:
- "**/ThirdPartyNotices.txt"
- packages/compiler/test/formatter/scenarios/**
- packages/http-client-java/generator/http-client-generator-test/**
- packages/http-client-java/generator/http-client-generator-standard-test/**
- pnpm-lock.yaml
- "**/*.mp4"
- .git/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public static void getAsyncSyncClients(Client client, ServiceClient serviceClien
}
}

final int count = serviceClient.getMethodGroupClients().size() + asyncClients.size();
final int count
= serviceClient.getMethodGroupClients().size() + Math.max(asyncClients.size(), syncClients.size());
for (MethodGroupClient methodGroupClient : serviceClient.getMethodGroupClients()) {
AsyncSyncClient.Builder builder = new AsyncSyncClient.Builder().packageName(packageName)
.serviceClient(serviceClient)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
# Use case:
#
# The purpose of this script is to compact the steps required to regenerate TypeSpec into a single script.
#
param (
[int] $Parallelization = [Environment]::ProcessorCount
)


$ExitCode = 0

if ($Parallelization -lt 1) {
$Parallelization = 1
}

Write-Host "Parallelization: $Parallelization"


$generateScript = {
$tspFile = $_

$tspClientFile = $tspFile -replace 'main.tsp', 'client.tsp'
if (($tspClientFile -match 'client.tsp$') -and (Test-Path $tspClientFile)) {
$tspFile = $tspClientFile
}

# With TypeSpec code generation being parallelized, we need to make sure that the output directory is unique
# for each test run. We do this by appending a random number to the output directory.
# Without this, we could have multiple runs trying to write to the same directory which introduces race conditions.
$tspOptions = "--option ""@typespec/http-client-java.emitter-output-dir={project-root}/tsp-output/$(Get-Random)"""
if ($tspFile -match "type[\\/]enum[\\/]extensible[\\/]") {
# override namespace for reserved keyword "enum"
$tspOptions += " --option ""@typespec/http-client-java.namespace=type.enums.extensible"""
} elseif ($tspFile -match "type[\\/]enum[\\/]fixed[\\/]") {
# override namespace for reserved keyword "enum"
$tspOptions += " --option ""@typespec/http-client-java.namespace=type.enums.fixed"""
} elseif ($tspFile -match "type[\\/]array" -or $tspFile -match "type[\\/]dictionary") {
# TODO https://github.com/Azure/autorest.java/issues/2964
# also serve as a test for "use-object-for-unknown" emitter option
$tspOptions += " --option ""@typespec/http-client-java.use-object-for-unknown=true"""
}

$tspTrace = "--trace import-resolution --trace projection --trace http-client-java"
$tspCommand = "npx --no-install tsp compile $tspFile $tspOptions $tspTrace"

$timer = [Diagnostics.Stopwatch]::StartNew()
$generateOutput = Invoke-Expression $tspCommand
$timer.Stop()

$global:ExitCode = $global:ExitCode -bor $LASTEXITCODE

if ($LASTEXITCODE -ne 0) {
Write-Host "
========================
$tspCommand
========================
FAILED (Time elapsed: $($timer.ToString()))
$([String]::Join("`n", $generateOutput))
"
} else {
Write-Host "
========================
$tspCommand
========================
SUCCEEDED (Time elapsed: $($timer.ToString()))
"
}

if ($global:ExitCode -ne 0) {
exit $global:ExitCode
}
}

./Setup.ps1

if (Test-Path ./src/main) {
Remove-Item ./src/main -Recurse -Force
}
if (Test-Path ./src/samples) {
Remove-Item ./src/samples -Recurse -Force
}
if (Test-Path ./tsp-output) {
Remove-Item ./tsp-output -Recurse -Force
}

# generate for http-specs/azure-http-specs test sources
Copy-Item -Path node_modules/@typespec/http-specs/specs -Destination ./ -Recurse -Force
# remove xml tests, emitter has not supported xml model
Remove-Item ./specs/payload/xml -Recurse -Force

# ServiceVersion not in core
Remove-Item ./specs/versioning -Recurse -Force
weidongxu-microsoft marked this conversation as resolved.
Show resolved Hide resolved
Remove-Item ./specs/server/path -Recurse -Force
Remove-Item ./specs/server/versions -Recurse -Force
# TokenCredential not in core
Remove-Item ./specs/authentication/oauth2 -Recurse -Force
Remove-Item ./specs/authentication/union -Recurse -Force
# Base64Url not in core
Remove-Item ./specs/encode/bytes -Recurse -Force
# DateTimeRfc1123 is private in beta.1, should now be public in main
Remove-Item ./specs/encode/datetime -Recurse -Force
Remove-Item ./specs/special-headers -Recurse -Force
# JacksonAdapter not in core
Remove-Item ./specs/encode/duration -Recurse -Force

$job = (Get-ChildItem ./specs -Include "main.tsp","old.tsp" -File -Recurse) | ForEach-Object -Parallel $generateScript -ThrottleLimit $Parallelization -AsJob

$job | Wait-Job -Timeout 1200
$job | Receive-Job

Remove-Item ./specs -Recurse -Force

Copy-Item -Path ./tsp-output/*/src -Destination ./ -Recurse -Force -Exclude @("module-info.java")

Remove-Item ./tsp-output -Recurse -Force

if (Test-Path ./src/main/resources/META-INF/client-structure-service_apiview_properties.json) {
# client structure is generated from multiple client.tsp files and the last one to execute overwrites
# the api view properties file. Because the tests run in parallel, the order is not guaranteed. This
# causes git diff check to fail as the checked in file is not the same as the generated one.
Remove-Item ./src/main/resources/META-INF/client-structure-service_apiview_properties.json -Force
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# re-build http-client-java
Set-Location (Resolve-Path (Join-Path $PSScriptRoot '..' '..'))

./Setup.ps1

Set-Location $PSScriptRoot

npm run clean && npm install
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@typespec/http-client-java-standard-tests",
"version": "0.1.0",
"type": "module",
"scripts": {
"clean": "rimraf ./node_modules/@typespec/http-client-java ./package-lock.json ./tsp-output",
"format": "npm run -s prettier -- --write",
"check-format": "npm run prettier -- --check",
"prettier": "prettier --config ./.prettierrc.yaml **/*.tsp",
"spector-serve": "tsp-spector serve ./node_modules/@typespec/http-specs/specs --coverageFile ./tsp-spector-coverage-java-standard.json",
"spector-start": "tsp-spector server start ./node_modules/@typespec/http-specs/specs --coverageFile ./tsp-spector-coverage-java-standard.json",
"spector-stop": "tsp-spector server stop"
},
"dependencies": {
"@typespec/http-specs": "0.1.0-alpha.5",
"@typespec/http-client-java": "file:../../typespec-http-client-java-0.1.5.tgz",
"@typespec/http-client-java-tests": "file:"
},
"overrides": {
"@typespec/compiler": "~0.63.0",
"@typespec/http": "~0.63.0",
"@typespec/rest": "~0.63.0",
"@typespec/versioning": "~0.63.0",
"@typespec/openapi": "~0.63.0",
"@typespec/xml": "~0.63.0",
"@azure-tools/typespec-azure-core": "~0.49.0",
"@azure-tools/typespec-client-generator-core": "~0.49.0",
"@azure-tools/typespec-azure-resource-manager": "~0.49.0",
"@azure-tools/typespec-autorest": "~0.49.0"
},
"private": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.microsoft.typespec</groupId>
<artifactId>typespec-java-generator</artifactId>
<version>1.0.0-beta.1</version>
</parent>

<groupId>com.microsoft.typespec</groupId>
<artifactId>http-client-generator-standard-test</artifactId>
<packaging>jar</packaging>

<name>http-client-generator-test</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spotless.config.path>../</spotless.config.path>
</properties>

<dependencies>
<dependency>
<groupId>io.clientcore</groupId>
<artifactId>core</artifactId>
<version>1.0.0-beta.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<version>3.4.41</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.11.0</version>
<scope>test</scope>
</dependency>
<!-- bytebuddy dependencies are required for mockito 4.11.0 to work with Java 21. Mockito 4.11.0 is the last release -->
<!-- of Mockito supporting Java 8 as a baseline. -->
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.15.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.15.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Code generated by Microsoft (R) TypeSpec Code Generator.

package authentication.apikey;

import authentication.apikey.implementation.ApiKeyClientImpl;
import io.clientcore.core.annotation.Metadata;
import io.clientcore.core.annotation.ServiceClient;
import io.clientcore.core.http.exception.HttpResponseException;
import io.clientcore.core.http.models.RequestOptions;
import io.clientcore.core.http.models.Response;

/**
* Initializes a new instance of the synchronous ApiKeyClient type.
*/
@ServiceClient(builder = ApiKeyClientBuilder.class)
public final class ApiKeyClient {
@Metadata(generated = true)
private final ApiKeyClientImpl serviceClient;

/**
* Initializes an instance of ApiKeyClient class.
*
* @param serviceClient the service client implementation.
*/
@Metadata(generated = true)
ApiKeyClient(ApiKeyClientImpl serviceClient) {
this.serviceClient = serviceClient;
}

/**
* Check whether client is authenticated.
*
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
* @throws HttpResponseException thrown if the service returns an error.
* @return the response.
*/
@Metadata(generated = true)
public Response<Void> validWithResponse(RequestOptions requestOptions) {
return this.serviceClient.validWithResponse(requestOptions);
}

/**
* Check whether client is authenticated.
*
* @param requestOptions The options to configure the HTTP request before HTTP client sends it.
* @throws HttpResponseException thrown if the service returns an error.
* @return the response.
*/
@Metadata(generated = true)
public Response<Void> invalidWithResponse(RequestOptions requestOptions) {
return this.serviceClient.invalidWithResponse(requestOptions);
}

/**
* Check whether client is authenticated.
*
* @throws HttpResponseException thrown if the service returns an error.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
*/
@Metadata(generated = true)
public void valid() {
// Generated convenience method for validWithResponse
RequestOptions requestOptions = new RequestOptions();
validWithResponse(requestOptions).getValue();
}

/**
* Check whether client is authenticated.
*
* @throws HttpResponseException thrown if the service returns an error.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
*/
@Metadata(generated = true)
public void invalid() {
// Generated convenience method for invalidWithResponse
RequestOptions requestOptions = new RequestOptions();
invalidWithResponse(requestOptions).getValue();
}
}
Loading
Loading