Skip to content

Commit

Permalink
Bunch of changes
Browse files Browse the repository at this point in the history
- Make HttpRestMessageInspector generic logger not thighed to PowerShell
- Inject Moq in the Store tests
- Change GetAzureStoreAvailableAddOnTest to use Moq
- Refactor the Store related code into StoreClient.cs
- Add Get-AzureStoreAddOn cmdlet
  • Loading branch information
ogail committed Feb 12, 2013
1 parent be4ab39 commit 29e39bc
Show file tree
Hide file tree
Showing 22 changed files with 728 additions and 233 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -10,4 +10,5 @@ TestResults
*.orig
*.ncrunchsolution
/Publish
/Package
/Package
/WindowsAzurePowershell/src/packages
Expand Up @@ -116,7 +116,7 @@ public override void ExecuteCmdlet()
ServiceBinding,
new Uri(ServiceEndpoint),
CurrentSubscription.Certificate,
new HttpRestMessageInspector(this));
new HttpRestMessageInspector(text => this.WriteDebug(text)));
}

IsServiceBusNamespaceAvailable(CurrentSubscription.SubscriptionId, Name);
Expand Down
Expand Up @@ -63,6 +63,9 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Moq">
<HintPath>..\packages\Moq.4.0.10827\lib\NET40\Moq.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
Expand All @@ -81,7 +84,7 @@
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Stubs\SimpleMarketplaceManagement.cs" />
<Compile Include="UnitTests\Cmdlet\GetAzureStoreAddOnTest.cs" />
<Compile Include="UnitTests\Cmdlet\GetAzureStoreAvailableAddOnTest.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -106,7 +109,9 @@
<Name>ServiceManagement</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
Expand Down

This file was deleted.

