Skip to content

Commit c1a7975

Browse files
committed
Fixing tests and updating converters to handle null values
1 parent 3de5d1e commit c1a7975

File tree

8 files changed

+82
-31
lines changed

8 files changed

+82
-31
lines changed

src/ResourceManager/Profile/Commands.Profile.Test/ContextCmdletTests.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Linq;
2323
using Xunit;
2424
using System;
25+
using Microsoft.Azure.Commands.Profile.Models;
2526
using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
2627

2728
namespace Microsoft.Azure.Commands.ResourceManager.Profile.Test
@@ -54,8 +55,8 @@ public void GetAzureContext()
5455

5556
// Verify
5657
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
57-
var context = (AzureContext) commandRuntimeMock.OutputPipeline[0];
58-
Assert.Equal("test", context.Subscription.Name);
58+
var context = (PSAzureContext) commandRuntimeMock.OutputPipeline[0];
59+
Assert.Equal("test", context.Subscription.SubscriptionName);
5960
}
6061

6162
[Fact]
@@ -75,9 +76,9 @@ public void SelectAzureContextWithSubscriptionAndTenant()
7576

7677
// Verify
7778
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
78-
var context = (AzureContext)commandRuntimeMock.OutputPipeline[0];
79-
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.Id.ToString());
80-
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.Id.ToString());
79+
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
80+
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.SubscriptionId);
81+
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.TenantId);
8182
}
8283

8384
[Fact]
@@ -96,8 +97,8 @@ public void SelectAzureContextWithSubscriptionAndNoTenant()
9697

9798
// Verify
9899
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
99-
var context = (AzureContext)commandRuntimeMock.OutputPipeline[0];
100-
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.Id.ToString());
100+
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
101+
Assert.Equal("db1ab6f0-4769-4b27-930e-01e2ef9c123c", context.Subscription.SubscriptionId);
101102
}
102103

103104
[Fact]
@@ -116,8 +117,8 @@ public void SelectAzureContextWithNoSubscriptionAndTenant()
116117

117118
// Verify
118119
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
119-
var context = (AzureContext)commandRuntimeMock.OutputPipeline[0];
120-
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.Id.ToString());
120+
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
121+
Assert.Equal("72f988bf-86f1-41af-91ab-2d7cd011db47", context.Tenant.TenantId);
121122
}
122123

123124
[Fact]
@@ -135,7 +136,7 @@ public void SelectAzureContextWithNoSubscriptionAndNoTenant()
135136

136137
// Verify
137138
Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
138-
var context = (AzureContext)commandRuntimeMock.OutputPipeline[0];
139+
var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];
139140
Assert.NotNull(context);
140141
}
141142
}

src/ResourceManager/Profile/Commands.Profile.Test/SubscriptionCmdletTests.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function Test-GetSubscriptionsEndToEnd
2222
$firstSubscription = $allSubscriptions[0]
2323
$id = $firstSubscription.SubscriptionId
2424
$tenant = $firstSubscription.TenantId
25+
$name = $firstSubscription.SubscriptionName
2526
$subscription = $firstSubscription | Get-AzureRmSubscription
2627
Assert-True { $subscription -ne $null }
2728
Assert-AreEqual $id $subscription.SubscriptionId

src/ResourceManager/Profile/Commands.Profile/Models/PSAzureAccount.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public static implicit operator PSAzureAccount(AzureAccount account)
3333
{
3434
return new PSAzureAccount
3535
{
36-
Id = account.Id,
37-
AccountType = account.Type.ToString()
36+
Id = account != null ? account.Id : null,
37+
AccountType = account != null? account.Type.ToString() : null
3838
};
3939
}
4040

@@ -47,9 +47,9 @@ public static implicit operator AzureAccount(PSAzureAccount account)
4747
{
4848
return new AzureAccount
4949
{
50-
Id = account.Id,
51-
Type = ((AzureAccount.AccountType)Enum.Parse(typeof(AzureAccount.AccountType),
52-
account.AccountType, true))
50+
Id = account != null? account.Id : null,
51+
Type = account != null? ((AzureAccount.AccountType)Enum.Parse(typeof(AzureAccount.AccountType),
52+
account.AccountType, true)) : AzureAccount.AccountType.User
5353
};
5454
}
5555

src/ResourceManager/Profile/Commands.Profile/Models/PSAzureContext.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public static implicit operator PSAzureContext(AzureContext context)
2222
{
2323
return new PSAzureContext
2424
{
25-
Account = context.Account,
26-
Environment = context.Environment,
27-
Subscription = context.Subscription,
28-
Tenant = context.Tenant,
29-
TokenCache = context.TokenCache
25+
Account = context != null ? context.Account : null,
26+
Environment = context != null ? context.Environment : null,
27+
Subscription = context != null ? context.Subscription : null,
28+
Tenant = context != null ? context.Tenant : null,
29+
TokenCache = context != null ? context.TokenCache : null
3030
};
3131
}
3232

