From 99abec93e9551e61a4393f6fd55e99db54efd3e9 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Tue, 10 Sep 2024 13:45:00 +0800 Subject: [PATCH] http-client-java_add-cadl-ranch-tests (#4368) add cadl-ranch tests main focus is routes --- .../emitter/src/code-model-builder.ts | 7 +- .../http-client-generator-test/package.json | 1 + ...rametersQueryContinuationExplodesImpl.java | 20 ++--- ...yParametersQueryExpansionExplodesImpl.java | 20 ++--- .../_specs_/azure/core/basic/CoreTests.java | 6 ++ .../_specs_/azure/core/page/PageTests.java | 8 +- .../com/encode/numeric/StringEncodeTests.java | 3 + .../src/test/java/com/routes/RouteTests.java | 85 +++++++++++++++++++ .../conditionalrequest/ConditionalTests.java | 9 ++ 9 files changed, 133 insertions(+), 26 deletions(-) create mode 100644 packages/http-client-java/generator/http-client-generator-test/src/test/java/com/routes/RouteTests.java diff --git a/packages/http-client-java/emitter/src/code-model-builder.ts b/packages/http-client-java/emitter/src/code-model-builder.ts index a6b4255009..263da6dd15 100644 --- a/packages/http-client-java/emitter/src/code-model-builder.ts +++ b/packages/http-client-java/emitter/src/code-model-builder.ts @@ -288,7 +288,7 @@ export class CodeModelBuilder { }, language: { default: { - serializedName: arg.name, + serializedName: arg.serializedName, }, }, // TODO: deprecate this logic of string/url for x-ms-skip-url-encoding @@ -1127,6 +1127,11 @@ export class CodeModelBuilder { explode = true; break; } + + if (param.explode && !param.collectionFormat) { + style = SerializationStyle.Form; + explode = true; + } } else if (param.kind === "header") { const format = param.collectionFormat; switch (format) { diff --git a/packages/http-client-java/generator/http-client-generator-test/package.json b/packages/http-client-java/generator/http-client-generator-test/package.json index 0fe46ae64c..0e86b34aeb 100644 --- a/packages/http-client-java/generator/http-client-generator-test/package.json +++ b/packages/http-client-java/generator/http-client-generator-test/package.json @@ -3,6 +3,7 @@ "version": "0.1.0", "type": "module", "scripts": { + "clean": "rimraf ./node_modules/@typespec/http-client-java ./package-lock.json", "format": "npm run -s prettier -- --write", "check-format": "npm run prettier -- --check", "prettier": "prettier --config ./.prettierrc.yaml **/*.tsp", diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/routes/implementation/QueryParametersQueryContinuationExplodesImpl.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/routes/implementation/QueryParametersQueryContinuationExplodesImpl.java index 4302b3777a..0eb8e462cf 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/routes/implementation/QueryParametersQueryContinuationExplodesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/routes/implementation/QueryParametersQueryContinuationExplodesImpl.java @@ -84,8 +84,9 @@ Response primitiveSync(@HostParam("endpoint") String endpoint, @QueryParam @UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 }) @UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 }) @UnexpectedResponseExceptionType(HttpResponseException.class) - Mono> array(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestOptions requestOptions, Context context); + Mono> array(@HostParam("endpoint") String endpoint, + @QueryParam(value = "param", multipleQueryParams = true) List param, RequestOptions requestOptions, + Context context); @Get("/routes/query/query-continuation/explode/array?fixed=true") @ExpectedResponses({ 204 }) @@ -93,8 +94,9 @@ Mono> array(@HostParam("endpoint") String endpoint, @QueryParam(" @UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 }) @UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 }) @UnexpectedResponseExceptionType(HttpResponseException.class) - Response arraySync(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestOptions requestOptions, Context context); + Response arraySync(@HostParam("endpoint") String endpoint, + @QueryParam(value = "param", multipleQueryParams = true) List param, RequestOptions requestOptions, + Context context); @Get("/routes/query/query-continuation/explode/record?fixed=true") @ExpectedResponses({ 204 }) @@ -161,9 +163,8 @@ public Response primitiveWithResponse(String param, RequestOptions request */ @ServiceMethod(returns = ReturnType.SINGLE) public Mono> arrayWithResponseAsync(List param, RequestOptions requestOptions) { - String paramConverted = param.stream() - .map(paramItemValue -> Objects.toString(paramItemValue, "")) - .collect(Collectors.joining(",")); + List paramConverted + = param.stream().map(item -> Objects.toString(item, "")).collect(Collectors.toList()); return FluxUtil .withContext(context -> service.array(this.client.getEndpoint(), paramConverted, requestOptions, context)); } @@ -181,9 +182,8 @@ public Mono> arrayWithResponseAsync(List param, RequestOp */ @ServiceMethod(returns = ReturnType.SINGLE) public Response arrayWithResponse(List param, RequestOptions requestOptions) { - String paramConverted = param.stream() - .map(paramItemValue -> Objects.toString(paramItemValue, "")) - .collect(Collectors.joining(",")); + List paramConverted + = param.stream().map(item -> Objects.toString(item, "")).collect(Collectors.toList()); return service.arraySync(this.client.getEndpoint(), paramConverted, requestOptions, Context.NONE); } diff --git a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/routes/implementation/QueryParametersQueryExpansionExplodesImpl.java b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/routes/implementation/QueryParametersQueryExpansionExplodesImpl.java index a1f086f138..8db3d92a9f 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/routes/implementation/QueryParametersQueryExpansionExplodesImpl.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/main/java/com/routes/implementation/QueryParametersQueryExpansionExplodesImpl.java @@ -84,8 +84,9 @@ Response primitiveSync(@HostParam("endpoint") String endpoint, @QueryParam @UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 }) @UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 }) @UnexpectedResponseExceptionType(HttpResponseException.class) - Mono> array(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestOptions requestOptions, Context context); + Mono> array(@HostParam("endpoint") String endpoint, + @QueryParam(value = "param", multipleQueryParams = true) List param, RequestOptions requestOptions, + Context context); @Get("/routes/query/query-expansion/explode/array") @ExpectedResponses({ 204 }) @@ -93,8 +94,9 @@ Mono> array(@HostParam("endpoint") String endpoint, @QueryParam(" @UnexpectedResponseExceptionType(value = ResourceNotFoundException.class, code = { 404 }) @UnexpectedResponseExceptionType(value = ResourceModifiedException.class, code = { 409 }) @UnexpectedResponseExceptionType(HttpResponseException.class) - Response arraySync(@HostParam("endpoint") String endpoint, @QueryParam("param") String param, - RequestOptions requestOptions, Context context); + Response arraySync(@HostParam("endpoint") String endpoint, + @QueryParam(value = "param", multipleQueryParams = true) List param, RequestOptions requestOptions, + Context context); @Get("/routes/query/query-expansion/explode/record") @ExpectedResponses({ 204 }) @@ -161,9 +163,8 @@ public Response primitiveWithResponse(String param, RequestOptions request */ @ServiceMethod(returns = ReturnType.SINGLE) public Mono> arrayWithResponseAsync(List param, RequestOptions requestOptions) { - String paramConverted = param.stream() - .map(paramItemValue -> Objects.toString(paramItemValue, "")) - .collect(Collectors.joining(",")); + List paramConverted + = param.stream().map(item -> Objects.toString(item, "")).collect(Collectors.toList()); return FluxUtil .withContext(context -> service.array(this.client.getEndpoint(), paramConverted, requestOptions, context)); } @@ -181,9 +182,8 @@ public Mono> arrayWithResponseAsync(List param, RequestOp */ @ServiceMethod(returns = ReturnType.SINGLE) public Response arrayWithResponse(List param, RequestOptions requestOptions) { - String paramConverted = param.stream() - .map(paramItemValue -> Objects.toString(paramItemValue, "")) - .collect(Collectors.joining(",")); + List paramConverted + = param.stream().map(item -> Objects.toString(item, "")).collect(Collectors.toList()); return service.arraySync(this.client.getEndpoint(), paramConverted, requestOptions, Context.NONE); } diff --git a/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/_specs_/azure/core/basic/CoreTests.java b/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/_specs_/azure/core/basic/CoreTests.java index c632467527..593a25a17b 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/_specs_/azure/core/basic/CoreTests.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/_specs_/azure/core/basic/CoreTests.java @@ -4,6 +4,7 @@ package com._specs_.azure.core.basic; import com._specs_.azure.core.basic.models.User; +import com._specs_.azure.core.basic.models.UserList; import com.azure.core.http.HttpClient; import com.azure.core.http.rest.PagedFlux; import com.azure.core.http.rest.PagedIterable; @@ -116,6 +117,11 @@ public void testAction() { }) .expectComplete() .verify(); + + UserList userList = syncClient.exportAllUsers("json"); + Assertions.assertEquals(2, userList.getUsers().size()); + Assertions.assertEquals("Madge", userList.getUsers().get(0).getName()); + Assertions.assertEquals("John", userList.getUsers().get(1).getName()); } @Test diff --git a/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/_specs_/azure/core/page/PageTests.java b/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/_specs_/azure/core/page/PageTests.java index d0587e60b9..da0bec7e62 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/_specs_/azure/core/page/PageTests.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/_specs_/azure/core/page/PageTests.java @@ -19,9 +19,9 @@ public class PageTests { public void testListNoModel() { // verification here is that there is no Page or CustomPage class generated in models - client.listWithPage(); + client.listWithPage().stream().count(); - client.listWithCustomPageModel(); + client.listWithCustomPageModel().stream().count(); } @Test @@ -34,9 +34,7 @@ public void testListTwoModels() { } @Test - public void testPage() { - client.listWithPage().forEach(u -> {}); - + public void testPageRequestBody() { client.listWithParameters(new ListItemInputBody("Madge"), ListItemInputExtensibleEnum.SECOND).stream().count(); } } diff --git a/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/encode/numeric/StringEncodeTests.java b/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/encode/numeric/StringEncodeTests.java index 6373fb9eb8..24055f1a0b 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/encode/numeric/StringEncodeTests.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/encode/numeric/StringEncodeTests.java @@ -5,6 +5,7 @@ import com.encode.numeric.models.SafeintAsStringProperty; import com.encode.numeric.models.Uint32AsStringProperty; +import com.encode.numeric.models.Uint8AsStringProperty; import org.junit.jupiter.api.Test; public class StringEncodeTests { @@ -16,5 +17,7 @@ public void testIntEncodedAsString() { client.safeintAsString(new SafeintAsStringProperty(10000000000L)); client.uint32AsStringOptional(new Uint32AsStringProperty().setValue(1)); + + client.uint8AsString(new Uint8AsStringProperty(255)); } } diff --git a/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/routes/RouteTests.java b/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/routes/RouteTests.java new file mode 100644 index 0000000000..0c667de749 --- /dev/null +++ b/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/routes/RouteTests.java @@ -0,0 +1,85 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.routes; + +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class RouteTests { + + @Test + public void testFixed() { + new RoutesClientBuilder().buildClient().fixed(); + + new RoutesClientBuilder().buildInInterfaceClient().fixed(); + } + + @Test + public void testPath() { + var client = new RoutesClientBuilder().buildPathParametersClient(); + + client.templateOnly("a"); + + client.explicit("a"); + + client.annotationOnly("a"); + } + + @Test + public void testPathReservedExpansion() { + var client = new RoutesClientBuilder().buildPathParametersReservedExpansionClient(); + + // TODO, need enhancement in core or codegen + client.template("foo/bar baz".replace(" ", "%20")); + client.annotation("foo/bar baz".replace(" ", "%20")); + } + + @Test + public void testQuery() { + var client = new RoutesClientBuilder().buildQueryParametersClient(); + + client.templateOnly("a"); + + client.explicit("a"); + + client.annotationOnly("a"); + } + + @Test + public void testQueryExpansionStandard() { + var client = new RoutesClientBuilder().buildQueryParametersQueryExpansionStandardClient(); + + client.primitive("a"); + + client.array(List.of("a", "b")); + } + + @Test + public void testQueryExpansionContinuationStandard() { + var client = new RoutesClientBuilder().buildQueryParametersQueryContinuationStandardClient(); + + client.primitive("a"); + + client.array(List.of("a", "b")); + } + + @Test + public void testQueryExpansionExplode() { + var client = new RoutesClientBuilder().buildQueryParametersQueryExpansionExplodeClient(); + + client.primitive("a"); + +// client.array(List.of("a", "b")); + } + + @Test + public void buildQueryParametersQueryContinuationExplode() { + var client = new RoutesClientBuilder().buildQueryParametersQueryContinuationExplodeClient(); + + client.primitive("a"); + +// client.array(List.of("a", "b")); + } +} diff --git a/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/specialheaders/conditionalrequest/ConditionalTests.java b/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/specialheaders/conditionalrequest/ConditionalTests.java index d2cd0c3805..1be5fbe118 100644 --- a/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/specialheaders/conditionalrequest/ConditionalTests.java +++ b/packages/http-client-java/generator/http-client-generator-test/src/test/java/com/specialheaders/conditionalrequest/ConditionalTests.java @@ -1,15 +1,24 @@ package com.specialheaders.conditionalrequest; +import com.azure.core.util.DateTimeRfc1123; import org.junit.jupiter.api.Test; +import java.time.OffsetDateTime; + public class ConditionalTests { private final ConditionalRequestClient client = new ConditionalRequestClientBuilder().buildClient(); + private static final OffsetDateTime DATE_TIME = new DateTimeRfc1123("Fri, 26 Aug 2022 14:38:00 GMT").getDateTime(); + @Test public void testConditional() { client.postIfMatch("\"valid\""); client.postIfNoneMatch("\"invalid\""); + + client.headIfModifiedSince(DATE_TIME); + + client.postIfUnmodifiedSince(DATE_TIME); } }