Skip to content

Commit

Permalink
add test cases to validate the body of ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
ArcturusZhang committed Dec 13, 2024
1 parent 8e025ce commit 631b9aa
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Microsoft.Generator.CSharp.ClientModel.Providers;
using Microsoft.Generator.CSharp.Expressions;
using Microsoft.Generator.CSharp.Input;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void SetUp()
apiKeyAuth: apiKeyAuth,
oauth2Auth: oauth2Auth,
clients: clients,
clientPipelineApi: _hasAuth ? TestClientPipelineApi.Instance : null);
clientPipelineApi: TestClientPipelineApi.Instance);
}

[Test]
Expand Down Expand Up @@ -227,9 +228,9 @@ public void TestBuildConstructors_PrimaryConstructor(List<InputParameter> inputP
var expectedPrimaryCtorCount = _hasKeyAuth && _hasOAuth2 ? 2 : 1;
Assert.AreEqual(expectedPrimaryCtorCount, primaryPublicConstructors.Length);

foreach (var primaryCtor in primaryPublicConstructors)
for (int i = 0; i < primaryPublicConstructors.Length; i++)
{
ValidatePrimaryConstructor(primaryCtor, inputParameters);
ValidatePrimaryConstructor(primaryPublicConstructors[i], inputParameters, i);
}
}

Expand Down Expand Up @@ -330,7 +331,10 @@ public void TestBuildConstructors_ForSubClient_BothAuth()

private void ValidatePrimaryConstructor(
ConstructorProvider primaryPublicConstructor,
List<InputParameter> inputParameters)
List<InputParameter> inputParameters,
int ctorIndex,
[CallerMemberName] string method = "",
[CallerFilePath] string filePath = "")
{
var primaryCtorParams = primaryPublicConstructor?.Signature?.Parameters;
// in no auth case, the ctor only have two parameters: endpoint and options
Expand Down Expand Up @@ -375,8 +379,11 @@ private void ValidatePrimaryConstructor(
}

// validate the body of the primary ctor
var caseName = TestContext.CurrentContext.Test.Properties.Get("caseName");
var expected = Helpers.GetExpectedFromFile($"{caseName},{_hasKeyAuth},{_hasOAuth2},{ctorIndex}", method, filePath);
var primaryCtorBody = primaryPublicConstructor?.BodyStatements;
Assert.IsNotNull(primaryCtorBody);
Assert.AreEqual(expected, primaryCtorBody?.ToDisplayString());
}

private void ValidateSecondaryConstructor(
Expand Down Expand Up @@ -960,7 +967,7 @@ public static IEnumerable<TestCaseData> BuildConstructorsTestCases
defaultValue: InputFactory.Constant.String("someValue"),
kind: InputOperationParameterKind.Client,
isEndpoint: true)
});
}).SetProperty("caseName", "WithDefault");
// scenario where endpoint is required
yield return new TestCaseData(new List<InputParameter>
{
Expand All @@ -976,7 +983,7 @@ public static IEnumerable<TestCaseData> BuildConstructorsTestCases
InputPrimitiveType.String,
location: RequestLocation.None,
kind: InputOperationParameterKind.Client)
});
}).SetProperty("caseName", "WithRequired");
}
}

Expand Down Expand Up @@ -1185,47 +1192,24 @@ private static IEnumerable<TestCaseData> ValidateApiVersionPathParameterTestCase
}

// TODO -- this is temporary here before System.ClientModel officially supports OAuth2 auth
private record TestClientPipelineApi : ClientPipelineApi
private record TestClientPipelineApi : ClientPipelineProvider
{
private static ClientPipelineApi? _instance;
internal static ClientPipelineApi Instance => _instance ??= new TestClientPipelineApi(Empty);
internal new static ClientPipelineApi Instance => _instance ??= new TestClientPipelineApi(Empty);

public TestClientPipelineApi(ValueExpression original) : base(typeof(string), original)
public TestClientPipelineApi(ValueExpression original) : base(original)
{
}

public override CSharpType ClientPipelineType => typeof(string);

public override CSharpType ClientPipelineOptionsType => typeof(string);

public override CSharpType PipelinePolicyType => typeof(string);

public override CSharpType KeyCredentialType => typeof(ApiKeyCredential);

public override CSharpType TokenCredentialType => typeof(FakeTokenCredential);

public override ValueExpression Create(ValueExpression options, ValueExpression perRetryPolicies)
=> Original.Invoke("GetFakeCreate", [options, perRetryPolicies]);

public override ValueExpression CreateMessage(HttpRequestOptionsApi requestOptions, ValueExpression responseClassifier)
=> Original.Invoke("GetFakeCreateMessage", [requestOptions, responseClassifier]);

public override ClientPipelineApi FromExpression(ValueExpression expression)
=> new TestClientPipelineApi(expression);

public override ValueExpression KeyAuthorizationPolicy(ValueExpression credential, ValueExpression headerName, ValueExpression? keyPrefix = null)
=> Original.Invoke("GetFakeApiKeyAuthorizationPolicy", keyPrefix != null ? [credential, headerName, keyPrefix] : [credential, headerName]);


public override ValueExpression TokenAuthorizationPolicy(ValueExpression credential, ValueExpression scopes)
=> Original.Invoke("GetFakeTokenAuthorizationPolicy", [credential, scopes]);

public override ClientPipelineApi ToExpression() => this;

public override MethodBodyStatement[] ProcessMessage(HttpMessageApi message, HttpRequestOptionsApi options)
=> [Original.Invoke("GetFakeProcessMessage", [message, options]).Terminate()];

public override MethodBodyStatement[] ProcessMessageAsync(HttpMessageApi message, HttpRequestOptionsApi options)
=> [Original.Invoke("GetFakeProcessMessageAsync", [message, options]).Terminate()];
}