@@ -37,9 +37,9 @@ public static implicit operator PSAzureContext(AzureContext context)
3737
/// <returns>The converted context.</returns>
3838
public static implicit operator AzureContext(PSAzureContext context)
3939
{
40-
var result= new AzureContext(context.Subscription, context.Account,
41-
context.Environment, context.Tenant);
42-
result.TokenCache = context.TokenCache;
40+
var result= new AzureContext(context != null ? context.Subscription : null, context != null ? context.Account : null,
41+
context != null ? context.Environment : null, context != null ? context.Tenant : null);
42+
result.TokenCache = context!= null? context.TokenCache : null;
4343
return result;
4444
}
4545

src/ResourceManager/Profile/Commands.Profile/Models/PSAzureEnvironment.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public class PSAzureEnvironment
2828
/// <returns>The internal representation of the Azure environment, as used by .Net authentication libraries.</returns>
2929
public static implicit operator AzureEnvironment(PSAzureEnvironment environment)
3030
{
31+
if (environment == null)
32+
{
33+
return null;
34+
}
35+
3136
var newEnvironment = new AzureEnvironment
3237
{
3338
Name = environment.Name,
@@ -62,6 +67,11 @@ public static implicit operator AzureEnvironment(PSAzureEnvironment environment)
6267
/// <returns>The PowerShell;-friendly representation of the environment.</returns>
6368
public static implicit operator PSAzureEnvironment(AzureEnvironment environment)
6469
{
70+
if (environment == null)
71+
{
72+
return null;
73+
}
74+
6575
return new PSAzureEnvironment(environment);
6676
}
6777

src/ResourceManager/Profile/Commands.Profile/Models/PSAzureProfile.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public class PSAzureProfile
2323
/// <returns>The converted profile.</returns>
2424
public static implicit operator PSAzureProfile(AzureRMProfile profile)
2525
{
26+
if (profile == null)
27+
{
28+
return null;
29+
}
30+
2631
var result = new PSAzureProfile
2732
{
2833
Context = profile.Context
@@ -40,6 +45,11 @@ public static implicit operator PSAzureProfile(AzureRMProfile profile)
4045
/// <returns>The converted profile.</returns>
4146
public static implicit operator AzureRMProfile(PSAzureProfile profile)
4247
{
48+
if (profile == null)
49+
{
50+
return null;
51+
}
52+
4353
var result = new AzureRMProfile
4454
{
4555
Context = profile.Context

src/ResourceManager/Profile/Commands.Profile/Models/PSAzureSubscription.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public static implicit operator PSAzureSubscription(AzureSubscription other)
3333
{
3434
return new PSAzureSubscription
3535
{
36-
SubscriptionId = other.Id.ToString(),
37-
SubscriptionName = other.Name,
38-
TenantId = other.IsPropertySet(AzureSubscription.Property.Tenants)?
36+
SubscriptionId = other != null? other.Id.ToString() : null,
37+
SubscriptionName = other != null? other.Name : null,
38+
TenantId = other != null && other.IsPropertySet(AzureSubscription.Property.Tenants)?
3939
other.GetProperty(AzureSubscription.Property.Tenants) : null
4040
};
4141
}
@@ -47,16 +47,30 @@ public static implicit operator PSAzureSubscription(AzureSubscription other)
4747
/// <returns>The converted subscription.</returns>
4848
public static implicit operator AzureSubscription(PSAzureSubscription other)
4949
{
50+
if (other == null)
51+
{
52+
return null;
53+
}
54+
5055
var result = new AzureSubscription
5156
{
52-
Id = Guid.Parse(other.SubscriptionId),
5357
Name = other.SubscriptionName
5458
};
5559

60+
if (other.SubscriptionId != null)
61+
{
62+
Guid subscriptionId;
63+
if (Guid.TryParse(other.SubscriptionId, out subscriptionId))
64+
{
65+
result.Id = subscriptionId;
66+
}
67+
}
68+
5669
if (other.TenantId != null)
5770
{
5871
result.Properties.SetProperty(AzureSubscription.Property.Tenants, other.TenantId);
5972
}
73+
6074
return result;
6175
}
6276

src/ResourceManager/Profile/Commands.Profile/Models/PSAzureTenant.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public static implicit operator PSAzureTenant(AzureTenant other)
3333
{
3434
return new PSAzureTenant
3535
{
36-
TenantId = other.Id.ToString(),
37-
Domain = other.Domain,
36+
TenantId = other != null? other.Id.ToString() : null,
37+
Domain = other != null? other.Domain : null
3838
};
3939
}
4040

@@ -45,11 +45,26 @@ public static implicit operator PSAzureTenant(AzureTenant other)
4545
/// <returns>The converted subscription.</returns>
4646
public static implicit operator AzureTenant(PSAzureTenant other)
4747
{
48-
return new AzureTenant
48+
if (other == null)
49+
{
50+
return null;
51+
}
52+
53+
var result = new AzureTenant
4954
{
50-
Id = Guid.Parse(other.TenantId),
5155
Domain = other.Domain
5256
};
57+
58+
if (other.TenantId != null)
59+
{
60+
Guid tenantId;
61+
if (Guid.TryParse(other.TenantId, out tenantId))
62+
{
63+
result.Id = tenantId;
64+
}
65+
}
66+
67+
return result;
5368
}
5469

5570
/// <summary>

0 commit comments

Comments
 (0)