From 4be5cf61b096263ad258dc6cc1d2efdcdda42066 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Wed, 2 Nov 2022 11:41:46 +0800 Subject: [PATCH 1/8] refactor --- .../azure/autorest/model/clientmodel/ClientBuilder.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/javagen/src/main/java/com/azure/autorest/model/clientmodel/ClientBuilder.java b/javagen/src/main/java/com/azure/autorest/model/clientmodel/ClientBuilder.java index 1ca3a3e315..ec6393214e 100644 --- a/javagen/src/main/java/com/azure/autorest/model/clientmodel/ClientBuilder.java +++ b/javagen/src/main/java/com/azure/autorest/model/clientmodel/ClientBuilder.java @@ -6,7 +6,6 @@ import com.azure.autorest.extension.base.plugin.JavaSettings; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.Set; @@ -23,14 +22,6 @@ public class ClientBuilder { private final List asyncClients; private final List builderTraits = new ArrayList<>(); - public ClientBuilder(String packageName, String className, ServiceClient serviceClient) { - this.packageName = Objects.requireNonNull(packageName); - this.className = Objects.requireNonNull(className); - this.serviceClient = Objects.requireNonNull(serviceClient); - this.syncClients = Collections.emptyList(); - this.asyncClients = Collections.emptyList(); - } - public ClientBuilder(String packageName, String className, ServiceClient serviceClient, List syncClients, List asyncClients) { From e2a0f02ad44e112fad52fed9287ce2b391b00a46 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Wed, 2 Nov 2022 15:03:14 +0800 Subject: [PATCH 2/8] refactor --- .../com/azure/autorest/fluent/FluentGen.java | 4 +-- .../autorest/fluent/mapper/FluentMapper.java | 6 ++-- ...estParser.java => MockUnitTestParser.java} | 32 +++++++++---------- .../model/clientmodel/FluentClient.java | 8 ++--- ...est.java => FluentMethodMockUnitTest.java} | 6 ++-- .../model/javamodel/FluentJavaPackage.java | 4 +-- .../template/FluentMethodTestTemplate.java | 30 ++++++++--------- 7 files changed, 45 insertions(+), 45 deletions(-) rename fluentgen/src/main/java/com/azure/autorest/fluent/mapper/{UnitTestParser.java => MockUnitTestParser.java} (88%) rename fluentgen/src/main/java/com/azure/autorest/fluent/model/clientmodel/examplemodel/{FluentMethodUnitTest.java => FluentMethodMockUnitTest.java} (97%) diff --git a/fluentgen/src/main/java/com/azure/autorest/fluent/FluentGen.java b/fluentgen/src/main/java/com/azure/autorest/fluent/FluentGen.java index b459ebd186..950a272a39 100755 --- a/fluentgen/src/main/java/com/azure/autorest/fluent/FluentGen.java +++ b/fluentgen/src/main/java/com/azure/autorest/fluent/FluentGen.java @@ -19,7 +19,7 @@ import com.azure.autorest.fluent.model.clientmodel.FluentResourceCollection; import com.azure.autorest.fluent.model.clientmodel.FluentResourceModel; import com.azure.autorest.fluent.model.clientmodel.FluentStatic; -import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentMethodUnitTest; +import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentMethodMockUnitTest; import com.azure.autorest.fluent.model.javamodel.FluentJavaPackage; import com.azure.autorest.fluent.model.projectmodel.FluentProject; import com.azure.autorest.fluent.util.FluentUtils; @@ -358,7 +358,7 @@ FluentClient handleFluentLite(CodeModel codeModel, Client client, FluentJavaPack } // Unit tests for APIs - for (FluentMethodUnitTest unitTest : fluentClient.getUnitTests()) { + for (FluentMethodMockUnitTest unitTest : fluentClient.getMockUnitTests()) { javaPackage.addOperationUnitTest(unitTest); } // enable inline mock maker for final class/method diff --git a/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/FluentMapper.java b/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/FluentMapper.java index a4ee475100..e83439c42d 100644 --- a/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/FluentMapper.java +++ b/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/FluentMapper.java @@ -76,10 +76,10 @@ public FluentClient map(CodeModel codeModel, Client client) { .collect(Collectors.toList())); // mock API tests - UnitTestParser unitTestParser = new UnitTestParser(); - fluentClient.getUnitTests().addAll( + MockUnitTestParser mockUnitTestParser = new MockUnitTestParser(); + fluentClient.getMockUnitTests().addAll( fluentClient.getResourceCollections().stream() - .flatMap(rc -> unitTestParser.parseResourceCollectionForUnitTest(rc).stream()) + .flatMap(rc -> mockUnitTestParser.parseResourceCollectionForUnitTest(rc).stream()) .collect(Collectors.toList())); } diff --git a/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/UnitTestParser.java b/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java similarity index 88% rename from fluentgen/src/main/java/com/azure/autorest/fluent/mapper/UnitTestParser.java rename to fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java index 41f69b5887..ca0a4d662a 100644 --- a/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/UnitTestParser.java +++ b/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java @@ -10,7 +10,7 @@ import com.azure.autorest.fluent.model.clientmodel.FluentResourceCollection; import com.azure.autorest.fluent.model.clientmodel.MethodParameter; import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentCollectionMethodExample; -import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentMethodUnitTest; +import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentMethodMockUnitTest; import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentResourceCreateExample; import com.azure.autorest.fluent.model.clientmodel.fluentmodel.create.ResourceCreate; import com.azure.autorest.model.clientmodel.ClassType; @@ -32,30 +32,30 @@ import java.util.List; import java.util.Map; -public class UnitTestParser extends ExampleParser { +public class MockUnitTestParser extends ExampleParser { - private static final Logger LOGGER = new PluginLogger(FluentGen.getPluginInstance(), UnitTestParser.class); + private static final Logger LOGGER = new PluginLogger(FluentGen.getPluginInstance(), MockUnitTestParser.class); - public List parseResourceCollectionForUnitTest(FluentResourceCollection resourceCollection) { - List fluentMethodUnitTests = new ArrayList<>(); + public List parseResourceCollectionForUnitTest(FluentResourceCollection resourceCollection) { + List fluentMethodMockUnitTests = new ArrayList<>(); resourceCollection.getMethodsForTemplate().forEach(m -> { - FluentMethodUnitTest example = parseMethod(resourceCollection, m); + FluentMethodMockUnitTest example = parseMethod(resourceCollection, m); if (example != null) { - fluentMethodUnitTests.add(example); + fluentMethodMockUnitTests.add(example); } }); resourceCollection.getResourceCreates().forEach(rc -> { - FluentMethodUnitTest example = parseResourceCreate(resourceCollection, rc); + FluentMethodMockUnitTest example = parseResourceCreate(resourceCollection, rc); if (example != null) { - fluentMethodUnitTests.add(example); + fluentMethodMockUnitTests.add(example); } }); - return fluentMethodUnitTests; + return fluentMethodMockUnitTests; } - private static FluentMethodUnitTest parseResourceCreate(FluentResourceCollection collection, ResourceCreate resourceCreate) { - FluentMethodUnitTest unitTest = null; + private static FluentMethodMockUnitTest parseResourceCreate(FluentResourceCollection collection, ResourceCreate resourceCreate) { + FluentMethodMockUnitTest unitTest = null; try { List collectionMethods = resourceCreate.getMethodReferences(); @@ -69,7 +69,7 @@ private static FluentMethodUnitTest parseResourceCreate(FluentResourceCollection parseResourceCreate(collection, resourceCreate, proxyMethodExample, methodParameters, requestBodyParameter); ResponseInfo responseInfo = createProxyMethodExampleResponse(clientMethod); - unitTest = new FluentMethodUnitTest(resourceCreateExample, collection, collectionMethod, + unitTest = new FluentMethodMockUnitTest(resourceCreateExample, collection, collectionMethod, responseInfo.responseExample, responseInfo.verificationObjectName, responseInfo.verificationNode); break; @@ -81,8 +81,8 @@ private static FluentMethodUnitTest parseResourceCreate(FluentResourceCollection return unitTest; } - private static FluentMethodUnitTest parseMethod(FluentResourceCollection collection, FluentCollectionMethod collectionMethod) { - FluentMethodUnitTest unitTest = null; + private static FluentMethodMockUnitTest parseMethod(FluentResourceCollection collection, FluentCollectionMethod collectionMethod) { + FluentMethodMockUnitTest unitTest = null; try { ClientMethod clientMethod = collectionMethod.getInnerClientMethod(); @@ -93,7 +93,7 @@ private static FluentMethodUnitTest parseMethod(FluentResourceCollection collect parseMethodForExample(collection, collectionMethod, methodParameters, proxyMethodExample.getName(), proxyMethodExample); ResponseInfo responseInfo = createProxyMethodExampleResponse(clientMethod); - unitTest = new FluentMethodUnitTest(collectionMethodExample, collection, collectionMethod, + unitTest = new FluentMethodMockUnitTest(collectionMethodExample, collection, collectionMethod, responseInfo.responseExample, responseInfo.verificationObjectName, responseInfo.verificationNode); } } catch (PossibleCredentialException e) { diff --git a/fluentgen/src/main/java/com/azure/autorest/fluent/model/clientmodel/FluentClient.java b/fluentgen/src/main/java/com/azure/autorest/fluent/model/clientmodel/FluentClient.java index e2b13a1fab..28ced68ad9 100644 --- a/fluentgen/src/main/java/com/azure/autorest/fluent/model/clientmodel/FluentClient.java +++ b/fluentgen/src/main/java/com/azure/autorest/fluent/model/clientmodel/FluentClient.java @@ -3,7 +3,7 @@ package com.azure.autorest.fluent.model.clientmodel; -import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentMethodUnitTest; +import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentMethodMockUnitTest; import com.azure.autorest.model.clientmodel.Client; import com.azure.autorest.model.clientmodel.ModuleInfo; @@ -26,7 +26,7 @@ public class FluentClient { private final List resourceCollections = new ArrayList<>(); private final List examples = new ArrayList<>(); - private final List unitTests = new ArrayList<>(); + private final List mockUnitTests = new ArrayList<>(); private final List liveTests = new ArrayList<>(); @@ -66,8 +66,8 @@ public List getExamples() { return examples; } - public List getUnitTests() { - return unitTests; + public List getMockUnitTests() { + return mockUnitTests; } public List getLiveTests() { diff --git a/fluentgen/src/main/java/com/azure/autorest/fluent/model/clientmodel/examplemodel/FluentMethodUnitTest.java b/fluentgen/src/main/java/com/azure/autorest/fluent/model/clientmodel/examplemodel/FluentMethodMockUnitTest.java similarity index 97% rename from fluentgen/src/main/java/com/azure/autorest/fluent/model/clientmodel/examplemodel/FluentMethodUnitTest.java rename to fluentgen/src/main/java/com/azure/autorest/fluent/model/clientmodel/examplemodel/FluentMethodMockUnitTest.java index d1a1247848..0170f2e7de 100644 --- a/fluentgen/src/main/java/com/azure/autorest/fluent/model/clientmodel/examplemodel/FluentMethodUnitTest.java +++ b/fluentgen/src/main/java/com/azure/autorest/fluent/model/clientmodel/examplemodel/FluentMethodMockUnitTest.java @@ -8,7 +8,7 @@ import com.azure.autorest.model.clientmodel.ProxyMethodExample; import com.azure.autorest.model.clientmodel.examplemodel.ExampleNode; -public class FluentMethodUnitTest { +public class FluentMethodMockUnitTest { // method with mock data private final FluentMethodExample fluentMethodExample; @@ -23,7 +23,7 @@ public class FluentMethodUnitTest { private final String responseVerificationVariableName; private final ExampleNode responseVerificationNode; - public FluentMethodUnitTest( + public FluentMethodMockUnitTest( FluentMethodExample fluentMethodExample, FluentResourceCollection resourceCollection, FluentCollectionMethod fluentCollectionMethod, ProxyMethodExample.Response response, @@ -38,7 +38,7 @@ public FluentMethodUnitTest( this.responseVerificationNode = responseVerificationNode; } - public FluentMethodUnitTest( + public FluentMethodMockUnitTest( FluentResourceCreateExample fluentResourceCreateExample, FluentResourceCollection resourceCollection, FluentCollectionMethod collectionMethod, ProxyMethodExample.Response response, diff --git a/fluentgen/src/main/java/com/azure/autorest/fluent/model/javamodel/FluentJavaPackage.java b/fluentgen/src/main/java/com/azure/autorest/fluent/model/javamodel/FluentJavaPackage.java index 2e51bbf121..88db5f6c00 100644 --- a/fluentgen/src/main/java/com/azure/autorest/fluent/model/javamodel/FluentJavaPackage.java +++ b/fluentgen/src/main/java/com/azure/autorest/fluent/model/javamodel/FluentJavaPackage.java @@ -11,7 +11,7 @@ import com.azure.autorest.fluent.model.clientmodel.FluentResourceCollection; import com.azure.autorest.fluent.model.clientmodel.FluentResourceModel; import com.azure.autorest.fluent.model.clientmodel.ModelNaming; -import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentMethodUnitTest; +import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentMethodMockUnitTest; import com.azure.autorest.fluent.model.projectmodel.Changelog; import com.azure.autorest.fluent.model.projectmodel.FluentProject; import com.azure.autorest.fluent.template.FluentMethodTestTemplate; @@ -110,7 +110,7 @@ public final JavaFile addSample(FluentExample example) { return javaFile; } - public void addOperationUnitTest(FluentMethodUnitTest unitTest) { + public void addOperationUnitTest(FluentMethodMockUnitTest unitTest) { String className = unitTest.getResourceCollection().getInterfaceType().getName() + CodeNamer.toPascalCase(unitTest.getCollectionMethod().getMethodName()) diff --git a/fluentgen/src/main/java/com/azure/autorest/fluent/template/FluentMethodTestTemplate.java b/fluentgen/src/main/java/com/azure/autorest/fluent/template/FluentMethodTestTemplate.java index 9e67a12de4..ca5897e68c 100644 --- a/fluentgen/src/main/java/com/azure/autorest/fluent/template/FluentMethodTestTemplate.java +++ b/fluentgen/src/main/java/com/azure/autorest/fluent/template/FluentMethodTestTemplate.java @@ -3,7 +3,7 @@ package com.azure.autorest.fluent.template; -import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentMethodUnitTest; +import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentMethodMockUnitTest; import com.azure.autorest.model.clientmodel.ClassType; import com.azure.autorest.model.clientmodel.ClientMethod; import com.azure.autorest.model.clientmodel.IType; @@ -35,11 +35,11 @@ public class FluentMethodTestTemplate implements IJavaTemplate Date: Wed, 2 Nov 2022 15:08:29 +0800 Subject: [PATCH 3/8] refactor --- .../autorest/fluent/mapper/ExampleParser.java | 34 ++++--------------- .../autorest/fluent/util/FluentUtils.java | 21 ++++++++++++ 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/ExampleParser.java b/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/ExampleParser.java index 403ed99ba3..e05efc28e6 100644 --- a/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/ExampleParser.java +++ b/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/ExampleParser.java @@ -38,7 +38,6 @@ import com.azure.autorest.model.clientmodel.ClassType; import com.azure.autorest.model.clientmodel.ClientMethod; import com.azure.autorest.model.clientmodel.ClientMethodParameter; -import com.azure.autorest.model.clientmodel.ClientMethodType; import com.azure.autorest.model.clientmodel.ClientModel; import com.azure.autorest.model.clientmodel.IType; import com.azure.autorest.model.clientmodel.ListType; @@ -163,7 +162,7 @@ private static List parseMethod(FluentResourceCol List ret = null; ClientMethod clientMethod = collectionMethod.getInnerClientMethod(); - if (needToGenerateExample(clientMethod)) { + if (FluentUtils.validToGenerateExample(clientMethod)) { ret = new ArrayList<>(); List methodParameters = getParameters(clientMethod); @@ -181,7 +180,7 @@ private static List parseMethod(FluentResourceCol private static List parseMethod(MethodGroupClient methodGroup, ClientMethod clientMethod) { List ret = null; - if (needToGenerateExample(clientMethod)) { + if (FluentUtils.validToGenerateExample(clientMethod)) { ret = new ArrayList<>(); List methodParameters = getParameters(clientMethod); @@ -208,7 +207,7 @@ protected static FluentCollectionMethodExample parseMethodForExample(FluentResou } public static FluentCollectionMethodExample parseMethodExample(FluentResourceCollection resourceCollection, Collection collectionMethods, ProxyMethodExample example) { - FluentCollectionMethod collectionMethod = collectionMethods.stream().filter(method -> requiresExample(method.getInnerClientMethod())).findFirst().get(); + FluentCollectionMethod collectionMethod = collectionMethods.stream().filter(method -> FluentUtils.requiresExample(method.getInnerClientMethod())).findFirst().get(); return parseMethodForExample(resourceCollection, collectionMethod, getParameters(collectionMethod.getInnerClientMethod()), example.getName(), example); } @@ -246,7 +245,7 @@ private static List parseResourceCreate(FluentResou List collectionMethods = resourceCreate.getMethodReferences(); for (FluentCollectionMethod collectionMethod : collectionMethods) { ClientMethod clientMethod = collectionMethod.getInnerClientMethod(); - if (needToGenerateExample(clientMethod)) { + if (FluentUtils.validToGenerateExample(clientMethod)) { if (ret == null) { ret = new ArrayList<>(); } @@ -341,7 +340,7 @@ public static FluentResourceCreateExample parseResourceCreate(FluentResourceColl List methodParameters = getParameters( create.getMethodReferences() .stream() - .filter(collectionMethod-> requiresExample(collectionMethod.getInnerClientMethod())) + .filter(collectionMethod-> FluentUtils.requiresExample(collectionMethod.getInnerClientMethod())) .findFirst().get() .getInnerClientMethod()); MethodParameter requestBodyParameter = findRequestBodyParameter(methodParameters); @@ -362,7 +361,7 @@ private static List parseResourceUpdate(FluentResou List collectionMethods = resourceUpdate.getMethodReferences(); for (FluentCollectionMethod collectionMethod : collectionMethods) { ClientMethod clientMethod = collectionMethod.getInnerClientMethod(); - if (needToGenerateExample(clientMethod)) { + if (FluentUtils.validToGenerateExample(clientMethod)) { if (ret == null) { ret = new ArrayList<>(); } @@ -389,14 +388,6 @@ private static List parseResourceUpdate(FluentResou return ret; } - private static boolean needToGenerateExample(ClientMethod clientMethod) { - String requestContentType = clientMethod.getProxyMethod().getRequestContentType(); - return clientMethod.getProxyMethod().getExamples() != null - && requiresExample(clientMethod) - // currently only generate for json payload, i.e. "text/json", "application/json" - && requestContentType != null && requestContentType.contains("json"); - } - private static FluentCollectionMethod findResourceGetMethod(ResourceUpdate resourceUpdate) { FluentCollectionMethod resourceGetMethod = null; if (resourceUpdate.getResourceModel().getResourceRefresh() != null) { @@ -459,7 +450,7 @@ public static FluentResourceUpdateExample parseResourceUpdate(FluentResourceColl List methodParameters = getParameters( resourceUpdate.getMethodReferences() .stream() - .filter(collectionMethod-> requiresExample(collectionMethod.getInnerClientMethod())) + .filter(collectionMethod-> FluentUtils.requiresExample(collectionMethod.getInnerClientMethod())) .findFirst().get() .getInnerClientMethod() ); @@ -600,17 +591,6 @@ protected static List getParameters(ClientMethod clientMethod) .collect(Collectors.toList()); } - private static boolean requiresExample(ClientMethod clientMethod) { - if (clientMethod.getType() == ClientMethodType.SimpleSync - || clientMethod.getType() == ClientMethodType.SimpleSyncRestResponse - || clientMethod.getType() == ClientMethodType.PagingSync - || clientMethod.getType() == ClientMethodType.LongRunningSync) { - // generate example for the method with full parameters - return clientMethod.getParameters().stream().anyMatch(p -> ClassType.Context.equals(p.getClientType())); - } - return false; - } - private static boolean methodIsCreateOrUpdate(FluentResourceModel resourceModel) { return resourceModel.getResourceCreate() != null && resourceModel.getResourceUpdate() != null && Objects.equals(resourceModel.getResourceCreate().getMethodReferences().iterator().next().getMethodName(), resourceModel.getResourceUpdate().getMethodReferences().iterator().next().getMethodName()); diff --git a/fluentgen/src/main/java/com/azure/autorest/fluent/util/FluentUtils.java b/fluentgen/src/main/java/com/azure/autorest/fluent/util/FluentUtils.java index af4babe3dd..cf11940f88 100644 --- a/fluentgen/src/main/java/com/azure/autorest/fluent/util/FluentUtils.java +++ b/fluentgen/src/main/java/com/azure/autorest/fluent/util/FluentUtils.java @@ -12,6 +12,8 @@ import com.azure.autorest.fluent.model.clientmodel.FluentResourceModel; import com.azure.autorest.fluent.model.clientmodel.FluentStatic; import com.azure.autorest.fluent.model.clientmodel.ModelNaming; +import com.azure.autorest.model.clientmodel.ClientMethod; +import com.azure.autorest.model.clientmodel.ClientMethodType; import com.azure.autorest.model.clientmodel.ClientModels; import com.azure.autorest.model.clientmodel.ModelProperty; import com.azure.autorest.fluent.model.clientmodel.fluentmodel.LocalVariable; @@ -351,4 +353,23 @@ public static boolean exampleIsUpdate(String name) { name = name.toLowerCase(Locale.ROOT); return name.contains("update") && !name.contains("create"); } + + public static boolean validToGenerateExample(ClientMethod clientMethod) { + String requestContentType = clientMethod.getProxyMethod().getRequestContentType(); + return clientMethod.getProxyMethod().getExamples() != null + && requiresExample(clientMethod) + // currently only generate for json payload, i.e. "text/json", "application/json" + && requestContentType != null && requestContentType.contains("json"); + } + + public static boolean requiresExample(ClientMethod clientMethod) { + if (clientMethod.getType() == ClientMethodType.SimpleSync + || clientMethod.getType() == ClientMethodType.SimpleSyncRestResponse + || clientMethod.getType() == ClientMethodType.PagingSync + || clientMethod.getType() == ClientMethodType.LongRunningSync) { + // generate example for the method with full parameters + return clientMethod.getParameters().stream().anyMatch(p -> ClassType.Context.equals(p.getClientType())); + } + return false; + } } From ad3752f45bbf62fdc74a3f94fcdbe4ba4423f4cc Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Wed, 2 Nov 2022 15:09:03 +0800 Subject: [PATCH 4/8] refactor --- .../fluent/mapper/MockUnitTestParser.java | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java b/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java index ca0a4d662a..5993ab7d25 100644 --- a/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java +++ b/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java @@ -13,6 +13,7 @@ import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentMethodMockUnitTest; import com.azure.autorest.fluent.model.clientmodel.examplemodel.FluentResourceCreateExample; import com.azure.autorest.fluent.model.clientmodel.fluentmodel.create.ResourceCreate; +import com.azure.autorest.fluent.util.FluentUtils; import com.azure.autorest.model.clientmodel.ClassType; import com.azure.autorest.model.clientmodel.ClientMethod; import com.azure.autorest.model.clientmodel.ClientMethodType; @@ -61,7 +62,7 @@ private static FluentMethodMockUnitTest parseResourceCreate(FluentResourceCollec List collectionMethods = resourceCreate.getMethodReferences(); for (FluentCollectionMethod collectionMethod : collectionMethods) { ClientMethod clientMethod = collectionMethod.getInnerClientMethod(); - if (requiresExample(clientMethod)) { + if (FluentUtils.requiresExample(clientMethod)) { List methodParameters = getParameters(clientMethod); MethodParameter requestBodyParameter = findRequestBodyParameter(methodParameters); ProxyMethodExample proxyMethodExample = createProxyMethodExample(clientMethod, methodParameters); @@ -86,7 +87,7 @@ private static FluentMethodMockUnitTest parseMethod(FluentResourceCollection col try { ClientMethod clientMethod = collectionMethod.getInnerClientMethod(); - if (requiresExample(clientMethod)) { + if (FluentUtils.requiresExample(clientMethod)) { List methodParameters = getParameters(clientMethod); ProxyMethodExample proxyMethodExample = createProxyMethodExample(clientMethod, methodParameters); FluentCollectionMethodExample collectionMethodExample = @@ -180,20 +181,6 @@ private static ResponseInfo createProxyMethodExampleResponse(ClientMethod client return new ResponseInfo(new ProxyMethodExample.Response(statusCode, responseObject), verificationNode, verificationObjectName); } - private static boolean requiresExample(ClientMethod clientMethod) { - if (clientMethod.getType() == ClientMethodType.SimpleSync - || clientMethod.getType() == ClientMethodType.PagingSync - // limit the scope of LRO to status code of 200 - || (clientMethod.getType() == ClientMethodType.LongRunningSync - && clientMethod.getProxyMethod().getResponseExpectedStatusCodes().contains(200) - // also azure-core-management does not support LRO from GET - && clientMethod.getProxyMethod().getHttpMethod() != HttpMethod.GET)) { - // generate example for the method with full parameters - return clientMethod.getParameters().stream().anyMatch(p -> ClassType.Context.equals(p.getClientType())); - } - return false; - } - @SuppressWarnings("unchecked") private static void setProvisioningState(Object jsonObject) { // properties.provisioningState = Succeeded From bf56f0ea198c11604677e4389638976c8bfdc37a Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Wed, 2 Nov 2022 15:13:14 +0800 Subject: [PATCH 5/8] use same filter rule as samples --- .../com/azure/autorest/fluent/mapper/MockUnitTestParser.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java b/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java index 5993ab7d25..1680e23df5 100644 --- a/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java +++ b/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java @@ -62,7 +62,7 @@ private static FluentMethodMockUnitTest parseResourceCreate(FluentResourceCollec List collectionMethods = resourceCreate.getMethodReferences(); for (FluentCollectionMethod collectionMethod : collectionMethods) { ClientMethod clientMethod = collectionMethod.getInnerClientMethod(); - if (FluentUtils.requiresExample(clientMethod)) { + if (FluentUtils.validToGenerateExample(clientMethod)) { List methodParameters = getParameters(clientMethod); MethodParameter requestBodyParameter = findRequestBodyParameter(methodParameters); ProxyMethodExample proxyMethodExample = createProxyMethodExample(clientMethod, methodParameters); @@ -87,7 +87,7 @@ private static FluentMethodMockUnitTest parseMethod(FluentResourceCollection col try { ClientMethod clientMethod = collectionMethod.getInnerClientMethod(); - if (FluentUtils.requiresExample(clientMethod)) { + if (FluentUtils.validToGenerateExample(clientMethod)) { List methodParameters = getParameters(clientMethod); ProxyMethodExample proxyMethodExample = createProxyMethodExample(clientMethod, methodParameters); FluentCollectionMethodExample collectionMethodExample = From df8b5fc0af46181fd641ef46e3b92f4107988b96 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Wed, 2 Nov 2022 15:26:46 +0800 Subject: [PATCH 6/8] add password to potential credential --- .../src/main/java/com/azure/autorest/util/ModelTestCaseUtil.java | 1 + 1 file changed, 1 insertion(+) diff --git a/javagen/src/main/java/com/azure/autorest/util/ModelTestCaseUtil.java b/javagen/src/main/java/com/azure/autorest/util/ModelTestCaseUtil.java index bbae10fe92..a9a5289e13 100644 --- a/javagen/src/main/java/com/azure/autorest/util/ModelTestCaseUtil.java +++ b/javagen/src/main/java/com/azure/autorest/util/ModelTestCaseUtil.java @@ -219,6 +219,7 @@ private static void addToJsonObject(Map jsonObject, List "key", "code", "credential", + "password", "token", "secret" ); From 7c37aed471abee0be4a13354a9e65bf5ac2d3f24 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Wed, 2 Nov 2022 15:45:42 +0800 Subject: [PATCH 7/8] redact sample for possible credential string --- .../azure/autorest/util/ModelExampleUtil.java | 8 +++++++ .../autorest/util/ModelTestCaseUtil.java | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/javagen/src/main/java/com/azure/autorest/util/ModelExampleUtil.java b/javagen/src/main/java/com/azure/autorest/util/ModelExampleUtil.java index e08c952d5b..f3d29d1f05 100644 --- a/javagen/src/main/java/com/azure/autorest/util/ModelExampleUtil.java +++ b/javagen/src/main/java/com/azure/autorest/util/ModelExampleUtil.java @@ -132,6 +132,14 @@ public static ExampleNode parseNode(IType type, IType wireType, Object objectVal ExampleNode childNode = parseNode(modelProperty.getClientType(), modelProperty.getWireType(), childObjectValue); node.getChildNodes().add(childNode); clientModelNode.getClientModelProperties().put(childNode, modelProperty); + + // redact possible credential + if (childNode instanceof LiteralNode && childObjectValue instanceof String) { + LiteralNode literalChildNode = (LiteralNode) childNode; + if (literalChildNode.getClientType() == ClassType.String && literalChildNode.getLiteralsValue() != null) { + literalChildNode.setLiteralsValue(ModelTestCaseUtil.redactStringValue(jsonPropertyNames, literalChildNode.getLiteralsValue())); + } + } } } diff --git a/javagen/src/main/java/com/azure/autorest/util/ModelTestCaseUtil.java b/javagen/src/main/java/com/azure/autorest/util/ModelTestCaseUtil.java index a9a5289e13..abfd908046 100644 --- a/javagen/src/main/java/com/azure/autorest/util/ModelTestCaseUtil.java +++ b/javagen/src/main/java/com/azure/autorest/util/ModelTestCaseUtil.java @@ -86,6 +86,13 @@ private static Map jsonFromModel(int depth, ClientModel model) { return jsonObject; } + /** + * Compose a random JSON object according to the structure of client model. + * + * @param depth the current depth of the object from its root + * @param type the type + * @return the JSON object as Map + */ public static Object jsonFromType(int depth, IType type) { if (type.asNullable() == ClassType.Integer) { return RANDOM.nextInt() & Integer.MAX_VALUE; @@ -160,6 +167,20 @@ public static Object jsonFromType(int depth, IType type) { return null; } + + public static String redactStringValue(List serializedNames, String value) { + for (String keyName : serializedNames) { + String keyNameLower = keyName.toLowerCase(Locale.ROOT); + for (String key : POSSIBLE_CREDENTIAL_KEY) { + if (keyNameLower.contains(key)) { + value = "fakeTokenPlaceholder"; + break; + } + } + } + return value; + } + private static void addForProperty(int depth, Map jsonObject, ClientModelProperty property, boolean modelNeedsFlatten) { Object value = null; From 5d97bc649082f3d2cb74d70a7006fcb4adf817e3 Mon Sep 17 00:00:00 2001 From: Weidong Xu Date: Wed, 2 Nov 2022 15:49:58 +0800 Subject: [PATCH 8/8] revert --- .../fluent/mapper/MockUnitTestParser.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java b/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java index 1680e23df5..5b66157b27 100644 --- a/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java +++ b/fluentgen/src/main/java/com/azure/autorest/fluent/mapper/MockUnitTestParser.java @@ -62,7 +62,7 @@ private static FluentMethodMockUnitTest parseResourceCreate(FluentResourceCollec List collectionMethods = resourceCreate.getMethodReferences(); for (FluentCollectionMethod collectionMethod : collectionMethods) { ClientMethod clientMethod = collectionMethod.getInnerClientMethod(); - if (FluentUtils.validToGenerateExample(clientMethod)) { + if (FluentUtils.validToGenerateExample(clientMethod) && requiresExample(clientMethod)) { List methodParameters = getParameters(clientMethod); MethodParameter requestBodyParameter = findRequestBodyParameter(methodParameters); ProxyMethodExample proxyMethodExample = createProxyMethodExample(clientMethod, methodParameters); @@ -87,7 +87,7 @@ private static FluentMethodMockUnitTest parseMethod(FluentResourceCollection col try { ClientMethod clientMethod = collectionMethod.getInnerClientMethod(); - if (FluentUtils.validToGenerateExample(clientMethod)) { + if (FluentUtils.validToGenerateExample(clientMethod) && requiresExample(clientMethod)) { List methodParameters = getParameters(clientMethod); ProxyMethodExample proxyMethodExample = createProxyMethodExample(clientMethod, methodParameters); FluentCollectionMethodExample collectionMethodExample = @@ -181,6 +181,20 @@ private static ResponseInfo createProxyMethodExampleResponse(ClientMethod client return new ResponseInfo(new ProxyMethodExample.Response(statusCode, responseObject), verificationNode, verificationObjectName); } + private static boolean requiresExample(ClientMethod clientMethod) { + if (clientMethod.getType() == ClientMethodType.SimpleSync + || clientMethod.getType() == ClientMethodType.PagingSync + // limit the scope of LRO to status code of 200 + || (clientMethod.getType() == ClientMethodType.LongRunningSync + && clientMethod.getProxyMethod().getResponseExpectedStatusCodes().contains(200) + // also azure-core-management does not support LRO from GET + && clientMethod.getProxyMethod().getHttpMethod() != HttpMethod.GET)) { + // generate example for the method with full parameters + return clientMethod.getParameters().stream().anyMatch(p -> ClassType.Context.equals(p.getClientType())); + } + return false; + } + @SuppressWarnings("unchecked") private static void setProvisioningState(Object jsonObject) { // properties.provisioningState = Succeeded