internal class FakeTokenCredential { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global::Sample.Argument.AssertNotNull(endpoint, nameof(endpoint));

options ??= new global::Sample.TestClientOptions();

_endpoint = endpoint;
Pipeline = global::System.ClientModel.Primitives.ClientPipeline.Create(options, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>(), Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>(), Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>());
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
global::Sample.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::Sample.Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));

options ??= new global::Sample.TestClientOptions();

_endpoint = endpoint;
_tokenCredential = tokenCredential;
Pipeline = global::System.ClientModel.Primitives.ClientPipeline.Create(options, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>(), new global::System.ClientModel.Primitives.PipelinePolicy[] { this.GetFakeTokenAuthorizationPolicy(_tokenCredential, AuthorizationScopes) }, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>());
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
global::Sample.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::Sample.Argument.AssertNotNull(keyCredential, nameof(keyCredential));

options ??= new global::Sample.TestClientOptions();

_endpoint = endpoint;
_keyCredential = keyCredential;
Pipeline = global::System.ClientModel.Primitives.ClientPipeline.Create(options, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>(), new global::System.ClientModel.Primitives.PipelinePolicy[] { global::System.ClientModel.Primitives.ApiKeyAuthenticationPolicy.CreateHeaderApiKeyPolicy(_keyCredential, AuthorizationHeader) }, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>());
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
global::Sample.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::Sample.Argument.AssertNotNull(keyCredential, nameof(keyCredential));

options ??= new global::Sample.TestClientOptions();

_endpoint = endpoint;
_keyCredential = keyCredential;
Pipeline = global::System.ClientModel.Primitives.ClientPipeline.Create(options, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>(), new global::System.ClientModel.Primitives.PipelinePolicy[] { global::System.ClientModel.Primitives.ApiKeyAuthenticationPolicy.CreateHeaderApiKeyPolicy(_keyCredential, AuthorizationHeader) }, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>());
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
global::Sample.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::Sample.Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));

options ??= new global::Sample.TestClientOptions();

_endpoint = endpoint;
_tokenCredential = tokenCredential;
Pipeline = global::System.ClientModel.Primitives.ClientPipeline.Create(options, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>(), new global::System.ClientModel.Primitives.PipelinePolicy[] { this.GetFakeTokenAuthorizationPolicy(_tokenCredential, AuthorizationScopes) }, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>());
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
global::Sample.Argument.AssertNotNull(endpoint, nameof(endpoint));

options ??= new global::Sample.TestClientOptions();

_endpoint = endpoint;
Pipeline = global::System.ClientModel.Primitives.ClientPipeline.Create(options, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>(), Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>(), Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>());
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
global::Sample.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::Sample.Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));

options ??= new global::Sample.TestClientOptions();

_endpoint = endpoint;
_tokenCredential = tokenCredential;
Pipeline = global::System.ClientModel.Primitives.ClientPipeline.Create(options, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>(), new global::System.ClientModel.Primitives.PipelinePolicy[] { this.GetFakeTokenAuthorizationPolicy(_tokenCredential, AuthorizationScopes) }, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>());
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
global::Sample.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::Sample.Argument.AssertNotNull(keyCredential, nameof(keyCredential));

options ??= new global::Sample.TestClientOptions();

_endpoint = endpoint;
_keyCredential = keyCredential;
Pipeline = global::System.ClientModel.Primitives.ClientPipeline.Create(options, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>(), new global::System.ClientModel.Primitives.PipelinePolicy[] { global::System.ClientModel.Primitives.ApiKeyAuthenticationPolicy.CreateHeaderApiKeyPolicy(_keyCredential, AuthorizationHeader) }, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>());
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
global::Sample.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::Sample.Argument.AssertNotNull(keyCredential, nameof(keyCredential));

options ??= new global::Sample.TestClientOptions();

_endpoint = endpoint;
_keyCredential = keyCredential;
Pipeline = global::System.ClientModel.Primitives.ClientPipeline.Create(options, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>(), new global::System.ClientModel.Primitives.PipelinePolicy[] { global::System.ClientModel.Primitives.ApiKeyAuthenticationPolicy.CreateHeaderApiKeyPolicy(_keyCredential, AuthorizationHeader) }, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>());
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
global::Sample.Argument.AssertNotNull(endpoint, nameof(endpoint));
global::Sample.Argument.AssertNotNull(tokenCredential, nameof(tokenCredential));

options ??= new global::Sample.TestClientOptions();

_endpoint = endpoint;
_tokenCredential = tokenCredential;
Pipeline = global::System.ClientModel.Primitives.ClientPipeline.Create(options, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>(), new global::System.ClientModel.Primitives.PipelinePolicy[] { this.GetFakeTokenAuthorizationPolicy(_tokenCredential, AuthorizationScopes) }, Array.Empty<global::System.ClientModel.Primitives.PipelinePolicy>());
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace UnbrandedTypeSpec
public partial class UnbrandedTypeSpecClient
{
private readonly Uri _endpoint;
private const string AuthorizationHeader = "my-api-key";
/// <summary> A credential used to authenticate to the service. </summary>
private readonly ApiKeyCredential _keyCredential;
private const string AuthorizationHeader = "my-api-key";
private readonly string _apiVersion;

/// <summary> Initializes a new instance of UnbrandedTypeSpecClient for mocking. </summary>
Expand Down

0 comments on commit 631b9aa

Please sign in to comment.