@@ -0,0 +1,208 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.WindowsAzure.Management.Store.Test.UnitTests.Cmdlet
{
using Microsoft.Samples.WindowsAzure.ServiceManagement.Store.Contract;
using Microsoft.WindowsAzure.Management.Store.Cmdlet;
using Microsoft.WindowsAzure.Management.Test.Stubs;
using Microsoft.WindowsAzure.Management.Test.Tests.Utilities;
using Moq;
using VisualStudio.TestTools.UnitTesting;
using System;
using Microsoft.Samples.WindowsAzure.ServiceManagement.Store.ResourceModel;
using System.Management.Automation;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Management.Store.Model;

[TestClass]
public class GetAzureStoreAddOnTests : TestBase
{
[TestInitialize]
public void SetupTest()
{
Management.Extensions.CmdletSubscriptionExtensions.SessionManager = new InMemorySessionManager();
new FileSystemHelper(this).CreateAzureSdkDirectoryAndImportPublishSettings();
}

[TestMethod]
public void GetAzureStoreAddOnWithEmptyCloudService()
{
// Setup
Mock<ICommandRuntime> mockCommandRuntime = new Mock<ICommandRuntime>();
List<PSObject> actual = new List<PSObject>();
mockCommandRuntime.Setup(f => f.WriteObject(It.IsAny<List<PSObject>>())).Callback((object obj) => actual = (List<PSObject>)obj);
Mock<StoreClient> mockStoreClient = new Mock<StoreClient>();
mockStoreClient.Setup(f => f.GetAddOn(It.IsAny<AddOnSearchOptions>())).Returns(new List<AddOn>());
GetAzureStoreAddOnCommand cmdlet = new GetAzureStoreAddOnCommand() { StoreClient = mockStoreClient.Object, CommandRuntime = mockCommandRuntime.Object };

// Test
cmdlet.ExecuteCmdlet();

// Assert
mockStoreClient.Verify(f => f.GetAddOn(new AddOnSearchOptions(null, null, null)), Times.Exactly(1));
Assert.AreEqual<int>(0, actual.Count);
}

[TestMethod]
public void GetAzureStoreAddOnWithoutSearchOptions()
{
// Setup
Mock<ICommandRuntime> mockCommandRuntime = new Mock<ICommandRuntime>();
List<PSObject> actual = new List<PSObject>();
mockCommandRuntime.Setup(f => f.WriteObject(It.IsAny<List<PSObject>>(), true)).Callback((object obj, bool isCollection) => actual = (List<PSObject>)obj);
Mock<StoreClient> mockStoreClient = new Mock<StoreClient>();
List<AddOn> expected = new List<AddOn>();
expected.Add(new AddOn(new Resource() { Name = "BingSearchAddOn" }, "West US"));
expected.Add(new AddOn(new Resource() { Name = "BingTranslateAddOn" }, "West US"));
mockStoreClient.Setup(f => f.GetAddOn(It.IsAny<AddOnSearchOptions>())).Returns(expected);
GetAzureStoreAddOnCommand cmdlet = new GetAzureStoreAddOnCommand() { StoreClient = mockStoreClient.Object, CommandRuntime = mockCommandRuntime.Object };

// Test
cmdlet.ExecuteCmdlet();

// Assert
mockStoreClient.Verify(f => f.GetAddOn(new AddOnSearchOptions(null, null, null)), Times.Exactly(1));
Assert.AreEqual<int>(expected.Count, actual.Count);

for (int i = 0; i < expected.Count; i++)
{
Assert.AreEqual<string>(expected[i].Info.Name, actual[i].GetVariableValue<string>("Name"));
Assert.AreEqual<string>(expected[i].GeoRegion, actual[i].GetVariableValue<string>("Location"));
}
}

[TestMethod]
public void GetAzureStoreAddOnWithNameFilter()
{
// Setup
Mock<ICommandRuntime> mockCommandRuntime = new Mock<ICommandRuntime>();
PSObject actual = new PSObject();
mockCommandRuntime.Setup(f => f.WriteObject(It.IsAny<PSObject>())).Callback((object obj) => actual = (PSObject)obj);
Mock<StoreClient> mockStoreClient = new Mock<StoreClient>();
List<AddOn> expected = new List<AddOn>();
expected.Add(new AddOn(new Resource() { Name = "BingTranslateAddOn" }, "West US"));
mockStoreClient.Setup(f => f.GetAddOn(new AddOnSearchOptions("BingTranslateAddOn", null, null))).Returns(expected);
GetAzureStoreAddOnCommand cmdlet = new GetAzureStoreAddOnCommand() {
StoreClient = mockStoreClient.Object,
CommandRuntime = mockCommandRuntime.Object,
Name = "BingTranslateAddOn"
};

// Test
cmdlet.ExecuteCmdlet();

// Assert
mockStoreClient.Verify(f => f.GetAddOn(new AddOnSearchOptions("BingTranslateAddOn", null, null)), Times.Exactly(1));
Assert.AreEqual<string>(expected[0].Info.Name, actual.GetVariableValue<string>("Name"));
Assert.AreEqual<string>(expected[0].GeoRegion, actual.GetVariableValue<string>("Location"));
}

[TestMethod]
public void GetAzureStoreAddOnWithLocationFilter()
{
// Setup
Mock<ICommandRuntime> mockCommandRuntime = new Mock<ICommandRuntime>();
List<PSObject> actual = new List<PSObject>();
mockCommandRuntime.Setup(f => f.WriteObject(It.IsAny<List<PSObject>>(), true)).Callback((object obj, bool isCollection) => actual = (List<PSObject>)obj);
Mock<StoreClient> mockStoreClient = new Mock<StoreClient>();
List<AddOn> expected = new List<AddOn>();
expected.Add(new AddOn(new Resource() { Name = "BingSearchAddOn" }, "West US"));
expected.Add(new AddOn(new Resource() { Name = "MongoDB" }, "West US"));
expected.Add(new AddOn(new Resource() { Name = "BingTranslateAddOn" }, "West US"));
mockStoreClient.Setup(f => f.GetAddOn(new AddOnSearchOptions(null, null, "West US"))).Returns(expected);
GetAzureStoreAddOnCommand cmdlet = new GetAzureStoreAddOnCommand() {
StoreClient = mockStoreClient.Object,
CommandRuntime = mockCommandRuntime.Object,
Location = "West US"
};

// Test
cmdlet.ExecuteCmdlet();

// Assert
mockStoreClient.Verify(f => f.GetAddOn(new AddOnSearchOptions(null, null, "West US")), Times.Exactly(1));
Assert.AreEqual<int>(expected.Count, actual.Count);

for (int i = 0; i < expected.Count; i++)
{
Assert.AreEqual<string>(expected[i].Info.Name, actual[i].GetVariableValue<string>("Name"));
Assert.AreEqual<string>("West US", actual[i].GetVariableValue<string>("Location"));
}
}

[TestMethod]
public void GetAzureStoreAddOnWithProviderFilter()
{
// Setup
Mock<ICommandRuntime> mockCommandRuntime = new Mock<ICommandRuntime>();
List<PSObject> actual = new List<PSObject>();
mockCommandRuntime.Setup(f => f.WriteObject(It.IsAny<List<PSObject>>(), true)).Callback((object obj, bool isCollection) => actual = (List<PSObject>)obj);
Mock<StoreClient> mockStoreClient = new Mock<StoreClient>();
List<AddOn> expected = new List<AddOn>();
expected.Add(new AddOn(new Resource() { Name = "BingSearchAddOn", ResourceProviderNamespace = "Microsoft" }, "West US"));
expected.Add(new AddOn(new Resource() { Name = "BingTranslateAddOn", ResourceProviderNamespace = "Microsoft" }, "West US"));
mockStoreClient.Setup(f => f.GetAddOn(new AddOnSearchOptions(null, "Microsoft", null))).Returns(expected);
GetAzureStoreAddOnCommand cmdlet = new GetAzureStoreAddOnCommand()
{
StoreClient = mockStoreClient.Object,
CommandRuntime = mockCommandRuntime.Object,
Provider = "Microsoft"
};

// Test
cmdlet.ExecuteCmdlet();

// Assert
mockStoreClient.Verify(f => f.GetAddOn(new AddOnSearchOptions(null, "Microsoft", null)), Times.Exactly(1));
Assert.AreEqual<int>(expected.Count, actual.Count);

for (int i = 0; i < expected.Count; i++)
{
Assert.AreEqual<string>(expected[i].Info.Name, actual[i].GetVariableValue<string>("Name"));
Assert.AreEqual<string>("Microsoft", actual[i].GetVariableValue<string>("Provider"));
}
}

[TestMethod]
public void GetAzureStoreAddOnWithCompleteSearchOptions()
{
// Setup
Mock<ICommandRuntime> mockCommandRuntime = new Mock<ICommandRuntime>();
PSObject actual = new PSObject();
mockCommandRuntime.Setup(f => f.WriteObject(It.IsAny<PSObject>())).Callback((object obj) => actual = (PSObject)obj);
Mock<StoreClient> mockStoreClient = new Mock<StoreClient>();
List<AddOn> expected = new List<AddOn>();
expected.Add(new AddOn(new Resource() { Name = "BingSearchAddOn", ResourceProviderNamespace = "Microsoft" }, "West US"));
mockStoreClient.Setup(f => f.GetAddOn(new AddOnSearchOptions("BingSearchAddOn", "Microsoft", "West US"))).Returns(expected);
GetAzureStoreAddOnCommand cmdlet = new GetAzureStoreAddOnCommand()
{
StoreClient = mockStoreClient.Object,
CommandRuntime = mockCommandRuntime.Object,
Provider = "Microsoft",
Name = "BingSearchAddOn",
Location = "West US"
};

// Test
cmdlet.ExecuteCmdlet();

// Assert
mockStoreClient.Verify(f => f.GetAddOn(new AddOnSearchOptions("BingSearchAddOn", "Microsoft", "West US")), Times.Exactly(1));
Assert.AreEqual<string>("BingSearchAddOn", actual.GetVariableValue<string>("Name"));
Assert.AreEqual<string>("Microsoft", actual.GetVariableValue<string>("Provider"));
Assert.AreEqual<string>("West US", actual.GetVariableValue<string>("Location"));
}
}
}

0 comments on commit 29e39bc

Please sign in to comment.